summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/lib/processors/xbl-bindings.js
blob: dc09550f29e1f3ae78a191b873dadea7473641a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
 * @fileoverview Converts functions and handlers from XBL bindings into JS
 * functions
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

"use strict";

const NS_XBL = "http://www.mozilla.org/xbl";

let sax = require("sax");

// Converts sax's error message to something that eslint will understand
let errorRegex = /(.*)\nLine: (\d+)\nColumn: (\d+)\nChar: (.*)/
function parseError(err) {
  let matches = err.message.match(errorRegex);
  if (!matches)
    return null;

  return {
    fatal: true,
    message: matches[1],
    line: parseInt(matches[2]) + 1,
    column: parseInt(matches[3])
  }
}

let entityRegex = /&[\w][\w-\.]*;/g;

// A simple sax listener that generates a tree of element information
function XMLParser(parser) {
  this.parser = parser;
  parser.onopentag = this.onOpenTag.bind(this);
  parser.onclosetag = this.onCloseTag.bind(this);
  parser.ontext = this.onText.bind(this);
  parser.onopencdata = this.onOpenCDATA.bind(this);
  parser.oncdata = this.onCDATA.bind(this);
  parser.oncomment = this.onComment.bind(this);

  this.document = {
    local: "#document",
    uri: null,
    children: [],
    comments: [],
  }
  this._currentNode = this.document;
}

XMLParser.prototype = {
  parser: null,

  onOpenTag: function(tag) {
    let node = {
      parentNode: this._currentNode,
      local: tag.local,
      namespace: tag.uri,
      attributes: {},
      children: [],
      comments: [],
      textContent: "",
      textLine: this.parser.line,
      textColumn: this.parser.column,
      textEndLine: this.parser.line
    }

    for (let attr of Object.keys(tag.attributes)) {
      if (tag.attributes[attr].uri == "") {
        node.attributes[attr] = tag.attributes[attr].value;
      }
    }

    this._currentNode.children.push(node);
    this._currentNode = node;
  },

  onCloseTag: function(tagname) {
    this._currentNode.textEndLine = this.parser.line;
    this._currentNode = this._currentNode.parentNode;
  },

  addText: function(text) {
    this._currentNode.textContent += text;
  },

  onText: function(text) {
    // Replace entities with some valid JS token.
    this.addText(text.replace(entityRegex, "null"));
  },

  onOpenCDATA: function() {
    // Turn the CDATA opening tag into whitespace for indent alignment
    this.addText(" ".repeat("<![CDATA[".length));
  },

  onCDATA: function(text) {
    this.addText(text);
  },

  onComment: function(text) {
    this._currentNode.comments.push(text);
  }
}

// -----------------------------------------------------------------------------
// Processor Definition
// -----------------------------------------------------------------------------

const INDENT_LEVEL = 2;

function indent(count) {
  return " ".repeat(count * INDENT_LEVEL);
}

// Stores any XML parse error
let xmlParseError = null;

// Stores the lines of JS code generated from the XBL
let scriptLines = [];
// Stores a map from the synthetic line number to the real line number
// and column offset.
let lineMap = [];

function addSyntheticLine(line, linePos, addDisableLine) {
  lineMap[scriptLines.length] = { line: linePos, offset: null };
  scriptLines.push(line + (addDisableLine ? "" : " // eslint-disable-line"));
}

/**
 * Adds generated lines from an XBL node to the script to be passed back to eslint.
 */
function addNodeLines(node, reindent) {
  let lines = node.textContent.split("\n");
  let startLine = node.textLine;
  let startColumn = node.textColumn;

  // The case where there are no whitespace lines before the first text is
  // treated differently for indentation
  let indentFirst = false;

  // Strip off any preceeding whitespace only lines. These are often used to
  // format the XML and CDATA blocks.
  while (lines.length && lines[0].trim() == "") {
    indentFirst = true;
    startLine++;
    lines.shift();
  }

  // Strip off any whitespace lines at the end. These are often used to line
  // up the closing tags
  while (lines.length && lines[lines.length - 1].trim() == "") {
    lines.pop();
  }

  if (!indentFirst) {
    let firstLine = lines.shift();
    firstLine = " ".repeat(reindent * INDENT_LEVEL) + firstLine;
    // ESLint counts columns starting at 1 rather than 0
    lineMap[scriptLines.length] = { line: startLine, offset: reindent * INDENT_LEVEL - (startColumn - 1) };
    scriptLines.push(firstLine);
    startLine++;
  }

  // Find the preceeding whitespace for all lines that aren't entirely whitespace
  let indents = lines.filter(s => s.trim().length > 0)
                     .map(s => s.length - s.trimLeft().length);
  // Find the smallest indent level in use
  let minIndent = Math.min.apply(null, indents);

  for (let line of lines) {
    if (line.trim().length == 0) {
      // Don't offset lines that are only whitespace, the only possible JS error
      // is trailing whitespace and we want it to point at the right place
      lineMap[scriptLines.length] = { line: startLine, offset: 0 };
    } else {
      line = " ".repeat(reindent * INDENT_LEVEL) + line.substring(minIndent);
      lineMap[scriptLines.length] = { line: startLine, offset: reindent * INDENT_LEVEL - (minIndent - 1) };
    }

    scriptLines.push(line);
    startLine++;
  }
}

module.exports = {
  preprocess: function(text, filename) {
    xmlParseError = null;
    scriptLines = [];
    lineMap = [];

    // Non-strict allows us to ignore many errors from entities and
    // preprocessing at the expense of failing to report some XML errors.
    // Unfortunately it also throws away the case of tagnames and attributes
    let parser = sax.parser(false, {
      lowercase: true,
      xmlns: true,
    });

    parser.onerror = function(err) {
      xmlParseError = parseError(err);
    }

    let xp = new XMLParser(parser);
    parser.write(text);

    // Sanity checks to make sure we're dealing with an XBL document
    let document = xp.document;
    if (document.children.length != 1) {
      return [];
    }

    let bindings = document.children[0];
    if (bindings.local != "bindings" || bindings.namespace != NS_XBL) {
      return [];
    }

    for (let comment of document.comments) {
      addSyntheticLine(`/*`, 0, true);
      for (let line of comment.split("\n")) {
        addSyntheticLine(`${line.trim()}`, 0, true);
      }
      addSyntheticLine(`*/`, 0, true);
    }

    addSyntheticLine(`this.bindings = {`, bindings.textLine);

    for (let binding of bindings.children) {
      if (binding.local != "binding" || binding.namespace != NS_XBL) {
        continue;
      }

      addSyntheticLine(indent(1) + `"${binding.attributes.id}": {`, binding.textLine);

      for (let part of binding.children) {
        if (part.namespace != NS_XBL) {
          continue;
        }

        if (part.local == "implementation") {
          addSyntheticLine(indent(2) + `implementation: {`, part.textLine);
        } else if (part.local == "handlers") {
          addSyntheticLine(indent(2) + `handlers: [`, part.textLine);
        } else {
          continue;
        }

        for (let item of part.children) {
          if (item.namespace != NS_XBL) {
            continue;
          }

          switch (item.local) {
            case "field": {
              // Fields are something like lazy getter functions

              // Ignore empty fields
              if (item.textContent.trim().length == 0) {
                continue;
              }

              addSyntheticLine(indent(3) + `get ${item.attributes.name}() {`, item.textLine);
              addSyntheticLine(indent(4) + `return (`, item.textLine);

              // Remove trailing semicolons, as we are adding our own
              item.textContent = item.textContent.replace(/;(?=\s*$)/, "");
              addNodeLines(item, 5);

              addSyntheticLine(indent(4) + `);`, item.textLine);
              addSyntheticLine(indent(3) + `},`, item.textEndLine);
              break;
            }
            case "constructor":
            case "destructor": {
              // Constructors and destructors become function declarations
              addSyntheticLine(indent(3) + `${item.local}() {`, item.textLine);
              addNodeLines(item, 4);
              addSyntheticLine(indent(3) + `},`, item.textEndLine);
              break;
            }
            case "method": {
              // Methods become function declarations with the appropriate params

              let params = item.children.filter(n => n.local == "parameter" && n.namespace == NS_XBL)
                                        .map(n => n.attributes.name)
                                        .join(", ");
              let body = item.children.filter(n => n.local == "body" && n.namespace == NS_XBL)[0];

              addSyntheticLine(indent(3) + `${item.attributes.name}(${params}) {`, item.textLine);
              addNodeLines(body, 4);
              addSyntheticLine(indent(3) + `},`, item.textEndLine);
              break;
            }
            case "property": {
              // Properties become one or two function declarations
              for (let propdef of item.children) {
                if (propdef.namespace != NS_XBL) {
                  continue;
                }

                if (propdef.local == "setter") {
                  addSyntheticLine(indent(3) + `set ${item.attributes.name}(val) {`, propdef.textLine);
                } else if (propdef.local == "getter") {
                  addSyntheticLine(indent(3) + `get ${item.attributes.name}() {`, propdef.textLine);
                } else {
                  continue;
                }
                addNodeLines(propdef, 4);
                addSyntheticLine(indent(3) + `},`, propdef.textEndLine);
              }
              break;
            }
            case "handler": {
              // Handlers become a function declaration with an `event` parameter
              addSyntheticLine(indent(3) + `function(event) {`, item.textLine);
              addNodeLines(item, 4);
              addSyntheticLine(indent(3) + `},`, item.textEndLine);
              break;
            }
            default:
              continue;
          }
        }

        addSyntheticLine(indent(2) + (part.local == "implementation" ? `},` : `],`), part.textEndLine);
      }
      addSyntheticLine(indent(1) + `},`, binding.textEndLine);
    }
    addSyntheticLine(`};`, bindings.textEndLine);

    let script = scriptLines.join("\n") + "\n";
    return [script];
  },

  postprocess: function(messages, filename) {
    // If there was an XML parse error then just return that
    if (xmlParseError) {
      return [xmlParseError];
    }

    // For every message from every script block update the line to point to the
    // correct place.
    let errors = [];
    for (let i = 0; i < messages.length; i++) {
      for (let message of messages[i]) {
        // ESLint indexes lines starting at 1 but our arrays start at 0
        let mapped = lineMap[message.line - 1];

        message.line = mapped.line + 1;
        if (mapped.offset) {
          message.column -= mapped.offset;
        } else {
          message.column = NaN;
        }

        errors.push(message);
      }
    }

    return errors;
  }
};