Bug 1375418 - Add eslint 4 support to eslint-plugin-mozilla. r=mossop

Change how comments are handled due to ESLint's 4 reworked comment handling.

MozReview-Commit-ID: BG4cvbhy45Z

--HG--
extra : rebase_source : 8fc4d40bf2a8cb74be7964c9d308c056edf51058
This commit is contained in:
Mark Banner 2017-06-21 15:06:04 +01:00
parent 57d89b1ef4
commit 2991ef15bd
3 changed files with 43 additions and 60 deletions

View File

@ -80,20 +80,44 @@ function GlobalsForNode(filePath) {
}
GlobalsForNode.prototype = {
BlockComment(node, parents) {
let value = node.value.trim();
let match = /^import-globals-from\s+(.+)$/.exec(value);
if (!match) {
return [];
Program(node) {
let globals = [];
for (let comment of node.comments) {
if (comment.type !== "Block") {
continue;
}
let value = comment.value.trim();
value = value.replace(/\n/g, "");
// We have to discover any globals that ESLint would have defined through
// comment directives.
let match = /^globals?\s+(.+)/.exec(value);
if (match) {
let values = parseBooleanConfig(match[1].trim(), node);
for (let name of Object.keys(values)) {
globals.push({
name,
writable: values[name].value
});
}
// We matched globals, so we won't match import-globals-from.
continue;
}
match = /^import-globals-from\s+(.+)$/.exec(value);
if (!match) {
continue;
}
let filePath = match[1].trim();
if (!path.isAbsolute(filePath)) {
filePath = path.resolve(this.dirname, filePath);
}
globals = globals.concat(module.exports.getGlobalsForFile(filePath));
}
let filePath = match[1].trim();
if (!path.isAbsolute(filePath)) {
filePath = path.resolve(this.dirname, filePath);
}
return module.exports.getGlobalsForFile(filePath);
return globals;
},
ExpressionStatement(node, parents, globalScope) {
@ -163,23 +187,6 @@ module.exports = {
let handler = new GlobalsForNode(filePath);
helpers.walkAST(ast, (type, node, parents) => {
// We have to discover any globals that ESLint would have defined through
// comment directives
if (type == "BlockComment") {
let value = node.value.trim();
value = value.replace(/\n/g, "");
let match = /^globals?\s+(.+)/.exec(value);
if (match) {
let values = parseBooleanConfig(match[1].trim(), node);
for (let name of Object.keys(values)) {
globals.push({
name,
writable: values[name].value
});
}
}
}
if (type in handler) {
let newGlobals = handler[type](node, parents, globalScope);
globals.push.apply(globals, newGlobals);

View File

@ -115,8 +115,8 @@ module.exports = {
},
/**
* This walks an AST in a manner similar to ESLint passing node and comment
* events to the listener. The listener is expected to be a simple function
* This walks an AST in a manner similar to ESLint passing node events to the
* listener. The listener is expected to be a simple function
* which accepts node type, node and parents arguments.
*
* @param {Object} ast
@ -128,41 +128,14 @@ module.exports = {
walkAST(ast, listener) {
let parents = [];
let seenComments = new Set();
function sendCommentEvents(comments) {
if (!comments) {
return;
}
for (let comment of comments) {
if (seenComments.has(comment)) {
return;
}
seenComments.add(comment);
listener(comment.type + "Comment", comment, parents);
}
}
estraverse.traverse(ast, {
enter(node, parent) {
// Comments are held in node.comments for empty programs
let leadingComments = node.leadingComments;
if (node.type === "Program" && node.body.length == 0) {
leadingComments = node.comments;
}
sendCommentEvents(leadingComments);
listener(node.type, node, parents);
sendCommentEvents(node.trailingComments);
parents.push(node);
},
leave(node, parent) {
// TODO send comment exit events
listener(node.type + ":exit", node, parents);
if (parents.length == 0) {
throw new Error("Left more nodes than entered.");
}

View File

@ -1,6 +1,6 @@
{
"name": "eslint-plugin-mozilla",
"version": "0.3.4",
"version": "0.4.0",
"description": "A collection of rules that help enforce JavaScript coding standard in the Mozilla project.",
"keywords": [
"eslint",
@ -30,11 +30,14 @@
"devDependencies": {
"mocha": "3.2.0"
},
"peerDependencies": {
"eslint": "^3.0.0 || ^4.0.0"
},
"engines": {
"node": ">=6.9.1"
},
"scripts": {
"prepublishOnly": "node scripts/createExports.js",
"prepack": "node scripts/createExports.js",
"test": "mocha -R dot tests",
"postpublish": "rm -f lib/modules.json lib/environments/saved-globals.json"
},