tools: update ESLint to 5.3.0

PR-URL: https://github.com/nodejs/node/pull/22134
Reviewed-By: Bryan English <bryan@bryanenglish.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
This commit is contained in:
Rich Trott 2018-08-04 21:42:22 -07:00
parent 142660c989
commit 6ad12d47f5
174 changed files with 9821 additions and 5705 deletions

View File

@ -83,6 +83,7 @@ module.exports = {
"newline-per-chained-call": "off",
"no-alert": "off",
"no-array-constructor": "off",
"no-async-promise-executor": "off",
"no-await-in-loop": "off",
"no-bitwise": "off",
"no-buffer-constructor": "off",
@ -139,6 +140,7 @@ module.exports = {
"no-lonely-if": "off",
"no-loop-func": "off",
"no-magic-numbers": "off",
"no-misleading-character-class": "off",
"no-mixed-operators": "off",
"no-mixed-requires": "off",
"no-mixed-spaces-and-tabs": "error",
@ -240,8 +242,10 @@ module.exports = {
"quote-props": "off",
quotes: "off",
radix: "off",
"require-atomic-updates": "off",
"require-await": "off",
"require-jsdoc": "off",
"require-unicode-regexp": "off",
"require-yield": "error",
"rest-spread-spacing": "off",
semi: "off",

View File

@ -22,7 +22,7 @@ const fs = require("fs"),
IgnoredPaths = require("./ignored-paths"),
Config = require("./config"),
LintResultCache = require("./util/lint-result-cache"),
globUtil = require("./util/glob-util"),
globUtils = require("./util/glob-utils"),
validator = require("./config/config-validator"),
hash = require("./util/hash"),
ModuleResolver = require("./util/module-resolver"),
@ -41,7 +41,7 @@ const resolver = new ModuleResolver();
* The options to configure a CLI engine with.
* @typedef {Object} CLIEngineOptions
* @property {boolean} allowInlineConfig Enable or disable inline configuration comments.
* @property {boolean|Object} baseConfig Base config object. True enables recommend rules and environments.
* @property {Object} baseConfig Base config object, extended by all configs used with this CLIEngine instance
* @property {boolean} cache Enable result caching.
* @property {string} cacheLocation The cache file to use instead of .eslintcache.
* @property {string} configFile The configuration file to use.
@ -492,7 +492,7 @@ class CLIEngine {
* @returns {string[]} The equivalent glob patterns.
*/
resolveFileGlobPatterns(patterns) {
return globUtil.resolveFileGlobPatterns(patterns.filter(Boolean), this.options);
return globUtils.resolveFileGlobPatterns(patterns.filter(Boolean), this.options);
}
/**
@ -511,25 +511,25 @@ class CLIEngine {
}
const startTime = Date.now();
const fileList = globUtil.listFilesToProcess(patterns, options);
const fileList = globUtils.listFilesToProcess(patterns, options);
const results = fileList.map(fileInfo => {
if (fileInfo.ignored) {
return createIgnoreResult(fileInfo.filename, options.cwd);
}
if (options.cache) {
/*
* get the descriptor for this file
* with the metadata and the flag that determines if
* the file has changed
*/
const cachedLintResults = lintResultCache.getCachedLintResults(fileInfo.filename);
if (cachedLintResults) {
debug(`Skipping file since it hasn't changed: ${fileInfo.filename}`);
const resultHadMessages = cachedLintResults.messages && cachedLintResults.messages.length;
return cachedLintResults;
if (resultHadMessages && options.fix) {
debug(`Reprocessing cached file to allow autofix: ${fileInfo.filename}`);
} else {
debug(`Skipping file since it hasn't changed: ${fileInfo.filename}`);
return cachedLintResults;
}
}
}

View File

@ -14,7 +14,7 @@ const assert = require("assert"),
CodePathSegment = require("./code-path-segment"),
IdGenerator = require("./id-generator"),
debug = require("./debug-helpers"),
astUtils = require("../ast-utils");
astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -15,7 +15,7 @@ const path = require("path"),
ConfigFile = require("./config/config-file"),
ConfigCache = require("./config/config-cache"),
Plugins = require("./config/plugins"),
FileFinder = require("./file-finder"),
FileFinder = require("./util/file-finder"),
isResolvable = require("is-resolvable");
const debug = require("debug")("eslint:config");

View File

@ -16,9 +16,9 @@ const util = require("util"),
autoconfig = require("./autoconfig.js"),
ConfigFile = require("./config-file"),
ConfigOps = require("./config-ops"),
getSourceCodeOfFiles = require("../util/source-code-util").getSourceCodeOfFiles,
getSourceCodeOfFiles = require("../util/source-code-utils").getSourceCodeOfFiles,
ModuleResolver = require("../util/module-resolver"),
npmUtil = require("../util/npm-util"),
npmUtils = require("../util/npm-utils"),
recConfig = require("../../conf/eslint-recommended"),
log = require("../util/logging");
@ -61,7 +61,7 @@ function writeFile(config, format) {
/**
* Get the peer dependencies of the given module.
* This adds the gotten value to cache at the first time, then reuses it.
* In a process, this function is called twice, but `npmUtil.fetchPeerDependencies` needs to access network which is relatively slow.
* In a process, this function is called twice, but `npmUtils.fetchPeerDependencies` needs to access network which is relatively slow.
* @param {string} moduleName The module name to get.
* @returns {Object} The peer dependencies of the given module.
* This object is the object of `peerDependencies` field of `package.json`.
@ -73,7 +73,7 @@ function getPeerDependencies(moduleName) {
if (!result) {
log.info(`Checking peerDependencies of ${moduleName}`);
result = npmUtil.fetchPeerDependencies(moduleName);
result = npmUtils.fetchPeerDependencies(moduleName);
getPeerDependencies.cache.set(moduleName, result);
}
@ -109,7 +109,7 @@ function getModulesList(config, installESLint) {
if (installESLint === false) {
delete modules.eslint;
} else {
const installStatus = npmUtil.checkDevDeps(["eslint"]);
const installStatus = npmUtils.checkDevDeps(["eslint"]);
// Mark to show messages if it's new installation of eslint.
if (installStatus.eslint === false) {
@ -373,7 +373,7 @@ function hasESLintVersionConflict(answers) {
*/
function installModules(modules) {
log.info(`Installing ${modules.join(", ")}`);
npmUtil.installSyncSaveDev(modules);
npmUtils.installSyncSaveDev(modules);
}
/* istanbul ignore next: no need to test inquirer */
@ -438,7 +438,7 @@ function promptUser() {
{ name: "Google (https://github.com/google/eslint-config-google)", value: "google" }
],
when(answers) {
answers.packageJsonExists = npmUtil.checkPackageJson();
answers.packageJsonExists = npmUtils.checkPackageJson();
return answers.source === "guide" && answers.packageJsonExists;
}
},

View File

@ -12,7 +12,7 @@
const fs = require("fs"),
path = require("path"),
ignore = require("ignore"),
pathUtil = require("./util/path-util");
pathUtils = require("./util/path-utils");
const debug = require("debug")("eslint:ignored-paths");
@ -79,6 +79,40 @@ function mergeDefaultOptions(options) {
return Object.assign({}, DEFAULT_OPTIONS, options);
}
/* eslint-disable valid-jsdoc */
/**
* Normalize the path separators in a given string.
* On Windows environment, this replaces `\` by `/`.
* Otherwrise, this does nothing.
* @param {string} str The path string to normalize.
* @returns {string} The normalized path.
*/
const normalizePathSeps = path.sep === "/"
? (str => str)
: ((seps, str) => str.replace(seps, "/")).bind(null, new RegExp(`\\${path.sep}`, "g"));
/* eslint-enable valid-jsdoc */
/**
* Converts a glob pattern to a new glob pattern relative to a different directory
* @param {string} globPattern The glob pattern, relative the the old base directory
* @param {string} relativePathToOldBaseDir A relative path from the new base directory to the old one
* @returns {string} A glob pattern relative to the new base directory
*/
function relativize(globPattern, relativePathToOldBaseDir) {
if (relativePathToOldBaseDir === "") {
return globPattern;
}
const prefix = globPattern.startsWith("!") ? "!" : "";
const globWithoutPrefix = globPattern.replace(/^!/, "");
if (globWithoutPrefix.startsWith("/")) {
return `${prefix}/${normalizePathSeps(relativePathToOldBaseDir)}${globWithoutPrefix}`;
}
return globPattern;
}
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
@ -96,24 +130,30 @@ class IgnoredPaths {
this.cache = {};
/**
* add pattern to node-ignore instance
* @param {Object} ig, instance of node-ignore
* @param {string} pattern, pattern do add to ig
* @returns {array} raw ignore rules
*/
function addPattern(ig, pattern) {
return ig.addPattern(pattern);
}
this.defaultPatterns = [].concat(DEFAULT_IGNORE_DIRS, options.patterns || []);
this.baseDir = options.cwd;
this.ignoreFileDir = options.ignore !== false && options.ignorePath
? path.dirname(path.resolve(options.cwd, options.ignorePath))
: options.cwd;
this.options = options;
this._baseDir = null;
this.ig = {
custom: ignore(),
default: ignore()
};
this.defaultPatterns.forEach(pattern => this.addPatternRelativeToCwd(this.ig.default, pattern));
if (options.dotfiles !== true) {
/*
* ignore files beginning with a dot, but not files in a parent or
* ancestor directory (which in relative format will begin with `../`).
*/
this.addPatternRelativeToCwd(this.ig.default, ".*");
this.addPatternRelativeToCwd(this.ig.default, "!../");
}
/*
* Add a way to keep track of ignored files. This was present in node-ignore
* 2.x, but dropped for now as of 3.0.10.
@ -121,17 +161,6 @@ class IgnoredPaths {
this.ig.custom.ignoreFiles = [];
this.ig.default.ignoreFiles = [];
if (options.dotfiles !== true) {
/*
* ignore files beginning with a dot, but not files in a parent or
* ancestor directory (which in relative format will begin with `../`).
*/
addPattern(this.ig.default, [".*", "!../"]);
}
addPattern(this.ig.default, this.defaultPatterns);
if (options.ignore !== false) {
let ignorePath;
@ -154,13 +183,11 @@ class IgnoredPaths {
debug(`Loaded ignore file ${ignorePath}`);
} catch (e) {
debug("Could not find ignore file in cwd");
this.options = options;
}
}
if (ignorePath) {
debug(`Adding ${ignorePath}`);
this.baseDir = path.dirname(path.resolve(options.cwd, ignorePath));
this.addIgnoreFile(this.ig.custom, ignorePath);
this.addIgnoreFile(this.ig.default, ignorePath);
} else {
@ -187,8 +214,8 @@ class IgnoredPaths {
if (packageJSONOptions.eslintIgnore) {
if (Array.isArray(packageJSONOptions.eslintIgnore)) {
packageJSONOptions.eslintIgnore.forEach(pattern => {
addPattern(this.ig.custom, pattern);
addPattern(this.ig.default, pattern);
this.addPatternRelativeToIgnoreFile(this.ig.custom, pattern);
this.addPatternRelativeToIgnoreFile(this.ig.default, pattern);
});
} else {
throw new TypeError("Package.json eslintIgnore property requires an array of paths");
@ -202,12 +229,68 @@ class IgnoredPaths {
}
if (options.ignorePattern) {
addPattern(this.ig.custom, options.ignorePattern);
addPattern(this.ig.default, options.ignorePattern);
this.addPatternRelativeToCwd(this.ig.custom, options.ignorePattern);
this.addPatternRelativeToCwd(this.ig.default, options.ignorePattern);
}
}
}
this.options = options;
/*
* If `ignoreFileDir` is a subdirectory of `cwd`, all paths will be normalized to be relative to `cwd`.
* Otherwise, all paths will be normalized to be relative to `ignoreFileDir`.
* This ensures that the final normalized ignore rule will not contain `..`, which is forbidden in
* ignore rules.
*/
addPatternRelativeToCwd(ig, pattern) {
const baseDir = this.getBaseDir();
const cookedPattern = baseDir === this.options.cwd
? pattern
: relativize(pattern, path.relative(baseDir, this.options.cwd));
ig.addPattern(cookedPattern);
debug("addPatternRelativeToCwd:\n original = %j\n cooked = %j", pattern, cookedPattern);
}
addPatternRelativeToIgnoreFile(ig, pattern) {
const baseDir = this.getBaseDir();
const cookedPattern = baseDir === this.ignoreFileDir
? pattern
: relativize(pattern, path.relative(baseDir, this.ignoreFileDir));
ig.addPattern(cookedPattern);
debug("addPatternRelativeToIgnoreFile:\n original = %j\n cooked = %j", pattern, cookedPattern);
}
// Detect the common ancestor
getBaseDir() {
if (!this._baseDir) {
const a = path.resolve(this.options.cwd);
const b = path.resolve(this.ignoreFileDir);
let lastSepPos = 0;
// Set the shorter one (it's the common ancestor if one includes the other).
this._baseDir = a.length < b.length ? a : b;
// Set the common ancestor.
for (let i = 0; i < a.length && i < b.length; ++i) {
if (a[i] !== b[i]) {
this._baseDir = a.slice(0, lastSepPos);
break;
}
if (a[i] === path.sep) {
lastSepPos = i;
}
}
// If it's only Windows drive letter, it needs \
if (/^[A-Z]:$/.test(this._baseDir)) {
this._baseDir += "\\";
}
debug("baseDir = %j", this._baseDir);
}
return this._baseDir;
}
/**
@ -217,7 +300,7 @@ class IgnoredPaths {
*/
readIgnoreFile(filePath) {
if (typeof this.cache[filePath] === "undefined") {
this.cache[filePath] = fs.readFileSync(filePath, "utf8");
this.cache[filePath] = fs.readFileSync(filePath, "utf8").split(/\r?\n/g).filter(Boolean);
}
return this.cache[filePath];
}
@ -226,11 +309,13 @@ class IgnoredPaths {
* add ignore file to node-ignore instance
* @param {Object} ig, instance of node-ignore
* @param {string} filePath, file to add to ig
* @returns {array} raw ignore rules
* @returns {void}
*/
addIgnoreFile(ig, filePath) {
ig.ignoreFiles.push(filePath);
return ig.add(this.readIgnoreFile(filePath));
this
.readIgnoreFile(filePath)
.forEach(ignoreRule => this.addPatternRelativeToIgnoreFile(ig, ignoreRule));
}
/**
@ -243,7 +328,7 @@ class IgnoredPaths {
let result = false;
const absolutePath = path.resolve(this.options.cwd, filepath);
const relativePath = pathUtil.getRelativePath(absolutePath, this.baseDir);
const relativePath = pathUtils.getRelativePath(absolutePath, this.getBaseDir());
if (typeof category === "undefined") {
result = (this.ig.default.filter([relativePath]).length === 0) ||
@ -251,6 +336,9 @@ class IgnoredPaths {
} else {
result = (this.ig[category].filter([relativePath]).length === 0);
}
debug("contains:");
debug(" target = %j", filepath);
debug(" result = %j", result);
return result;
@ -261,13 +349,15 @@ class IgnoredPaths {
* @returns {function()} method to check whether a folder should be ignored by glob.
*/
getIgnoredFoldersGlobChecker() {
const baseDir = this.getBaseDir();
const ig = ignore();
const ig = ignore().add(DEFAULT_IGNORE_DIRS);
DEFAULT_IGNORE_DIRS.forEach(ignoreDir => this.addPatternRelativeToCwd(ig, ignoreDir));
if (this.options.dotfiles !== true) {
// Ignore hidden folders. (This cannot be ".*", or else it's not possible to unignore hidden files)
ig.add([".*/*", "!../"]);
ig.add([".*/*", "!../*"]);
}
if (this.options.ignore) {
@ -276,10 +366,8 @@ class IgnoredPaths {
const filter = ig.createFilter();
const base = this.baseDir;
return function(absolutePath) {
const relative = pathUtil.getRelativePath(absolutePath, base);
const relative = pathUtils.getRelativePath(absolutePath, baseDir);
if (!relative) {
return false;

View File

@ -25,7 +25,7 @@ const eslintScope = require("eslint-scope"),
createReportTranslator = require("./report-translator"),
Rules = require("./rules"),
timing = require("./util/timing"),
astUtils = require("./ast-utils"),
astUtils = require("./util/ast-utils"),
pkg = require("../package.json"),
SourceCodeFixer = require("./util/source-code-fixer");

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -4,7 +4,7 @@
*/
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -11,7 +11,7 @@
const lodash = require("lodash");
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const util = require("../ast-utils");
const util = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
//------------------------------------------------------------------------------
const LETTER_PATTERN = require("../util/patterns/letters");
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -10,7 +10,7 @@
//------------------------------------------------------------------------------
const lodash = require("lodash");
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -4,7 +4,7 @@
*/
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -12,7 +12,7 @@
const lodash = require("lodash");
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -4,7 +4,7 @@
*/
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -10,7 +10,7 @@
const lodash = require("lodash");
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//--------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
const esutils = require("esutils");
//--------------------------------------------------------------------------

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
/**
* Checks whether or not a given variable is a function name.

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -12,7 +12,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -1,5 +1,5 @@
/**
* @fileoverview This option sets a specific tab width for your code
* @fileoverview This rule sets a specific indentation style and width for your code
*
* @author Teddy Katz
* @author Vitaly Puzrin
@ -13,7 +13,7 @@
//------------------------------------------------------------------------------
const lodash = require("lodash");
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
const createTree = require("functional-red-black-tree");
//------------------------------------------------------------------------------
@ -855,7 +855,11 @@ module.exports = {
previousElement &&
previousElementLastToken.loc.end.line - countTrailingLinebreaks(previousElementLastToken.value) > startToken.loc.end.line
) {
offsets.setDesiredOffsets(element.range, firstTokenOfPreviousElement, 0);
offsets.setDesiredOffsets(
[previousElement.range[1], element.range[1]],
firstTokenOfPreviousElement,
0
);
}
}
});
@ -997,6 +1001,31 @@ module.exports = {
return !node || node.loc.start.line === token.loc.start.line;
}
/**
* Check whether there are any blank (whitespace-only) lines between
* two tokens on separate lines.
* @param {Token} firstToken The first token.
* @param {Token} secondToken The second token.
* @returns {boolean} `true` if the tokens are on separate lines and
* there exists a blank line between them, `false` otherwise.
*/
function hasBlankLinesBetween(firstToken, secondToken) {
const firstTokenLine = firstToken.loc.end.line;
const secondTokenLine = secondToken.loc.start.line;
if (firstTokenLine === secondTokenLine || firstTokenLine === secondTokenLine - 1) {
return false;
}
for (let line = firstTokenLine + 1; line < secondTokenLine; ++line) {
if (!tokenInfo.firstTokensByLineNumber.has(line)) {
return true;
}
}
return false;
}
const ignoredNodeFirstTokens = new Set();
const baseOffsetListeners = {
@ -1536,10 +1565,13 @@ module.exports = {
const tokenBefore = precedingTokens.get(firstTokenOfLine);
const tokenAfter = tokenBefore ? sourceCode.getTokenAfter(tokenBefore) : sourceCode.ast.tokens[0];
const mayAlignWithBefore = tokenBefore && !hasBlankLinesBetween(tokenBefore, firstTokenOfLine);
const mayAlignWithAfter = tokenAfter && !hasBlankLinesBetween(firstTokenOfLine, tokenAfter);
// If a comment matches the expected indentation of the token immediately before or after, don't report it.
if (
tokenBefore && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenBefore)) ||
tokenAfter && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenAfter))
mayAlignWithBefore && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenBefore)) ||
mayAlignWithAfter && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenAfter))
) {
return;
}

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Constants

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers
@ -540,7 +540,7 @@ module.exports = {
const length = properties.length,
widths = properties.map(getKeyWidth), // Width of keys, including quotes
align = alignmentOptions.on; // "value" or "colon"
let targetWidth = Math.max.apply(null, widths),
let targetWidth = Math.max(...widths),
beforeColon, afterColon, mode;
if (alignmentOptions && length > 1) { // When aligning values within a group, use the alignment configuration.

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils"),
const astUtils = require("../util/ast-utils"),
keywords = require("../util/keywords");
//------------------------------------------------------------------------------

View File

@ -4,7 +4,7 @@
*/
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition
@ -59,7 +59,7 @@ module.exports = {
above = !options || options === "above";
} else {
above = options.position === "above";
above = !options.position || options.position === "above";
ignorePattern = options.ignorePattern;
if (options.hasOwnProperty("applyDefaultIgnorePatterns")) {

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
//------------------------------------------------------------------------------
const lodash = require("lodash"),
astUtils = require("../ast-utils");
astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -6,7 +6,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -4,7 +4,7 @@
*/
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -266,7 +266,7 @@ module.exports = {
// list of comments to ignore
comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? sourceCode.getAllComments() : [];
// we iterate over comments in parallel with the lines
// we iterate over comments in parallel with the lines
let commentsIndex = 0;
const strings = getAllStrings();

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Constants

View File

@ -9,7 +9,7 @@
//------------------------------------------------------------------------------
const lodash = require("lodash");
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -11,7 +11,7 @@
const lodash = require("lodash");
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -11,7 +11,7 @@
const lodash = require("lodash");
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -4,7 +4,7 @@
*/
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -10,7 +10,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -6,7 +6,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const getPropertyName = require("../ast-utils").getStaticPropertyName;
const getPropertyName = require("../util/ast-utils").getStaticPropertyName;
//------------------------------------------------------------------------------
// Helpers

View File

@ -0,0 +1,33 @@
/**
* @fileoverview disallow using an async function as a Promise executor
* @author Teddy Katz
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: "disallow using an async function as a Promise executor",
category: "Possible Errors",
recommended: false,
url: "https://eslint.org/docs/rules/no-async-promise-executor"
},
fixable: null,
schema: []
},
create(context) {
return {
"NewExpression[callee.name='Promise'][arguments.0.async=true]"(node) {
context.report({
node: context.getSourceCode().getFirstToken(node.arguments[0], token => token.value === "async"),
message: "Promise executor functions should not be async."
});
}
};
}
};

View File

@ -10,7 +10,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -4,7 +4,7 @@
*/
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
const NODE_DESCRIPTIONS = {
DoWhileStatement: "a 'do...while' statement",

View File

@ -6,7 +6,7 @@
"use strict";
const astUtils = require("../ast-utils.js");
const astUtils = require("../util/ast-utils.js");
//------------------------------------------------------------------------------
// Helpers

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
const FixTracker = require("../util/fix-tracker");
//------------------------------------------------------------------------------

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
const globals = require("globals");
//------------------------------------------------------------------------------

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -8,7 +8,7 @@
// Rule Definition
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils.js");
const astUtils = require("../util/ast-utils.js");
module.exports = {
meta: {

View File

@ -10,7 +10,7 @@
//------------------------------------------------------------------------------
const FixTracker = require("../util/fix-tracker");
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -4,7 +4,7 @@
*/
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -10,7 +10,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Constants

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -0,0 +1,189 @@
/**
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";
const { CALL, CONSTRUCT, ReferenceTracker, getStringIfConstant } = require("eslint-utils");
const { RegExpParser, visitRegExpAST } = require("regexpp");
const { isCombiningCharacter, isEmojiModifier, isRegionalIndicatorSymbol, isSurrogatePair } = require("../util/unicode");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Iterate character sequences of a given nodes.
*
* CharacterClassRange syntax can steal a part of character sequence,
* so this function reverts CharacterClassRange syntax and restore the sequence.
*
* @param {regexpp.AST.CharacterClassElement[]} nodes The node list to iterate character sequences.
* @returns {IterableIterator<number[]>} The list of character sequences.
*/
function *iterateCharacterSequence(nodes) {
let seq = [];
for (const node of nodes) {
switch (node.type) {
case "Character":
seq.push(node.value);
break;
case "CharacterClassRange":
seq.push(node.min.value);
yield seq;
seq = [node.max.value];
break;
case "CharacterSet":
if (seq.length > 0) {
yield seq;
seq = [];
}
break;
// no default
}
}
if (seq.length > 0) {
yield seq;
}
}
const hasCharacterSequence = {
surrogatePairWithoutUFlag(chars) {
return chars.some((c, i) => i !== 0 && isSurrogatePair(chars[i - 1], c));
},
combiningClass(chars) {
return chars.some((c, i) => (
i !== 0 &&
isCombiningCharacter(c) &&
!isCombiningCharacter(chars[i - 1])
));
},
emojiModifier(chars) {
return chars.some((c, i) => (
i !== 0 &&
isEmojiModifier(c) &&
!isEmojiModifier(chars[i - 1])
));
},
regionalIndicatorSymbol(chars) {
return chars.some((c, i) => (
i !== 0 &&
isRegionalIndicatorSymbol(c) &&
isRegionalIndicatorSymbol(chars[i - 1])
));
},
zwj(chars) {
const lastIndex = chars.length - 1;
return chars.some((c, i) => (
i !== 0 &&
i !== lastIndex &&
c === 0x200d &&
chars[i - 1] !== 0x200d &&
chars[i + 1] !== 0x200d
));
}
};
const kinds = Object.keys(hasCharacterSequence);
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: "disallow characters which are made with multiple code points in character class syntax",
category: "Possible Errors",
recommended: false,
url: "https://eslint.org/docs/rules/no-misleading-character-class"
},
schema: [],
messages: {
surrogatePairWithoutUFlag: "Unexpected surrogate pair in character class. Use 'u' flag.",
combiningClass: "Unexpected combined character in character class.",
emojiModifier: "Unexpected modified Emoji in character class.",
regionalIndicatorSymbol: "Unexpected national flag in character class.",
zwj: "Unexpected joined character sequence in character class."
}
},
create(context) {
const parser = new RegExpParser();
/**
* Verify a given regular expression.
* @param {Node} node The node to report.
* @param {string} pattern The regular expression pattern to verify.
* @param {string} flags The flags of the regular expression.
* @returns {void}
*/
function verify(node, pattern, flags) {
const patternNode = parser.parsePattern(
pattern,
0,
pattern.length,
flags.includes("u")
);
const has = {
surrogatePairWithoutUFlag: false,
combiningClass: false,
variationSelector: false,
emojiModifier: false,
regionalIndicatorSymbol: false,
zwj: false
};
visitRegExpAST(patternNode, {
onCharacterClassEnter(ccNode) {
for (const chars of iterateCharacterSequence(ccNode.elements)) {
for (const kind of kinds) {
has[kind] = has[kind] || hasCharacterSequence[kind](chars);
}
}
}
});
for (const kind of kinds) {
if (has[kind]) {
context.report({ node, messageId: kind });
}
}
}
return {
"Literal[regex]"(node) {
verify(node, node.regex.pattern, node.regex.flags);
},
"Program"() {
const scope = context.getScope();
const tracker = new ReferenceTracker(scope);
/*
* Iterate calls of RegExp.
* E.g., `new RegExp()`, `RegExp()`, `new window.RegExp()`,
* `const {RegExp: a} = window; new a()`, etc...
*/
for (const { node } of tracker.iterateGlobalReferences({
RegExp: { [CALL]: true, [CONSTRUCT]: true }
})) {
const [patternNode, flagsNode] = node.arguments;
const pattern = getStringIfConstant(patternNode, scope);
const flags = getStringIfConstant(flagsNode, scope);
if (typeof pattern === "string") {
verify(node, pattern, flags || "");
}
}
}
};
}
};

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils.js");
const astUtils = require("../util/ast-utils.js");
//------------------------------------------------------------------------------
// Helpers

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -4,7 +4,7 @@
*/
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition

View File

@ -10,13 +10,12 @@
//------------------------------------------------------------------------------
const Traverser = require("../util/traverser"),
astUtils = require("../ast-utils");
astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const pushAll = Function.apply.bind(Array.prototype.push);
const SENTINEL_PATTERN = /(?:(?:Call|Class|Function|Member|New|Yield)Expression|Statement|Declaration)$/;
const LOOP_PATTERN = /^(?:DoWhile|For|While)Statement$/; // for-in/of statements don't have `test` property.
const GROUP_PATTERN = /^(?:BinaryExpression|ConditionalExpression)$/;
@ -356,7 +355,7 @@ module.exports = {
let scope;
while ((scope = queue.pop())) {
pushAll(queue, scope.childScopes);
queue.push(...scope.childScopes);
scope.variables.forEach(checkReferences);
}

View File

@ -5,7 +5,7 @@
"use strict";
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
// Operators that always result in a boolean value
const BOOLEAN_OPERATORS = new Set(["==", "===", "!=", "!==", ">", ">=", "<", "<=", "in", "instanceof"]);

View File

@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Helpers

Some files were not shown because too many files have changed in this diff Show More