diff options
Diffstat (limited to 'tools/lint/eslint/eslint-plugin-spidermonkey-js/lib')
3 files changed, 328 insertions, 0 deletions
diff --git a/tools/lint/eslint/eslint-plugin-spidermonkey-js/lib/environments/self-hosted.js b/tools/lint/eslint/eslint-plugin-spidermonkey-js/lib/environments/self-hosted.js new file mode 100644 index 0000000000..37ae42bfa3 --- /dev/null +++ b/tools/lint/eslint/eslint-plugin-spidermonkey-js/lib/environments/self-hosted.js @@ -0,0 +1,180 @@ +/** + * @fileoverview Add environment defaults to SpiderMonkey's self-hosted JS. + * + * 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 path = require("path"); +const fs = require("fs"); + +let gRootDir = null; + +// Copied from `tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js`. +function getRootDir() { + if (!gRootDir) { + function searchUpForIgnore(dirName, filename) { + let parsed = path.parse(dirName); + while (parsed.root !== dirName) { + if (fs.existsSync(path.join(dirName, filename))) { + return dirName; + } + // Move up a level + dirName = parsed.dir; + parsed = path.parse(dirName); + } + return null; + } + + let possibleRoot = searchUpForIgnore( + path.dirname(module.filename), + ".eslintignore" + ); + if (!possibleRoot) { + possibleRoot = searchUpForIgnore(path.resolve(), ".eslintignore"); + } + if (!possibleRoot) { + possibleRoot = searchUpForIgnore(path.resolve(), "package.json"); + } + if (!possibleRoot) { + // We've couldn't find a root from the module or CWD, so lets just go + // for the CWD. We really don't want to throw if possible, as that + // tends to give confusing results when used with ESLint. + possibleRoot = process.cwd(); + } + + gRootDir = possibleRoot; + } + + return gRootDir; +} + +function tryReadFile(filePath) { + let absPath = path.join(getRootDir(), filePath); + if (!fs.existsSync(absPath)) { + // Safely handle the case when the file wasn't found, because throwing + // errors can lead to confusing result when used with ESLint. + return ""; + } + return fs.readFileSync(absPath, "utf-8"); +} + +// Search for top-level declarations, #defines, and #includes. +function addGlobalsFrom(dirName, fileName, globals) { + let filePath = path.join(dirName, fileName); + + // Definitions are separated by line. + let lines = tryReadFile(filePath).split("\n"); + + // We don't have to fully parse the source code, because it's formatted + // through "prettier", which means we know the exact code structure. + // + // |class| is disallowed in self-hosted code, so we don't have to handle it. + for (let line of lines) { + if ( + line.startsWith("function") || + line.startsWith("function*") || + line.startsWith("async function") || + line.startsWith("async function*") + ) { + let m = line.match(/^(?:async )?function(?:\*)?\s+([\w\$]+)\s*\(/); + if (m) { + globals[m[1]] = "readonly"; + } + } else if ( + line.startsWith("var") || + line.startsWith("let") || + line.startsWith("const") + ) { + let m = line.match(/^(?:var|let|const)\s+([\w\$]+)\s*[;=]/); + if (m) { + globals[m[1]] = "readonly"; + } + } else if (line.startsWith("#define")) { + let m = line.match(/^#define (\w+)/); + if (m) { + globals[m[1]] = "readonly"; + } + } else if (line.startsWith("#include")) { + let m = line.match(/^#include \"([\w\.]+)\"$/); + if (m) { + // Also process definitions from includes. + addGlobalsFrom(dirName, m[1], globals); + } + } + } +} + +function selfHostingDefines(dirName = "js/src/builtin/") { + let absDir = path.join(getRootDir(), dirName); + if (!fs.existsSync(absDir)) { + // See |tryReadFile| for why we avoid to throw any errors. + return {}; + } + + // Search sub-directories and js-files within |dirName|. + let dirs = []; + let jsFiles = []; + for (let name of fs.readdirSync(absDir)) { + let stat = fs.statSync(path.join(absDir, name)); + if (stat.isDirectory()) { + dirs.push(name); + } else if (stat.isFile() && name.endsWith(".js")) { + jsFiles.push(name); + } + } + + let globals = Object.create(null); + + // Process each js-file. + for (let jsFile of jsFiles) { + addGlobalsFrom(dirName, jsFile, globals); + } + + // Recursively traverse all sub-directories. + for (let dir of dirs) { + globals = { ...globals, ...selfHostingDefines(path.join(dirName, dir)) }; + } + + return globals; +} + +function selfHostingFunctions() { + // Definitions can be spread across multiple lines and may have extra + // whitespace, so we simply remove all whitespace and match over the complete + // file. + let content = tryReadFile("js/src/vm/SelfHosting.cpp").replace(/\s+/g, ""); + + let globals = Object.create(null); + for (let m of content.matchAll(/(?:JS_FN|JS_INLINABLE_FN)\("(\w+)"/g)) { + globals[m[1]] = "readonly"; + } + return globals; +} + +function errorNumbers() { + // Definitions are separated by line. + let lines = tryReadFile("js/public/friend/ErrorNumbers.msg").split("\n"); + + let globals = Object.create(null); + for (let line of lines) { + let m = line.match(/^MSG_DEF\((\w+),/); + if (m) { + globals[m[1]] = "readonly"; + } + } + return globals; +} + +const globals = { + ...selfHostingDefines(), + ...selfHostingFunctions(), + ...errorNumbers(), +}; + +module.exports = { + globals, +}; diff --git a/tools/lint/eslint/eslint-plugin-spidermonkey-js/lib/index.js b/tools/lint/eslint/eslint-plugin-spidermonkey-js/lib/index.js new file mode 100644 index 0000000000..d9d40af8c6 --- /dev/null +++ b/tools/lint/eslint/eslint-plugin-spidermonkey-js/lib/index.js @@ -0,0 +1,20 @@ +/** + * @fileoverview A processor to help parse the spidermonkey js code. + * 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"; + +// ------------------------------------------------------------------------------ +// Plugin Definition +// ------------------------------------------------------------------------------ +module.exports = { + processors: { + processor: require("../lib/processors/self-hosted"), + }, + environments: { + environment: require("../lib/environments/self-hosted"), + }, +}; diff --git a/tools/lint/eslint/eslint-plugin-spidermonkey-js/lib/processors/self-hosted.js b/tools/lint/eslint/eslint-plugin-spidermonkey-js/lib/processors/self-hosted.js new file mode 100644 index 0000000000..8a607ffbe5 --- /dev/null +++ b/tools/lint/eslint/eslint-plugin-spidermonkey-js/lib/processors/self-hosted.js @@ -0,0 +1,128 @@ +/** + * @fileoverview Remove macros from SpiderMonkey's self-hosted JS. + * + * 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 path = require("path"); +const fs = require("fs"); + +const selfHostedRegex = /js\/src\/builtin\/.*?\.js$/; +const macroRegex = /\s*\#(if|ifdef|else|elif|endif|include|define|undef).*/; + +function isSelfHostedFile(filename) { + if (path.win32) { + filename = filename.split(path.sep).join("/"); + } + return selfHostedRegex.test(filename); +} + +function tryReadFile(filePath) { + if (!path.isAbsolute(filePath)) { + return ""; + } + if (!fs.existsSync(filePath)) { + // Safely handle the case when the file wasn't found, because throwing + // errors can lead to confusing result when used with ESLint. + return ""; + } + return fs.readFileSync(filePath, "utf-8"); +} + +// Adjust the range of fixes to match the original source code. +function createFix(lines, message) { + let { line, column, fix } = message; + + // Line and column are 1-based. Make sure we got a valid input. + if (line <= 0 || column <= 0) { + return null; + } + + // Reject to create a fix when the line is out of range for some reason. + if (line > lines.length) { + return null; + } + + // Find the absolute start position of the line in the original file. + let startOfLine = 0; + for (let i = 0; i < line - 1; ++i) { + // Add the length of the line, including its line separator. + startOfLine += lines[i].length + "\n".length; + } + + // Add the 1-based column to the start of line to get the start position. + let start = startOfLine + (column - 1); + + // Add the fix range to get the end position. + let end = start + (fix.range[1] - fix.range[0]); + + // And finally return the new fix object. + return { text: fix.text, range: [start, end] }; +} + +module.exports = { + preprocess(text, filename) { + if (!isSelfHostedFile(filename)) { + return [text]; + } + + let lines = text.split(/\n/); + for (let i = 0; i < lines.length; i++) { + if (!macroRegex.test(lines[i])) { + // No macro here, nothing to do. + continue; + } + + for (; i < lines.length; i++) { + // The macro isn't correctly indented, so we need to instruct + // prettier to ignore them. + lines[i] = "// prettier-ignore -- " + lines[i]; + + // If the line ends with a backslash (\), the next line + // is also part of part of the macro. + if (!lines[i].endsWith("\\")) { + break; + } + } + } + + return [lines.join("\n")]; + }, + + postprocess(messages, filename) { + // Don't attempt to create fixes for any non-selfhosted files. + if (!isSelfHostedFile(filename)) { + return [].concat(...messages); + } + + let lines = null; + + let result = []; + for (let message of messages.flat()) { + if (message.fix) { + if (lines === null) { + lines = tryReadFile(filename).split(/\n/); + } + + let fix = createFix(lines, message); + if (fix) { + message.fix = fix; + } else { + // We couldn't create a fix, so we better remove the passed in fix, + // because its range points to the preprocessor output, but the post- + // processor must translate it into a range of the original source. + delete message.fix; + } + } + + result.push(message); + } + return result; + }, + + supportsAutofix: true, +}; |