Bug 1347709 - Allow modules.json to be loaded from a local version for out-of-tree uses of eslint-plugin-mozilla. r=mossop

MozReview-Commit-ID: 7RzAUqNJQ15

--HG--
extra : rebase_source : c71873fd9864032345fb48047f2c272c2703a210
This commit is contained in:
Mark Banner 2017-04-13 12:03:30 +01:00
Родитель 4ef57a002d
Коммит f515ecdff3
8 изменённых файлов: 61 добавлений и 48 удалений

Просмотреть файл

@ -19,7 +19,7 @@ var helpers = require("../helpers");
var globals = require("../globals");
var placesGlobals = require("./places-overlay").globals;
const rootDir = helpers.getRootDir(module.filename);
const rootDir = helpers.rootDir;
// These are scripts not included in global-scripts.inc, but which are loaded
// via overlays.

Просмотреть файл

@ -16,9 +16,7 @@
var path = require("path");
var helpers = require("../helpers");
var globals = require("../globals");
var root = helpers.getRootDir(module.filename);
var modules = require(path.join(root,
"tools", "lint", "eslint", "modules.json"));
var modules = helpers.modulesGlobalData;
const placesOverlayFiles = [
"toolkit/content/globalOverlay.js",
@ -48,7 +46,7 @@ const placesOverlayModules = [
function getScriptGlobals() {
let fileGlobals = [];
for (let file of placesOverlayFiles) {
let fileName = path.join(root, file);
let fileName = path.join(helpers.rootDir, file);
try {
fileGlobals = fileGlobals.concat(globals.getGlobalsForFile(fileName));
} catch (e) {

Просмотреть файл

@ -27,7 +27,7 @@ const simpleTestPath = "testing/mochitest/tests/SimpleTest";
function getScriptGlobals() {
let fileGlobals = [];
let root = helpers.getRootDir(module.filename);
let root = helpers.rootDir;
for (let file of simpleTestFiles) {
let fileName = path.join(root, simpleTestPath, file);
try {

Просмотреть файл

@ -77,7 +77,6 @@ var globalDiscoveryInProgressForFiles = new Set();
function GlobalsForNode(filePath) {
this.path = filePath;
this.dirname = path.dirname(this.path);
this.root = helpers.getRootDir(this.path);
}
GlobalsForNode.prototype = {
@ -99,7 +98,7 @@ GlobalsForNode.prototype = {
ExpressionStatement(node, parents, globalScope) {
let isGlobal = helpers.getIsGlobalScope(parents);
let globals = helpers.convertExpressionToGlobals(node, isGlobal, this.root);
let globals = helpers.convertExpressionToGlobals(node, isGlobal);
// Map these globals now, as getGlobalsForFile is pre-mapped.
globals = globals.map(name => {
return { name, writable: true };
@ -110,7 +109,7 @@ GlobalsForNode.prototype = {
// the environment directly.
if (globalScope && globalScope.set.get("importScripts")) {
let workerDetails = helpers.convertWorkerExpressionToGlobals(node,
isGlobal, this.root, this.dirname);
isGlobal, this.dirname);
globals = globals.concat(workerDetails);
}

Просмотреть файл

@ -12,7 +12,8 @@ var path = require("path");
var fs = require("fs");
var ini = require("ini-parser");
var modules = null;
var gModules = null;
var gRootDir = null;
var directoryManifests = new Map();
var definitions = [
@ -40,6 +41,18 @@ var imports = [
var workerImportFilenameMatch = /(.*\/)*(.*?\.jsm?)/;
module.exports = {
get modulesGlobalData() {
if (!gModules) {
if (this.isMozillaCentralBased()) {
gModules = require(path.join(this.rootDir, "tools", "lint", "eslint", "modules.json"));
} else {
gModules = require("./modules.json");
}
}
return gModules;
},
/**
* Gets the abstract syntax tree (AST) of the JavaScript source code contained
* in sourceText.
@ -168,8 +181,6 @@ module.exports = {
* The AST node to convert.
* @param {boolean} isGlobal
* True if the current node is in the global scope.
* @param {String} repository
* The root of the repository.
*
* @return {Array}
* An array of objects that contain details about the globals:
@ -178,14 +189,10 @@ module.exports = {
* - {Boolean} writable
* If the global is writeable or not.
*/
convertWorkerExpressionToGlobals(node, isGlobal, repository,
dirname) {
convertWorkerExpressionToGlobals(node, isGlobal, dirname) {
var getGlobalsForFile = require("./globals").getGlobalsForFile;
if (!modules) {
modules = require(path.join(repository,
"tools", "lint", "eslint", "modules.json"));
}
let globalModules = this.modulesGlobalData;
let results = [];
let expr = node.expression;
@ -203,8 +210,8 @@ module.exports = {
let additionalGlobals = getGlobalsForFile(filePath);
results = results.concat(additionalGlobals);
}
} else if (match[2] in modules) {
results = results.concat(modules[match[2]].map(name => {
} else if (match[2] in globalModules) {
results = results.concat(globalModules[match[2]].map(name => {
return { name, writable: true };
}));
}
@ -215,12 +222,7 @@ module.exports = {
return results;
},
convertExpressionToGlobals(node, isGlobal, repository) {
if (!modules) {
modules = require(path.join(repository,
"tools", "lint", "eslint", "modules.json"));
}
convertExpressionToGlobals(node, isGlobal) {
try {
var source = this.getASTSource(node);
} catch (e) {
@ -239,6 +241,8 @@ module.exports = {
}
}
let globalModules = this.modulesGlobalData;
for (reg of imports) {
let match = source.match(reg);
if (match) {
@ -247,8 +251,8 @@ module.exports = {
return [];
}
if (match[1] in modules) {
return modules[match[1]];
if (match[1] in globalModules) {
return globalModules[match[1]];
}
return [match[2]];
@ -513,15 +517,13 @@ module.exports = {
},
/**
* Gets the root directory of the repository by walking up directories until
* a .eslintignore file is found.
* @param {String} fileName
* The absolute path of a file in the repository
*
* Gets the root directory of the repository by walking up directories from
* this file until a .eslintignore file is found.
* @return {String} The absolute path of the repository directory
*/
getRootDir(fileName) {
var dirName = path.dirname(fileName);
get rootDir() {
if (!gRootDir) {
let dirName = path.dirname(module.filename);
while (dirName && !fs.existsSync(path.join(dirName, ".eslintignore"))) {
dirName = path.dirname(dirName);
@ -531,7 +533,10 @@ module.exports = {
throw new Error("Unable to find root of repository");
}
return dirName;
gRootDir = dirName;
}
return gRootDir;
},
/**
@ -576,7 +581,7 @@ module.exports = {
},
get globalScriptsPath() {
return path.join(this.getRootDir(module.filename), "browser",
return path.join(this.rootDir, "browser",
"base", "content", "global-scripts.inc");
},

Просмотреть файл

@ -25,7 +25,7 @@ module.exports = function(context) {
return {
Program(node) {
let filePath = helpers.getAbsoluteFilePath(context);
let relativePath = path.relative(helpers.getRootDir(filePath), filePath);
let relativePath = path.relative(helpers.rootDir, filePath);
if (browserWindowEnv.browserjsScripts &&
browserWindowEnv.browserjsScripts.includes(relativePath)) {

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "eslint-plugin-mozilla",
"version": "0.2.37",
"version": "0.2.38",
"description": "A collection of rules that help enforce JavaScript coding standard in the Mozilla project.",
"keywords": [
"eslint",
@ -32,7 +32,8 @@
},
"scripts": {
"prepublishOnly": "node scripts/createExports.js",
"test": "node tests/test-run-all.js"
"test": "node tests/test-run-all.js",
"postpublish": "rm -f lib/modules.json lib/environments/saved-globals.json"
},
"license": "MPL-2.0"
}

Просмотреть файл

@ -10,10 +10,20 @@ var fs = require("fs");
var path = require("path");
var helpers = require("../lib/helpers");
const globalsFile = path.join(helpers.getRootDir(module.filename),
"tools", "lint", "eslint", "eslint-plugin-mozilla",
const eslintDir = path.join(helpers.getRootDir(module.filename),
"tools", "lint", "eslint");
const globalsFile = path.join(eslintDir, "eslint-plugin-mozilla",
"lib", "environments", "saved-globals.json");
console.log("Copying modules.json");
const modulesFile = path.join(eslintDir, "modules.json");
const shipModulesFile = path.join(eslintDir, "eslint-plugin-mozilla", "lib",
"modules.json");
fs.writeFileSync(shipModulesFile, fs.readFileSync(modulesFile));
console.log("Generating globals file");
// We only export the configs section.