Comparing version 8.42.0 to 8.55.0
@@ -96,2 +96,15 @@ #!/usr/bin/env node | ||
/** | ||
* Tracks error messages that are shown to the user so we only ever show the | ||
* same message once. | ||
* @type {Set<string>} | ||
*/ | ||
const displayedErrors = new Set(); | ||
/** | ||
* Tracks whether an unexpected error was caught | ||
* @type {boolean} | ||
*/ | ||
let hadFatalError = false; | ||
/** | ||
* Catch and report unexpected error. | ||
@@ -103,7 +116,6 @@ * @param {any} error The thrown error object. | ||
process.exitCode = 2; | ||
hadFatalError = true; | ||
const { version } = require("../package.json"); | ||
const message = getErrorMessage(error); | ||
console.error(` | ||
const message = ` | ||
Oops! Something went wrong! :( | ||
@@ -113,3 +125,8 @@ | ||
${message}`); | ||
${getErrorMessage(error)}`; | ||
if (!displayedErrors.has(message)) { | ||
console.error(message); | ||
displayedErrors.add(message); | ||
} | ||
} | ||
@@ -138,3 +155,3 @@ | ||
// Otherwise, call the CLI. | ||
process.exitCode = await require("../lib/cli").execute( | ||
const exitCode = await require("../lib/cli").execute( | ||
process.argv, | ||
@@ -144,2 +161,18 @@ process.argv.includes("--stdin") ? await readStdin() : null, | ||
); | ||
/* | ||
* If an uncaught exception or unhandled rejection was detected in the meantime, | ||
* keep the fatal exit code 2 that is already assigned to `process.exitCode`. | ||
* Without this condition, exit code 2 (unsuccessful execution) could be overwritten with | ||
* 1 (successful execution, lint problems found) or even 0 (successful execution, no lint problems found). | ||
* This ensures that unexpected errors that seemingly don't affect the success | ||
* of the execution will still cause a non-zero exit code, as it's a common | ||
* practice and the default behavior of Node.js to exit with non-zero | ||
* in case of an uncaught exception or unhandled rejection. | ||
* | ||
* Otherwise, assign the exit code returned from CLI. | ||
*/ | ||
if (!hadFatalError) { | ||
process.exitCode = exitCode; | ||
} | ||
}()).catch(onFatalError); |
@@ -131,3 +131,7 @@ /** | ||
const es2024 = { | ||
...es2023 | ||
}; | ||
//----------------------------------------------------------------------------- | ||
@@ -149,3 +153,4 @@ // Exports | ||
es2022, | ||
es2023 | ||
es2023, | ||
es2024 | ||
}; |
{ | ||
"types": [ | ||
{ "name": "problem", "displayName": "Possible Problems", "description": "These rules relate to possible logic errors in code:" }, | ||
{ "name": "suggestion", "displayName": "Suggestions", "description": "These rules suggest alternate ways of doing things:" }, | ||
{ "name": "layout", "displayName": "Layout & Formatting", "description": "These rules care about how the code looks rather than how it executes:" } | ||
], | ||
"deprecated": { | ||
"name": "Deprecated", | ||
"description": "These rules have been deprecated in accordance with the <a href=\"{{ '/use/rule-deprecation' | url }}\">deprecation policy</a>, and replaced by newer rules:", | ||
"rules": [] | ||
"types": { | ||
"problem": [], | ||
"suggestion": [], | ||
"layout": [] | ||
}, | ||
"removed": { | ||
"name": "Removed", | ||
"description": "These rules from older versions of ESLint (before the <a href=\"{{ '/use/rule-deprecation' | url }}\">deprecation policy</a> existed) have been replaced by newer rules:", | ||
"rules": [ | ||
{ "removed": "generator-star", "replacedBy": ["generator-star-spacing"] }, | ||
{ "removed": "global-strict", "replacedBy": ["strict"] }, | ||
{ "removed": "no-arrow-condition", "replacedBy": ["no-confusing-arrow", "no-constant-condition"] }, | ||
{ "removed": "no-comma-dangle", "replacedBy": ["comma-dangle"] }, | ||
{ "removed": "no-empty-class", "replacedBy": ["no-empty-character-class"] }, | ||
{ "removed": "no-empty-label", "replacedBy": ["no-labels"] }, | ||
{ "removed": "no-extra-strict", "replacedBy": ["strict"] }, | ||
{ "removed": "no-reserved-keys", "replacedBy": ["quote-props"] }, | ||
{ "removed": "no-space-before-semi", "replacedBy": ["semi-spacing"] }, | ||
{ "removed": "no-wrap-func", "replacedBy": ["no-extra-parens"] }, | ||
{ "removed": "space-after-function-name", "replacedBy": ["space-before-function-paren"] }, | ||
{ "removed": "space-after-keywords", "replacedBy": ["keyword-spacing"] }, | ||
{ "removed": "space-before-function-parentheses", "replacedBy": ["space-before-function-paren"] }, | ||
{ "removed": "space-before-keywords", "replacedBy": ["keyword-spacing"] }, | ||
{ "removed": "space-in-brackets", "replacedBy": ["object-curly-spacing", "array-bracket-spacing"] }, | ||
{ "removed": "space-return-throw-case", "replacedBy": ["keyword-spacing"] }, | ||
{ "removed": "space-unary-word-ops", "replacedBy": ["space-unary-ops"] }, | ||
{ "removed": "spaced-line-comment", "replacedBy": ["spaced-comment"] } | ||
] | ||
} | ||
"deprecated": [], | ||
"removed": [ | ||
{ "removed": "generator-star", "replacedBy": ["generator-star-spacing"] }, | ||
{ "removed": "global-strict", "replacedBy": ["strict"] }, | ||
{ "removed": "no-arrow-condition", "replacedBy": ["no-confusing-arrow", "no-constant-condition"] }, | ||
{ "removed": "no-comma-dangle", "replacedBy": ["comma-dangle"] }, | ||
{ "removed": "no-empty-class", "replacedBy": ["no-empty-character-class"] }, | ||
{ "removed": "no-empty-label", "replacedBy": ["no-labels"] }, | ||
{ "removed": "no-extra-strict", "replacedBy": ["strict"] }, | ||
{ "removed": "no-reserved-keys", "replacedBy": ["quote-props"] }, | ||
{ "removed": "no-space-before-semi", "replacedBy": ["semi-spacing"] }, | ||
{ "removed": "no-wrap-func", "replacedBy": ["no-extra-parens"] }, | ||
{ "removed": "space-after-function-name", "replacedBy": ["space-before-function-paren"] }, | ||
{ "removed": "space-after-keywords", "replacedBy": ["keyword-spacing"] }, | ||
{ "removed": "space-before-function-parentheses", "replacedBy": ["space-before-function-paren"] }, | ||
{ "removed": "space-before-keywords", "replacedBy": ["keyword-spacing"] }, | ||
{ "removed": "space-in-brackets", "replacedBy": ["object-curly-spacing", "array-bracket-spacing"] }, | ||
{ "removed": "space-return-throw-case", "replacedBy": ["keyword-spacing"] }, | ||
{ "removed": "space-unary-word-ops", "replacedBy": ["space-unary-ops"] }, | ||
{ "removed": "spaced-line-comment", "replacedBy": ["spaced-comment"] } | ||
] | ||
} |
@@ -161,3 +161,13 @@ /** | ||
function calculateStatsPerFile(messages) { | ||
return messages.reduce((stat, message) => { | ||
const stat = { | ||
errorCount: 0, | ||
fatalErrorCount: 0, | ||
warningCount: 0, | ||
fixableErrorCount: 0, | ||
fixableWarningCount: 0 | ||
}; | ||
for (let i = 0; i < messages.length; i++) { | ||
const message = messages[i]; | ||
if (message.fatal || message.severity === 2) { | ||
@@ -177,10 +187,4 @@ stat.errorCount++; | ||
} | ||
return stat; | ||
}, { | ||
errorCount: 0, | ||
fatalErrorCount: 0, | ||
warningCount: 0, | ||
fixableErrorCount: 0, | ||
fixableWarningCount: 0 | ||
}); | ||
} | ||
return stat; | ||
} | ||
@@ -195,3 +199,13 @@ | ||
function calculateStatsPerRun(results) { | ||
return results.reduce((stat, result) => { | ||
const stat = { | ||
errorCount: 0, | ||
fatalErrorCount: 0, | ||
warningCount: 0, | ||
fixableErrorCount: 0, | ||
fixableWarningCount: 0 | ||
}; | ||
for (let i = 0; i < results.length; i++) { | ||
const result = results[i]; | ||
stat.errorCount += result.errorCount; | ||
@@ -202,10 +216,5 @@ stat.fatalErrorCount += result.fatalErrorCount; | ||
stat.fixableWarningCount += result.fixableWarningCount; | ||
return stat; | ||
}, { | ||
errorCount: 0, | ||
fatalErrorCount: 0, | ||
warningCount: 0, | ||
fixableErrorCount: 0, | ||
fixableWarningCount: 0 | ||
}); | ||
} | ||
return stat; | ||
} | ||
@@ -212,0 +221,0 @@ |
@@ -131,12 +131,24 @@ /** | ||
const cachedResults = fileDescriptor.meta.results; | ||
// Just in case, not sure if this can ever happen. | ||
if (!cachedResults) { | ||
return cachedResults; | ||
} | ||
/* | ||
* Shallow clone the object to ensure that any properties added or modified afterwards | ||
* will not be accidentally stored in the cache file when `reconcile()` is called. | ||
* https://github.com/eslint/eslint/issues/13507 | ||
* All intentional changes to the cache file must be done through `setCachedLintResults()`. | ||
*/ | ||
const results = { ...cachedResults }; | ||
// If source is present but null, need to reread the file from the filesystem. | ||
if ( | ||
fileDescriptor.meta.results && | ||
fileDescriptor.meta.results.source === null | ||
) { | ||
if (results.source === null) { | ||
debug(`Rereading cached result source from filesystem: ${filePath}`); | ||
fileDescriptor.meta.results.source = fs.readFileSync(filePath, "utf-8"); | ||
results.source = fs.readFileSync(filePath, "utf-8"); | ||
} | ||
return fileDescriptor.meta.results; | ||
return results; | ||
} | ||
@@ -143,0 +155,0 @@ |
@@ -94,3 +94,4 @@ /** | ||
rule, | ||
rulesdir | ||
rulesdir, | ||
warnIgnored | ||
}, configType) { | ||
@@ -186,2 +187,3 @@ | ||
options.ignorePatterns = ignorePattern; | ||
options.warnIgnored = warnIgnored; | ||
} else { | ||
@@ -321,3 +323,10 @@ options.resolvePluginsRelativeTo = resolvePluginsRelativeTo; | ||
debug("Error parsing CLI options:", error.message); | ||
log.error(error.message); | ||
let errorMessage = error.message; | ||
if (usingFlatConfig) { | ||
errorMessage += "\nYou're using eslint.config.js, some command line flags are no longer available. Please see https://eslint.org/docs/latest/use/command-line-interface for details."; | ||
} | ||
log.error(errorMessage); | ||
return 2; | ||
@@ -391,3 +400,5 @@ } | ||
filePath: options.stdinFilename, | ||
warnIgnored: true | ||
// flatConfig respects CLI flag and constructor warnIgnored, eslintrc forces true for backwards compatibility | ||
warnIgnored: usingFlatConfig ? void 0 : true | ||
}); | ||
@@ -394,0 +405,0 @@ } else { |
@@ -9,2 +9,12 @@ /** | ||
//----------------------------------------------------------------------------- | ||
// Requirements | ||
//----------------------------------------------------------------------------- | ||
/* | ||
* Note: This can be removed in ESLint v9 because structuredClone is available globally | ||
* starting in Node.js v17. | ||
*/ | ||
const structuredClone = require("@ungap/structured-clone").default; | ||
//----------------------------------------------------------------------------- | ||
// Type Definitions | ||
@@ -123,3 +133,3 @@ //----------------------------------------------------------------------------- | ||
finalOptions[0] = ruleSeverities.get(finalOptions[0]); | ||
return finalOptions; | ||
return structuredClone(finalOptions); | ||
} | ||
@@ -184,5 +194,3 @@ | ||
function assertIsRuleSeverity(ruleId, value) { | ||
const severity = typeof value === "string" | ||
? ruleSeverities.get(value.toLowerCase()) | ||
: ruleSeverities.get(value); | ||
const severity = ruleSeverities.get(value); | ||
@@ -218,2 +226,34 @@ if (typeof severity === "undefined") { | ||
/** | ||
* The error type when there's an eslintrc-style options in a flat config. | ||
*/ | ||
class IncompatibleKeyError extends Error { | ||
/** | ||
* @param {string} key The invalid key. | ||
*/ | ||
constructor(key) { | ||
super("This appears to be in eslintrc format rather than flat config format."); | ||
this.messageTemplate = "eslintrc-incompat"; | ||
this.messageData = { key }; | ||
} | ||
} | ||
/** | ||
* The error type when there's an eslintrc-style plugins array found. | ||
*/ | ||
class IncompatiblePluginsError extends Error { | ||
/** | ||
* Creates a new instance. | ||
* @param {Array<string>} plugins The plugins array. | ||
*/ | ||
constructor(plugins) { | ||
super("This appears to be in eslintrc format (array of strings) rather than flat config format (object)."); | ||
this.messageTemplate = "eslintrc-plugins"; | ||
this.messageData = { plugins }; | ||
} | ||
} | ||
//----------------------------------------------------------------------------- | ||
@@ -310,2 +350,7 @@ // Low-Level Schemas | ||
// make sure it's not an array, which would mean eslintrc-style is used | ||
if (Array.isArray(value)) { | ||
throw new IncompatiblePluginsError(value); | ||
} | ||
// second check the keys to make sure they are objects | ||
@@ -351,44 +396,53 @@ for (const key of Object.keys(value)) { | ||
for (const ruleId of Object.keys(result)) { | ||
// avoid hairy edge case | ||
if (ruleId === "__proto__") { | ||
try { | ||
/* eslint-disable-next-line no-proto -- Though deprecated, may still be present */ | ||
delete result.__proto__; | ||
continue; | ||
} | ||
// avoid hairy edge case | ||
if (ruleId === "__proto__") { | ||
result[ruleId] = normalizeRuleOptions(result[ruleId]); | ||
/* eslint-disable-next-line no-proto -- Though deprecated, may still be present */ | ||
delete result.__proto__; | ||
continue; | ||
} | ||
/* | ||
* If either rule config is missing, then the correct | ||
* config is already present and we just need to normalize | ||
* the severity. | ||
*/ | ||
if (!(ruleId in first) || !(ruleId in second)) { | ||
continue; | ||
} | ||
result[ruleId] = normalizeRuleOptions(result[ruleId]); | ||
const firstRuleOptions = normalizeRuleOptions(first[ruleId]); | ||
const secondRuleOptions = normalizeRuleOptions(second[ruleId]); | ||
/* | ||
* If either rule config is missing, then the correct | ||
* config is already present and we just need to normalize | ||
* the severity. | ||
*/ | ||
if (!(ruleId in first) || !(ruleId in second)) { | ||
continue; | ||
} | ||
/* | ||
* If the second rule config only has a severity (length of 1), | ||
* then use that severity and keep the rest of the options from | ||
* the first rule config. | ||
*/ | ||
if (secondRuleOptions.length === 1) { | ||
result[ruleId] = [secondRuleOptions[0], ...firstRuleOptions.slice(1)]; | ||
continue; | ||
const firstRuleOptions = normalizeRuleOptions(first[ruleId]); | ||
const secondRuleOptions = normalizeRuleOptions(second[ruleId]); | ||
/* | ||
* If the second rule config only has a severity (length of 1), | ||
* then use that severity and keep the rest of the options from | ||
* the first rule config. | ||
*/ | ||
if (secondRuleOptions.length === 1) { | ||
result[ruleId] = [secondRuleOptions[0], ...firstRuleOptions.slice(1)]; | ||
continue; | ||
} | ||
/* | ||
* In any other situation, then the second rule config takes | ||
* precedence. That means the value at `result[ruleId]` is | ||
* already correct and no further work is necessary. | ||
*/ | ||
} catch (ex) { | ||
throw new Error(`Key "${ruleId}": ${ex.message}`, { cause: ex }); | ||
} | ||
/* | ||
* In any other situation, then the second rule config takes | ||
* precedence. That means the value at `result[ruleId]` is | ||
* already correct and no further work is necessary. | ||
*/ | ||
} | ||
return result; | ||
}, | ||
@@ -447,2 +501,30 @@ | ||
/** | ||
* Creates a schema that always throws an error. Useful for warning | ||
* about eslintrc-style keys. | ||
* @param {string} key The eslintrc key to create a schema for. | ||
* @returns {ObjectPropertySchema} The schema. | ||
*/ | ||
function createEslintrcErrorSchema(key) { | ||
return { | ||
merge: "replace", | ||
validate() { | ||
throw new IncompatibleKeyError(key); | ||
} | ||
}; | ||
} | ||
const eslintrcKeys = [ | ||
"env", | ||
"extends", | ||
"globals", | ||
"ignorePatterns", | ||
"noInlineConfig", | ||
"overrides", | ||
"parser", | ||
"parserOptions", | ||
"reportUnusedDisableDirectives", | ||
"root" | ||
]; | ||
//----------------------------------------------------------------------------- | ||
@@ -452,3 +534,8 @@ // Full schema | ||
exports.flatConfigSchema = { | ||
const flatConfigSchema = { | ||
// eslintrc-style keys that should always error | ||
...Object.fromEntries(eslintrcKeys.map(key => [key, createEslintrcErrorSchema(key)])), | ||
// flat config keys | ||
settings: deepObjectAssignSchema, | ||
@@ -474,1 +561,11 @@ linterOptions: { | ||
}; | ||
//----------------------------------------------------------------------------- | ||
// Exports | ||
//----------------------------------------------------------------------------- | ||
module.exports = { | ||
flatConfigSchema, | ||
assertIsRuleSeverity, | ||
assertIsRuleOptions | ||
}; |
@@ -12,3 +12,4 @@ /** | ||
const ajv = require("../shared/ajv")(); | ||
const ajvImport = require("../shared/ajv"); | ||
const ajv = ajvImport(); | ||
const { | ||
@@ -15,0 +16,0 @@ parseRuleId, |
@@ -597,5 +597,5 @@ /** | ||
if (isInNodeModules) { | ||
message = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override."; | ||
message = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to disable file ignore settings or use \"--no-warn-ignored\" to suppress this warning."; | ||
} else { | ||
message = "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override."; | ||
message = "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to disable file ignore settings or use \"--no-warn-ignored\" to suppress this warning."; | ||
} | ||
@@ -680,2 +680,3 @@ | ||
reportUnusedDisableDirectives = null, // ← should be null by default because if it's a string then it overrides the 'reportUnusedDisableDirectives' setting in config files. And we cannot use `overrideConfig.reportUnusedDisableDirectives` instead because we cannot configure the `error` severity with that. | ||
warnIgnored = true, | ||
...unknownOptions | ||
@@ -786,2 +787,5 @@ }) { | ||
} | ||
if (typeof warnIgnored !== "boolean") { | ||
errors.push("'warnIgnored' must be a boolean."); | ||
} | ||
if (errors.length > 0) { | ||
@@ -801,3 +805,3 @@ throw new ESLintInvalidOptionsError(errors); | ||
overrideConfig, | ||
cwd, | ||
cwd: path.normalize(cwd), | ||
errorOnUnmatchedPattern, | ||
@@ -809,3 +813,4 @@ fix, | ||
ignorePatterns, | ||
reportUnusedDisableDirectives | ||
reportUnusedDisableDirectives, | ||
warnIgnored | ||
}; | ||
@@ -812,0 +817,0 @@ } |
@@ -292,3 +292,3 @@ /** | ||
configFile: overrideConfigFile, | ||
cwd, | ||
cwd: path.normalize(cwd), | ||
errorOnUnmatchedPattern, | ||
@@ -295,0 +295,0 @@ extensions, |
@@ -87,2 +87,3 @@ /** | ||
* @property {"error" | "warn" | "off"} [reportUnusedDisableDirectives] the severity to report unused eslint-disable directives. | ||
* @property {boolean} warnIgnored Show warnings when the file list includes ignored files | ||
*/ | ||
@@ -107,3 +108,13 @@ | ||
function calculateStatsPerFile(messages) { | ||
return messages.reduce((stat, message) => { | ||
const stat = { | ||
errorCount: 0, | ||
fatalErrorCount: 0, | ||
warningCount: 0, | ||
fixableErrorCount: 0, | ||
fixableWarningCount: 0 | ||
}; | ||
for (let i = 0; i < messages.length; i++) { | ||
const message = messages[i]; | ||
if (message.fatal || message.severity === 2) { | ||
@@ -123,36 +134,7 @@ stat.errorCount++; | ||
} | ||
return stat; | ||
}, { | ||
errorCount: 0, | ||
fatalErrorCount: 0, | ||
warningCount: 0, | ||
fixableErrorCount: 0, | ||
fixableWarningCount: 0 | ||
}); | ||
} | ||
return stat; | ||
} | ||
/** | ||
* It will calculate the error and warning count for collection of results from all files | ||
* @param {LintResult[]} results Collection of messages from all the files | ||
* @returns {Object} Contains the stats | ||
* @private | ||
*/ | ||
function calculateStatsPerRun(results) { | ||
return results.reduce((stat, result) => { | ||
stat.errorCount += result.errorCount; | ||
stat.fatalErrorCount += result.fatalErrorCount; | ||
stat.warningCount += result.warningCount; | ||
stat.fixableErrorCount += result.fixableErrorCount; | ||
stat.fixableWarningCount += result.fixableWarningCount; | ||
return stat; | ||
}, { | ||
errorCount: 0, | ||
fatalErrorCount: 0, | ||
warningCount: 0, | ||
fixableErrorCount: 0, | ||
fixableWarningCount: 0 | ||
}); | ||
} | ||
/** | ||
* Create rulesMeta object. | ||
@@ -557,39 +539,2 @@ * @param {Map<string,Rule>} rules a map of rules from which to generate the object. | ||
/** | ||
* Collect used deprecated rules. | ||
* @param {Array<FlatConfig>} configs The configs to evaluate. | ||
* @returns {IterableIterator<DeprecatedRuleInfo>} Used deprecated rules. | ||
*/ | ||
function *iterateRuleDeprecationWarnings(configs) { | ||
const processedRuleIds = new Set(); | ||
for (const config of configs) { | ||
for (const [ruleId, ruleConfig] of Object.entries(config.rules)) { | ||
// Skip if it was processed. | ||
if (processedRuleIds.has(ruleId)) { | ||
continue; | ||
} | ||
processedRuleIds.add(ruleId); | ||
// Skip if it's not used. | ||
if (!getRuleSeverity(ruleConfig)) { | ||
continue; | ||
} | ||
const rule = getRuleFromConfig(ruleId, config); | ||
// Skip if it's not deprecated. | ||
if (!(rule && rule.meta && rule.meta.deprecated)) { | ||
continue; | ||
} | ||
// This rule was used and deprecated. | ||
yield { | ||
ruleId, | ||
replacedBy: rule.meta.replacedBy || [] | ||
}; | ||
} | ||
} | ||
} | ||
/** | ||
* Creates an error to be thrown when an array of results passed to `getRulesMetaForResults` was not created by the current engine. | ||
@@ -639,3 +584,2 @@ * @returns {TypeError} An error object. | ||
defaultConfigs, | ||
defaultIgnores: () => false, | ||
configs: null | ||
@@ -779,8 +723,6 @@ }); | ||
// ensure the rule exists | ||
if (!rule) { | ||
throw new TypeError(`Could not find the rule "${ruleId}".`); | ||
// ignore unknown rules | ||
if (rule) { | ||
resultRules.set(ruleId, rule); | ||
} | ||
resultRules.set(ruleId, rule); | ||
} | ||
@@ -817,6 +759,6 @@ } | ||
globInputPaths, | ||
errorOnUnmatchedPattern | ||
errorOnUnmatchedPattern, | ||
warnIgnored | ||
} = eslintOptions; | ||
const startTime = Date.now(); | ||
const usedConfigs = []; | ||
const fixTypesSet = fixTypes ? new Set(fixTypes) : null; | ||
@@ -865,3 +807,7 @@ | ||
if (ignored) { | ||
return createIgnoreResult(filePath, cwd); | ||
if (warnIgnored) { | ||
return createIgnoreResult(filePath, cwd); | ||
} | ||
return void 0; | ||
} | ||
@@ -880,11 +826,2 @@ | ||
/* | ||
* Store used configs for: | ||
* - this method uses to collect used deprecated rules. | ||
* - `--fix-type` option uses to get the loaded rule's meta data. | ||
*/ | ||
if (!usedConfigs.includes(config)) { | ||
usedConfigs.push(config); | ||
} | ||
// Skip if there is cached result. | ||
@@ -958,18 +895,6 @@ if (lintResultCache) { | ||
let usedDeprecatedRules; | ||
const finalResults = results.filter(result => !!result); | ||
return processLintReport(this, { | ||
results: finalResults, | ||
...calculateStatsPerRun(finalResults), | ||
// Initialize it lazily because CLI and `ESLint` API don't use it. | ||
get usedDeprecatedRules() { | ||
if (!usedDeprecatedRules) { | ||
usedDeprecatedRules = Array.from( | ||
iterateRuleDeprecationWarnings(usedConfigs) | ||
); | ||
} | ||
return usedDeprecatedRules; | ||
} | ||
results: finalResults | ||
}); | ||
@@ -1002,3 +927,3 @@ } | ||
filePath, | ||
warnIgnored = false, | ||
warnIgnored, | ||
...unknownOptions | ||
@@ -1017,3 +942,3 @@ } = options || {}; | ||
if (typeof warnIgnored !== "boolean") { | ||
if (typeof warnIgnored !== "boolean" && typeof warnIgnored !== "undefined") { | ||
throw new Error("'options.warnIgnored' must be a boolean or undefined"); | ||
@@ -1033,3 +958,4 @@ } | ||
fix, | ||
reportUnusedDisableDirectives | ||
reportUnusedDisableDirectives, | ||
warnIgnored: constructorWarnIgnored | ||
} = eslintOptions; | ||
@@ -1039,7 +965,8 @@ const results = []; | ||
const resolvedFilename = path.resolve(cwd, filePath || "__placeholder__.js"); | ||
let config; | ||
// Clear the last used config arrays. | ||
if (resolvedFilename && await this.isPathIgnored(resolvedFilename)) { | ||
if (warnIgnored) { | ||
const shouldWarnIgnored = typeof warnIgnored === "boolean" ? warnIgnored : constructorWarnIgnored; | ||
if (shouldWarnIgnored) { | ||
results.push(createIgnoreResult(resolvedFilename, cwd)); | ||
@@ -1049,5 +976,2 @@ } | ||
// TODO: Needed? | ||
config = configs.getConfig(resolvedFilename); | ||
// Do lint. | ||
@@ -1067,17 +991,5 @@ results.push(verifyText({ | ||
debug(`Linting complete in: ${Date.now() - startTime}ms`); | ||
let usedDeprecatedRules; | ||
return processLintReport(this, { | ||
results, | ||
...calculateStatsPerRun(results), | ||
// Initialize it lazily because CLI and `ESLint` API don't use it. | ||
get usedDeprecatedRules() { | ||
if (!usedDeprecatedRules) { | ||
usedDeprecatedRules = Array.from( | ||
iterateRuleDeprecationWarnings(config) | ||
); | ||
} | ||
return usedDeprecatedRules; | ||
} | ||
results | ||
}); | ||
@@ -1084,0 +996,0 @@ |
@@ -33,3 +33,3 @@ /** | ||
* Groups a set of directives into sub-arrays by their parent comment. | ||
* @param {Directive[]} directives Unused directives to be removed. | ||
* @param {Iterable<Directive>} directives Unused directives to be removed. | ||
* @returns {Directive[][]} Directives grouped by their parent comment. | ||
@@ -91,3 +91,3 @@ */ | ||
const regex = new RegExp(String.raw`(?:^|\s*,\s*)${escapeRegExp(ruleId)}(?:\s*,\s*|$)`, "u"); | ||
const regex = new RegExp(String.raw`(?:^|\s*,\s*)(?<quote>['"]?)${escapeRegExp(ruleId)}\k<quote>(?:\s*,\s*|$)`, "u"); | ||
const match = regex.exec(listText); | ||
@@ -182,6 +182,6 @@ const matchedText = match[0]; | ||
* Parses details from directives to create output Problems. | ||
* @param {Directive[]} allDirectives Unused directives to be removed. | ||
* @param {Iterable<Directive>} allDirectives Unused directives to be removed. | ||
* @returns {{ description, fix, unprocessedDirective }[]} Details for later creation of output Problems. | ||
*/ | ||
function processUnusedDisableDirectives(allDirectives) { | ||
function processUnusedDirectives(allDirectives) { | ||
const directiveGroups = groupByParentComment(allDirectives); | ||
@@ -206,2 +206,91 @@ | ||
/** | ||
* Collect eslint-enable comments that are removing suppressions by eslint-disable comments. | ||
* @param {Directive[]} directives The directives to check. | ||
* @returns {Set<Directive>} The used eslint-enable comments | ||
*/ | ||
function collectUsedEnableDirectives(directives) { | ||
/** | ||
* A Map of `eslint-enable` keyed by ruleIds that may be marked as used. | ||
* If `eslint-enable` does not have a ruleId, the key will be `null`. | ||
* @type {Map<string|null, Directive>} | ||
*/ | ||
const enabledRules = new Map(); | ||
/** | ||
* A Set of `eslint-enable` marked as used. | ||
* It is also the return value of `collectUsedEnableDirectives` function. | ||
* @type {Set<Directive>} | ||
*/ | ||
const usedEnableDirectives = new Set(); | ||
/* | ||
* Checks the directives backwards to see if the encountered `eslint-enable` is used by the previous `eslint-disable`, | ||
* and if so, stores the `eslint-enable` in `usedEnableDirectives`. | ||
*/ | ||
for (let index = directives.length - 1; index >= 0; index--) { | ||
const directive = directives[index]; | ||
if (directive.type === "disable") { | ||
if (enabledRules.size === 0) { | ||
continue; | ||
} | ||
if (directive.ruleId === null) { | ||
// If encounter `eslint-disable` without ruleId, | ||
// mark all `eslint-enable` currently held in enabledRules as used. | ||
// e.g. | ||
// /* eslint-disable */ <- current directive | ||
// /* eslint-enable rule-id1 */ <- used | ||
// /* eslint-enable rule-id2 */ <- used | ||
// /* eslint-enable */ <- used | ||
for (const enableDirective of enabledRules.values()) { | ||
usedEnableDirectives.add(enableDirective); | ||
} | ||
enabledRules.clear(); | ||
} else { | ||
const enableDirective = enabledRules.get(directive.ruleId); | ||
if (enableDirective) { | ||
// If encounter `eslint-disable` with ruleId, and there is an `eslint-enable` with the same ruleId in enabledRules, | ||
// mark `eslint-enable` with ruleId as used. | ||
// e.g. | ||
// /* eslint-disable rule-id */ <- current directive | ||
// /* eslint-enable rule-id */ <- used | ||
usedEnableDirectives.add(enableDirective); | ||
} else { | ||
const enabledDirectiveWithoutRuleId = enabledRules.get(null); | ||
if (enabledDirectiveWithoutRuleId) { | ||
// If encounter `eslint-disable` with ruleId, and there is no `eslint-enable` with the same ruleId in enabledRules, | ||
// mark `eslint-enable` without ruleId as used. | ||
// e.g. | ||
// /* eslint-disable rule-id */ <- current directive | ||
// /* eslint-enable */ <- used | ||
usedEnableDirectives.add(enabledDirectiveWithoutRuleId); | ||
} | ||
} | ||
} | ||
} else if (directive.type === "enable") { | ||
if (directive.ruleId === null) { | ||
// If encounter `eslint-enable` without ruleId, the `eslint-enable` that follows it are unused. | ||
// So clear enabledRules. | ||
// e.g. | ||
// /* eslint-enable */ <- current directive | ||
// /* eslint-enable rule-id *// <- unused | ||
// /* eslint-enable */ <- unused | ||
enabledRules.clear(); | ||
enabledRules.set(null, directive); | ||
} else { | ||
enabledRules.set(directive.ruleId, directive); | ||
} | ||
} | ||
} | ||
return usedEnableDirectives; | ||
} | ||
/** | ||
* This is the same as the exported function, except that it | ||
@@ -213,3 +302,3 @@ * doesn't handle disable-line and disable-next-line directives, and it always reports unused | ||
* (this function always reports unused disable directives). | ||
* @returns {{problems: LintMessage[], unusedDisableDirectives: LintMessage[]}} An object with a list | ||
* @returns {{problems: LintMessage[], unusedDirectives: LintMessage[]}} An object with a list | ||
* of problems (including suppressed ones) and unused eslint-disable directives | ||
@@ -266,13 +355,38 @@ */ | ||
const processed = processUnusedDisableDirectives(unusedDisableDirectivesToReport); | ||
const unusedDisableDirectives = processed | ||
const unusedEnableDirectivesToReport = new Set( | ||
options.directives.filter(directive => directive.unprocessedDirective.type === "enable") | ||
); | ||
/* | ||
* If directives has the eslint-enable directive, | ||
* check whether the eslint-enable comment is used. | ||
*/ | ||
if (unusedEnableDirectivesToReport.size > 0) { | ||
for (const directive of collectUsedEnableDirectives(options.directives)) { | ||
unusedEnableDirectivesToReport.delete(directive); | ||
} | ||
} | ||
const processed = processUnusedDirectives(unusedDisableDirectivesToReport) | ||
.concat(processUnusedDirectives(unusedEnableDirectivesToReport)); | ||
const unusedDirectives = processed | ||
.map(({ description, fix, unprocessedDirective }) => { | ||
const { parentComment, type, line, column } = unprocessedDirective; | ||
let message; | ||
if (type === "enable") { | ||
message = description | ||
? `Unused eslint-enable directive (no matching eslint-disable directives were found for ${description}).` | ||
: "Unused eslint-enable directive (no matching eslint-disable directives were found)."; | ||
} else { | ||
message = description | ||
? `Unused eslint-disable directive (no problems were reported from ${description}).` | ||
: "Unused eslint-disable directive (no problems were reported)."; | ||
} | ||
return { | ||
ruleId: null, | ||
message: description | ||
? `Unused eslint-disable directive (no problems were reported from ${description}).` | ||
: "Unused eslint-disable directive (no problems were reported).", | ||
message, | ||
line: type === "disable-next-line" ? parentComment.commentToken.loc.start.line : line, | ||
@@ -286,3 +400,3 @@ column: type === "disable-next-line" ? parentComment.commentToken.loc.start.column + 1 : column, | ||
return { problems, unusedDisableDirectives }; | ||
return { problems, unusedDirectives }; | ||
} | ||
@@ -354,6 +468,6 @@ | ||
? lineDirectivesResult.problems | ||
.concat(blockDirectivesResult.unusedDisableDirectives) | ||
.concat(lineDirectivesResult.unusedDisableDirectives) | ||
.concat(blockDirectivesResult.unusedDirectives) | ||
.concat(lineDirectivesResult.unusedDirectives) | ||
.sort(compareLocations) | ||
: lineDirectivesResult.problems; | ||
}; |
@@ -195,11 +195,14 @@ /** | ||
if (currentSegment !== headSegment && currentSegment) { | ||
debug.dump(`onCodePathSegmentEnd ${currentSegment.id}`); | ||
if (currentSegment.reachable) { | ||
analyzer.emitter.emit( | ||
"onCodePathSegmentEnd", | ||
currentSegment, | ||
node | ||
); | ||
} | ||
const eventName = currentSegment.reachable | ||
? "onCodePathSegmentEnd" | ||
: "onUnreachableCodePathSegmentEnd"; | ||
debug.dump(`${eventName} ${currentSegment.id}`); | ||
analyzer.emitter.emit( | ||
eventName, | ||
currentSegment, | ||
node | ||
); | ||
} | ||
@@ -217,12 +220,15 @@ } | ||
if (currentSegment !== headSegment && headSegment) { | ||
debug.dump(`onCodePathSegmentStart ${headSegment.id}`); | ||
const eventName = headSegment.reachable | ||
? "onCodePathSegmentStart" | ||
: "onUnreachableCodePathSegmentStart"; | ||
debug.dump(`${eventName} ${headSegment.id}`); | ||
CodePathSegment.markUsed(headSegment); | ||
if (headSegment.reachable) { | ||
analyzer.emitter.emit( | ||
"onCodePathSegmentStart", | ||
headSegment, | ||
node | ||
); | ||
} | ||
analyzer.emitter.emit( | ||
eventName, | ||
headSegment, | ||
node | ||
); | ||
} | ||
@@ -246,11 +252,13 @@ } | ||
const currentSegment = currentSegments[i]; | ||
const eventName = currentSegment.reachable | ||
? "onCodePathSegmentEnd" | ||
: "onUnreachableCodePathSegmentEnd"; | ||
debug.dump(`onCodePathSegmentEnd ${currentSegment.id}`); | ||
if (currentSegment.reachable) { | ||
analyzer.emitter.emit( | ||
"onCodePathSegmentEnd", | ||
currentSegment, | ||
node | ||
); | ||
} | ||
debug.dump(`${eventName} ${currentSegment.id}`); | ||
analyzer.emitter.emit( | ||
eventName, | ||
currentSegment, | ||
node | ||
); | ||
} | ||
@@ -257,0 +265,0 @@ |
/** | ||
* @fileoverview A class of the code path segment. | ||
* @fileoverview The CodePathSegment class. | ||
* @author Toru Nagashima | ||
@@ -33,2 +33,13 @@ */ | ||
* A code path segment. | ||
* | ||
* Each segment is arranged in a series of linked lists (implemented by arrays) | ||
* that keep track of the previous and next segments in a code path. In this way, | ||
* you can navigate between all segments in any code path so long as you have a | ||
* reference to any segment in that code path. | ||
* | ||
* When first created, the segment is in a detached state, meaning that it knows the | ||
* segments that came before it but those segments don't know that this new segment | ||
* follows it. Only when `CodePathSegment#markUsed()` is called on a segment does it | ||
* officially become part of the code path by updating the previous segments to know | ||
* that this new segment follows. | ||
*/ | ||
@@ -38,2 +49,3 @@ class CodePathSegment { | ||
/** | ||
* Creates a new instance. | ||
* @param {string} id An identifier. | ||
@@ -54,3 +66,3 @@ * @param {CodePathSegment[]} allPrevSegments An array of the previous segments. | ||
/** | ||
* An array of the next segments. | ||
* An array of the next reachable segments. | ||
* @type {CodePathSegment[]} | ||
@@ -61,3 +73,3 @@ */ | ||
/** | ||
* An array of the previous segments. | ||
* An array of the previous reachable segments. | ||
* @type {CodePathSegment[]} | ||
@@ -68,4 +80,3 @@ */ | ||
/** | ||
* An array of the next segments. | ||
* This array includes unreachable segments. | ||
* An array of all next segments including reachable and unreachable. | ||
* @type {CodePathSegment[]} | ||
@@ -76,4 +87,3 @@ */ | ||
/** | ||
* An array of the previous segments. | ||
* This array includes unreachable segments. | ||
* An array of all previous segments including reachable and unreachable. | ||
* @type {CodePathSegment[]} | ||
@@ -92,3 +102,7 @@ */ | ||
value: { | ||
// determines if the segment has been attached to the code path | ||
used: false, | ||
// array of previous segments coming from the end of a loop | ||
loopedPrevSegments: [] | ||
@@ -123,5 +137,6 @@ } | ||
/** | ||
* Creates a segment that follows given segments. | ||
* Creates a new segment and appends it after the given segments. | ||
* @param {string} id An identifier. | ||
* @param {CodePathSegment[]} allPrevSegments An array of the previous segments. | ||
* @param {CodePathSegment[]} allPrevSegments An array of the previous segments | ||
* to append to. | ||
* @returns {CodePathSegment} The created segment. | ||
@@ -138,3 +153,3 @@ */ | ||
/** | ||
* Creates an unreachable segment that follows given segments. | ||
* Creates an unreachable segment and appends it after the given segments. | ||
* @param {string} id An identifier. | ||
@@ -149,3 +164,3 @@ * @param {CodePathSegment[]} allPrevSegments An array of the previous segments. | ||
* In `if (a) return a; foo();` case, the unreachable segment preceded by | ||
* the return statement is not used but must not be remove. | ||
* the return statement is not used but must not be removed. | ||
*/ | ||
@@ -170,3 +185,3 @@ CodePathSegment.markUsed(segment); | ||
/** | ||
* Makes a given segment being used. | ||
* Marks a given segment as used. | ||
* | ||
@@ -186,2 +201,9 @@ * And this function registers the segment into the previous segments as a next. | ||
if (segment.reachable) { | ||
/* | ||
* If the segment is reachable, then it's officially part of the | ||
* code path. This loops through all previous segments to update | ||
* their list of next segments. Because the segment is reachable, | ||
* it's added to both `nextSegments` and `allNextSegments`. | ||
*/ | ||
for (i = 0; i < segment.allPrevSegments.length; ++i) { | ||
@@ -194,2 +216,9 @@ const prevSegment = segment.allPrevSegments[i]; | ||
} else { | ||
/* | ||
* If the segment is not reachable, then it's not officially part of the | ||
* code path. This loops through all previous segments to update | ||
* their list of next segments. Because the segment is not reachable, | ||
* it's added only to `allNextSegments`. | ||
*/ | ||
for (i = 0; i < segment.allPrevSegments.length; ++i) { | ||
@@ -212,9 +241,10 @@ segment.allPrevSegments[i].allNextSegments.push(segment); | ||
/** | ||
* Replaces unused segments with the previous segments of each unused segment. | ||
* @param {CodePathSegment[]} segments An array of segments to replace. | ||
* @returns {CodePathSegment[]} The replaced array. | ||
* Creates a new array based on an array of segments. If any segment in the | ||
* array is unused, then it is replaced by all of its previous segments. | ||
* All used segments are returned as-is without replacement. | ||
* @param {CodePathSegment[]} segments The array of segments to flatten. | ||
* @returns {CodePathSegment[]} The flattened array. | ||
*/ | ||
static flattenUnusedSegments(segments) { | ||
const done = Object.create(null); | ||
const retv = []; | ||
const done = new Set(); | ||
@@ -225,3 +255,3 @@ for (let i = 0; i < segments.length; ++i) { | ||
// Ignores duplicated. | ||
if (done[segment.id]) { | ||
if (done.has(segment)) { | ||
continue; | ||
@@ -235,14 +265,12 @@ } | ||
if (!done[prevSegment.id]) { | ||
done[prevSegment.id] = true; | ||
retv.push(prevSegment); | ||
if (!done.has(prevSegment)) { | ||
done.add(prevSegment); | ||
} | ||
} | ||
} else { | ||
done[segment.id] = true; | ||
retv.push(segment); | ||
done.add(segment); | ||
} | ||
} | ||
return retv; | ||
return [...done]; | ||
} | ||
@@ -249,0 +277,0 @@ } |
@@ -83,3 +83,5 @@ /** | ||
/** | ||
* The initial code path segment. | ||
* The initial code path segment. This is the segment that is at the head | ||
* of the code path. | ||
* This is a passthrough to the underlying `CodePathState`. | ||
* @type {CodePathSegment} | ||
@@ -92,4 +94,6 @@ */ | ||
/** | ||
* Final code path segments. | ||
* This array is a mix of `returnedSegments` and `thrownSegments`. | ||
* Final code path segments. These are the terminal (tail) segments in the | ||
* code path, which is the combination of `returnedSegments` and `thrownSegments`. | ||
* All segments in this array are reachable. | ||
* This is a passthrough to the underlying `CodePathState`. | ||
* @type {CodePathSegment[]} | ||
@@ -102,5 +106,10 @@ */ | ||
/** | ||
* Final code path segments which is with `return` statements. | ||
* This array contains the last path segment if it's reachable. | ||
* Since the reachable last path returns `undefined`. | ||
* Final code path segments that represent normal completion of the code path. | ||
* For functions, this means both explicit `return` statements and implicit returns, | ||
* such as the last reachable segment in a function that does not have an | ||
* explicit `return` as this implicitly returns `undefined`. For scripts, | ||
* modules, class field initializers, and class static blocks, this means | ||
* all lines of code have been executed. | ||
* These segments are also present in `finalSegments`. | ||
* This is a passthrough to the underlying `CodePathState`. | ||
* @type {CodePathSegment[]} | ||
@@ -113,3 +122,5 @@ */ | ||
/** | ||
* Final code path segments which is with `throw` statements. | ||
* Final code path segments that represent `throw` statements. | ||
* This is a passthrough to the underlying `CodePathState`. | ||
* These segments are also present in `finalSegments`. | ||
* @type {CodePathSegment[]} | ||
@@ -122,4 +133,10 @@ */ | ||
/** | ||
* Current code path segments. | ||
* Tracks the traversal of the code path through each segment. This array | ||
* starts empty and segments are added or removed as the code path is | ||
* traversed. This array always ends up empty at the end of a code path | ||
* traversal. The `CodePathState` uses this to track its progress through | ||
* the code path. | ||
* This is a passthrough to the underlying `CodePathState`. | ||
* @type {CodePathSegment[]} | ||
* @deprecated | ||
*/ | ||
@@ -133,3 +150,3 @@ get currentSegments() { | ||
* | ||
* codePath.traverseSegments(function(segment, controller) { | ||
* codePath.traverseSegments((segment, controller) => { | ||
* // do something. | ||
@@ -140,36 +157,60 @@ * }); | ||
* | ||
* The `controller` object has two methods. | ||
* The `controller` argument has two methods: | ||
* | ||
* - `controller.skip()` - Skip the following segments in this branch. | ||
* - `controller.break()` - Skip all following segments. | ||
* @param {Object} [options] Omittable. | ||
* @param {CodePathSegment} [options.first] The first segment to traverse. | ||
* @param {CodePathSegment} [options.last] The last segment to traverse. | ||
* - `skip()` - skips the following segments in this branch | ||
* - `break()` - skips all following segments in the traversal | ||
* | ||
* A note on the parameters: the `options` argument is optional. This means | ||
* the first argument might be an options object or the callback function. | ||
* @param {Object} [optionsOrCallback] Optional first and last segments to traverse. | ||
* @param {CodePathSegment} [optionsOrCallback.first] The first segment to traverse. | ||
* @param {CodePathSegment} [optionsOrCallback.last] The last segment to traverse. | ||
* @param {Function} callback A callback function. | ||
* @returns {void} | ||
*/ | ||
traverseSegments(options, callback) { | ||
traverseSegments(optionsOrCallback, callback) { | ||
// normalize the arguments into a callback and options | ||
let resolvedOptions; | ||
let resolvedCallback; | ||
if (typeof options === "function") { | ||
resolvedCallback = options; | ||
if (typeof optionsOrCallback === "function") { | ||
resolvedCallback = optionsOrCallback; | ||
resolvedOptions = {}; | ||
} else { | ||
resolvedOptions = options || {}; | ||
resolvedOptions = optionsOrCallback || {}; | ||
resolvedCallback = callback; | ||
} | ||
// determine where to start traversing from based on the options | ||
const startSegment = resolvedOptions.first || this.internal.initialSegment; | ||
const lastSegment = resolvedOptions.last; | ||
let item = null; | ||
// set up initial location information | ||
let record = null; | ||
let index = 0; | ||
let end = 0; | ||
let segment = null; | ||
const visited = Object.create(null); | ||
// segments that have already been visited during traversal | ||
const visited = new Set(); | ||
// tracks the traversal steps | ||
const stack = [[startSegment, 0]]; | ||
// tracks the last skipped segment during traversal | ||
let skippedSegment = null; | ||
// indicates if we exited early from the traversal | ||
let broken = false; | ||
/** | ||
* Maintains traversal state. | ||
*/ | ||
const controller = { | ||
/** | ||
* Skip the following segments in this branch. | ||
* @returns {void} | ||
*/ | ||
skip() { | ||
@@ -182,2 +223,8 @@ if (stack.length <= 1) { | ||
}, | ||
/** | ||
* Stop traversal completely - do not traverse to any | ||
* other segments. | ||
* @returns {void} | ||
*/ | ||
break() { | ||
@@ -189,3 +236,3 @@ broken = true; | ||
/** | ||
* Checks a given previous segment has been visited. | ||
* Checks if a given previous segment has been visited. | ||
* @param {CodePathSegment} prevSegment A previous segment to check. | ||
@@ -196,3 +243,3 @@ * @returns {boolean} `true` if the segment has been visited. | ||
return ( | ||
visited[prevSegment.id] || | ||
visited.has(prevSegment) || | ||
segment.isLoopedPrevSegment(prevSegment) | ||
@@ -202,11 +249,25 @@ ); | ||
// the traversal | ||
while (stack.length > 0) { | ||
item = stack[stack.length - 1]; | ||
segment = item[0]; | ||
index = item[1]; | ||
/* | ||
* This isn't a pure stack. We use the top record all the time | ||
* but don't always pop it off. The record is popped only if | ||
* one of the following is true: | ||
* | ||
* 1) We have already visited the segment. | ||
* 2) We have not visited *all* of the previous segments. | ||
* 3) We have traversed past the available next segments. | ||
* | ||
* Otherwise, we just read the value and sometimes modify the | ||
* record as we traverse. | ||
*/ | ||
record = stack[stack.length - 1]; | ||
segment = record[0]; | ||
index = record[1]; | ||
if (index === 0) { | ||
// Skip if this segment has been visited already. | ||
if (visited[segment.id]) { | ||
if (visited.has(segment)) { | ||
stack.pop(); | ||
@@ -225,14 +286,25 @@ continue; | ||
// Reset the flag of skipping if all branches have been skipped. | ||
// Reset the skipping flag if all branches have been skipped. | ||
if (skippedSegment && segment.prevSegments.includes(skippedSegment)) { | ||
skippedSegment = null; | ||
} | ||
visited[segment.id] = true; | ||
visited.add(segment); | ||
// Call the callback when the first time. | ||
/* | ||
* If the most recent segment hasn't been skipped, then we call | ||
* the callback, passing in the segment and the controller. | ||
*/ | ||
if (!skippedSegment) { | ||
resolvedCallback.call(this, segment, controller); | ||
// exit if we're at the last segment | ||
if (segment === lastSegment) { | ||
controller.skip(); | ||
} | ||
/* | ||
* If the previous statement was executed, or if the callback | ||
* called a method on the controller, we might need to exit the | ||
* loop, so check for that and break accordingly. | ||
*/ | ||
if (broken) { | ||
@@ -247,8 +319,31 @@ break; | ||
if (index < end) { | ||
item[1] += 1; | ||
/* | ||
* If we haven't yet visited all of the next segments, update | ||
* the current top record on the stack to the next index to visit | ||
* and then push a record for the current segment on top. | ||
* | ||
* Setting the current top record's index lets us know how many | ||
* times we've been here and ensures that the segment won't be | ||
* reprocessed (because we only process segments with an index | ||
* of 0). | ||
*/ | ||
record[1] += 1; | ||
stack.push([segment.nextSegments[index], 0]); | ||
} else if (index === end) { | ||
item[0] = segment.nextSegments[index]; | ||
item[1] = 0; | ||
/* | ||
* If we are at the last next segment, then reset the top record | ||
* in the stack to next segment and set its index to 0 so it will | ||
* be processed next. | ||
*/ | ||
record[0] = segment.nextSegments[index]; | ||
record[1] = 0; | ||
} else { | ||
/* | ||
* If index > end, that means we have no more segments that need | ||
* processing. So, we pop that record off of the stack in order to | ||
* continue traversing at the next level up. | ||
*/ | ||
stack.pop(); | ||
@@ -255,0 +350,0 @@ } |
@@ -24,4 +24,4 @@ /** | ||
/** | ||
* Gets whether or not a given segment is reachable. | ||
* @param {CodePathSegment} segment A segment to get. | ||
* Determines whether or not a given segment is reachable. | ||
* @param {CodePathSegment} segment The segment to check. | ||
* @returns {boolean} `true` if the segment is reachable. | ||
@@ -34,22 +34,53 @@ */ | ||
/** | ||
* Creates new segments from the specific range of `context.segmentsList`. | ||
* Creates a new segment for each fork in the given context and appends it | ||
* to the end of the specified range of segments. Ultimately, this ends up calling | ||
* `new CodePathSegment()` for each of the forks using the `create` argument | ||
* as a wrapper around special behavior. | ||
* | ||
* The `startIndex` and `endIndex` arguments specify a range of segments in | ||
* `context` that should become `allPrevSegments` for the newly created | ||
* `CodePathSegment` objects. | ||
* | ||
* When `context.segmentsList` is `[[a, b], [c, d], [e, f]]`, `begin` is `0`, and | ||
* `end` is `-1`, this creates `[g, h]`. This `g` is from `a`, `c`, and `e`. | ||
* This `h` is from `b`, `d`, and `f`. | ||
* @param {ForkContext} context An instance. | ||
* @param {number} begin The first index of the previous segments. | ||
* @param {number} end The last index of the previous segments. | ||
* @param {Function} create A factory function of new segments. | ||
* @returns {CodePathSegment[]} New segments. | ||
* `end` is `-1`, this creates two new segments, `[g, h]`. This `g` is appended to | ||
* the end of the path from `a`, `c`, and `e`. This `h` is appended to the end of | ||
* `b`, `d`, and `f`. | ||
* @param {ForkContext} context An instance from which the previous segments | ||
* will be obtained. | ||
* @param {number} startIndex The index of the first segment in the context | ||
* that should be specified as previous segments for the newly created segments. | ||
* @param {number} endIndex The index of the last segment in the context | ||
* that should be specified as previous segments for the newly created segments. | ||
* @param {Function} create A function that creates new `CodePathSegment` | ||
* instances in a particular way. See the `CodePathSegment.new*` methods. | ||
* @returns {Array<CodePathSegment>} An array of the newly-created segments. | ||
*/ | ||
function makeSegments(context, begin, end, create) { | ||
function createSegments(context, startIndex, endIndex, create) { | ||
/** @type {Array<Array<CodePathSegment>>} */ | ||
const list = context.segmentsList; | ||
const normalizedBegin = begin >= 0 ? begin : list.length + begin; | ||
const normalizedEnd = end >= 0 ? end : list.length + end; | ||
/* | ||
* Both `startIndex` and `endIndex` work the same way: if the number is zero | ||
* or more, then the number is used as-is. If the number is negative, | ||
* then that number is added to the length of the segments list to | ||
* determine the index to use. That means -1 for either argument | ||
* is the last element, -2 is the second to last, and so on. | ||
* | ||
* So if `startIndex` is 0, `endIndex` is -1, and `list.length` is 3, the | ||
* effective `startIndex` is 0 and the effective `endIndex` is 2, so this function | ||
* will include items at indices 0, 1, and 2. | ||
* | ||
* Therefore, if `startIndex` is -1 and `endIndex` is -1, that means we'll only | ||
* be using the last segment in `list`. | ||
*/ | ||
const normalizedBegin = startIndex >= 0 ? startIndex : list.length + startIndex; | ||
const normalizedEnd = endIndex >= 0 ? endIndex : list.length + endIndex; | ||
/** @type {Array<CodePathSegment>} */ | ||
const segments = []; | ||
for (let i = 0; i < context.count; ++i) { | ||
// this is passed into `new CodePathSegment` to add to code path. | ||
const allPrevSegments = []; | ||
@@ -61,2 +92,3 @@ | ||
// note: `create` is just a wrapper that augments `new CodePathSegment`. | ||
segments.push(create(context.idGenerator.next(), allPrevSegments)); | ||
@@ -69,9 +101,8 @@ } | ||
/** | ||
* `segments` becomes doubly in a `finally` block. Then if a code path exits by a | ||
* control statement (such as `break`, `continue`) from the `finally` block, the | ||
* destination's segments may be half of the source segments. In that case, this | ||
* merges segments. | ||
* @param {ForkContext} context An instance. | ||
* @param {CodePathSegment[]} segments Segments to merge. | ||
* @returns {CodePathSegment[]} The merged segments. | ||
* Inside of a `finally` block we end up with two parallel paths. If the code path | ||
* exits by a control statement (such as `break` or `continue`) from the `finally` | ||
* block, then we need to merge the remaining parallel paths back into one. | ||
* @param {ForkContext} context The fork context to work on. | ||
* @param {Array<CodePathSegment>} segments Segments to merge. | ||
* @returns {Array<CodePathSegment>} The merged segments. | ||
*/ | ||
@@ -81,6 +112,29 @@ function mergeExtraSegments(context, segments) { | ||
/* | ||
* We need to ensure that the array returned from this function contains no more | ||
* than the number of segments that the context allows. `context.count` indicates | ||
* how many items should be in the returned array to ensure that the new segment | ||
* entries will line up with the already existing segment entries. | ||
*/ | ||
while (currentSegments.length > context.count) { | ||
const merged = []; | ||
for (let i = 0, length = currentSegments.length / 2 | 0; i < length; ++i) { | ||
/* | ||
* Because `context.count` is a factor of 2 inside of a `finally` block, | ||
* we can divide the segment count by 2 to merge the paths together. | ||
* This loops through each segment in the list and creates a new `CodePathSegment` | ||
* that has the segment and the segment two slots away as previous segments. | ||
* | ||
* If `currentSegments` is [a,b,c,d], this will create new segments e and f, such | ||
* that: | ||
* | ||
* When `i` is 0: | ||
* a->e | ||
* c->e | ||
* | ||
* When `i` is 1: | ||
* b->f | ||
* d->f | ||
*/ | ||
for (let i = 0, length = Math.floor(currentSegments.length / 2); i < length; ++i) { | ||
merged.push(CodePathSegment.newNext( | ||
@@ -91,4 +145,11 @@ context.idGenerator.next(), | ||
} | ||
/* | ||
* Go through the loop condition one more time to see if we have the | ||
* number of segments for the context. If not, we'll keep merging paths | ||
* of the merged segments until we get there. | ||
*/ | ||
currentSegments = merged; | ||
} | ||
return currentSegments; | ||
@@ -102,3 +163,3 @@ } | ||
/** | ||
* A class to manage forking. | ||
* Manages the forking of code paths. | ||
*/ | ||
@@ -108,10 +169,40 @@ class ForkContext { | ||
/** | ||
* Creates a new instance. | ||
* @param {IdGenerator} idGenerator An identifier generator for segments. | ||
* @param {ForkContext|null} upper An upper fork context. | ||
* @param {number} count A number of parallel segments. | ||
* @param {ForkContext|null} upper The preceding fork context. | ||
* @param {number} count The number of parallel segments in each element | ||
* of `segmentsList`. | ||
*/ | ||
constructor(idGenerator, upper, count) { | ||
/** | ||
* The ID generator that will generate segment IDs for any new | ||
* segments that are created. | ||
* @type {IdGenerator} | ||
*/ | ||
this.idGenerator = idGenerator; | ||
/** | ||
* The preceding fork context. | ||
* @type {ForkContext|null} | ||
*/ | ||
this.upper = upper; | ||
/** | ||
* The number of elements in each element of `segmentsList`. In most | ||
* cases, this is 1 but can be 2 when there is a `finally` present, | ||
* which forks the code path outside of normal flow. In the case of nested | ||
* `finally` blocks, this can be a multiple of 2. | ||
* @type {number} | ||
*/ | ||
this.count = count; | ||
/** | ||
* The segments within this context. Each element in this array has | ||
* `count` elements that represent one step in each fork. For example, | ||
* when `segmentsList` is `[[a, b], [c, d], [e, f]]`, there is one path | ||
* a->c->e and one path b->d->f, and `count` is 2 because each element | ||
* is an array with two elements. | ||
* @type {Array<Array<CodePathSegment>>} | ||
*/ | ||
this.segmentsList = []; | ||
@@ -121,4 +212,4 @@ } | ||
/** | ||
* The head segments. | ||
* @type {CodePathSegment[]} | ||
* The segments that begin this fork context. | ||
* @type {Array<CodePathSegment>} | ||
*/ | ||
@@ -132,3 +223,3 @@ get head() { | ||
/** | ||
* A flag which shows empty. | ||
* Indicates if the context contains no segments. | ||
* @type {boolean} | ||
@@ -141,3 +232,3 @@ */ | ||
/** | ||
* A flag which shows reachable. | ||
* Indicates if there are any segments that are reachable. | ||
* @type {boolean} | ||
@@ -152,38 +243,49 @@ */ | ||
/** | ||
* Creates new segments from this context. | ||
* @param {number} begin The first index of previous segments. | ||
* @param {number} end The last index of previous segments. | ||
* @returns {CodePathSegment[]} New segments. | ||
* Creates new segments in this context and appends them to the end of the | ||
* already existing `CodePathSegment`s specified by `startIndex` and | ||
* `endIndex`. | ||
* @param {number} startIndex The index of the first segment in the context | ||
* that should be specified as previous segments for the newly created segments. | ||
* @param {number} endIndex The index of the last segment in the context | ||
* that should be specified as previous segments for the newly created segments. | ||
* @returns {Array<CodePathSegment>} An array of the newly created segments. | ||
*/ | ||
makeNext(begin, end) { | ||
return makeSegments(this, begin, end, CodePathSegment.newNext); | ||
makeNext(startIndex, endIndex) { | ||
return createSegments(this, startIndex, endIndex, CodePathSegment.newNext); | ||
} | ||
/** | ||
* Creates new segments from this context. | ||
* The new segments is always unreachable. | ||
* @param {number} begin The first index of previous segments. | ||
* @param {number} end The last index of previous segments. | ||
* @returns {CodePathSegment[]} New segments. | ||
* Creates new unreachable segments in this context and appends them to the end of the | ||
* already existing `CodePathSegment`s specified by `startIndex` and | ||
* `endIndex`. | ||
* @param {number} startIndex The index of the first segment in the context | ||
* that should be specified as previous segments for the newly created segments. | ||
* @param {number} endIndex The index of the last segment in the context | ||
* that should be specified as previous segments for the newly created segments. | ||
* @returns {Array<CodePathSegment>} An array of the newly created segments. | ||
*/ | ||
makeUnreachable(begin, end) { | ||
return makeSegments(this, begin, end, CodePathSegment.newUnreachable); | ||
makeUnreachable(startIndex, endIndex) { | ||
return createSegments(this, startIndex, endIndex, CodePathSegment.newUnreachable); | ||
} | ||
/** | ||
* Creates new segments from this context. | ||
* The new segments don't have connections for previous segments. | ||
* But these inherit the reachable flag from this context. | ||
* @param {number} begin The first index of previous segments. | ||
* @param {number} end The last index of previous segments. | ||
* @returns {CodePathSegment[]} New segments. | ||
* Creates new segments in this context and does not append them to the end | ||
* of the already existing `CodePathSegment`s specified by `startIndex` and | ||
* `endIndex`. The `startIndex` and `endIndex` are only used to determine if | ||
* the new segments should be reachable. If any of the segments in this range | ||
* are reachable then the new segments are also reachable; otherwise, the new | ||
* segments are unreachable. | ||
* @param {number} startIndex The index of the first segment in the context | ||
* that should be considered for reachability. | ||
* @param {number} endIndex The index of the last segment in the context | ||
* that should be considered for reachability. | ||
* @returns {Array<CodePathSegment>} An array of the newly created segments. | ||
*/ | ||
makeDisconnected(begin, end) { | ||
return makeSegments(this, begin, end, CodePathSegment.newDisconnected); | ||
makeDisconnected(startIndex, endIndex) { | ||
return createSegments(this, startIndex, endIndex, CodePathSegment.newDisconnected); | ||
} | ||
/** | ||
* Adds segments into this context. | ||
* The added segments become the head. | ||
* @param {CodePathSegment[]} segments Segments to add. | ||
* Adds segments to the head of this context. | ||
* @param {Array<CodePathSegment>} segments The segments to add. | ||
* @returns {void} | ||
@@ -193,3 +295,2 @@ */ | ||
assert(segments.length >= this.count, `${segments.length} >= ${this.count}`); | ||
this.segmentsList.push(mergeExtraSegments(this, segments)); | ||
@@ -199,11 +300,13 @@ } | ||
/** | ||
* Replaces the head segments with given segments. | ||
* Replaces the head segments with the given segments. | ||
* The current head segments are removed. | ||
* @param {CodePathSegment[]} segments Segments to add. | ||
* @param {Array<CodePathSegment>} replacementHeadSegments The new head segments. | ||
* @returns {void} | ||
*/ | ||
replaceHead(segments) { | ||
assert(segments.length >= this.count, `${segments.length} >= ${this.count}`); | ||
this.segmentsList.splice(-1, 1, mergeExtraSegments(this, segments)); | ||
replaceHead(replacementHeadSegments) { | ||
assert( | ||
replacementHeadSegments.length >= this.count, | ||
`${replacementHeadSegments.length} >= ${this.count}` | ||
); | ||
this.segmentsList.splice(-1, 1, mergeExtraSegments(this, replacementHeadSegments)); | ||
} | ||
@@ -213,13 +316,8 @@ | ||
* Adds all segments of a given fork context into this context. | ||
* @param {ForkContext} context A fork context to add. | ||
* @param {ForkContext} otherForkContext The fork context to add from. | ||
* @returns {void} | ||
*/ | ||
addAll(context) { | ||
assert(context.count === this.count); | ||
const source = context.segmentsList; | ||
for (let i = 0; i < source.length; ++i) { | ||
this.segmentsList.push(source[i]); | ||
} | ||
addAll(otherForkContext) { | ||
assert(otherForkContext.count === this.count); | ||
this.segmentsList.push(...otherForkContext.segmentsList); | ||
} | ||
@@ -236,3 +334,4 @@ | ||
/** | ||
* Creates the root fork context. | ||
* Creates a new root context, meaning that there are no parent | ||
* fork contexts. | ||
* @param {IdGenerator} idGenerator An identifier generator for segments. | ||
@@ -252,10 +351,12 @@ * @returns {ForkContext} New fork context. | ||
* @param {ForkContext} parentContext The parent fork context. | ||
* @param {boolean} forkLeavingPath A flag which shows inside of `finally` block. | ||
* @param {boolean} shouldForkLeavingPath Indicates that we are inside of | ||
* a `finally` block and should therefore fork the path that leaves | ||
* `finally`. | ||
* @returns {ForkContext} New fork context. | ||
*/ | ||
static newEmpty(parentContext, forkLeavingPath) { | ||
static newEmpty(parentContext, shouldForkLeavingPath) { | ||
return new ForkContext( | ||
parentContext.idGenerator, | ||
parentContext, | ||
(forkLeavingPath ? 2 : 1) * parentContext.count | ||
(shouldForkLeavingPath ? 2 : 1) * parentContext.count | ||
); | ||
@@ -262,0 +363,0 @@ } |
@@ -142,3 +142,3 @@ /** | ||
string.split(",").forEach(name => { | ||
const trimmedName = name.trim(); | ||
const trimmedName = name.trim().replace(/^(?<quote>['"]?)(?<ruleId>.*)\k<quote>$/us, "$<ruleId>"); | ||
@@ -145,0 +145,0 @@ if (trimmedName) { |
@@ -104,2 +104,18 @@ /** | ||
/** | ||
* Clones the given fix object. | ||
* @param {Fix|null} fix The fix to clone. | ||
* @returns {Fix|null} Deep cloned fix object or `null` if `null` or `undefined` was passed in. | ||
*/ | ||
function cloneFix(fix) { | ||
if (!fix) { | ||
return null; | ||
} | ||
return { | ||
range: [fix.range[0], fix.range[1]], | ||
text: fix.text | ||
}; | ||
} | ||
/** | ||
* Check that a fix has a valid range. | ||
@@ -141,3 +157,3 @@ * @param {Fix|null} fix The fix to validate. | ||
if (fixes.length === 1) { | ||
return fixes[0]; | ||
return cloneFix(fixes[0]); | ||
} | ||
@@ -188,3 +204,3 @@ | ||
assertValidFix(fix); | ||
return fix; | ||
return cloneFix(fix); | ||
} | ||
@@ -191,0 +207,0 @@ |
@@ -50,3 +50,3 @@ /** | ||
* @property {string} [printConfig] Print the configuration for the given file | ||
* @property {boolean | undefined} reportUnusedDisableDirectives Adds reported errors for unused eslint-disable directives | ||
* @property {boolean | undefined} reportUnusedDisableDirectives Adds reported errors for unused eslint-disable and eslint-enable directives | ||
* @property {string} [resolvePluginsRelativeTo] A folder where plugins should be resolved from, CWD by default | ||
@@ -59,2 +59,3 @@ * @property {Object} [rule] Specify rules | ||
* @property {boolean} [version] Output the version number | ||
* @property {boolean} warnIgnored Show warnings when the file list includes ignored files | ||
* @property {string[]} _ Positional filenames or patterns | ||
@@ -144,2 +145,13 @@ */ | ||
let warnIgnoredFlag; | ||
if (usingFlatConfig) { | ||
warnIgnoredFlag = { | ||
option: "warn-ignored", | ||
type: "Boolean", | ||
default: "true", | ||
description: "Suppress warnings when the file list includes ignored files" | ||
}; | ||
} | ||
return optionator({ | ||
@@ -298,3 +310,3 @@ prepend: "eslint [options] file.js [file.js] [dir]", | ||
default: void 0, | ||
description: "Adds reported errors for unused eslint-disable directives" | ||
description: "Adds reported errors for unused eslint-disable and eslint-enable directives" | ||
}, | ||
@@ -356,2 +368,3 @@ { | ||
}, | ||
warnIgnoredFlag, | ||
{ | ||
@@ -358,0 +371,0 @@ option: "debug", |
@@ -19,3 +19,5 @@ /** | ||
{ getRuleOptionsSchema } = require("../config/flat-config-helpers"), | ||
{ Linter, SourceCodeFixer, interpolate } = require("../linter"); | ||
{ Linter, SourceCodeFixer, interpolate } = require("../linter"), | ||
CodePath = require("../linter/code-path-analysis/code-path"); | ||
const { FlatConfigArray } = require("../config/flat-config-array"); | ||
@@ -36,4 +38,5 @@ const { defaultConfig } = require("../config/default-config"); | ||
/** @typedef {import("../shared/types").LanguageOptions} LanguageOptions */ | ||
/** @typedef {import("../shared/types").Rule} Rule */ | ||
/* eslint-disable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/4#issuecomment-778805577 */ | ||
/** | ||
@@ -77,3 +80,2 @@ * A test case that is expected to pass lint. | ||
*/ | ||
/* eslint-enable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/4#issuecomment-778805577 */ | ||
@@ -137,2 +139,11 @@ //------------------------------------------------------------------------------ | ||
const forbiddenMethods = [ | ||
"applyInlineConfig", | ||
"applyLanguageOptions", | ||
"finalize" | ||
]; | ||
/** @type {Map<string,WeakSet>} */ | ||
const forbiddenMethodCalls = new Map(forbiddenMethods.map(methodName => ([methodName, new WeakSet()]))); | ||
const hasOwnProperty = Function.call.bind(Object.hasOwnProperty); | ||
@@ -281,2 +292,45 @@ | ||
/** | ||
* Emit a deprecation warning if rule uses CodePath#currentSegments. | ||
* @param {string} ruleName Name of the rule. | ||
* @returns {void} | ||
*/ | ||
function emitCodePathCurrentSegmentsWarning(ruleName) { | ||
if (!emitCodePathCurrentSegmentsWarning[`warned-${ruleName}`]) { | ||
emitCodePathCurrentSegmentsWarning[`warned-${ruleName}`] = true; | ||
process.emitWarning( | ||
`"${ruleName}" rule uses CodePath#currentSegments and will stop working in ESLint v9. Please read the documentation for how to update your code: https://eslint.org/docs/latest/extend/code-path-analysis#usage-examples`, | ||
"DeprecationWarning" | ||
); | ||
} | ||
} | ||
/** | ||
* Function to replace forbidden `SourceCode` methods. Allows just one call per method. | ||
* @param {string} methodName The name of the method to forbid. | ||
* @param {Function} prototype The prototype with the original method to call. | ||
* @returns {Function} The function that throws the error. | ||
*/ | ||
function throwForbiddenMethodError(methodName, prototype) { | ||
const original = prototype[methodName]; | ||
return function(...args) { | ||
const called = forbiddenMethodCalls.get(methodName); | ||
/* eslint-disable no-invalid-this -- needed to operate as a method. */ | ||
if (!called.has(this)) { | ||
called.add(this); | ||
return original.apply(this, args); | ||
} | ||
/* eslint-enable no-invalid-this -- not needed past this point */ | ||
throw new Error( | ||
`\`SourceCode#${methodName}()\` cannot be called inside a rule.` | ||
); | ||
}; | ||
} | ||
//------------------------------------------------------------------------------ | ||
@@ -455,3 +509,3 @@ // Public Interface | ||
* @param {string} ruleName The name of the rule to run. | ||
* @param {Function} rule The rule to test. | ||
* @param {Function | Rule} rule The rule to test. | ||
* @param {{ | ||
@@ -490,2 +544,3 @@ * valid: (ValidTestCase | string)[], | ||
const baseConfig = [ | ||
{ files: ["**"] }, // Make sure the default config matches for all files | ||
{ | ||
@@ -672,6 +727,2 @@ plugins: { | ||
// Verify the code. | ||
const { getComments } = SourceCode.prototype; | ||
let messages; | ||
// check for validation errors | ||
@@ -686,9 +737,30 @@ try { | ||
// Verify the code. | ||
const { getComments, applyLanguageOptions, applyInlineConfig, finalize } = SourceCode.prototype; | ||
const originalCurrentSegments = Object.getOwnPropertyDescriptor(CodePath.prototype, "currentSegments"); | ||
let messages; | ||
try { | ||
SourceCode.prototype.getComments = getCommentsDeprecation; | ||
Object.defineProperty(CodePath.prototype, "currentSegments", { | ||
get() { | ||
emitCodePathCurrentSegmentsWarning(ruleName); | ||
return originalCurrentSegments.get.call(this); | ||
} | ||
}); | ||
forbiddenMethods.forEach(methodName => { | ||
SourceCode.prototype[methodName] = throwForbiddenMethodError(methodName, SourceCode.prototype); | ||
}); | ||
messages = linter.verify(code, configs, filename); | ||
} finally { | ||
SourceCode.prototype.getComments = getComments; | ||
Object.defineProperty(CodePath.prototype, "currentSegments", originalCurrentSegments); | ||
SourceCode.prototype.applyInlineConfig = applyInlineConfig; | ||
SourceCode.prototype.applyLanguageOptions = applyLanguageOptions; | ||
SourceCode.prototype.finalize = finalize; | ||
} | ||
const fatalErrorMessage = messages.find(m => m.fatal); | ||
@@ -1024,25 +1096,31 @@ | ||
* one of the templates above. | ||
* The test suites for valid/invalid are created conditionally as | ||
* test runners (eg. vitest) fail for empty test suites. | ||
*/ | ||
this.constructor.describe(ruleName, () => { | ||
this.constructor.describe("valid", () => { | ||
test.valid.forEach(valid => { | ||
this.constructor[valid.only ? "itOnly" : "it"]( | ||
sanitize(typeof valid === "object" ? valid.name || valid.code : valid), | ||
() => { | ||
testValidTemplate(valid); | ||
} | ||
); | ||
if (test.valid.length > 0) { | ||
this.constructor.describe("valid", () => { | ||
test.valid.forEach(valid => { | ||
this.constructor[valid.only ? "itOnly" : "it"]( | ||
sanitize(typeof valid === "object" ? valid.name || valid.code : valid), | ||
() => { | ||
testValidTemplate(valid); | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
} | ||
this.constructor.describe("invalid", () => { | ||
test.invalid.forEach(invalid => { | ||
this.constructor[invalid.only ? "itOnly" : "it"]( | ||
sanitize(invalid.name || invalid.code), | ||
() => { | ||
testInvalidTemplate(invalid); | ||
} | ||
); | ||
if (test.invalid.length > 0) { | ||
this.constructor.describe("invalid", () => { | ||
test.invalid.forEach(invalid => { | ||
this.constructor[invalid.only ? "itOnly" : "it"]( | ||
sanitize(invalid.name || invalid.code), | ||
() => { | ||
testInvalidTemplate(invalid); | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
@@ -1049,0 +1127,0 @@ } |
@@ -51,3 +51,4 @@ /** | ||
{ getRuleOptionsSchema, validate } = require("../shared/config-validator"), | ||
{ Linter, SourceCodeFixer, interpolate } = require("../linter"); | ||
{ Linter, SourceCodeFixer, interpolate } = require("../linter"), | ||
CodePath = require("../linter/code-path-analysis/code-path"); | ||
@@ -66,4 +67,5 @@ const ajv = require("../shared/ajv")({ strictDefaults: true }); | ||
/** @typedef {import("../shared/types").Parser} Parser */ | ||
/** @typedef {import("../shared/types").Rule} Rule */ | ||
/* eslint-disable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/4#issuecomment-778805577 */ | ||
/** | ||
@@ -113,3 +115,2 @@ * A test case that is expected to pass lint. | ||
*/ | ||
/* eslint-enable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/4#issuecomment-778805577 */ | ||
@@ -168,4 +169,39 @@ //------------------------------------------------------------------------------ | ||
const forbiddenMethods = [ | ||
"applyInlineConfig", | ||
"applyLanguageOptions", | ||
"finalize" | ||
]; | ||
const hasOwnProperty = Function.call.bind(Object.hasOwnProperty); | ||
const DEPRECATED_SOURCECODE_PASSTHROUGHS = { | ||
getSource: "getText", | ||
getSourceLines: "getLines", | ||
getAllComments: "getAllComments", | ||
getNodeByRangeIndex: "getNodeByRangeIndex", | ||
// getComments: "getComments", -- already handled by a separate error | ||
getCommentsBefore: "getCommentsBefore", | ||
getCommentsAfter: "getCommentsAfter", | ||
getCommentsInside: "getCommentsInside", | ||
getJSDocComment: "getJSDocComment", | ||
getFirstToken: "getFirstToken", | ||
getFirstTokens: "getFirstTokens", | ||
getLastToken: "getLastToken", | ||
getLastTokens: "getLastTokens", | ||
getTokenAfter: "getTokenAfter", | ||
getTokenBefore: "getTokenBefore", | ||
getTokenByRangeStart: "getTokenByRangeStart", | ||
getTokens: "getTokens", | ||
getTokensAfter: "getTokensAfter", | ||
getTokensBefore: "getTokensBefore", | ||
getTokensBetween: "getTokensBetween", | ||
getScope: "getScope", | ||
getAncestors: "getAncestors", | ||
getDeclaredVariables: "getDeclaredVariables", | ||
markVariableAsUsed: "markVariableAsUsed" | ||
}; | ||
/** | ||
@@ -313,2 +349,15 @@ * Clones a given value deeply. | ||
/** | ||
* Function to replace forbidden `SourceCode` methods. | ||
* @param {string} methodName The name of the method to forbid. | ||
* @returns {Function} The function that throws the error. | ||
*/ | ||
function throwForbiddenMethodError(methodName) { | ||
return () => { | ||
throw new Error( | ||
`\`SourceCode#${methodName}()\` cannot be called inside a rule.` | ||
); | ||
}; | ||
} | ||
/** | ||
* Emit a deprecation warning if function-style format is being used. | ||
@@ -343,2 +392,49 @@ * @param {string} ruleName Name of the rule. | ||
/** | ||
* Emit a deprecation warning if a rule uses a deprecated `context` method. | ||
* @param {string} ruleName Name of the rule. | ||
* @param {string} methodName The name of the method on `context` that was used. | ||
* @returns {void} | ||
*/ | ||
function emitDeprecatedContextMethodWarning(ruleName, methodName) { | ||
if (!emitDeprecatedContextMethodWarning[`warned-${ruleName}-${methodName}`]) { | ||
emitDeprecatedContextMethodWarning[`warned-${ruleName}-${methodName}`] = true; | ||
process.emitWarning( | ||
`"${ruleName}" rule is using \`context.${methodName}()\`, which is deprecated and will be removed in ESLint v9. Please use \`sourceCode.${DEPRECATED_SOURCECODE_PASSTHROUGHS[methodName]}()\` instead.`, | ||
"DeprecationWarning" | ||
); | ||
} | ||
} | ||
/** | ||
* Emit a deprecation warning if rule uses CodePath#currentSegments. | ||
* @param {string} ruleName Name of the rule. | ||
* @returns {void} | ||
*/ | ||
function emitCodePathCurrentSegmentsWarning(ruleName) { | ||
if (!emitCodePathCurrentSegmentsWarning[`warned-${ruleName}`]) { | ||
emitCodePathCurrentSegmentsWarning[`warned-${ruleName}`] = true; | ||
process.emitWarning( | ||
`"${ruleName}" rule uses CodePath#currentSegments and will stop working in ESLint v9. Please read the documentation for how to update your code: https://eslint.org/docs/latest/extend/code-path-analysis#usage-examples`, | ||
"DeprecationWarning" | ||
); | ||
} | ||
} | ||
/** | ||
* Emit a deprecation warning if `context.parserServices` is used. | ||
* @param {string} ruleName Name of the rule. | ||
* @returns {void} | ||
*/ | ||
function emitParserServicesWarning(ruleName) { | ||
if (!emitParserServicesWarning[`warned-${ruleName}`]) { | ||
emitParserServicesWarning[`warned-${ruleName}`] = true; | ||
process.emitWarning( | ||
`"${ruleName}" rule is using \`context.parserServices\`, which is deprecated and will be removed in ESLint v9. Please use \`sourceCode.parserServices\` instead.`, | ||
"DeprecationWarning" | ||
); | ||
} | ||
} | ||
//------------------------------------------------------------------------------ | ||
@@ -518,6 +614,9 @@ // Public Interface | ||
* @param {string} name The name of the rule to define. | ||
* @param {Function} rule The rule definition. | ||
* @param {Function | Rule} rule The rule definition. | ||
* @returns {void} | ||
*/ | ||
defineRule(name, rule) { | ||
if (typeof rule === "function") { | ||
emitLegacyRuleAPIWarning(name); | ||
} | ||
this.rules[name] = rule; | ||
@@ -529,3 +628,3 @@ } | ||
* @param {string} ruleName The name of the rule to run. | ||
* @param {Function} rule The rule to test. | ||
* @param {Function | Rule} rule The rule to test. | ||
* @param {{ | ||
@@ -574,3 +673,34 @@ * valid: (ValidTestCase | string)[], | ||
return (typeof rule === "function" ? rule : rule.create)(context); | ||
// wrap all deprecated methods | ||
const newContext = Object.create( | ||
context, | ||
Object.fromEntries(Object.keys(DEPRECATED_SOURCECODE_PASSTHROUGHS).map(methodName => [ | ||
methodName, | ||
{ | ||
value(...args) { | ||
// emit deprecation warning | ||
emitDeprecatedContextMethodWarning(ruleName, methodName); | ||
// call the original method | ||
return context[methodName].call(this, ...args); | ||
}, | ||
enumerable: true | ||
} | ||
])) | ||
); | ||
// emit warning about context.parserServices | ||
const parserServices = context.parserServices; | ||
Object.defineProperty(newContext, "parserServices", { | ||
get() { | ||
emitParserServicesWarning(ruleName); | ||
return parserServices; | ||
} | ||
}); | ||
Object.freeze(newContext); | ||
return (typeof rule === "function" ? rule : rule.create)(newContext); | ||
} | ||
@@ -694,3 +824,4 @@ })); | ||
// Verify the code. | ||
const { getComments } = SourceCode.prototype; | ||
const { getComments, applyLanguageOptions, applyInlineConfig, finalize } = SourceCode.prototype; | ||
const originalCurrentSegments = Object.getOwnPropertyDescriptor(CodePath.prototype, "currentSegments"); | ||
let messages; | ||
@@ -700,5 +831,20 @@ | ||
SourceCode.prototype.getComments = getCommentsDeprecation; | ||
Object.defineProperty(CodePath.prototype, "currentSegments", { | ||
get() { | ||
emitCodePathCurrentSegmentsWarning(ruleName); | ||
return originalCurrentSegments.get.call(this); | ||
} | ||
}); | ||
forbiddenMethods.forEach(methodName => { | ||
SourceCode.prototype[methodName] = throwForbiddenMethodError(methodName); | ||
}); | ||
messages = linter.verify(code, config, filename); | ||
} finally { | ||
SourceCode.prototype.getComments = getComments; | ||
Object.defineProperty(CodePath.prototype, "currentSegments", originalCurrentSegments); | ||
SourceCode.prototype.applyInlineConfig = applyInlineConfig; | ||
SourceCode.prototype.applyLanguageOptions = applyLanguageOptions; | ||
SourceCode.prototype.finalize = finalize; | ||
} | ||
@@ -1036,25 +1182,31 @@ | ||
* one of the templates above. | ||
* The test suites for valid/invalid are created conditionally as | ||
* test runners (eg. vitest) fail for empty test suites. | ||
*/ | ||
this.constructor.describe(ruleName, () => { | ||
this.constructor.describe("valid", () => { | ||
test.valid.forEach(valid => { | ||
this.constructor[valid.only ? "itOnly" : "it"]( | ||
sanitize(typeof valid === "object" ? valid.name || valid.code : valid), | ||
() => { | ||
testValidTemplate(valid); | ||
} | ||
); | ||
if (test.valid.length > 0) { | ||
this.constructor.describe("valid", () => { | ||
test.valid.forEach(valid => { | ||
this.constructor[valid.only ? "itOnly" : "it"]( | ||
sanitize(typeof valid === "object" ? valid.name || valid.code : valid), | ||
() => { | ||
testValidTemplate(valid); | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
} | ||
this.constructor.describe("invalid", () => { | ||
test.invalid.forEach(invalid => { | ||
this.constructor[invalid.only ? "itOnly" : "it"]( | ||
sanitize(invalid.name || invalid.code), | ||
() => { | ||
testInvalidTemplate(invalid); | ||
} | ||
); | ||
if (test.invalid.length > 0) { | ||
this.constructor.describe("invalid", () => { | ||
test.invalid.forEach(invalid => { | ||
this.constructor[invalid.only ? "itOnly" : "it"]( | ||
sanitize(invalid.name || invalid.code), | ||
() => { | ||
testInvalidTemplate(invalid); | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
@@ -1061,0 +1213,0 @@ } |
@@ -227,49 +227,41 @@ /** | ||
/** | ||
* Creates a new `AccessorData` object for the given getter or setter node. | ||
* @param {ASTNode} node A getter or setter node. | ||
* @returns {AccessorData} New `AccessorData` object that contains the given node. | ||
* Checks accessor pairs in the given list of nodes. | ||
* @param {ASTNode[]} nodes The list to check. | ||
* @returns {void} | ||
* @private | ||
*/ | ||
function createAccessorData(node) { | ||
const name = astUtils.getStaticPropertyName(node); | ||
const key = (name !== null) ? name : sourceCode.getTokens(node.key); | ||
function checkList(nodes) { | ||
const accessors = []; | ||
let found = false; | ||
return { | ||
key, | ||
getters: node.kind === "get" ? [node] : [], | ||
setters: node.kind === "set" ? [node] : [] | ||
}; | ||
} | ||
for (let i = 0; i < nodes.length; i++) { | ||
const node = nodes[i]; | ||
/** | ||
* Merges the given `AccessorData` object into the given accessors list. | ||
* @param {AccessorData[]} accessors The list to merge into. | ||
* @param {AccessorData} accessorData The object to merge. | ||
* @returns {AccessorData[]} The same instance with the merged object. | ||
* @private | ||
*/ | ||
function mergeAccessorData(accessors, accessorData) { | ||
const equalKeyElement = accessors.find(a => areEqualKeys(a.key, accessorData.key)); | ||
if (isAccessorKind(node)) { | ||
if (equalKeyElement) { | ||
equalKeyElement.getters.push(...accessorData.getters); | ||
equalKeyElement.setters.push(...accessorData.setters); | ||
} else { | ||
accessors.push(accessorData); | ||
} | ||
// Creates a new `AccessorData` object for the given getter or setter node. | ||
const name = astUtils.getStaticPropertyName(node); | ||
const key = (name !== null) ? name : sourceCode.getTokens(node.key); | ||
return accessors; | ||
} | ||
// Merges the given `AccessorData` object into the given accessors list. | ||
for (let j = 0; j < accessors.length; j++) { | ||
const accessor = accessors[j]; | ||
/** | ||
* Checks accessor pairs in the given list of nodes. | ||
* @param {ASTNode[]} nodes The list to check. | ||
* @returns {void} | ||
* @private | ||
*/ | ||
function checkList(nodes) { | ||
const accessors = nodes | ||
.filter(isAccessorKind) | ||
.map(createAccessorData) | ||
.reduce(mergeAccessorData, []); | ||
if (areEqualKeys(accessor.key, key)) { | ||
accessor.getters.push(...node.kind === "get" ? [node] : []); | ||
accessor.setters.push(...node.kind === "set" ? [node] : []); | ||
found = true; | ||
break; | ||
} | ||
} | ||
if (!found) { | ||
accessors.push({ | ||
key, | ||
getters: node.kind === "get" ? [node] : [], | ||
setters: node.kind === "set" ? [node] : [] | ||
}); | ||
} | ||
found = false; | ||
} | ||
} | ||
@@ -276,0 +268,0 @@ for (const { getters, setters } of accessors) { |
/** | ||
* @fileoverview Rule to enforce linebreaks after open and before close array brackets | ||
* @author Jan Peer Stöcklmair <https://github.com/JPeer264> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -17,2 +18,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -19,0 +22,0 @@ |
/** | ||
* @fileoverview Disallows or enforces spaces inside of array brackets. | ||
* @author Jamund Ferguson | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -16,2 +17,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -18,0 +21,0 @@ |
@@ -22,11 +22,2 @@ /** | ||
/** | ||
* Checks a given code path segment is reachable. | ||
* @param {CodePathSegment} segment A segment to check. | ||
* @returns {boolean} `true` if the segment is reachable. | ||
*/ | ||
function isReachable(segment) { | ||
return segment.reachable; | ||
} | ||
/** | ||
* Checks a given node is a member access which has the specified name's | ||
@@ -43,2 +34,18 @@ * property. | ||
/** | ||
* Checks all segments in a set and returns true if any are reachable. | ||
* @param {Set<CodePathSegment>} segments The segments to check. | ||
* @returns {boolean} True if any segment is reachable; false otherwise. | ||
*/ | ||
function isAnySegmentReachable(segments) { | ||
for (const segment of segments) { | ||
if (segment.reachable) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Returns a human-legible description of an array method | ||
@@ -134,2 +141,72 @@ * @param {string} arrayMethodName A method name to fully qualify | ||
/** | ||
* Checks if the given node is a void expression. | ||
* @param {ASTNode} node The node to check. | ||
* @returns {boolean} - `true` if the node is a void expression | ||
*/ | ||
function isExpressionVoid(node) { | ||
return node.type === "UnaryExpression" && node.operator === "void"; | ||
} | ||
/** | ||
* Fixes the linting error by prepending "void " to the given node | ||
* @param {Object} sourceCode context given by context.sourceCode | ||
* @param {ASTNode} node The node to fix. | ||
* @param {Object} fixer The fixer object provided by ESLint. | ||
* @returns {Array<Object>} - An array of fix objects to apply to the node. | ||
*/ | ||
function voidPrependFixer(sourceCode, node, fixer) { | ||
const requiresParens = | ||
// prepending `void ` will fail if the node has a lower precedence than void | ||
astUtils.getPrecedence(node) < astUtils.getPrecedence({ type: "UnaryExpression", operator: "void" }) && | ||
// check if there are parentheses around the node to avoid redundant parentheses | ||
!astUtils.isParenthesised(sourceCode, node); | ||
// avoid parentheses issues | ||
const returnOrArrowToken = sourceCode.getTokenBefore( | ||
node, | ||
node.parent.type === "ArrowFunctionExpression" | ||
? astUtils.isArrowToken | ||
// isReturnToken | ||
: token => token.type === "Keyword" && token.value === "return" | ||
); | ||
const firstToken = sourceCode.getTokenAfter(returnOrArrowToken); | ||
const prependSpace = | ||
// is return token, as => allows void to be adjacent | ||
returnOrArrowToken.value === "return" && | ||
// If two tokens (return and "(") are adjacent | ||
returnOrArrowToken.range[1] === firstToken.range[0]; | ||
return [ | ||
fixer.insertTextBefore(firstToken, `${prependSpace ? " " : ""}void ${requiresParens ? "(" : ""}`), | ||
fixer.insertTextAfter(node, requiresParens ? ")" : "") | ||
]; | ||
} | ||
/** | ||
* Fixes the linting error by `wrapping {}` around the given node's body. | ||
* @param {Object} sourceCode context given by context.sourceCode | ||
* @param {ASTNode} node The node to fix. | ||
* @param {Object} fixer The fixer object provided by ESLint. | ||
* @returns {Array<Object>} - An array of fix objects to apply to the node. | ||
*/ | ||
function curlyWrapFixer(sourceCode, node, fixer) { | ||
const arrowToken = sourceCode.getTokenBefore(node.body, astUtils.isArrowToken); | ||
const firstToken = sourceCode.getTokenAfter(arrowToken); | ||
const lastToken = sourceCode.getLastToken(node); | ||
return [ | ||
fixer.insertTextBefore(firstToken, "{"), | ||
fixer.insertTextAfter(lastToken, "}") | ||
]; | ||
} | ||
//------------------------------------------------------------------------------ | ||
@@ -150,2 +227,5 @@ // Rule Definition | ||
// eslint-disable-next-line eslint-plugin/require-meta-has-suggestions -- false positive | ||
hasSuggestions: true, | ||
schema: [ | ||
@@ -162,2 +242,6 @@ { | ||
default: false | ||
}, | ||
allowVoid: { | ||
type: "boolean", | ||
default: false | ||
} | ||
@@ -173,3 +257,5 @@ }, | ||
expectedReturnValue: "{{arrayMethodName}}() expects a return value from {{name}}.", | ||
expectedNoReturnValue: "{{arrayMethodName}}() expects no useless return value from {{name}}." | ||
expectedNoReturnValue: "{{arrayMethodName}}() expects no useless return value from {{name}}.", | ||
wrapBraces: "Wrap the expression in `{}`.", | ||
prependVoid: "Prepend `void` to the expression." | ||
} | ||
@@ -180,3 +266,3 @@ }, | ||
const options = context.options[0] || { allowImplicit: false, checkForEach: false }; | ||
const options = context.options[0] || { allowImplicit: false, checkForEach: false, allowVoid: false }; | ||
const sourceCode = context.sourceCode; | ||
@@ -208,15 +294,44 @@ | ||
let messageId = null; | ||
const messageAndSuggestions = { messageId: "", suggest: [] }; | ||
if (funcInfo.arrayMethodName === "forEach") { | ||
if (options.checkForEach && node.type === "ArrowFunctionExpression" && node.expression) { | ||
messageId = "expectedNoReturnValue"; | ||
if (options.allowVoid) { | ||
if (isExpressionVoid(node.body)) { | ||
return; | ||
} | ||
messageAndSuggestions.messageId = "expectedNoReturnValue"; | ||
messageAndSuggestions.suggest = [ | ||
{ | ||
messageId: "wrapBraces", | ||
fix(fixer) { | ||
return curlyWrapFixer(sourceCode, node, fixer); | ||
} | ||
}, | ||
{ | ||
messageId: "prependVoid", | ||
fix(fixer) { | ||
return voidPrependFixer(sourceCode, node.body, fixer); | ||
} | ||
} | ||
]; | ||
} else { | ||
messageAndSuggestions.messageId = "expectedNoReturnValue"; | ||
messageAndSuggestions.suggest = [{ | ||
messageId: "wrapBraces", | ||
fix(fixer) { | ||
return curlyWrapFixer(sourceCode, node, fixer); | ||
} | ||
}]; | ||
} | ||
} | ||
} else { | ||
if (node.body.type === "BlockStatement" && funcInfo.codePath.currentSegments.some(isReachable)) { | ||
messageId = funcInfo.hasReturn ? "expectedAtEnd" : "expectedInside"; | ||
if (node.body.type === "BlockStatement" && isAnySegmentReachable(funcInfo.currentSegments)) { | ||
messageAndSuggestions.messageId = funcInfo.hasReturn ? "expectedAtEnd" : "expectedInside"; | ||
} | ||
} | ||
if (messageId) { | ||
if (messageAndSuggestions.messageId) { | ||
const name = astUtils.getFunctionNameWithKind(node); | ||
@@ -227,4 +342,5 @@ | ||
loc: astUtils.getFunctionHeadLoc(node, sourceCode), | ||
messageId, | ||
data: { name, arrayMethodName: fullMethodName(funcInfo.arrayMethodName) } | ||
messageId: messageAndSuggestions.messageId, | ||
data: { name, arrayMethodName: fullMethodName(funcInfo.arrayMethodName) }, | ||
suggest: messageAndSuggestions.suggest.length !== 0 ? messageAndSuggestions.suggest : null | ||
}); | ||
@@ -254,3 +370,4 @@ } | ||
!node.generator, | ||
node | ||
node, | ||
currentSegments: new Set() | ||
}; | ||
@@ -264,2 +381,19 @@ }, | ||
onUnreachableCodePathSegmentStart(segment) { | ||
funcInfo.currentSegments.add(segment); | ||
}, | ||
onUnreachableCodePathSegmentEnd(segment) { | ||
funcInfo.currentSegments.delete(segment); | ||
}, | ||
onCodePathSegmentStart(segment) { | ||
funcInfo.currentSegments.add(segment); | ||
}, | ||
onCodePathSegmentEnd(segment) { | ||
funcInfo.currentSegments.delete(segment); | ||
}, | ||
// Checks the return statement is valid. | ||
@@ -274,3 +408,3 @@ ReturnStatement(node) { | ||
let messageId = null; | ||
const messageAndSuggestions = { messageId: "", suggest: [] }; | ||
@@ -281,3 +415,18 @@ if (funcInfo.arrayMethodName === "forEach") { | ||
if (options.checkForEach && node.argument) { | ||
messageId = "expectedNoReturnValue"; | ||
if (options.allowVoid) { | ||
if (isExpressionVoid(node.argument)) { | ||
return; | ||
} | ||
messageAndSuggestions.messageId = "expectedNoReturnValue"; | ||
messageAndSuggestions.suggest = [{ | ||
messageId: "prependVoid", | ||
fix(fixer) { | ||
return voidPrependFixer(sourceCode, node.argument, fixer); | ||
} | ||
}]; | ||
} else { | ||
messageAndSuggestions.messageId = "expectedNoReturnValue"; | ||
} | ||
} | ||
@@ -288,14 +437,15 @@ } else { | ||
if (!options.allowImplicit && !node.argument) { | ||
messageId = "expectedReturnValue"; | ||
messageAndSuggestions.messageId = "expectedReturnValue"; | ||
} | ||
} | ||
if (messageId) { | ||
if (messageAndSuggestions.messageId) { | ||
context.report({ | ||
node, | ||
messageId, | ||
messageId: messageAndSuggestions.messageId, | ||
data: { | ||
name: astUtils.getFunctionNameWithKind(funcInfo.node), | ||
arrayMethodName: fullMethodName(funcInfo.arrayMethodName) | ||
} | ||
}, | ||
suggest: messageAndSuggestions.suggest.length !== 0 ? messageAndSuggestions.suggest : null | ||
}); | ||
@@ -302,0 +452,0 @@ } |
/** | ||
* @fileoverview Rule to enforce line breaks after each array element | ||
* @author Jan Peer Stöcklmair <https://github.com/JPeer264> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -17,2 +18,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -244,7 +247,11 @@ | ||
const linebreaksCount = node.elements.map((element, i) => { | ||
let linebreaksCount = 0; | ||
for (let i = 0; i < node.elements.length; i++) { | ||
const element = node.elements[i]; | ||
const previousElement = elements[i - 1]; | ||
if (i === 0 || element === null || previousElement === null) { | ||
return false; | ||
continue; | ||
} | ||
@@ -256,4 +263,6 @@ | ||
return !astUtils.isTokenOnSameLine(lastTokenOfPreviousElement, firstTokenOfCurrentElement); | ||
}).filter(isBreak => isBreak === true).length; | ||
if (!astUtils.isTokenOnSameLine(lastTokenOfPreviousElement, firstTokenOfCurrentElement)) { | ||
linebreaksCount++; | ||
} | ||
} | ||
@@ -260,0 +269,0 @@ const needsLinebreaks = ( |
/** | ||
* @fileoverview Rule to require parens in arrow function arguments. | ||
* @author Jxck | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -33,2 +34,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -35,0 +38,0 @@ |
/** | ||
* @fileoverview Rule to define spacing before/after arrow function's arrow. | ||
* @author Jxck | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -20,2 +21,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -22,0 +25,0 @@ |
@@ -25,3 +25,3 @@ /** | ||
messages: { | ||
outOfScope: "'{{name}}' used outside of binding context." | ||
outOfScope: "'{{name}}' declared on line {{definitionLine}} column {{definitionColumn}} is used outside of binding context." | ||
} | ||
@@ -54,8 +54,18 @@ }, | ||
* @param {eslint-scope.Reference} reference A reference to report. | ||
* @param {eslint-scope.Definition} definition A definition for which to report reference. | ||
* @returns {void} | ||
*/ | ||
function report(reference) { | ||
function report(reference, definition) { | ||
const identifier = reference.identifier; | ||
const definitionPosition = definition.name.loc.start; | ||
context.report({ node: identifier, messageId: "outOfScope", data: { name: identifier.name } }); | ||
context.report({ | ||
node: identifier, | ||
messageId: "outOfScope", | ||
data: { | ||
name: identifier.name, | ||
definitionLine: definitionPosition.line, | ||
definitionColumn: definitionPosition.column + 1 | ||
} | ||
}); | ||
} | ||
@@ -97,3 +107,3 @@ | ||
.filter(isOutsideOfScope) | ||
.forEach(report); | ||
.forEach(ref => report(ref, variables[i].defs.find(def => def.parent === node))); | ||
} | ||
@@ -100,0 +110,0 @@ } |
/** | ||
* @fileoverview A rule to disallow or enforce spaces inside of single line blocks. | ||
* @author Toru Nagashima | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -17,2 +18,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -19,0 +22,0 @@ |
/** | ||
* @fileoverview Rule to flag block statements that do not use the one true brace style | ||
* @author Ian Christian Myers | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -17,2 +18,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -19,0 +22,0 @@ |
/** | ||
* @fileoverview Rule to forbid or enforce dangling commas. | ||
* @author Ian Christian Myers | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -76,2 +77,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -78,0 +81,0 @@ |
/** | ||
* @fileoverview Comma spacing - validates spacing before and after comma | ||
* @author Vignesh Anand aka vegetableman. | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -16,2 +17,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -18,0 +21,0 @@ |
/** | ||
* @fileoverview Comma style - enforces comma styles of two types: last and first | ||
* @author Vignesh Anand aka vegetableman | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -17,2 +18,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -19,0 +22,0 @@ |
/** | ||
* @fileoverview Disallows or enforces spaces inside computed properties. | ||
* @author Jamund Ferguson | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -16,2 +17,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -18,0 +21,0 @@ |
@@ -19,8 +19,15 @@ /** | ||
/** | ||
* Checks whether or not a given code path segment is unreachable. | ||
* @param {CodePathSegment} segment A CodePathSegment to check. | ||
* @returns {boolean} `true` if the segment is unreachable. | ||
* Checks all segments in a set and returns true if all are unreachable. | ||
* @param {Set<CodePathSegment>} segments The segments to check. | ||
* @returns {boolean} True if all segments are unreachable; false otherwise. | ||
*/ | ||
function isUnreachable(segment) { | ||
return !segment.reachable; | ||
function areAllSegmentsUnreachable(segments) { | ||
for (const segment of segments) { | ||
if (segment.reachable) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
@@ -92,3 +99,3 @@ | ||
if (!funcInfo.hasReturnValue || | ||
funcInfo.codePath.currentSegments.every(isUnreachable) || | ||
areAllSegmentsUnreachable(funcInfo.currentSegments) || | ||
astUtils.isES5Constructor(node) || | ||
@@ -146,3 +153,4 @@ isClassConstructor(node) | ||
messageId: "", | ||
node | ||
node, | ||
currentSegments: new Set() | ||
}; | ||
@@ -154,2 +162,19 @@ }, | ||
onUnreachableCodePathSegmentStart(segment) { | ||
funcInfo.currentSegments.add(segment); | ||
}, | ||
onUnreachableCodePathSegmentEnd(segment) { | ||
funcInfo.currentSegments.delete(segment); | ||
}, | ||
onCodePathSegmentStart(segment) { | ||
funcInfo.currentSegments.add(segment); | ||
}, | ||
onCodePathSegmentEnd(segment) { | ||
funcInfo.currentSegments.delete(segment); | ||
}, | ||
// Reports a given return statement if it's inconsistent. | ||
@@ -156,0 +181,0 @@ ReturnStatement(node) { |
@@ -13,8 +13,15 @@ /** | ||
/** | ||
* Checks whether a given code path segment is reachable or not. | ||
* @param {CodePathSegment} segment A code path segment to check. | ||
* @returns {boolean} `true` if the segment is reachable. | ||
* Checks all segments in a set and returns true if any are reachable. | ||
* @param {Set<CodePathSegment>} segments The segments to check. | ||
* @returns {boolean} True if any segment is reachable; false otherwise. | ||
*/ | ||
function isReachable(segment) { | ||
return segment.reachable; | ||
function isAnySegmentReachable(segments) { | ||
for (const segment of segments) { | ||
if (segment.reachable) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
@@ -214,3 +221,4 @@ | ||
superIsConstructor: isPossibleConstructor(superClass), | ||
codePath | ||
codePath, | ||
currentSegments: new Set() | ||
}; | ||
@@ -223,3 +231,4 @@ } else { | ||
superIsConstructor: false, | ||
codePath | ||
codePath, | ||
currentSegments: new Set() | ||
}; | ||
@@ -267,2 +276,5 @@ } | ||
onCodePathSegmentStart(segment) { | ||
funcInfo.currentSegments.add(segment); | ||
if (!(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends)) { | ||
@@ -288,2 +300,15 @@ return; | ||
onUnreachableCodePathSegmentStart(segment) { | ||
funcInfo.currentSegments.add(segment); | ||
}, | ||
onUnreachableCodePathSegmentEnd(segment) { | ||
funcInfo.currentSegments.delete(segment); | ||
}, | ||
onCodePathSegmentEnd(segment) { | ||
funcInfo.currentSegments.delete(segment); | ||
}, | ||
/** | ||
@@ -352,8 +377,7 @@ * Update information of the code path segment when a code path was | ||
if (funcInfo.hasExtends) { | ||
const segments = funcInfo.codePath.currentSegments; | ||
const segments = funcInfo.currentSegments; | ||
let duplicate = false; | ||
let info = null; | ||
for (let i = 0; i < segments.length; ++i) { | ||
const segment = segments[i]; | ||
for (const segment of segments) { | ||
@@ -383,3 +407,3 @@ if (segment.reachable) { | ||
} | ||
} else if (funcInfo.codePath.currentSegments.some(isReachable)) { | ||
} else if (isAnySegmentReachable(funcInfo.currentSegments)) { | ||
context.report({ | ||
@@ -408,6 +432,5 @@ messageId: "unexpected", | ||
// Returning argument is a substitute of 'super()'. | ||
const segments = funcInfo.codePath.currentSegments; | ||
const segments = funcInfo.currentSegments; | ||
for (let i = 0; i < segments.length; ++i) { | ||
const segment = segments[i]; | ||
for (const segment of segments) { | ||
@@ -414,0 +437,0 @@ if (segment.reachable) { |
/** | ||
* @fileoverview Validates newlines before and after dots | ||
* @author Greg Cochard | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -17,2 +18,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -19,0 +22,0 @@ |
@@ -136,4 +136,3 @@ /** | ||
node.computed && | ||
node.property.type === "TemplateLiteral" && | ||
node.property.expressions.length === 0 | ||
astUtils.isStaticTemplateLiteral(node.property) | ||
) { | ||
@@ -140,0 +139,0 @@ checkComputedProperty(node, node.property.quasis[0].value.cooked); |
/** | ||
* @fileoverview Require or disallow newline at the end of files | ||
* @author Nodeca Team <https://github.com/nodeca> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -14,2 +15,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -16,0 +19,0 @@ |
@@ -9,2 +9,8 @@ /** | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
const { getStaticValue } = require("@eslint-community/eslint-utils"); | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
@@ -33,2 +39,3 @@ //------------------------------------------------------------------------------ | ||
create(context) { | ||
const { sourceCode } = context; | ||
@@ -51,13 +58,13 @@ /** | ||
* @param {int} dir expected direction that could either be turned around or invalidated | ||
* @returns {int} return dir, the negated dir or zero if it's not clear for identifiers | ||
* @returns {int} return dir, the negated dir, or zero if the counter does not change or the direction is not clear | ||
*/ | ||
function getRightDirection(update, dir) { | ||
if (update.right.type === "UnaryExpression") { | ||
if (update.right.operator === "-") { | ||
return -dir; | ||
} | ||
} else if (update.right.type === "Identifier") { | ||
return 0; | ||
const staticValue = getStaticValue(update.right, sourceCode.getScope(update)); | ||
if (staticValue && ["bigint", "boolean", "number"].includes(typeof staticValue.value)) { | ||
const sign = Math.sign(Number(staticValue.value)) || 0; // convert NaN to 0 | ||
return dir * sign; | ||
} | ||
return dir; | ||
return 0; | ||
} | ||
@@ -100,26 +107,33 @@ | ||
} | ||
return { | ||
ForStatement(node) { | ||
if (node.test && node.test.type === "BinaryExpression" && node.test.left.type === "Identifier" && node.update) { | ||
const counter = node.test.left.name; | ||
const operator = node.test.operator; | ||
const update = node.update; | ||
if (node.test && node.test.type === "BinaryExpression" && node.update) { | ||
for (const counterPosition of ["left", "right"]) { | ||
if (node.test[counterPosition].type !== "Identifier") { | ||
continue; | ||
} | ||
let wrongDirection; | ||
const counter = node.test[counterPosition].name; | ||
const operator = node.test.operator; | ||
const update = node.update; | ||
if (operator === "<" || operator === "<=") { | ||
wrongDirection = -1; | ||
} else if (operator === ">" || operator === ">=") { | ||
wrongDirection = 1; | ||
} else { | ||
return; | ||
} | ||
let wrongDirection; | ||
if (update.type === "UpdateExpression") { | ||
if (getUpdateDirection(update, counter) === wrongDirection) { | ||
if (operator === "<" || operator === "<=") { | ||
wrongDirection = counterPosition === "left" ? -1 : 1; | ||
} else if (operator === ">" || operator === ">=") { | ||
wrongDirection = counterPosition === "left" ? 1 : -1; | ||
} else { | ||
return; | ||
} | ||
if (update.type === "UpdateExpression") { | ||
if (getUpdateDirection(update, counter) === wrongDirection) { | ||
report(node); | ||
} | ||
} else if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) === wrongDirection) { | ||
report(node); | ||
} | ||
} else if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) === wrongDirection) { | ||
report(node); | ||
} | ||
@@ -126,0 +140,0 @@ } |
/** | ||
* @fileoverview Rule to control spacing within function calls | ||
* @author Matt DuVall <http://www.mattduvall.com> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -21,2 +22,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -23,0 +26,0 @@ |
/** | ||
* @fileoverview Rule to enforce line breaks between arguments of a function call | ||
* @author Alexey Gonchar <https://github.com/finico> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -15,2 +16,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -17,0 +20,0 @@ |
/** | ||
* @fileoverview enforce consistent line breaks inside function parentheses | ||
* @author Teddy Katz | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -20,2 +21,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -22,0 +25,0 @@ |
/** | ||
* @fileoverview Rule to check the spacing around the * in generator functions. | ||
* @author Jamund Ferguson | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -31,2 +32,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -33,0 +36,0 @@ |
@@ -17,11 +17,19 @@ /** | ||
//------------------------------------------------------------------------------ | ||
const TARGET_NODE_TYPE = /^(?:Arrow)?FunctionExpression$/u; | ||
/** | ||
* Checks a given code path segment is reachable. | ||
* @param {CodePathSegment} segment A segment to check. | ||
* @returns {boolean} `true` if the segment is reachable. | ||
* Checks all segments in a set and returns true if any are reachable. | ||
* @param {Set<CodePathSegment>} segments The segments to check. | ||
* @returns {boolean} True if any segment is reachable; false otherwise. | ||
*/ | ||
function isReachable(segment) { | ||
return segment.reachable; | ||
function isAnySegmentReachable(segments) { | ||
for (const segment of segments) { | ||
if (segment.reachable) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
@@ -75,3 +83,4 @@ | ||
shouldCheck: false, | ||
node: null | ||
node: null, | ||
currentSegments: [] | ||
}; | ||
@@ -90,3 +99,3 @@ | ||
if (funcInfo.shouldCheck && | ||
funcInfo.codePath.currentSegments.some(isReachable) | ||
isAnySegmentReachable(funcInfo.currentSegments) | ||
) { | ||
@@ -150,3 +159,4 @@ context.report({ | ||
shouldCheck: isGetter(node), | ||
node | ||
node, | ||
currentSegments: new Set() | ||
}; | ||
@@ -159,3 +169,18 @@ }, | ||
}, | ||
onUnreachableCodePathSegmentStart(segment) { | ||
funcInfo.currentSegments.add(segment); | ||
}, | ||
onUnreachableCodePathSegmentEnd(segment) { | ||
funcInfo.currentSegments.delete(segment); | ||
}, | ||
onCodePathSegmentStart(segment) { | ||
funcInfo.currentSegments.add(segment); | ||
}, | ||
onCodePathSegmentEnd(segment) { | ||
funcInfo.currentSegments.delete(segment); | ||
}, | ||
// Checks the return statement is valid. | ||
@@ -162,0 +187,0 @@ ReturnStatement(node) { |
@@ -141,39 +141,2 @@ /** | ||
/** | ||
* Creates a new `AccessorData` object for the given getter or setter node. | ||
* @param {ASTNode} node A getter or setter node. | ||
* @returns {AccessorData} New `AccessorData` object that contains the given node. | ||
* @private | ||
*/ | ||
function createAccessorData(node) { | ||
const name = astUtils.getStaticPropertyName(node); | ||
const key = (name !== null) ? name : sourceCode.getTokens(node.key); | ||
return { | ||
key, | ||
getters: node.kind === "get" ? [node] : [], | ||
setters: node.kind === "set" ? [node] : [] | ||
}; | ||
} | ||
/** | ||
* Merges the given `AccessorData` object into the given accessors list. | ||
* @param {AccessorData[]} accessors The list to merge into. | ||
* @param {AccessorData} accessorData The object to merge. | ||
* @returns {AccessorData[]} The same instance with the merged object. | ||
* @private | ||
*/ | ||
function mergeAccessorData(accessors, accessorData) { | ||
const equalKeyElement = accessors.find(a => areEqualKeys(a.key, accessorData.key)); | ||
if (equalKeyElement) { | ||
equalKeyElement.getters.push(...accessorData.getters); | ||
equalKeyElement.setters.push(...accessorData.setters); | ||
} else { | ||
accessors.push(accessorData); | ||
} | ||
return accessors; | ||
} | ||
/** | ||
* Checks accessor pairs in the given list of nodes. | ||
@@ -186,8 +149,36 @@ * @param {ASTNode[]} nodes The list to check. | ||
function checkList(nodes, shouldCheck) { | ||
const accessors = nodes | ||
.filter(shouldCheck) | ||
.filter(isAccessorKind) | ||
.map(createAccessorData) | ||
.reduce(mergeAccessorData, []); | ||
const accessors = []; | ||
let found = false; | ||
for (let i = 0; i < nodes.length; i++) { | ||
const node = nodes[i]; | ||
if (shouldCheck(node) && isAccessorKind(node)) { | ||
// Creates a new `AccessorData` object for the given getter or setter node. | ||
const name = astUtils.getStaticPropertyName(node); | ||
const key = (name !== null) ? name : sourceCode.getTokens(node.key); | ||
// Merges the given `AccessorData` object into the given accessors list. | ||
for (let j = 0; j < accessors.length; j++) { | ||
const accessor = accessors[j]; | ||
if (areEqualKeys(accessor.key, key)) { | ||
accessor.getters.push(...node.kind === "get" ? [node] : []); | ||
accessor.setters.push(...node.kind === "set" ? [node] : []); | ||
found = true; | ||
break; | ||
} | ||
} | ||
if (!found) { | ||
accessors.push({ | ||
key, | ||
getters: node.kind === "get" ? [node] : [], | ||
setters: node.kind === "set" ? [node] : [] | ||
}); | ||
} | ||
found = false; | ||
} | ||
} | ||
for (const { getters, setters } of accessors) { | ||
@@ -194,0 +185,0 @@ |
/** | ||
* @fileoverview enforce the location of arrow function bodies | ||
* @author Sharmila Jesupaul | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -15,2 +16,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -17,0 +20,0 @@ |
@@ -178,2 +178,3 @@ /** | ||
"no-obj-calls": () => require("./no-obj-calls"), | ||
"no-object-constructor": () => require("./no-object-constructor"), | ||
"no-octal": () => require("./no-octal"), | ||
@@ -180,0 +181,0 @@ "no-octal-escape": () => require("./no-octal-escape"), |
/** | ||
* @fileoverview A rule to ensure consistent quotes used in jsx syntax. | ||
* @author Mathias Schreck <https://github.com/lo1tuma> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -42,2 +43,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -44,0 +47,0 @@ |
/** | ||
* @fileoverview Rule to specify spacing of object literal keys and values | ||
* @author Brandon Mills | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -136,2 +137,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -138,0 +141,0 @@ |
/** | ||
* @fileoverview Rule to enforce spacing before and after keywords. | ||
* @author Toru Nagashima | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -67,2 +68,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -69,0 +72,0 @@ |
/** | ||
* @fileoverview Rule to enforce a single linebreak style. | ||
* @author Erik Mueller | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -21,2 +22,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -23,0 +26,0 @@ |
/** | ||
* @fileoverview Enforces empty lines around comments. | ||
* @author Jamund Ferguson | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -55,2 +56,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -57,0 +60,0 @@ |
/** | ||
* @fileoverview Rule to check empty newline between class members | ||
* @author 薛定谔的猫<hh_2013@foxmail.com> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -14,2 +15,17 @@ "use strict"; | ||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
/** | ||
* Types of class members. | ||
* Those have `test` method to check it matches to the given class member. | ||
* @private | ||
*/ | ||
const ClassMemberTypes = { | ||
"*": { test: () => true }, | ||
field: { test: node => node.type === "PropertyDefinition" }, | ||
method: { test: node => node.type === "MethodDefinition" } | ||
}; | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
@@ -21,2 +37,4 @@ //------------------------------------------------------------------------------ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -34,3 +52,28 @@ | ||
{ | ||
enum: ["always", "never"] | ||
anyOf: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
enforce: { | ||
type: "array", | ||
items: { | ||
type: "object", | ||
properties: { | ||
blankLine: { enum: ["always", "never"] }, | ||
prev: { enum: ["method", "field", "*"] }, | ||
next: { enum: ["method", "field", "*"] } | ||
}, | ||
additionalProperties: false, | ||
required: ["blankLine", "prev", "next"] | ||
}, | ||
minItems: 1 | ||
} | ||
}, | ||
additionalProperties: false, | ||
required: ["enforce"] | ||
}, | ||
{ | ||
enum: ["always", "never"] | ||
} | ||
] | ||
}, | ||
@@ -61,2 +104,3 @@ { | ||
const configureList = typeof options[0] === "object" ? options[0].enforce : [{ blankLine: options[0], prev: "*", next: "*" }]; | ||
const sourceCode = context.sourceCode; | ||
@@ -151,2 +195,34 @@ | ||
/** | ||
* Checks whether the given node matches the given type. | ||
* @param {ASTNode} node The class member node to check. | ||
* @param {string} type The class member type to check. | ||
* @returns {boolean} `true` if the class member node matched the type. | ||
* @private | ||
*/ | ||
function match(node, type) { | ||
return ClassMemberTypes[type].test(node); | ||
} | ||
/** | ||
* Finds the last matched configuration from the configureList. | ||
* @param {ASTNode} prevNode The previous node to match. | ||
* @param {ASTNode} nextNode The current node to match. | ||
* @returns {string|null} Padding type or `null` if no matches were found. | ||
* @private | ||
*/ | ||
function getPaddingType(prevNode, nextNode) { | ||
for (let i = configureList.length - 1; i >= 0; --i) { | ||
const configure = configureList[i]; | ||
const matched = | ||
match(prevNode, configure.prev) && | ||
match(nextNode, configure.next); | ||
if (matched) { | ||
return configure.blankLine; | ||
} | ||
} | ||
return null; | ||
} | ||
return { | ||
@@ -166,8 +242,9 @@ ClassBody(node) { | ||
const curLineLastToken = findLastConsecutiveTokenAfter(curLast, nextFirst, 0); | ||
const paddingType = getPaddingType(body[i], body[i + 1]); | ||
if ((options[0] === "always" && !skip && !isPadded) || | ||
(options[0] === "never" && isPadded)) { | ||
if (paddingType === "never" && isPadded) { | ||
context.report({ | ||
node: body[i + 1], | ||
messageId: isPadded ? "never" : "always", | ||
messageId: "never", | ||
fix(fixer) { | ||
@@ -177,8 +254,19 @@ if (hasTokenInPadding) { | ||
} | ||
return isPadded | ||
? fixer.replaceTextRange([beforePadding.range[1], afterPadding.range[0]], "\n") | ||
: fixer.insertTextAfter(curLineLastToken, "\n"); | ||
return fixer.replaceTextRange([beforePadding.range[1], afterPadding.range[0]], "\n"); | ||
} | ||
}); | ||
} else if (paddingType === "always" && !skip && !isPadded) { | ||
context.report({ | ||
node: body[i + 1], | ||
messageId: "always", | ||
fix(fixer) { | ||
if (hasTokenInPadding) { | ||
return null; | ||
} | ||
return fixer.insertTextAfter(curLineLastToken, "\n"); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -185,0 +273,0 @@ } |
@@ -153,2 +153,27 @@ /** | ||
/** | ||
* Gets the leftmost operand of a consecutive logical expression. | ||
* @param {SourceCode} sourceCode The ESLint source code object | ||
* @param {LogicalExpression} node LogicalExpression | ||
* @returns {Expression} Leftmost operand | ||
*/ | ||
function getLeftmostOperand(sourceCode, node) { | ||
let left = node.left; | ||
while (left.type === "LogicalExpression" && left.operator === node.operator) { | ||
if (astUtils.isParenthesised(sourceCode, left)) { | ||
/* | ||
* It should have associativity, | ||
* but ignore it if use parentheses to make the evaluation order clear. | ||
*/ | ||
return left; | ||
} | ||
left = left.left; | ||
} | ||
return left; | ||
} | ||
//------------------------------------------------------------------------------ | ||
@@ -192,3 +217,2 @@ // Rule Definition | ||
fixable: "code", | ||
// eslint-disable-next-line eslint-plugin/require-meta-has-suggestions -- Does not detect conditional suggestions | ||
hasSuggestions: true, | ||
@@ -324,3 +348,6 @@ messages: { | ||
"AssignmentExpression[operator='='][right.type='LogicalExpression']"(assignment) { | ||
if (!astUtils.isSameReference(assignment.left, assignment.right.left)) { | ||
const leftOperand = getLeftmostOperand(sourceCode, assignment.right); | ||
if (!astUtils.isSameReference(assignment.left, leftOperand) | ||
) { | ||
return; | ||
@@ -349,6 +376,6 @@ } | ||
// -> foo ||= bar | ||
const logicalOperatorToken = getOperatorToken(assignment.right); | ||
const logicalOperatorToken = getOperatorToken(leftOperand.parent); | ||
const firstRightOperandToken = sourceCode.getTokenAfter(logicalOperatorToken); | ||
yield ruleFixer.removeRange([assignment.right.range[0], firstRightOperandToken.range[0]]); | ||
yield ruleFixer.removeRange([leftOperand.parent.range[0], firstRightOperandToken.range[0]]); | ||
} | ||
@@ -378,4 +405,7 @@ }; | ||
const requiresOuterParenthesis = logical.parent.type !== "ExpressionStatement" && | ||
(astUtils.getPrecedence({ type: "AssignmentExpression" }) < astUtils.getPrecedence(logical.parent)); | ||
const parentPrecedence = astUtils.getPrecedence(logical.parent); | ||
const requiresOuterParenthesis = logical.parent.type !== "ExpressionStatement" && ( | ||
parentPrecedence === -1 || | ||
astUtils.getPrecedence({ type: "AssignmentExpression" }) < parentPrecedence | ||
); | ||
@@ -382,0 +412,0 @@ if (!astUtils.isParenthesised(sourceCode, logical) && requiresOuterParenthesis) { |
/** | ||
* @fileoverview Rule to check for max length on a line. | ||
* @author Matt DuVall <http://www.mattduvall.com> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -69,2 +70,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -256,15 +259,19 @@ | ||
/** | ||
* A reducer to group an AST node by line number, both start and end. | ||
* @param {Object} acc the accumulator | ||
* @param {ASTNode} node the AST node in question | ||
* @returns {Object} the modified accumulator | ||
* @private | ||
* | ||
* reduce an array of AST nodes by line number, both start and end. | ||
* @param {ASTNode[]} arr array of AST nodes | ||
* @returns {Object} accululated AST nodes | ||
*/ | ||
function groupByLineNumber(acc, node) { | ||
for (let i = node.loc.start.line; i <= node.loc.end.line; ++i) { | ||
ensureArrayAndPush(acc, i, node); | ||
function groupArrayByLineNumber(arr) { | ||
const obj = {}; | ||
for (let i = 0; i < arr.length; i++) { | ||
const node = arr[i]; | ||
for (let j = node.loc.start.line; j <= node.loc.end.line; ++j) { | ||
ensureArrayAndPush(obj, j, node); | ||
} | ||
} | ||
return acc; | ||
return obj; | ||
} | ||
@@ -317,9 +324,9 @@ | ||
const strings = getAllStrings(); | ||
const stringsByLine = strings.reduce(groupByLineNumber, {}); | ||
const stringsByLine = groupArrayByLineNumber(strings); | ||
const templateLiterals = getAllTemplateLiterals(); | ||
const templateLiteralsByLine = templateLiterals.reduce(groupByLineNumber, {}); | ||
const templateLiteralsByLine = groupArrayByLineNumber(templateLiterals); | ||
const regExpLiterals = getAllRegExpLiterals(); | ||
const regExpLiteralsByLine = regExpLiterals.reduce(groupByLineNumber, {}); | ||
const regExpLiteralsByLine = groupArrayByLineNumber(regExpLiterals); | ||
@@ -326,0 +333,0 @@ lines.forEach((line, i) => { |
/** | ||
* @fileoverview Specify the maximum number of statements allowed per line. | ||
* @author Kenneth Williams | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -20,2 +21,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -22,0 +25,0 @@ |
/** | ||
* @fileoverview Enforce newlines between operands of ternary expressions | ||
* @author Kai Cataldo | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -17,2 +18,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -19,0 +22,0 @@ |
/** | ||
* @fileoverview Rule to flag when using constructor without parentheses | ||
* @author Ilya Volodin | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -25,2 +26,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -27,0 +30,0 @@ |
@@ -215,3 +215,2 @@ /** | ||
messageId: "unexpected", | ||
data: { identifier: node.name }, | ||
fix(fixer) { | ||
@@ -235,3 +234,2 @@ const linesBetween = sourceCode.getText().slice(lastToken.range[1], nextToken.range[0]).split(astUtils.LINEBREAK_MATCHER); | ||
messageId: "expected", | ||
data: { identifier: node.name }, | ||
fix(fixer) { | ||
@@ -238,0 +236,0 @@ if ((noNextLineToken ? getLastCommentLineOfBlock(nextLineNum) : lastToken.loc.end.line) === nextToken.loc.start.line) { |
@@ -5,2 +5,3 @@ /** | ||
* @author Burak Yigit Kaya | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -19,2 +20,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -21,0 +24,0 @@ |
@@ -9,2 +9,14 @@ /** | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
const { | ||
getVariableByName, | ||
isClosingParenToken, | ||
isOpeningParenToken, | ||
isStartOfExpressionStatement, | ||
needsPrecedingSemicolon | ||
} = require("./utils/ast-utils"); | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
@@ -24,6 +36,10 @@ //------------------------------------------------------------------------------ | ||
hasSuggestions: true, | ||
schema: [], | ||
messages: { | ||
preferLiteral: "The array literal notation [] is preferable." | ||
preferLiteral: "The array literal notation [] is preferable.", | ||
useLiteral: "Replace with an array literal.", | ||
useLiteralAfterSemicolon: "Replace with an array literal, add preceding semicolon." | ||
} | ||
@@ -34,3 +50,29 @@ }, | ||
const sourceCode = context.sourceCode; | ||
/** | ||
* Gets the text between the calling parentheses of a CallExpression or NewExpression. | ||
* @param {ASTNode} node A CallExpression or NewExpression node. | ||
* @returns {string} The text between the calling parentheses, or an empty string if there are none. | ||
*/ | ||
function getArgumentsText(node) { | ||
const lastToken = sourceCode.getLastToken(node); | ||
if (!isClosingParenToken(lastToken)) { | ||
return ""; | ||
} | ||
let firstToken = node.callee; | ||
do { | ||
firstToken = sourceCode.getTokenAfter(firstToken); | ||
if (!firstToken || firstToken === lastToken) { | ||
return ""; | ||
} | ||
} while (!isOpeningParenToken(firstToken)); | ||
return sourceCode.text.slice(firstToken.range[1], lastToken.range[0]); | ||
} | ||
/** | ||
* Disallow construction of dense arrays using the Array constructor | ||
@@ -43,8 +85,45 @@ * @param {ASTNode} node node to evaluate | ||
if ( | ||
node.arguments.length !== 1 && | ||
node.callee.type === "Identifier" && | ||
node.callee.name === "Array" | ||
) { | ||
context.report({ node, messageId: "preferLiteral" }); | ||
node.callee.type !== "Identifier" || | ||
node.callee.name !== "Array" || | ||
node.arguments.length === 1 && | ||
node.arguments[0].type !== "SpreadElement") { | ||
return; | ||
} | ||
const variable = getVariableByName(sourceCode.getScope(node), "Array"); | ||
/* | ||
* Check if `Array` is a predefined global variable: predefined globals have no declarations, | ||
* meaning that the `identifiers` list of the variable object is empty. | ||
*/ | ||
if (variable && variable.identifiers.length === 0) { | ||
const argsText = getArgumentsText(node); | ||
let fixText; | ||
let messageId; | ||
/* | ||
* Check if the suggested change should include a preceding semicolon or not. | ||
* Due to JavaScript's ASI rules, a missing semicolon may be inserted automatically | ||
* before an expression like `Array()` or `new Array()`, but not when the expression | ||
* is changed into an array literal like `[]`. | ||
*/ | ||
if (isStartOfExpressionStatement(node) && needsPrecedingSemicolon(sourceCode, node)) { | ||
fixText = `;[${argsText}]`; | ||
messageId = "useLiteralAfterSemicolon"; | ||
} else { | ||
fixText = `[${argsText}]`; | ||
messageId = "useLiteral"; | ||
} | ||
context.report({ | ||
node, | ||
messageId: "preferLiteral", | ||
suggest: [ | ||
{ | ||
messageId, | ||
fix: fixer => fixer.replaceText(node, fixText) | ||
} | ||
] | ||
}); | ||
} | ||
} | ||
@@ -51,0 +130,0 @@ |
@@ -5,2 +5,3 @@ /** | ||
* @author Jxck <https://github.com/Jxck> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -32,2 +33,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "suggestion", | ||
@@ -34,0 +37,0 @@ |
@@ -46,4 +46,7 @@ /** | ||
hasSuggestions: true, | ||
messages: { | ||
unexpected: "Unexpected console statement." | ||
unexpected: "Unexpected console statement.", | ||
removeConsole: "Remove the console.{{ propertyName }}()." | ||
} | ||
@@ -99,2 +102,60 @@ }, | ||
/** | ||
* Checks if removing the ExpressionStatement node will cause ASI to | ||
* break. | ||
* eg. | ||
* foo() | ||
* console.log(); | ||
* [1, 2, 3].forEach(a => doSomething(a)) | ||
* | ||
* Removing the console.log(); statement should leave two statements, but | ||
* here the two statements will become one because [ causes continuation after | ||
* foo(). | ||
* @param {ASTNode} node The ExpressionStatement node to check. | ||
* @returns {boolean} `true` if ASI will break after removing the ExpressionStatement | ||
* node. | ||
*/ | ||
function maybeAsiHazard(node) { | ||
const SAFE_TOKENS_BEFORE = /^[:;{]$/u; // One of :;{ | ||
const UNSAFE_CHARS_AFTER = /^[-[(/+`]/u; // One of [(/+-` | ||
const tokenBefore = sourceCode.getTokenBefore(node); | ||
const tokenAfter = sourceCode.getTokenAfter(node); | ||
return ( | ||
Boolean(tokenAfter) && | ||
UNSAFE_CHARS_AFTER.test(tokenAfter.value) && | ||
tokenAfter.value !== "++" && | ||
tokenAfter.value !== "--" && | ||
Boolean(tokenBefore) && | ||
!SAFE_TOKENS_BEFORE.test(tokenBefore.value) | ||
); | ||
} | ||
/** | ||
* Checks if the MemberExpression node's parent.parent.parent is a | ||
* Program, BlockStatement, StaticBlock, or SwitchCase node. This check | ||
* is necessary to avoid providing a suggestion that might cause a syntax error. | ||
* | ||
* eg. if (a) console.log(b), removing console.log() here will lead to a | ||
* syntax error. | ||
* if (a) { console.log(b) }, removing console.log() here is acceptable. | ||
* | ||
* Additionally, it checks if the callee of the CallExpression node is | ||
* the node itself. | ||
* | ||
* eg. foo(console.log), cannot provide a suggestion here. | ||
* @param {ASTNode} node The MemberExpression node to check. | ||
* @returns {boolean} `true` if a suggestion can be provided for a node. | ||
*/ | ||
function canProvideSuggestions(node) { | ||
return ( | ||
node.parent.type === "CallExpression" && | ||
node.parent.callee === node && | ||
node.parent.parent.type === "ExpressionStatement" && | ||
astUtils.STATEMENT_LIST_PARENTS.has(node.parent.parent.parent.type) && | ||
!maybeAsiHazard(node.parent.parent) | ||
); | ||
} | ||
/** | ||
* Reports the given reference as a violation. | ||
@@ -107,6 +168,17 @@ * @param {eslint-scope.Reference} reference The reference to report. | ||
const propertyName = astUtils.getStaticPropertyName(node); | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
messageId: "unexpected" | ||
messageId: "unexpected", | ||
suggest: canProvideSuggestions(node) | ||
? [{ | ||
messageId: "removeConsole", | ||
data: { propertyName }, | ||
fix(fixer) { | ||
return fixer.remove(node.parent.parent); | ||
} | ||
}] | ||
: [] | ||
}); | ||
@@ -113,0 +185,0 @@ } |
@@ -17,2 +17,12 @@ /** | ||
onPatternEnter() { | ||
/* | ||
* `RegExpValidator` may parse the pattern twice in one `validatePattern`. | ||
* So `this._controlChars` should be cleared here as well. | ||
* | ||
* For example, the `/(?<a>\x1f)/` regex will parse the pattern twice. | ||
* This is based on the content described in Annex B. | ||
* If the regex contains a `GroupName` and the `u` flag is not used, `ParseText` will be called twice. | ||
* See https://tc39.es/ecma262/2023/multipage/additional-ecmascript-features-for-web-browsers.html#sec-parsepattern-annexb | ||
*/ | ||
this._controlChars = []; | ||
@@ -36,6 +46,9 @@ } | ||
const uFlag = typeof flags === "string" && flags.includes("u"); | ||
const vFlag = typeof flags === "string" && flags.includes("v"); | ||
this._controlChars = []; | ||
this._source = regexpStr; | ||
try { | ||
this._source = regexpStr; | ||
this._validator.validatePattern(regexpStr, void 0, void 0, uFlag); // Call onCharacter hook | ||
this._validator.validatePattern(regexpStr, void 0, void 0, { unicode: uFlag, unicodeSets: vFlag }); // Call onCharacter hook | ||
} catch { | ||
@@ -42,0 +55,0 @@ |
@@ -9,15 +9,13 @@ /** | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
const { RegExpParser, visitRegExpAST } = require("@eslint-community/regexpp"); | ||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
/* | ||
* plain-English description of the following regexp: | ||
* 0. `^` fix the match at the beginning of the string | ||
* 1. `([^\\[]|\\.|\[([^\\\]]|\\.)+\])*`: regexp contents; 0 or more of the following | ||
* 1.0. `[^\\[]`: any character that's not a `\` or a `[` (anything but escape sequences and character classes) | ||
* 1.1. `\\.`: an escape sequence | ||
* 1.2. `\[([^\\\]]|\\.)+\]`: a character class that isn't empty | ||
* 2. `$`: fix the match at the end of the string | ||
*/ | ||
const regex = /^([^\\[]|\\.|\[([^\\\]]|\\.)+\])*$/u; | ||
const parser = new RegExpParser(); | ||
const QUICK_TEST_REGEX = /\[\]/u; | ||
@@ -49,5 +47,28 @@ //------------------------------------------------------------------------------ | ||
"Literal[regex]"(node) { | ||
if (!regex.test(node.regex.pattern)) { | ||
context.report({ node, messageId: "unexpected" }); | ||
const { pattern, flags } = node.regex; | ||
if (!QUICK_TEST_REGEX.test(pattern)) { | ||
return; | ||
} | ||
let regExpAST; | ||
try { | ||
regExpAST = parser.parsePattern(pattern, 0, pattern.length, { | ||
unicode: flags.includes("u"), | ||
unicodeSets: flags.includes("v") | ||
}); | ||
} catch { | ||
// Ignore regular expressions that regexpp cannot parse | ||
return; | ||
} | ||
visitRegExpAST(regExpAST, { | ||
onCharacterClassEnter(characterClass) { | ||
if (!characterClass.negate && characterClass.elements.length === 0) { | ||
context.report({ node, messageId: "unexpected" }); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -54,0 +75,0 @@ }; |
@@ -7,2 +7,4 @@ /** | ||
const astUtils = require("./utils/ast-utils"); | ||
//------------------------------------------------------------------------------ | ||
@@ -23,3 +25,14 @@ // Rule Definition | ||
schema: [], | ||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
allowObjectPatternsAsParameters: { | ||
type: "boolean", | ||
default: false | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
], | ||
@@ -32,7 +45,29 @@ messages: { | ||
create(context) { | ||
const options = context.options[0] || {}, | ||
allowObjectPatternsAsParameters = options.allowObjectPatternsAsParameters || false; | ||
return { | ||
ObjectPattern(node) { | ||
if (node.properties.length === 0) { | ||
context.report({ node, messageId: "unexpected", data: { type: "object" } }); | ||
if (node.properties.length > 0) { | ||
return; | ||
} | ||
// Allow {} and {} = {} empty object patterns as parameters when allowObjectPatternsAsParameters is true | ||
if ( | ||
allowObjectPatternsAsParameters && | ||
( | ||
astUtils.isFunction(node.parent) || | ||
( | ||
node.parent.type === "AssignmentPattern" && | ||
astUtils.isFunction(node.parent.parent) && | ||
node.parent.right.type === "ObjectExpression" && | ||
node.parent.right.properties.length === 0 | ||
) | ||
) | ||
) { | ||
return; | ||
} | ||
context.report({ node, messageId: "unexpected", data: { type: "object" } }); | ||
}, | ||
@@ -39,0 +74,0 @@ ArrayPattern(node) { |
/** | ||
* @fileoverview Disallow parenthesising higher precedence subexpressions. | ||
* @author Michael Ficarra | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -17,2 +18,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -50,2 +53,3 @@ | ||
conditionalAssign: { type: "boolean" }, | ||
ternaryOperandBinaryExpressions: { type: "boolean" }, | ||
nestedBinaryExpressions: { type: "boolean" }, | ||
@@ -81,2 +85,3 @@ returnAssign: { type: "boolean" }, | ||
const EXCEPT_COND_ASSIGN = ALL_NODES && context.options[1] && context.options[1].conditionalAssign === false; | ||
const EXCEPT_COND_TERNARY = ALL_NODES && context.options[1] && context.options[1].ternaryOperandBinaryExpressions === false; | ||
const NESTED_BINARY = ALL_NODES && context.options[1] && context.options[1].nestedBinaryExpressions === false; | ||
@@ -393,2 +398,26 @@ const EXCEPT_RETURN_ASSIGN = ALL_NODES && context.options[1] && context.options[1].returnAssign === false; | ||
/** | ||
* Checks if a node is fixable. | ||
* A node is fixable if removing a single pair of surrounding parentheses does not turn it | ||
* into a directive after fixing other nodes. | ||
* Almost all nodes are fixable, except if all of the following conditions are met: | ||
* The node is a string Literal | ||
* It has a single pair of parentheses | ||
* It is the only child of an ExpressionStatement | ||
* @param {ASTNode} node The node to evaluate. | ||
* @returns {boolean} Whether or not the node is fixable. | ||
* @private | ||
*/ | ||
function isFixable(node) { | ||
// if it's not a string literal it can be autofixed | ||
if (node.type !== "Literal" || typeof node.value !== "string") { | ||
return true; | ||
} | ||
if (isParenthesisedTwice(node)) { | ||
return true; | ||
} | ||
return !astUtils.isTopLevelExpressionStatement(node.parent); | ||
} | ||
/** | ||
* Report the node | ||
@@ -436,10 +465,12 @@ * @param {ASTNode} node node to evaluate | ||
messageId: "unexpected", | ||
fix(fixer) { | ||
const parenthesizedSource = sourceCode.text.slice(leftParenToken.range[1], rightParenToken.range[0]); | ||
fix: isFixable(node) | ||
? fixer => { | ||
const parenthesizedSource = sourceCode.text.slice(leftParenToken.range[1], rightParenToken.range[0]); | ||
return fixer.replaceTextRange([ | ||
leftParenToken.range[0], | ||
rightParenToken.range[1] | ||
], (requiresLeadingSpace(node) ? " " : "") + parenthesizedSource + (requiresTrailingSpace(node) ? " " : "")); | ||
} | ||
return fixer.replaceTextRange([ | ||
leftParenToken.range[0], | ||
rightParenToken.range[1] | ||
], (requiresLeadingSpace(node) ? " " : "") + parenthesizedSource + (requiresTrailingSpace(node) ? " " : "")); | ||
} | ||
: null | ||
}); | ||
@@ -868,3 +899,7 @@ } | ||
} | ||
const availableTypes = new Set(["BinaryExpression", "LogicalExpression"]); | ||
if ( | ||
!(EXCEPT_COND_TERNARY && availableTypes.has(node.test.type)) && | ||
!isCondAssignException(node) && | ||
@@ -876,7 +911,11 @@ hasExcessParensWithPrecedence(node.test, precedence({ type: "LogicalExpression", operator: "||" })) | ||
if (hasExcessParensWithPrecedence(node.consequent, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { | ||
if ( | ||
!(EXCEPT_COND_TERNARY && availableTypes.has(node.consequent.type)) && | ||
hasExcessParensWithPrecedence(node.consequent, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { | ||
report(node.consequent); | ||
} | ||
if (hasExcessParensWithPrecedence(node.alternate, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { | ||
if ( | ||
!(EXCEPT_COND_TERNARY && availableTypes.has(node.alternate.type)) && | ||
hasExcessParensWithPrecedence(node.alternate, PRECEDENCE_OF_ASSIGNMENT_EXPR)) { | ||
report(node.alternate); | ||
@@ -883,0 +922,0 @@ } |
/** | ||
* @fileoverview Rule to flag use of unnecessary semicolons | ||
* @author Nicholas C. Zakas | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -22,2 +23,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "suggestion", | ||
@@ -43,2 +46,19 @@ | ||
/** | ||
* Checks if a node or token is fixable. | ||
* A node is fixable if it can be removed without turning a subsequent statement into a directive after fixing other nodes. | ||
* @param {Token} nodeOrToken The node or token to check. | ||
* @returns {boolean} Whether or not the node is fixable. | ||
*/ | ||
function isFixable(nodeOrToken) { | ||
const nextToken = sourceCode.getTokenAfter(nodeOrToken); | ||
if (!nextToken || nextToken.type !== "String") { | ||
return true; | ||
} | ||
const stringNode = sourceCode.getNodeByRangeIndex(nextToken.range[0]); | ||
return !astUtils.isTopLevelExpressionStatement(stringNode.parent); | ||
} | ||
/** | ||
* Reports an unnecessary semicolon error. | ||
@@ -52,13 +72,14 @@ * @param {Node|Token} nodeOrToken A node or a token to be reported. | ||
messageId: "unexpected", | ||
fix(fixer) { | ||
fix: isFixable(nodeOrToken) | ||
? fixer => | ||
/* | ||
* Expand the replacement range to include the surrounding | ||
* tokens to avoid conflicting with semi. | ||
* https://github.com/eslint/eslint/issues/7928 | ||
*/ | ||
return new FixTracker(fixer, context.sourceCode) | ||
.retainSurroundingTokens(nodeOrToken) | ||
.remove(nodeOrToken); | ||
} | ||
/* | ||
* Expand the replacement range to include the surrounding | ||
* tokens to avoid conflicting with semi. | ||
* https://github.com/eslint/eslint/issues/7928 | ||
*/ | ||
new FixTracker(fixer, context.sourceCode) | ||
.retainSurroundingTokens(nodeOrToken) | ||
.remove(nodeOrToken) | ||
: null | ||
}); | ||
@@ -65,0 +86,0 @@ } |
@@ -20,2 +20,18 @@ /** | ||
/** | ||
* Checks all segments in a set and returns true if any are reachable. | ||
* @param {Set<CodePathSegment>} segments The segments to check. | ||
* @returns {boolean} True if any segment is reachable; false otherwise. | ||
*/ | ||
function isAnySegmentReachable(segments) { | ||
for (const segment of segments) { | ||
if (segment.reachable) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Checks whether or not a given comment string is really a fallthrough comment and not an ESLint directive. | ||
@@ -56,11 +72,2 @@ * @param {string} comment The comment string to check. | ||
/** | ||
* Checks whether or not a given code path segment is reachable. | ||
* @param {CodePathSegment} segment A CodePathSegment to check. | ||
* @returns {boolean} `true` if the segment is reachable. | ||
*/ | ||
function isReachable(segment) { | ||
return segment.reachable; | ||
} | ||
/** | ||
* Checks whether a node and a token are separated by blank lines | ||
@@ -114,3 +121,4 @@ * @param {ASTNode} node The node to check | ||
const options = context.options[0] || {}; | ||
let currentCodePath = null; | ||
const codePathSegments = []; | ||
let currentCodePathSegments = new Set(); | ||
const sourceCode = context.sourceCode; | ||
@@ -132,9 +140,29 @@ const allowEmptyCase = options.allowEmptyCase || false; | ||
return { | ||
onCodePathStart(codePath) { | ||
currentCodePath = codePath; | ||
onCodePathStart() { | ||
codePathSegments.push(currentCodePathSegments); | ||
currentCodePathSegments = new Set(); | ||
}, | ||
onCodePathEnd() { | ||
currentCodePath = currentCodePath.upper; | ||
currentCodePathSegments = codePathSegments.pop(); | ||
}, | ||
onUnreachableCodePathSegmentStart(segment) { | ||
currentCodePathSegments.add(segment); | ||
}, | ||
onUnreachableCodePathSegmentEnd(segment) { | ||
currentCodePathSegments.delete(segment); | ||
}, | ||
onCodePathSegmentStart(segment) { | ||
currentCodePathSegments.add(segment); | ||
}, | ||
onCodePathSegmentEnd(segment) { | ||
currentCodePathSegments.delete(segment); | ||
}, | ||
SwitchCase(node) { | ||
@@ -164,3 +192,3 @@ | ||
*/ | ||
if (currentCodePath.currentSegments.some(isReachable) && | ||
if (isAnySegmentReachable(currentCodePathSegments) && | ||
(node.consequent.length > 0 || (!allowEmptyCase && hasBlankLinesBetween(node, nextToken))) && | ||
@@ -167,0 +195,0 @@ node.parent.cases[node.parent.cases.length - 1] !== node) { |
/** | ||
* @fileoverview Rule to flag use of a leading/trailing decimal point in a numeric literal | ||
* @author James Allardice | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -21,2 +22,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "suggestion", | ||
@@ -23,0 +26,0 @@ |
@@ -13,3 +13,3 @@ /** | ||
const validator = new RegExpValidator(); | ||
const validFlags = /[dgimsuy]/gu; | ||
const validFlags = /[dgimsuvy]/gu; | ||
const undefined1 = void 0; | ||
@@ -112,8 +112,10 @@ | ||
* @param {string} pattern The RegExp pattern to validate. | ||
* @param {boolean} uFlag The Unicode flag. | ||
* @param {Object} flags The RegExp flags to validate. | ||
* @param {boolean} [flags.unicode] The Unicode flag. | ||
* @param {boolean} [flags.unicodeSets] The UnicodeSets flag. | ||
* @returns {string|null} The syntax error. | ||
*/ | ||
function validateRegExpPattern(pattern, uFlag) { | ||
function validateRegExpPattern(pattern, flags) { | ||
try { | ||
validator.validatePattern(pattern, undefined1, undefined1, uFlag); | ||
validator.validatePattern(pattern, undefined1, undefined1, flags); | ||
return null; | ||
@@ -136,6 +138,15 @@ } catch (err) { | ||
validator.validateFlags(flags); | ||
return null; | ||
} catch { | ||
return `Invalid flags supplied to RegExp constructor '${flags}'`; | ||
} | ||
/* | ||
* `regexpp` checks the combination of `u` and `v` flags when parsing `Pattern` according to `ecma262`, | ||
* but this rule may check only the flag when the pattern is unidentifiable, so check it here. | ||
* https://tc39.es/ecma262/multipage/text-processing.html#sec-parsepattern | ||
*/ | ||
if (flags.includes("u") && flags.includes("v")) { | ||
return "Regex 'u' and 'v' flags cannot be used together"; | ||
} | ||
return null; | ||
} | ||
@@ -172,4 +183,8 @@ | ||
flags === null | ||
? validateRegExpPattern(pattern, true) && validateRegExpPattern(pattern, false) | ||
: validateRegExpPattern(pattern, flags.includes("u")) | ||
? ( | ||
validateRegExpPattern(pattern, { unicode: true, unicodeSets: false }) && | ||
validateRegExpPattern(pattern, { unicode: false, unicodeSets: true }) && | ||
validateRegExpPattern(pattern, { unicode: false, unicodeSets: false }) | ||
) | ||
: validateRegExpPattern(pattern, { unicode: flags.includes("u"), unicodeSets: flags.includes("v") }) | ||
); | ||
@@ -176,0 +191,0 @@ |
@@ -58,2 +58,6 @@ /** | ||
default: false | ||
}, | ||
skipJSXText: { | ||
type: "boolean", | ||
default: false | ||
} | ||
@@ -81,2 +85,3 @@ }, | ||
const skipTemplates = !!options.skipTemplates; | ||
const skipJSXText = !!options.skipJSXText; | ||
@@ -150,2 +155,14 @@ const sourceCode = context.sourceCode; | ||
/** | ||
* Checks JSX nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors | ||
* @param {ASTNode} node to check for matching errors. | ||
* @returns {void} | ||
* @private | ||
*/ | ||
function removeInvalidNodeErrorsInJSXText(node) { | ||
if (ALL_IRREGULARS.test(node.raw)) { | ||
removeWhitespaceError(node); | ||
} | ||
} | ||
/** | ||
* Checks the program source for irregular whitespace | ||
@@ -245,2 +262,3 @@ * @param {ASTNode} node The program node | ||
nodes.TemplateElement = skipTemplates ? removeInvalidNodeErrorsInTemplateLiteral : noop; | ||
nodes.JSXText = skipJSXText ? removeInvalidNodeErrorsInJSXText : noop; | ||
nodes["Program:exit"] = function() { | ||
@@ -247,0 +265,0 @@ if (skipComments) { |
@@ -189,3 +189,3 @@ /** | ||
const references = sourceCode.getScope(node).through; | ||
const unsafeRefs = references.filter(r => !isSafe(loopNode, r)).map(r => r.identifier.name); | ||
const unsafeRefs = references.filter(r => r.resolved && !isSafe(loopNode, r)).map(r => r.identifier.name); | ||
@@ -192,0 +192,0 @@ if (unsafeRefs.length > 0) { |
@@ -86,3 +86,3 @@ /** | ||
function addDecimalPointToNumber(stringNumber) { | ||
return `${stringNumber.slice(0, 1)}.${stringNumber.slice(1)}`; | ||
return `${stringNumber[0]}.${stringNumber.slice(1)}`; | ||
} | ||
@@ -96,3 +96,8 @@ | ||
function removeLeadingZeros(numberAsString) { | ||
return numberAsString.replace(/^0*/u, ""); | ||
for (let i = 0; i < numberAsString.length; i++) { | ||
if (numberAsString[i] !== "0") { | ||
return numberAsString.slice(i); | ||
} | ||
} | ||
return numberAsString; | ||
} | ||
@@ -106,3 +111,8 @@ | ||
function removeTrailingZeros(numberAsString) { | ||
return numberAsString.replace(/0*$/u, ""); | ||
for (let i = numberAsString.length - 1; i >= 0; i--) { | ||
if (numberAsString[i] !== "0") { | ||
return numberAsString.slice(0, i + 1); | ||
} | ||
} | ||
return numberAsString; | ||
} | ||
@@ -134,3 +144,3 @@ | ||
if (trimmedFloat.startsWith(".")) { | ||
const decimalDigits = trimmedFloat.split(".").pop(); | ||
const decimalDigits = trimmedFloat.slice(1); | ||
const significantDigits = removeLeadingZeros(decimalDigits); | ||
@@ -151,3 +161,2 @@ | ||
/** | ||
@@ -168,3 +177,2 @@ * Converts a base ten number to proper scientific notation | ||
return `${normalizedCoefficient}e${magnitude}`; | ||
} | ||
@@ -171,0 +179,0 @@ |
@@ -17,2 +17,7 @@ /** | ||
/** | ||
* @typedef {import('@eslint-community/regexpp').AST.Character} Character | ||
* @typedef {import('@eslint-community/regexpp').AST.CharacterClassElement} CharacterClassElement | ||
*/ | ||
/** | ||
* Iterate character sequences of a given nodes. | ||
@@ -22,6 +27,8 @@ * | ||
* 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. | ||
* @param {CharacterClassElement[]} nodes The node list to iterate character sequences. | ||
* @returns {IterableIterator<Character[]>} The list of character sequences. | ||
*/ | ||
function *iterateCharacterSequence(nodes) { | ||
/** @type {Character[]} */ | ||
let seq = []; | ||
@@ -32,12 +39,15 @@ | ||
case "Character": | ||
seq.push(node.value); | ||
seq.push(node); | ||
break; | ||
case "CharacterClassRange": | ||
seq.push(node.min.value); | ||
seq.push(node.min); | ||
yield seq; | ||
seq = [node.max.value]; | ||
seq = [node.max]; | ||
break; | ||
case "CharacterSet": | ||
case "CharacterClass": // [[]] nesting character class | ||
case "ClassStringDisjunction": // \q{...} | ||
case "ExpressionCharacterClass": // [A--B] | ||
if (seq.length > 0) { | ||
@@ -58,12 +68,54 @@ yield seq; | ||
/** | ||
* Checks whether the given character node is a Unicode code point escape or not. | ||
* @param {Character} char the character node to check. | ||
* @returns {boolean} `true` if the character node is a Unicode code point escape. | ||
*/ | ||
function isUnicodeCodePointEscape(char) { | ||
return /^\\u\{[\da-f]+\}$/iu.test(char.raw); | ||
} | ||
/** | ||
* Each function returns `true` if it detects that kind of problem. | ||
* @type {Record<string, (chars: Character[]) => boolean>} | ||
*/ | ||
const hasCharacterSequence = { | ||
surrogatePairWithoutUFlag(chars) { | ||
return chars.some((c, i) => i !== 0 && isSurrogatePair(chars[i - 1], c)); | ||
return chars.some((c, i) => { | ||
if (i === 0) { | ||
return false; | ||
} | ||
const c1 = chars[i - 1]; | ||
return ( | ||
isSurrogatePair(c1.value, c.value) && | ||
!isUnicodeCodePointEscape(c1) && | ||
!isUnicodeCodePointEscape(c) | ||
); | ||
}); | ||
}, | ||
surrogatePair(chars) { | ||
return chars.some((c, i) => { | ||
if (i === 0) { | ||
return false; | ||
} | ||
const c1 = chars[i - 1]; | ||
return ( | ||
isSurrogatePair(c1.value, c.value) && | ||
( | ||
isUnicodeCodePointEscape(c1) || | ||
isUnicodeCodePointEscape(c) | ||
) | ||
); | ||
}); | ||
}, | ||
combiningClass(chars) { | ||
return chars.some((c, i) => ( | ||
i !== 0 && | ||
isCombiningCharacter(c) && | ||
!isCombiningCharacter(chars[i - 1]) | ||
isCombiningCharacter(c.value) && | ||
!isCombiningCharacter(chars[i - 1].value) | ||
)); | ||
@@ -75,4 +127,4 @@ }, | ||
i !== 0 && | ||
isEmojiModifier(c) && | ||
!isEmojiModifier(chars[i - 1]) | ||
isEmojiModifier(c.value) && | ||
!isEmojiModifier(chars[i - 1].value) | ||
)); | ||
@@ -84,4 +136,4 @@ }, | ||
i !== 0 && | ||
isRegionalIndicatorSymbol(c) && | ||
isRegionalIndicatorSymbol(chars[i - 1]) | ||
isRegionalIndicatorSymbol(c.value) && | ||
isRegionalIndicatorSymbol(chars[i - 1].value) | ||
)); | ||
@@ -96,5 +148,5 @@ }, | ||
i !== lastIndex && | ||
c === 0x200d && | ||
chars[i - 1] !== 0x200d && | ||
chars[i + 1] !== 0x200d | ||
c.value === 0x200d && | ||
chars[i - 1].value !== 0x200d && | ||
chars[i + 1].value !== 0x200d | ||
)); | ||
@@ -127,2 +179,3 @@ } | ||
surrogatePairWithoutUFlag: "Unexpected surrogate pair in character class. Use 'u' flag.", | ||
surrogatePair: "Unexpected surrogate pair in character class.", | ||
combiningClass: "Unexpected combined character in character class.", | ||
@@ -155,3 +208,6 @@ emojiModifier: "Unexpected modified Emoji in character class.", | ||
pattern.length, | ||
flags.includes("u") | ||
{ | ||
unicode: flags.includes("u"), | ||
unicodeSets: flags.includes("v") | ||
} | ||
); | ||
@@ -158,0 +214,0 @@ } catch { |
/** | ||
* @fileoverview Rule to disallow mixed binary operators. | ||
* @author Toru Nagashima | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -88,2 +89,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "suggestion", | ||
@@ -90,0 +93,0 @@ |
/** | ||
* @fileoverview Disallow mixed spaces and tabs for indentation | ||
* @author Jary Niebur | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -14,2 +15,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -16,0 +19,0 @@ |
/** | ||
* @fileoverview Disallow use of multiple spaces. | ||
* @author Nicholas C. Zakas | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -17,2 +18,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -19,0 +22,0 @@ |
@@ -5,2 +5,3 @@ /** | ||
* @author Greg Cochard | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -16,2 +17,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -18,0 +21,0 @@ |
/** | ||
* @fileoverview A rule to disallow calls to the Object constructor | ||
* @author Matt DuVall <http://www.mattduvall.com/> | ||
* @deprecated in ESLint v8.50.0 | ||
*/ | ||
@@ -29,2 +30,8 @@ | ||
deprecated: true, | ||
replacedBy: [ | ||
"no-object-constructor" | ||
], | ||
schema: [], | ||
@@ -31,0 +38,0 @@ |
@@ -9,2 +9,8 @@ /** | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
const { getVariableByName } = require("./utils/ast-utils"); | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
@@ -32,2 +38,3 @@ //------------------------------------------------------------------------------ | ||
create(context) { | ||
const { sourceCode } = context; | ||
@@ -38,9 +45,14 @@ return { | ||
const wrapperObjects = ["String", "Number", "Boolean"]; | ||
const { name } = node.callee; | ||
if (wrapperObjects.includes(node.callee.name)) { | ||
context.report({ | ||
node, | ||
messageId: "noConstructor", | ||
data: { fn: node.callee.name } | ||
}); | ||
if (wrapperObjects.includes(name)) { | ||
const variable = getVariableByName(sourceCode.getScope(node), name); | ||
if (variable && variable.identifiers.length === 0) { | ||
context.report({ | ||
node, | ||
messageId: "noConstructor", | ||
data: { fn: name } | ||
}); | ||
} | ||
} | ||
@@ -47,0 +59,0 @@ } |
@@ -13,2 +13,3 @@ /** | ||
const { findVariable } = require("@eslint-community/eslint-utils"); | ||
const astUtils = require("./utils/ast-utils"); | ||
@@ -63,2 +64,74 @@ //------------------------------------------------------------------------------ | ||
/** | ||
* Checks if the given node is a void expression. | ||
* @param {ASTNode} node The node to check. | ||
* @returns {boolean} - `true` if the node is a void expression | ||
*/ | ||
function expressionIsVoid(node) { | ||
return node.type === "UnaryExpression" && node.operator === "void"; | ||
} | ||
/** | ||
* Fixes the linting error by prepending "void " to the given node | ||
* @param {Object} sourceCode context given by context.sourceCode | ||
* @param {ASTNode} node The node to fix. | ||
* @param {Object} fixer The fixer object provided by ESLint. | ||
* @returns {Array<Object>} - An array of fix objects to apply to the node. | ||
*/ | ||
function voidPrependFixer(sourceCode, node, fixer) { | ||
const requiresParens = | ||
// prepending `void ` will fail if the node has a lower precedence than void | ||
astUtils.getPrecedence(node) < astUtils.getPrecedence({ type: "UnaryExpression", operator: "void" }) && | ||
// check if there are parentheses around the node to avoid redundant parentheses | ||
!astUtils.isParenthesised(sourceCode, node); | ||
// avoid parentheses issues | ||
const returnOrArrowToken = sourceCode.getTokenBefore( | ||
node, | ||
node.parent.type === "ArrowFunctionExpression" | ||
? astUtils.isArrowToken | ||
// isReturnToken | ||
: token => token.type === "Keyword" && token.value === "return" | ||
); | ||
const firstToken = sourceCode.getTokenAfter(returnOrArrowToken); | ||
const prependSpace = | ||
// is return token, as => allows void to be adjacent | ||
returnOrArrowToken.value === "return" && | ||
// If two tokens (return and "(") are adjacent | ||
returnOrArrowToken.range[1] === firstToken.range[0]; | ||
return [ | ||
fixer.insertTextBefore(firstToken, `${prependSpace ? " " : ""}void ${requiresParens ? "(" : ""}`), | ||
fixer.insertTextAfter(node, requiresParens ? ")" : "") | ||
]; | ||
} | ||
/** | ||
* Fixes the linting error by `wrapping {}` around the given node's body. | ||
* @param {Object} sourceCode context given by context.sourceCode | ||
* @param {ASTNode} node The node to fix. | ||
* @param {Object} fixer The fixer object provided by ESLint. | ||
* @returns {Array<Object>} - An array of fix objects to apply to the node. | ||
*/ | ||
function curlyWrapFixer(sourceCode, node, fixer) { | ||
// https://github.com/eslint/eslint/pull/17282#issuecomment-1592795923 | ||
const arrowToken = sourceCode.getTokenBefore(node.body, astUtils.isArrowToken); | ||
const firstToken = sourceCode.getTokenAfter(arrowToken); | ||
const lastToken = sourceCode.getLastToken(node); | ||
return [ | ||
fixer.insertTextBefore(firstToken, "{"), | ||
fixer.insertTextAfter(lastToken, "}") | ||
]; | ||
} | ||
//------------------------------------------------------------------------------ | ||
@@ -79,6 +152,23 @@ // Rule Definition | ||
schema: [], | ||
hasSuggestions: true, | ||
schema: [{ | ||
type: "object", | ||
properties: { | ||
allowVoid: { | ||
type: "boolean", | ||
default: false | ||
} | ||
}, | ||
additionalProperties: false | ||
}], | ||
messages: { | ||
returnsValue: "Return values from promise executor functions cannot be read." | ||
returnsValue: "Return values from promise executor functions cannot be read.", | ||
// arrow and function suggestions | ||
prependVoid: "Prepend `void` to the expression.", | ||
// only arrow suggestions | ||
wrapBraces: "Wrap the expression in `{}`." | ||
} | ||
@@ -91,12 +181,6 @@ }, | ||
const sourceCode = context.sourceCode; | ||
const { | ||
allowVoid = false | ||
} = context.options[0] || {}; | ||
/** | ||
* Reports the given node. | ||
* @param {ASTNode} node Node to report. | ||
* @returns {void} | ||
*/ | ||
function report(node) { | ||
context.report({ node, messageId: "returnsValue" }); | ||
} | ||
return { | ||
@@ -107,7 +191,39 @@ | ||
upper: funcInfo, | ||
shouldCheck: functionTypesToCheck.has(node.type) && isPromiseExecutor(node, sourceCode.getScope(node)) | ||
shouldCheck: | ||
functionTypesToCheck.has(node.type) && | ||
isPromiseExecutor(node, sourceCode.getScope(node)) | ||
}; | ||
if (funcInfo.shouldCheck && node.type === "ArrowFunctionExpression" && node.expression) { | ||
report(node.body); | ||
if (// Is a Promise executor | ||
funcInfo.shouldCheck && | ||
node.type === "ArrowFunctionExpression" && | ||
node.expression && | ||
// Except void | ||
!(allowVoid && expressionIsVoid(node.body)) | ||
) { | ||
const suggest = []; | ||
// prevent useless refactors | ||
if (allowVoid) { | ||
suggest.push({ | ||
messageId: "prependVoid", | ||
fix(fixer) { | ||
return voidPrependFixer(sourceCode, node.body, fixer); | ||
} | ||
}); | ||
} | ||
suggest.push({ | ||
messageId: "wrapBraces", | ||
fix(fixer) { | ||
return curlyWrapFixer(sourceCode, node, fixer); | ||
} | ||
}); | ||
context.report({ | ||
node: node.body, | ||
messageId: "returnsValue", | ||
suggest | ||
}); | ||
} | ||
@@ -121,5 +237,27 @@ }, | ||
ReturnStatement(node) { | ||
if (funcInfo.shouldCheck && node.argument) { | ||
report(node); | ||
if (!(funcInfo.shouldCheck && node.argument)) { | ||
return; | ||
} | ||
// node is `return <expression>` | ||
if (!allowVoid) { | ||
context.report({ node, messageId: "returnsValue" }); | ||
return; | ||
} | ||
if (expressionIsVoid(node.argument)) { | ||
return; | ||
} | ||
// allowVoid && !expressionIsVoid | ||
context.report({ | ||
node, | ||
messageId: "returnsValue", | ||
suggest: [{ | ||
messageId: "prependVoid", | ||
fix(fixer) { | ||
return voidPrependFixer(sourceCode, node.argument, fixer); | ||
} | ||
}] | ||
}); | ||
} | ||
@@ -126,0 +264,0 @@ }; |
@@ -14,2 +14,33 @@ /** | ||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
/** | ||
* Returns true if the node or any of the objects | ||
* to the left of it in the member/call chain is optional. | ||
* | ||
* e.g. `a?.b`, `a?.b.c`, `a?.()`, `a()?.()` | ||
* @param {ASTNode} node The expression to check | ||
* @returns {boolean} `true` if there is a short-circuiting optional `?.` | ||
* in the same option chain to the left of this call or member expression, | ||
* or the node itself is an optional call or member `?.`. | ||
*/ | ||
function isAfterOptional(node) { | ||
let leftNode; | ||
if (node.type === "MemberExpression") { | ||
leftNode = node.object; | ||
} else if (node.type === "CallExpression") { | ||
leftNode = node.callee; | ||
} else { | ||
return false; | ||
} | ||
if (node.optional) { | ||
return true; | ||
} | ||
return isAfterOptional(leftNode); | ||
} | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
@@ -29,6 +60,9 @@ //------------------------------------------------------------------------------ | ||
hasSuggestions: true, | ||
schema: [], | ||
messages: { | ||
prototypeBuildIn: "Do not access Object.prototype method '{{prop}}' from target object." | ||
prototypeBuildIn: "Do not access Object.prototype method '{{prop}}' from target object.", | ||
callObjectPrototype: "Call Object.prototype.{{prop}} explicitly." | ||
} | ||
@@ -64,3 +98,57 @@ }, | ||
data: { prop: propName }, | ||
node | ||
node, | ||
suggest: [ | ||
{ | ||
messageId: "callObjectPrototype", | ||
data: { prop: propName }, | ||
fix(fixer) { | ||
const sourceCode = context.sourceCode; | ||
/* | ||
* A call after an optional chain (e.g. a?.b.hasOwnProperty(c)) | ||
* must be fixed manually because the call can be short-circuited | ||
*/ | ||
if (isAfterOptional(node)) { | ||
return null; | ||
} | ||
/* | ||
* A call on a ChainExpression (e.g. (a?.hasOwnProperty)(c)) will trigger | ||
* no-unsafe-optional-chaining which should be fixed before this suggestion | ||
*/ | ||
if (node.callee.type === "ChainExpression") { | ||
return null; | ||
} | ||
const objectVariable = astUtils.getVariableByName(sourceCode.getScope(node), "Object"); | ||
/* | ||
* We can't use Object if the global Object was shadowed, | ||
* or Object does not exist in the global scope for some reason | ||
*/ | ||
if (!objectVariable || objectVariable.scope.type !== "global" || objectVariable.defs.length > 0) { | ||
return null; | ||
} | ||
let objectText = sourceCode.getText(callee.object); | ||
if (astUtils.getPrecedence(callee.object) <= astUtils.getPrecedence({ type: "SequenceExpression" })) { | ||
objectText = `(${objectText})`; | ||
} | ||
const openParenToken = sourceCode.getTokenAfter( | ||
node.callee, | ||
astUtils.isOpeningParenToken | ||
); | ||
const isEmptyParameters = node.arguments.length === 0; | ||
const delim = isEmptyParameters ? "" : ", "; | ||
const fixes = [ | ||
fixer.replaceText(callee, `Object.prototype.${propName}.call`), | ||
fixer.insertTextAfter(openParenToken, objectText + delim) | ||
]; | ||
return fixes; | ||
} | ||
} | ||
] | ||
}); | ||
@@ -67,0 +155,0 @@ } |
@@ -80,3 +80,3 @@ /** | ||
try { | ||
regExpAST = regExpParser.parsePattern(pattern, 0, pattern.length, flags.includes("u")); | ||
regExpAST = regExpParser.parsePattern(pattern, 0, pattern.length, { unicode: flags.includes("u"), unicodeSets: flags.includes("v") }); | ||
} catch { | ||
@@ -159,3 +159,2 @@ | ||
const patternNode = node.arguments[0]; | ||
const flagsNode = node.arguments[1]; | ||
@@ -166,4 +165,20 @@ if (node.callee.type === "Identifier" && node.callee.name === "RegExp" && isString(patternNode) && !shadowed) { | ||
const rawPatternStartRange = patternNode.range[0] + 1; | ||
const flags = isString(flagsNode) ? flagsNode.value : ""; | ||
let flags; | ||
if (node.arguments.length < 2) { | ||
// It has no flags. | ||
flags = ""; | ||
} else { | ||
const flagsNode = node.arguments[1]; | ||
if (isString(flagsNode)) { | ||
flags = flagsNode.value; | ||
} else { | ||
// The flags cannot be determined. | ||
return; | ||
} | ||
} | ||
checkRegex( | ||
@@ -170,0 +185,0 @@ node, |
@@ -77,2 +77,5 @@ /** | ||
}, | ||
importNamePattern: { | ||
type: "string" | ||
}, | ||
message: { | ||
@@ -119,4 +122,8 @@ type: "string", | ||
patternAndEverything: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern.", | ||
patternAndEverythingWithRegexImportName: "* import is invalid because import name matching '{{importNames}}' pattern from '{{importSource}}' is restricted from being used.", | ||
// eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period | ||
patternAndEverythingWithCustomMessage: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}", | ||
// eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period | ||
patternAndEverythingWithRegexImportNameAndCustomMessage: "* import is invalid because import name matching '{{importNames}}' pattern from '{{importSource}}' is restricted from being used. {{customMessage}}", | ||
@@ -180,6 +187,7 @@ everything: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted.", | ||
// relative paths are supported for this rule | ||
const restrictedPatternGroups = restrictedPatterns.map(({ group, message, caseSensitive, importNames }) => ({ | ||
const restrictedPatternGroups = restrictedPatterns.map(({ group, message, caseSensitive, importNames, importNamePattern }) => ({ | ||
matcher: ignore({ allowRelativePaths: true, ignorecase: !caseSensitive }).add(group), | ||
customMessage: message, | ||
importNames | ||
importNames, | ||
importNamePattern | ||
})); | ||
@@ -268,2 +276,3 @@ | ||
const restrictedImportNames = group.importNames; | ||
const restrictedImportNamePattern = group.importNamePattern ? new RegExp(group.importNamePattern, "u") : null; | ||
@@ -274,3 +283,3 @@ /* | ||
*/ | ||
if (!restrictedImportNames) { | ||
if (!restrictedImportNames && !restrictedImportNamePattern) { | ||
context.report({ | ||
@@ -287,36 +296,50 @@ node, | ||
if (importNames.has("*")) { | ||
const specifierData = importNames.get("*")[0]; | ||
importNames.forEach((specifiers, importName) => { | ||
if (importName === "*") { | ||
const [specifier] = specifiers; | ||
context.report({ | ||
node, | ||
messageId: customMessage ? "patternAndEverythingWithCustomMessage" : "patternAndEverything", | ||
loc: specifierData.loc, | ||
data: { | ||
importSource, | ||
importNames: restrictedImportNames, | ||
customMessage | ||
if (restrictedImportNames) { | ||
context.report({ | ||
node, | ||
messageId: customMessage ? "patternAndEverythingWithCustomMessage" : "patternAndEverything", | ||
loc: specifier.loc, | ||
data: { | ||
importSource, | ||
importNames: restrictedImportNames, | ||
customMessage | ||
} | ||
}); | ||
} else { | ||
context.report({ | ||
node, | ||
messageId: customMessage ? "patternAndEverythingWithRegexImportNameAndCustomMessage" : "patternAndEverythingWithRegexImportName", | ||
loc: specifier.loc, | ||
data: { | ||
importSource, | ||
importNames: restrictedImportNamePattern, | ||
customMessage | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
restrictedImportNames.forEach(importName => { | ||
if (!importNames.has(importName)) { | ||
return; | ||
} | ||
const specifiers = importNames.get(importName); | ||
specifiers.forEach(specifier => { | ||
context.report({ | ||
node, | ||
messageId: customMessage ? "patternAndImportNameWithCustomMessage" : "patternAndImportName", | ||
loc: specifier.loc, | ||
data: { | ||
importSource, | ||
customMessage, | ||
importName | ||
} | ||
if ( | ||
(restrictedImportNames && restrictedImportNames.includes(importName)) || | ||
(restrictedImportNamePattern && restrictedImportNamePattern.test(importName)) | ||
) { | ||
specifiers.forEach(specifier => { | ||
context.report({ | ||
node, | ||
messageId: customMessage ? "patternAndImportNameWithCustomMessage" : "patternAndImportName", | ||
loc: specifier.loc, | ||
data: { | ||
importSource, | ||
customMessage, | ||
importName | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
@@ -323,0 +346,0 @@ } |
@@ -9,2 +9,8 @@ /** | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
const astUtils = require("./utils/ast-utils"); | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
@@ -121,11 +127,2 @@ //------------------------------------------------------------------------------ | ||
/** | ||
* Function to check if a node is a static string template literal. | ||
* @param {ASTNode} node The node to check. | ||
* @returns {boolean} If the node is a string template literal. | ||
*/ | ||
function isStaticTemplateLiteral(node) { | ||
return node && node.type === "TemplateLiteral" && node.expressions.length === 0; | ||
} | ||
/** | ||
* Function to check if a node is a require call. | ||
@@ -149,3 +146,3 @@ * @param {ASTNode} node The node to check. | ||
if (isStaticTemplateLiteral(node)) { | ||
if (astUtils.isStaticTemplateLiteral(node)) { | ||
return node.quasis[0].value.cooked.trim(); | ||
@@ -152,0 +149,0 @@ } |
/** | ||
* @fileoverview Disallows unnecessary `return await` | ||
* @author Jordan Harband | ||
* @deprecated in ESLint v8.46.0 | ||
*/ | ||
@@ -29,2 +30,6 @@ "use strict"; | ||
deprecated: true, | ||
replacedBy: [], | ||
schema: [ | ||
@@ -31,0 +36,0 @@ ], |
/** | ||
* @fileoverview Rule to check for tabs inside a file | ||
* @author Gyandeep Singh | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -22,2 +23,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -24,0 +27,0 @@ |
@@ -94,2 +94,17 @@ /** | ||
/** | ||
* Determines if every segment in a set has been called. | ||
* @param {Set<CodePathSegment>} segments The segments to search. | ||
* @returns {boolean} True if every segment has been called; false otherwise. | ||
*/ | ||
function isEverySegmentCalled(segments) { | ||
for (const segment of segments) { | ||
if (!isCalled(segment)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* Checks whether or not this is before `super()` is called. | ||
@@ -101,3 +116,3 @@ * @returns {boolean} `true` if this is before `super()` is called. | ||
isInConstructorOfDerivedClass() && | ||
!funcInfo.codePath.currentSegments.every(isCalled) | ||
!isEverySegmentCalled(funcInfo.currentSegments) | ||
); | ||
@@ -113,7 +128,5 @@ } | ||
function setInvalid(node) { | ||
const segments = funcInfo.codePath.currentSegments; | ||
const segments = funcInfo.currentSegments; | ||
for (let i = 0; i < segments.length; ++i) { | ||
const segment = segments[i]; | ||
for (const segment of segments) { | ||
if (segment.reachable) { | ||
@@ -130,7 +143,5 @@ segInfoMap[segment.id].invalidNodes.push(node); | ||
function setSuperCalled() { | ||
const segments = funcInfo.codePath.currentSegments; | ||
const segments = funcInfo.currentSegments; | ||
for (let i = 0; i < segments.length; ++i) { | ||
const segment = segments[i]; | ||
for (const segment of segments) { | ||
if (segment.reachable) { | ||
@@ -163,3 +174,4 @@ segInfoMap[segment.id].superCalled = true; | ||
), | ||
codePath | ||
codePath, | ||
currentSegments: new Set() | ||
}; | ||
@@ -171,3 +183,4 @@ } else { | ||
hasExtends: false, | ||
codePath | ||
codePath, | ||
currentSegments: new Set() | ||
}; | ||
@@ -220,2 +233,4 @@ } | ||
onCodePathSegmentStart(segment) { | ||
funcInfo.currentSegments.add(segment); | ||
if (!isInConstructorOfDerivedClass()) { | ||
@@ -235,2 +250,14 @@ return; | ||
onUnreachableCodePathSegmentStart(segment) { | ||
funcInfo.currentSegments.add(segment); | ||
}, | ||
onUnreachableCodePathSegmentEnd(segment) { | ||
funcInfo.currentSegments.delete(segment); | ||
}, | ||
onCodePathSegmentEnd(segment) { | ||
funcInfo.currentSegments.delete(segment); | ||
}, | ||
/** | ||
@@ -237,0 +264,0 @@ * Update information of the code path segment when a code path was |
/** | ||
* @fileoverview Disallow trailing spaces at the end of lines. | ||
* @author Nodeca Team <https://github.com/nodeca> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -20,2 +21,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -22,0 +25,0 @@ |
@@ -15,2 +15,18 @@ /** | ||
/** | ||
* Checks all segments in a set and returns true if any are reachable. | ||
* @param {Set<CodePathSegment>} segments The segments to check. | ||
* @returns {boolean} True if any segment is reachable; false otherwise. | ||
*/ | ||
function isAnySegmentReachable(segments) { | ||
for (const segment of segments) { | ||
if (segment.reachable) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Determines whether the given node is the first node in the code path to which a loop statement | ||
@@ -94,25 +110,32 @@ * 'loops' for the next iteration. | ||
let currentCodePath = null; | ||
const codePathSegments = []; | ||
let currentCodePathSegments = new Set(); | ||
return { | ||
onCodePathStart(codePath) { | ||
currentCodePath = codePath; | ||
onCodePathStart() { | ||
codePathSegments.push(currentCodePathSegments); | ||
currentCodePathSegments = new Set(); | ||
}, | ||
onCodePathEnd() { | ||
currentCodePath = currentCodePath.upper; | ||
currentCodePathSegments = codePathSegments.pop(); | ||
}, | ||
[loopSelector](node) { | ||
onUnreachableCodePathSegmentStart(segment) { | ||
currentCodePathSegments.add(segment); | ||
}, | ||
/** | ||
* Ignore unreachable loop statements to avoid unnecessary complexity in the implementation, or false positives otherwise. | ||
* For unreachable segments, the code path analysis does not raise events required for this implementation. | ||
*/ | ||
if (currentCodePath.currentSegments.some(segment => segment.reachable)) { | ||
loopsToReport.add(node); | ||
} | ||
onUnreachableCodePathSegmentEnd(segment) { | ||
currentCodePathSegments.delete(segment); | ||
}, | ||
onCodePathSegmentEnd(segment) { | ||
currentCodePathSegments.delete(segment); | ||
}, | ||
onCodePathSegmentStart(segment, node) { | ||
currentCodePathSegments.add(segment); | ||
if (isLoopingTarget(node)) { | ||
@@ -145,2 +168,14 @@ const loop = node.parent; | ||
[loopSelector](node) { | ||
/** | ||
* Ignore unreachable loop statements to avoid unnecessary complexity in the implementation, or false positives otherwise. | ||
* For unreachable segments, the code path analysis does not raise events required for this implementation. | ||
*/ | ||
if (isAnySegmentReachable(currentCodePathSegments)) { | ||
loopsToReport.add(node); | ||
} | ||
}, | ||
"Program:exit"() { | ||
@@ -147,0 +182,0 @@ loopsToReport.forEach( |
@@ -27,8 +27,15 @@ /** | ||
/** | ||
* Checks whether or not a given code path segment is unreachable. | ||
* @param {CodePathSegment} segment A CodePathSegment to check. | ||
* @returns {boolean} `true` if the segment is unreachable. | ||
* Checks all segments in a set and returns true if all are unreachable. | ||
* @param {Set<CodePathSegment>} segments The segments to check. | ||
* @returns {boolean} True if all segments are unreachable; false otherwise. | ||
*/ | ||
function isUnreachable(segment) { | ||
return !segment.reachable; | ||
function areAllSegmentsUnreachable(segments) { | ||
for (const segment of segments) { | ||
if (segment.reachable) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
@@ -128,3 +135,2 @@ | ||
create(context) { | ||
let currentCodePath = null; | ||
@@ -137,2 +143,8 @@ /** @type {ConstructorInfo | null} */ | ||
/** @type {Array<Set<CodePathSegment>>} */ | ||
const codePathSegments = []; | ||
/** @type {Set<CodePathSegment>} */ | ||
let currentCodePathSegments = new Set(); | ||
/** | ||
@@ -146,3 +158,3 @@ * Reports a given node if it's unreachable. | ||
if (node && (node.type === "PropertyDefinition" || currentCodePath.currentSegments.every(isUnreachable))) { | ||
if (node && (node.type === "PropertyDefinition" || areAllSegmentsUnreachable(currentCodePathSegments))) { | ||
@@ -188,10 +200,27 @@ // Store this statement to distinguish consecutive statements. | ||
// Manages the current code path. | ||
onCodePathStart(codePath) { | ||
currentCodePath = codePath; | ||
onCodePathStart() { | ||
codePathSegments.push(currentCodePathSegments); | ||
currentCodePathSegments = new Set(); | ||
}, | ||
onCodePathEnd() { | ||
currentCodePath = currentCodePath.upper; | ||
currentCodePathSegments = codePathSegments.pop(); | ||
}, | ||
onUnreachableCodePathSegmentStart(segment) { | ||
currentCodePathSegments.add(segment); | ||
}, | ||
onUnreachableCodePathSegmentEnd(segment) { | ||
currentCodePathSegments.delete(segment); | ||
}, | ||
onCodePathSegmentEnd(segment) { | ||
currentCodePathSegments.delete(segment); | ||
}, | ||
onCodePathSegmentStart(segment) { | ||
currentCodePathSegments.add(segment); | ||
}, | ||
// Registers for all statement nodes (excludes FunctionDeclaration). | ||
@@ -198,0 +227,0 @@ BlockStatement: reportIfUnreachable, |
@@ -7,2 +7,4 @@ /** | ||
const astUtils = require("./utils/ast-utils"); | ||
//------------------------------------------------------------------------------ | ||
@@ -116,4 +118,2 @@ // Rule Definition | ||
function isDirective(node) { | ||
const parent = node.parent, | ||
grandparent = parent.parent; | ||
@@ -126,5 +126,3 @@ /** | ||
*/ | ||
return (parent.type === "Program" || parent.type === "BlockStatement" && | ||
(/Function/u.test(grandparent.type))) && | ||
directives(parent).includes(node); | ||
return astUtils.isTopLevelExpressionStatement(node) && directives(node.parent).includes(node); | ||
} | ||
@@ -131,0 +129,0 @@ |
@@ -9,2 +9,8 @@ /** | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
const astUtils = require("./utils/ast-utils"); | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
@@ -51,2 +57,41 @@ //------------------------------------------------------------------------------ | ||
/** | ||
* Checks if a `LabeledStatement` node is fixable. | ||
* For a node to be fixable, there must be no comments between the label and the body. | ||
* Furthermore, is must be possible to remove the label without turning the body statement into a | ||
* directive after other fixes are applied. | ||
* @param {ASTNode} node The node to evaluate. | ||
* @returns {boolean} Whether or not the node is fixable. | ||
*/ | ||
function isFixable(node) { | ||
/* | ||
* Only perform a fix if there are no comments between the label and the body. This will be the case | ||
* when there is exactly one token/comment (the ":") between the label and the body. | ||
*/ | ||
if (sourceCode.getTokenAfter(node.label, { includeComments: true }) !== | ||
sourceCode.getTokenBefore(node.body, { includeComments: true })) { | ||
return false; | ||
} | ||
// Looking for the node's deepest ancestor which is not a `LabeledStatement`. | ||
let ancestor = node.parent; | ||
while (ancestor.type === "LabeledStatement") { | ||
ancestor = ancestor.parent; | ||
} | ||
if (ancestor.type === "Program" || | ||
(ancestor.type === "BlockStatement" && astUtils.isFunction(ancestor.parent))) { | ||
const { body } = node; | ||
if (body.type === "ExpressionStatement" && | ||
((body.expression.type === "Literal" && typeof body.expression.value === "string") || | ||
astUtils.isStaticTemplateLiteral(body.expression))) { | ||
return false; // potential directive | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* Removes the top of the stack. | ||
@@ -63,15 +108,3 @@ * At the same time, this reports the label if it's never used. | ||
data: node.label, | ||
fix(fixer) { | ||
/* | ||
* Only perform a fix if there are no comments between the label and the body. This will be the case | ||
* when there is exactly one token/comment (the ":") between the label and the body. | ||
*/ | ||
if (sourceCode.getTokenAfter(node.label, { includeComments: true }) === | ||
sourceCode.getTokenBefore(node.body, { includeComments: true })) { | ||
return fixer.removeRange([node.range[0], node.body.range[0]]); | ||
} | ||
return null; | ||
} | ||
fix: isFixable(node) ? fixer => fixer.removeRange([node.range[0], node.body.range[0]]) : null | ||
}); | ||
@@ -78,0 +111,0 @@ } |
@@ -469,3 +469,4 @@ /** | ||
parent.left === id && | ||
isUnusedExpression(parent) | ||
isUnusedExpression(parent) && | ||
!astUtils.isLogicalAssignmentOperator(parent.operator) | ||
) || | ||
@@ -472,0 +473,0 @@ ( |
@@ -98,3 +98,3 @@ /** | ||
try { | ||
regExpAST = parser.parsePattern(pattern, 0, pattern.length, flags.includes("u")); | ||
regExpAST = parser.parsePattern(pattern, 0, pattern.length, { unicode: flags.includes("u"), unicodeSets: flags.includes("v") }); | ||
} catch { | ||
@@ -101,0 +101,0 @@ |
@@ -9,3 +9,8 @@ /** | ||
const astUtils = require("./utils/ast-utils"); | ||
const { RegExpParser, visitRegExpAST } = require("@eslint-community/regexpp"); | ||
/** | ||
* @typedef {import('@eslint-community/regexpp').AST.CharacterClass} CharacterClass | ||
* @typedef {import('@eslint-community/regexpp').AST.ExpressionCharacterClass} ExpressionCharacterClass | ||
*/ | ||
//------------------------------------------------------------------------------ | ||
@@ -32,52 +37,14 @@ // Rule Definition | ||
/** | ||
* Parses a regular expression into a list of characters with character class info. | ||
* @param {string} regExpText The raw text used to create the regular expression | ||
* @returns {Object[]} A list of characters, each with info on escaping and whether they're in a character class. | ||
* @example | ||
* | ||
* parseRegExp("a\\b[cd-]"); | ||
* | ||
* // returns: | ||
* [ | ||
* { text: "a", index: 0, escaped: false, inCharClass: false, startsCharClass: false, endsCharClass: false }, | ||
* { text: "b", index: 2, escaped: true, inCharClass: false, startsCharClass: false, endsCharClass: false }, | ||
* { text: "c", index: 4, escaped: false, inCharClass: true, startsCharClass: true, endsCharClass: false }, | ||
* { text: "d", index: 5, escaped: false, inCharClass: true, startsCharClass: false, endsCharClass: false }, | ||
* { text: "-", index: 6, escaped: false, inCharClass: true, startsCharClass: false, endsCharClass: false } | ||
* ]; | ||
* | ||
/* | ||
* Set of characters that require escaping in character classes in `unicodeSets` mode. | ||
* ( ) [ ] { } / - \ | are ClassSetSyntaxCharacter | ||
*/ | ||
function parseRegExp(regExpText) { | ||
const charList = []; | ||
const REGEX_CLASSSET_CHARACTER_ESCAPES = union(REGEX_GENERAL_ESCAPES, new Set("q/[{}|()-")); | ||
regExpText.split("").reduce((state, char, index) => { | ||
if (!state.escapeNextChar) { | ||
if (char === "\\") { | ||
return Object.assign(state, { escapeNextChar: true }); | ||
} | ||
if (char === "[" && !state.inCharClass) { | ||
return Object.assign(state, { inCharClass: true, startingCharClass: true }); | ||
} | ||
if (char === "]" && state.inCharClass) { | ||
if (charList.length && charList[charList.length - 1].inCharClass) { | ||
charList[charList.length - 1].endsCharClass = true; | ||
} | ||
return Object.assign(state, { inCharClass: false, startingCharClass: false }); | ||
} | ||
} | ||
charList.push({ | ||
text: char, | ||
index, | ||
escaped: state.escapeNextChar, | ||
inCharClass: state.inCharClass, | ||
startsCharClass: state.startingCharClass, | ||
endsCharClass: false | ||
}); | ||
return Object.assign(state, { escapeNextChar: false, startingCharClass: false }); | ||
}, { escapeNextChar: false, inCharClass: false, startingCharClass: false }); | ||
/* | ||
* A single character set of ClassSetReservedDoublePunctuator. | ||
* && !! ## $$ %% ** ++ ,, .. :: ;; << == >> ?? @@ ^^ `` ~~ are ClassSetReservedDoublePunctuator | ||
*/ | ||
const REGEX_CLASS_SET_RESERVED_DOUBLE_PUNCTUATOR = new Set("!#$%&*+,.:;<=>?@^`~"); | ||
return charList; | ||
} | ||
/** @type {import('../shared/types').Rule} */ | ||
@@ -99,2 +66,3 @@ module.exports = { | ||
removeEscape: "Remove the `\\`. This maintains the current functionality.", | ||
removeEscapeDoNotKeepSemantics: "Remove the `\\` if it was inserted by mistake.", | ||
escapeBackslash: "Replace the `\\` with `\\\\` to include the actual backslash character." | ||
@@ -108,2 +76,3 @@ }, | ||
const sourceCode = context.sourceCode; | ||
const parser = new RegExpParser(); | ||
@@ -115,5 +84,6 @@ /** | ||
* @param {string} character The uselessly escaped character (not including the backslash) | ||
* @param {boolean} [disableEscapeBackslashSuggest] `true` if escapeBackslash suggestion should be turned off. | ||
* @returns {void} | ||
*/ | ||
function report(node, startOffset, character) { | ||
function report(node, startOffset, character, disableEscapeBackslashSuggest) { | ||
const rangeStart = node.range[0] + startOffset; | ||
@@ -133,3 +103,6 @@ const range = [rangeStart, rangeStart + 1]; | ||
{ | ||
messageId: "removeEscape", | ||
// Removing unnecessary `\` characters in a directive is not guaranteed to maintain functionality. | ||
messageId: astUtils.isDirective(node.parent) | ||
? "removeEscapeDoNotKeepSemantics" : "removeEscape", | ||
fix(fixer) { | ||
@@ -139,8 +112,12 @@ return fixer.removeRange(range); | ||
}, | ||
{ | ||
messageId: "escapeBackslash", | ||
fix(fixer) { | ||
return fixer.insertTextBeforeRange(range, "\\"); | ||
} | ||
} | ||
...disableEscapeBackslashSuggest | ||
? [] | ||
: [ | ||
{ | ||
messageId: "escapeBackslash", | ||
fix(fixer) { | ||
return fixer.insertTextBeforeRange(range, "\\"); | ||
} | ||
} | ||
] | ||
] | ||
@@ -189,2 +166,129 @@ }); | ||
/** | ||
* Checks if the escape character in given regexp is unnecessary. | ||
* @private | ||
* @param {ASTNode} node node to validate. | ||
* @returns {void} | ||
*/ | ||
function validateRegExp(node) { | ||
const { pattern, flags } = node.regex; | ||
let patternNode; | ||
const unicode = flags.includes("u"); | ||
const unicodeSets = flags.includes("v"); | ||
try { | ||
patternNode = parser.parsePattern(pattern, 0, pattern.length, { unicode, unicodeSets }); | ||
} catch { | ||
// Ignore regular expressions with syntax errors | ||
return; | ||
} | ||
/** @type {(CharacterClass | ExpressionCharacterClass)[]} */ | ||
const characterClassStack = []; | ||
visitRegExpAST(patternNode, { | ||
onCharacterClassEnter: characterClassNode => characterClassStack.unshift(characterClassNode), | ||
onCharacterClassLeave: () => characterClassStack.shift(), | ||
onExpressionCharacterClassEnter: characterClassNode => characterClassStack.unshift(characterClassNode), | ||
onExpressionCharacterClassLeave: () => characterClassStack.shift(), | ||
onCharacterEnter(characterNode) { | ||
if (!characterNode.raw.startsWith("\\")) { | ||
// It's not an escaped character. | ||
return; | ||
} | ||
const escapedChar = characterNode.raw.slice(1); | ||
if (escapedChar !== String.fromCodePoint(characterNode.value)) { | ||
// It's a valid escape. | ||
return; | ||
} | ||
let allowedEscapes; | ||
if (characterClassStack.length) { | ||
allowedEscapes = unicodeSets ? REGEX_CLASSSET_CHARACTER_ESCAPES : REGEX_GENERAL_ESCAPES; | ||
} else { | ||
allowedEscapes = REGEX_NON_CHARCLASS_ESCAPES; | ||
} | ||
if (allowedEscapes.has(escapedChar)) { | ||
return; | ||
} | ||
const reportedIndex = characterNode.start + 1; | ||
let disableEscapeBackslashSuggest = false; | ||
if (characterClassStack.length) { | ||
const characterClassNode = characterClassStack[0]; | ||
if (escapedChar === "^") { | ||
/* | ||
* The '^' character is also a special case; it must always be escaped outside of character classes, but | ||
* it only needs to be escaped in character classes if it's at the beginning of the character class. To | ||
* account for this, consider it to be a valid escape character outside of character classes, and filter | ||
* out '^' characters that appear at the start of a character class. | ||
*/ | ||
if (characterClassNode.start + 1 === characterNode.start) { | ||
return; | ||
} | ||
} | ||
if (!unicodeSets) { | ||
if (escapedChar === "-") { | ||
/* | ||
* The '-' character is a special case, because it's only valid to escape it if it's in a character | ||
* class, and is not at either edge of the character class. To account for this, don't consider '-' | ||
* characters to be valid in general, and filter out '-' characters that appear in the middle of a | ||
* character class. | ||
*/ | ||
if (characterClassNode.start + 1 !== characterNode.start && characterNode.end !== characterClassNode.end - 1) { | ||
return; | ||
} | ||
} | ||
} else { // unicodeSets mode | ||
if (REGEX_CLASS_SET_RESERVED_DOUBLE_PUNCTUATOR.has(escapedChar)) { | ||
// Escaping is valid if it is a ClassSetReservedDoublePunctuator. | ||
if (pattern[characterNode.end] === escapedChar) { | ||
return; | ||
} | ||
if (pattern[characterNode.start - 1] === escapedChar) { | ||
if (escapedChar !== "^") { | ||
return; | ||
} | ||
// If the previous character is a `negate` caret(`^`), escape to caret is unnecessary. | ||
if (!characterClassNode.negate) { | ||
return; | ||
} | ||
const negateCaretIndex = characterClassNode.start + 1; | ||
if (negateCaretIndex < characterNode.start - 1) { | ||
return; | ||
} | ||
} | ||
} | ||
if (characterNode.parent.type === "ClassIntersection" || characterNode.parent.type === "ClassSubtraction") { | ||
disableEscapeBackslashSuggest = true; | ||
} | ||
} | ||
} | ||
report( | ||
node, | ||
reportedIndex, | ||
escapedChar, | ||
disableEscapeBackslashSuggest | ||
); | ||
} | ||
}); | ||
} | ||
/** | ||
* Checks if a node has an escape. | ||
@@ -227,28 +331,3 @@ * @param {ASTNode} node node to check. | ||
} else if (node.regex) { | ||
parseRegExp(node.regex.pattern) | ||
/* | ||
* The '-' character is a special case, because it's only valid to escape it if it's in a character | ||
* class, and is not at either edge of the character class. To account for this, don't consider '-' | ||
* characters to be valid in general, and filter out '-' characters that appear in the middle of a | ||
* character class. | ||
*/ | ||
.filter(charInfo => !(charInfo.text === "-" && charInfo.inCharClass && !charInfo.startsCharClass && !charInfo.endsCharClass)) | ||
/* | ||
* The '^' character is also a special case; it must always be escaped outside of character classes, but | ||
* it only needs to be escaped in character classes if it's at the beginning of the character class. To | ||
* account for this, consider it to be a valid escape character outside of character classes, and filter | ||
* out '^' characters that appear at the start of a character class. | ||
*/ | ||
.filter(charInfo => !(charInfo.text === "^" && charInfo.startsCharClass)) | ||
// Filter out characters that aren't escaped. | ||
.filter(charInfo => charInfo.escaped) | ||
// Filter out characters that are valid to escape, based on their position in the regular expression. | ||
.filter(charInfo => !(charInfo.inCharClass ? REGEX_GENERAL_ESCAPES : REGEX_NON_CHARCLASS_ESCAPES).has(charInfo.text)) | ||
// Report all the remaining characters. | ||
.forEach(charInfo => report(node, charInfo.index, charInfo.text)); | ||
validateRegExp(node); | ||
} | ||
@@ -255,0 +334,0 @@ |
@@ -60,2 +60,18 @@ /** | ||
/** | ||
* Checks all segments in a set and returns true if any are reachable. | ||
* @param {Set<CodePathSegment>} segments The segments to check. | ||
* @returns {boolean} True if any segment is reachable; false otherwise. | ||
*/ | ||
function isAnySegmentReachable(segments) { | ||
for (const segment of segments) { | ||
if (segment.reachable) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
//------------------------------------------------------------------------------ | ||
@@ -209,3 +225,2 @@ // Rule Definition | ||
scopeInfo | ||
.codePath | ||
.currentSegments | ||
@@ -227,3 +242,4 @@ .forEach(segment => markReturnStatementsOnSegmentAsUsed(segment, new Set())); | ||
traversedTryBlockStatements: [], | ||
codePath | ||
codePath, | ||
currentSegments: new Set() | ||
}; | ||
@@ -265,2 +281,5 @@ }, | ||
onCodePathSegmentStart(segment) { | ||
scopeInfo.currentSegments.add(segment); | ||
const info = { | ||
@@ -275,2 +294,14 @@ uselessReturns: getUselessReturns([], segment.allPrevSegments), | ||
onUnreachableCodePathSegmentStart(segment) { | ||
scopeInfo.currentSegments.add(segment); | ||
}, | ||
onUnreachableCodePathSegmentEnd(segment) { | ||
scopeInfo.currentSegments.delete(segment); | ||
}, | ||
onCodePathSegmentEnd(segment) { | ||
scopeInfo.currentSegments.delete(segment); | ||
}, | ||
// Adds ReturnStatement node to check whether it's useless or not. | ||
@@ -287,3 +318,3 @@ ReturnStatement(node) { | ||
// Ignore `return` statements in unreachable places (https://github.com/eslint/eslint/issues/11647). | ||
!scopeInfo.codePath.currentSegments.some(s => s.reachable) | ||
!isAnySegmentReachable(scopeInfo.currentSegments) | ||
) { | ||
@@ -293,3 +324,3 @@ return; | ||
for (const segment of scopeInfo.codePath.currentSegments) { | ||
for (const segment of scopeInfo.currentSegments) { | ||
const info = segmentInfoMap.get(segment); | ||
@@ -296,0 +327,0 @@ |
/** | ||
* @fileoverview Rule to disallow whitespace before properties | ||
* @author Kai Cataldo | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -20,2 +21,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -22,0 +25,0 @@ |
/** | ||
* @fileoverview enforce the location of single-line statements | ||
* @author Teddy Katz | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -16,2 +17,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -18,0 +21,0 @@ |
/** | ||
* @fileoverview Rule to require or disallow line breaks inside braces. | ||
* @author Toru Nagashima | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -150,2 +151,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -152,0 +155,0 @@ |
/** | ||
* @fileoverview Disallows or enforces spaces inside of object literals. | ||
* @author Jamund Ferguson | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -16,2 +17,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -18,0 +21,0 @@ |
/** | ||
* @fileoverview Rule to enforce placing object properties on separate lines. | ||
* @author Vitor Balocco | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -15,2 +16,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -17,0 +20,0 @@ |
/** | ||
* @fileoverview Rule to check multiple var declarations per line | ||
* @author Alberto Rodríguez | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -14,2 +15,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "suggestion", | ||
@@ -16,0 +19,0 @@ |
/** | ||
* @fileoverview Operator linebreak - enforces operator linebreak style of two types: after and before | ||
* @author Benoît Zugmeyer | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -21,2 +22,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -23,0 +26,0 @@ |
/** | ||
* @fileoverview A rule to ensure blank lines within blocks. | ||
* @author Mathias Schreck <https://github.com/lo1tuma> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -21,2 +22,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -23,0 +26,0 @@ |
/** | ||
* @fileoverview Rule to require or disallow newlines between statements | ||
* @author Toru Nagashima | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -134,45 +135,2 @@ | ||
/** | ||
* Check whether the given node is a directive or not. | ||
* @param {ASTNode} node The node to check. | ||
* @param {SourceCode} sourceCode The source code object to get tokens. | ||
* @returns {boolean} `true` if the node is a directive. | ||
*/ | ||
function isDirective(node, sourceCode) { | ||
return ( | ||
node.type === "ExpressionStatement" && | ||
( | ||
node.parent.type === "Program" || | ||
( | ||
node.parent.type === "BlockStatement" && | ||
astUtils.isFunction(node.parent.parent) | ||
) | ||
) && | ||
node.expression.type === "Literal" && | ||
typeof node.expression.value === "string" && | ||
!astUtils.isParenthesised(sourceCode, node.expression) | ||
); | ||
} | ||
/** | ||
* Check whether the given node is a part of directive prologue or not. | ||
* @param {ASTNode} node The node to check. | ||
* @param {SourceCode} sourceCode The source code object to get tokens. | ||
* @returns {boolean} `true` if the node is a part of directive prologue. | ||
*/ | ||
function isDirectivePrologue(node, sourceCode) { | ||
if (isDirective(node, sourceCode)) { | ||
for (const sibling of node.parent.body) { | ||
if (sibling === node) { | ||
break; | ||
} | ||
if (!isDirective(sibling, sourceCode)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Gets the actual last token. | ||
@@ -370,8 +328,6 @@ * | ||
directive: { | ||
test: isDirectivePrologue | ||
test: astUtils.isDirective | ||
}, | ||
expression: { | ||
test: (node, sourceCode) => | ||
node.type === "ExpressionStatement" && | ||
!isDirectivePrologue(node, sourceCode) | ||
test: node => node.type === "ExpressionStatement" && !astUtils.isDirective(node) | ||
}, | ||
@@ -387,6 +343,6 @@ iife: { | ||
"multiline-expression": { | ||
test: (node, sourceCode) => | ||
test: node => | ||
node.loc.start.line !== node.loc.end.line && | ||
node.type === "ExpressionStatement" && | ||
!isDirectivePrologue(node, sourceCode) | ||
!astUtils.isDirective(node) | ||
}, | ||
@@ -434,2 +390,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -436,0 +394,0 @@ |
@@ -58,2 +58,3 @@ /** | ||
const parentPrecedence = astUtils.getPrecedence(parent); | ||
const needsParens = ( | ||
@@ -63,3 +64,3 @@ parent.type === "ClassDeclaration" || | ||
parent.type.endsWith("Expression") && | ||
astUtils.getPrecedence(parent) >= PRECEDENCE_OF_EXPONENTIATION_EXPR && | ||
(parentPrecedence === -1 || parentPrecedence >= PRECEDENCE_OF_EXPONENTIATION_EXPR) && | ||
!(parent.type === "BinaryExpression" && parent.operator === "**" && parent.right === node) && | ||
@@ -66,0 +67,0 @@ !((parent.type === "CallExpression" || parent.type === "NewExpression") && parent.arguments.includes(node)) && |
@@ -115,10 +115,13 @@ /** | ||
* @param {ASTNode} regexNode AST node which contains the regular expression. | ||
* @param {boolean} uFlag Flag indicates whether unicode mode is enabled or not. | ||
* @param {string|null} flags The regular expression flags to be checked. | ||
* @returns {void} | ||
*/ | ||
function checkRegex(pattern, node, regexNode, uFlag) { | ||
function checkRegex(pattern, node, regexNode, flags) { | ||
let ast; | ||
try { | ||
ast = parser.parsePattern(pattern, 0, pattern.length, uFlag); | ||
ast = parser.parsePattern(pattern, 0, pattern.length, { | ||
unicode: Boolean(flags && flags.includes("u")), | ||
unicodeSets: Boolean(flags && flags.includes("v")) | ||
}); | ||
} catch { | ||
@@ -152,3 +155,3 @@ | ||
if (node.regex) { | ||
checkRegex(node.regex.pattern, node, node, node.regex.flags.includes("u")); | ||
checkRegex(node.regex.pattern, node, node, node.regex.flags); | ||
} | ||
@@ -171,3 +174,3 @@ }, | ||
if (regex) { | ||
checkRegex(regex, refNode, refNode.arguments[0], flags && flags.includes("u")); | ||
checkRegex(regex, refNode, refNode.arguments[0], flags); | ||
} | ||
@@ -174,0 +177,0 @@ } |
@@ -40,11 +40,2 @@ /** | ||
/** | ||
* Determines whether the given node is a template literal without expressions. | ||
* @param {ASTNode} node Node to check. | ||
* @returns {boolean} True if the node is a template literal without expressions. | ||
*/ | ||
function isStaticTemplateLiteral(node) { | ||
return node.type === "TemplateLiteral" && node.expressions.length === 0; | ||
} | ||
const validPrecedingTokens = new Set([ | ||
@@ -182,3 +173,3 @@ "(", | ||
isGlobalReference(astUtils.skipChainExpression(node.tag).object) && | ||
isStaticTemplateLiteral(node.quasi); | ||
astUtils.isStaticTemplateLiteral(node.quasi); | ||
} | ||
@@ -196,3 +187,3 @@ | ||
if (isStaticTemplateLiteral(node)) { | ||
if (astUtils.isStaticTemplateLiteral(node)) { | ||
return node.quasis[0].value.cooked; | ||
@@ -215,3 +206,3 @@ } | ||
return isStringLiteral(node) || | ||
isStaticTemplateLiteral(node) || | ||
astUtils.isStaticTemplateLiteral(node) || | ||
isStringRawTaggedStaticTemplateLiteral(node); | ||
@@ -257,3 +248,3 @@ } | ||
* @param {number} ecmaVersion The ecmaVersion to convert. | ||
* @returns {import("regexpp/ecma-versions").EcmaVersion} The resulting ecmaVersion compatible for regexpp. | ||
* @returns {import("@eslint-community/regexpp/ecma-versions").EcmaVersion} The resulting ecmaVersion compatible for regexpp. | ||
*/ | ||
@@ -314,3 +305,6 @@ function getRegexppEcmaVersion(ecmaVersion) { | ||
try { | ||
validator.validatePattern(pattern, 0, pattern.length, flags ? flags.includes("u") : false); | ||
validator.validatePattern(pattern, 0, pattern.length, { | ||
unicode: flags ? flags.includes("u") : false, | ||
unicodeSets: flags ? flags.includes("v") : false | ||
}); | ||
if (flags) { | ||
@@ -479,3 +473,6 @@ validator.validateFlags(flags); | ||
const ast = new RegExpParser({ ecmaVersion: regexppEcmaVersion }).parsePattern(regexContent, 0, regexContent.length, flags ? flags.includes("u") : false); | ||
const ast = new RegExpParser({ ecmaVersion: regexppEcmaVersion }).parsePattern(regexContent, 0, regexContent.length, { | ||
unicode: flags ? flags.includes("u") : false, | ||
unicodeSets: flags ? flags.includes("v") : false | ||
}); | ||
@@ -482,0 +479,0 @@ visitRegExpAST(ast, { |
/** | ||
* @fileoverview Rule to flag non-quoted property names in object literals. | ||
* @author Mathias Bynens <http://mathiasbynens.be/> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -22,2 +23,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "suggestion", | ||
@@ -24,0 +27,0 @@ |
/** | ||
* @fileoverview A rule to choose between single and double quote marks | ||
* @author Matt DuVall <http://www.mattduvall.com/>, Brandon Payton | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -80,2 +81,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -161,3 +164,4 @@ | ||
* Checks whether or not a given node is a directive. | ||
* The directive is a `ExpressionStatement` which has only a string literal. | ||
* The directive is a `ExpressionStatement` which has only a string literal not surrounded by | ||
* parentheses. | ||
* @param {ASTNode} node A node to check. | ||
@@ -171,3 +175,4 @@ * @returns {boolean} Whether or not the node is a directive. | ||
node.expression.type === "Literal" && | ||
typeof node.expression.value === "string" | ||
typeof node.expression.value === "string" && | ||
!astUtils.isParenthesised(sourceCode, node.expression) | ||
); | ||
@@ -177,14 +182,13 @@ } | ||
/** | ||
* Checks whether or not a given node is a part of directive prologues. | ||
* See also: http://www.ecma-international.org/ecma-262/6.0/#sec-directive-prologues-and-the-use-strict-directive | ||
* Checks whether a specified node is either part of, or immediately follows a (possibly empty) directive prologue. | ||
* @see {@link http://www.ecma-international.org/ecma-262/6.0/#sec-directive-prologues-and-the-use-strict-directive} | ||
* @param {ASTNode} node A node to check. | ||
* @returns {boolean} Whether or not the node is a part of directive prologues. | ||
* @returns {boolean} Whether a specified node is either part of, or immediately follows a (possibly empty) directive prologue. | ||
* @private | ||
*/ | ||
function isPartOfDirectivePrologue(node) { | ||
const block = node.parent.parent; | ||
if (block.type !== "Program" && (block.type !== "BlockStatement" || !astUtils.isFunction(block.parent))) { | ||
function isExpressionInOrJustAfterDirectivePrologue(node) { | ||
if (!astUtils.isTopLevelExpressionStatement(node.parent)) { | ||
return false; | ||
} | ||
const block = node.parent.parent; | ||
@@ -219,3 +223,3 @@ // Check the node is at a prologue. | ||
case "ExpressionStatement": | ||
return isPartOfDirectivePrologue(node); | ||
return !astUtils.isParenthesised(sourceCode, node) && isExpressionInOrJustAfterDirectivePrologue(node); | ||
@@ -336,8 +340,7 @@ // LiteralPropertyName. | ||
fix(fixer) { | ||
if (isPartOfDirectivePrologue(node)) { | ||
if (astUtils.isTopLevelExpressionStatement(node.parent) && !astUtils.isParenthesised(sourceCode, node)) { | ||
/* | ||
* TemplateLiterals in a directive prologue aren't actually directives, but if they're | ||
* in the directive prologue, then fixing them might turn them into directives and change | ||
* the behavior of the code. | ||
* TemplateLiterals aren't actually directives, but fixing them might turn | ||
* them into directives and change the behavior of the code. | ||
*/ | ||
@@ -344,0 +347,0 @@ return null; |
@@ -216,3 +216,4 @@ /** | ||
codePath, | ||
referenceMap: shouldVerify ? createReferenceMap(scope) : null | ||
referenceMap: shouldVerify ? createReferenceMap(scope) : null, | ||
currentSegments: new Set() | ||
}; | ||
@@ -227,7 +228,21 @@ }, | ||
segmentInfo.initialize(segment); | ||
stack.currentSegments.add(segment); | ||
}, | ||
onUnreachableCodePathSegmentStart(segment) { | ||
stack.currentSegments.add(segment); | ||
}, | ||
onUnreachableCodePathSegmentEnd(segment) { | ||
stack.currentSegments.delete(segment); | ||
}, | ||
onCodePathSegmentEnd(segment) { | ||
stack.currentSegments.delete(segment); | ||
}, | ||
// Handle references to prepare verification. | ||
Identifier(node) { | ||
const { codePath, referenceMap } = stack; | ||
const { referenceMap } = stack; | ||
const reference = referenceMap && referenceMap.get(node); | ||
@@ -245,3 +260,3 @@ | ||
if (reference.isRead() && !(writeExpr && writeExpr.parent.operator === "=")) { | ||
segmentInfo.markAsRead(codePath.currentSegments, variable); | ||
segmentInfo.markAsRead(stack.currentSegments, variable); | ||
} | ||
@@ -273,6 +288,5 @@ | ||
":expression:exit"(node) { | ||
const { codePath, referenceMap } = stack; | ||
// referenceMap exists if this is in a resumable function scope. | ||
if (!referenceMap) { | ||
if (!stack.referenceMap) { | ||
return; | ||
@@ -283,3 +297,3 @@ } | ||
if (node.type === "AwaitExpression" || node.type === "YieldExpression") { | ||
segmentInfo.makeOutdated(codePath.currentSegments); | ||
segmentInfo.makeOutdated(stack.currentSegments); | ||
} | ||
@@ -296,3 +310,3 @@ | ||
if (segmentInfo.isOutdated(codePath.currentSegments, variable)) { | ||
if (segmentInfo.isOutdated(stack.currentSegments, variable)) { | ||
if (node.parent.left === reference.identifier) { | ||
@@ -299,0 +313,0 @@ context.report({ |
@@ -31,3 +31,3 @@ /** | ||
docs: { | ||
description: "Enforce the use of `u` flag on RegExp", | ||
description: "Enforce the use of `u` or `v` flag on RegExp", | ||
recommended: false, | ||
@@ -55,3 +55,3 @@ url: "https://eslint.org/docs/latest/rules/require-unicode-regexp" | ||
if (!flags.includes("u")) { | ||
if (!flags.includes("u") && !flags.includes("v")) { | ||
context.report({ | ||
@@ -90,3 +90,3 @@ messageId: "requireUFlag", | ||
if (!flagsNode || (typeof flags === "string" && !flags.includes("u"))) { | ||
if (!flagsNode || (typeof flags === "string" && !flags.includes("u") && !flags.includes("v"))) { | ||
context.report({ | ||
@@ -93,0 +93,0 @@ messageId: "requireUFlag", |
/** | ||
* @fileoverview Enforce spacing between rest and spread operators and their expressions. | ||
* @author Kai Cataldo | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -15,2 +16,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -17,0 +20,0 @@ |
/** | ||
* @fileoverview Validates spacing before and after semicolon | ||
* @author Mathias Schreck | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -17,2 +18,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -19,0 +22,0 @@ |
/** | ||
* @fileoverview Rule to enforce location of semicolons. | ||
* @author Toru Nagashima | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -73,2 +74,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -75,0 +78,0 @@ |
/** | ||
* @fileoverview Rule to flag missing semicolons. | ||
* @author Nicholas C. Zakas | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -21,2 +22,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -23,0 +26,0 @@ |
/** | ||
* @fileoverview A rule to ensure whitespace before blocks. | ||
* @author Mathias Schreck <https://github.com/lo1tuma> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -40,2 +41,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -42,0 +45,0 @@ |
/** | ||
* @fileoverview Rule to validate spacing before function paren. | ||
* @author Mathias Schreck <https://github.com/lo1tuma> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -20,2 +21,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -22,0 +25,0 @@ |
/** | ||
* @fileoverview Disallows or enforces spaces inside of parentheses. | ||
* @author Jonathan Rajavuori | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -16,2 +17,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -18,0 +21,0 @@ |
/** | ||
* @fileoverview Require spaces around infix operators | ||
* @author Michael Ficarra | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -16,2 +17,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -18,0 +21,0 @@ |
/** | ||
* @fileoverview This rule should require or disallow spaces before or after unary operations. | ||
* @author Marcin Kumorek | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -20,2 +21,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -22,0 +25,0 @@ |
/** | ||
* @fileoverview Source code for spaced-comments rule | ||
* @author Gyandeep Singh | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -152,2 +153,4 @@ "use strict"; | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "suggestion", | ||
@@ -154,0 +157,0 @@ |
/** | ||
* @fileoverview Rule to enforce spacing around colons of switch statements. | ||
* @author Toru Nagashima | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -21,2 +22,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -23,0 +26,0 @@ |
/** | ||
* @fileoverview Rule to enforce spacing around embedded expressions of template strings | ||
* @author Toru Nagashima | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -21,2 +22,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -23,0 +26,0 @@ |
/** | ||
* @fileoverview Rule to check spacing between template tags and their literals | ||
* @author Jonathan Wilsson | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -15,2 +16,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -17,0 +20,0 @@ |
@@ -11,3 +11,3 @@ /** | ||
const REGEXPP_LATEST_ECMA_VERSION = 2022; | ||
const REGEXPP_LATEST_ECMA_VERSION = 2024; | ||
@@ -32,3 +32,3 @@ /** | ||
try { | ||
validator.validatePattern(pattern, void 0, void 0, /* uFlag = */ true); | ||
validator.validatePattern(pattern, void 0, void 0, { unicode: /* uFlag = */ true }); | ||
} catch { | ||
@@ -35,0 +35,0 @@ return false; |
@@ -8,2 +8,8 @@ /** | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
const astUtils = require("./utils/ast-utils"); | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
@@ -92,3 +98,3 @@ //------------------------------------------------------------------------------ | ||
if (sibling.type === "Literal" || sibling.type === "TemplateLiteral" && !sibling.expressions.length) { | ||
if (sibling.type === "Literal" || astUtils.isStaticTemplateLiteral(sibling)) { | ||
const value = sibling.type === "Literal" ? sibling.value : sibling.quasis[0].value.cooked; | ||
@@ -95,0 +101,0 @@ |
/** | ||
* @fileoverview Rule to flag when IIFE is not wrapped in parens | ||
* @author Ilya Volodin | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -43,2 +44,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -45,0 +48,0 @@ |
/** | ||
* @fileoverview Rule to flag when regex literals are not wrapped in parens | ||
* @author Matt DuVall <http://www.mattduvall.com> | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -15,2 +16,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -17,0 +20,0 @@ |
/** | ||
* @fileoverview Rule to check the spacing around the * in yield* expressions. | ||
* @author Bryan Smith | ||
* @deprecated in ESLint v8.53.0 | ||
*/ | ||
@@ -15,2 +16,4 @@ | ||
meta: { | ||
deprecated: true, | ||
replacedBy: [], | ||
type: "layout", | ||
@@ -17,0 +20,0 @@ |
@@ -62,11 +62,2 @@ /** | ||
/** | ||
* Determines whether a node is a Template Literal which can be determined statically. | ||
* @param {ASTNode} node Node to test | ||
* @returns {boolean} True if the node is a Template Literal without expression. | ||
*/ | ||
function isStaticTemplateLiteral(node) { | ||
return node.type === "TemplateLiteral" && node.expressions.length === 0; | ||
} | ||
/** | ||
* Determines whether a non-Literal node should be treated as a single Literal node. | ||
@@ -77,3 +68,3 @@ * @param {ASTNode} node Node to test | ||
function looksLikeLiteral(node) { | ||
return isNegativeNumericLiteral(node) || isStaticTemplateLiteral(node); | ||
return isNegativeNumericLiteral(node) || astUtils.isStaticTemplateLiteral(node); | ||
} | ||
@@ -105,3 +96,3 @@ | ||
if (isStaticTemplateLiteral(node)) { | ||
if (astUtils.isStaticTemplateLiteral(node)) { | ||
return { | ||
@@ -108,0 +99,0 @@ type: "Literal", |
@@ -24,3 +24,3 @@ /** | ||
* @property {EcmaFeatures} [ecmaFeatures] The optional features. | ||
* @property {3|5|6|7|8|9|10|11|12|13|14|2015|2016|2017|2018|2019|2020|2021|2022|2023} [ecmaVersion] The ECMAScript version (or revision number). | ||
* @property {3|5|6|7|8|9|10|11|12|13|14|15|2015|2016|2017|2018|2019|2020|2021|2022|2023|2024} [ecmaVersion] The ECMAScript version (or revision number). | ||
* @property {"script"|"module"} [sourceType] The source code type. | ||
@@ -27,0 +27,0 @@ * @property {boolean} [allowReserved] Allowing the use of reserved words as identifiers in ES3. |
@@ -15,4 +15,12 @@ /** | ||
astUtils = require("../shared/ast-utils"), | ||
Traverser = require("../shared/traverser"); | ||
Traverser = require("../shared/traverser"), | ||
globals = require("../../conf/globals"), | ||
{ | ||
directivesPattern | ||
} = require("../shared/directives"), | ||
/* eslint-disable-next-line n/no-restricted-require -- Too messy to figure out right now. */ | ||
ConfigCommentParser = require("../linter/config-comment-parser"), | ||
eslintScope = require("eslint-scope"); | ||
//------------------------------------------------------------------------------ | ||
@@ -28,2 +36,4 @@ // Type Definitions | ||
const commentParser = new ConfigCommentParser(); | ||
/** | ||
@@ -55,2 +65,25 @@ * Validates that the given AST has the required information. | ||
/** | ||
* Retrieves globals for the given ecmaVersion. | ||
* @param {number} ecmaVersion The version to retrieve globals for. | ||
* @returns {Object} The globals for the given ecmaVersion. | ||
*/ | ||
function getGlobalsForEcmaVersion(ecmaVersion) { | ||
switch (ecmaVersion) { | ||
case 3: | ||
return globals.es3; | ||
case 5: | ||
return globals.es5; | ||
default: | ||
if (ecmaVersion < 2015) { | ||
return globals[`es${ecmaVersion + 2009}`]; | ||
} | ||
return globals[`es${ecmaVersion}`]; | ||
} | ||
} | ||
/** | ||
* Check to see if its a ES6 export declaration. | ||
@@ -90,2 +123,32 @@ * @param {ASTNode} astNode An AST node. | ||
/** | ||
* Normalizes a value for a global in a config | ||
* @param {(boolean|string|null)} configuredValue The value given for a global in configuration or in | ||
* a global directive comment | ||
* @returns {("readable"|"writeable"|"off")} The value normalized as a string | ||
* @throws Error if global value is invalid | ||
*/ | ||
function normalizeConfigGlobal(configuredValue) { | ||
switch (configuredValue) { | ||
case "off": | ||
return "off"; | ||
case true: | ||
case "true": | ||
case "writeable": | ||
case "writable": | ||
return "writable"; | ||
case null: | ||
case false: | ||
case "false": | ||
case "readable": | ||
case "readonly": | ||
return "readonly"; | ||
default: | ||
throw new Error(`'${configuredValue}' is not a valid configuration for a global (use 'readonly', 'writable', or 'off')`); | ||
} | ||
} | ||
/** | ||
* Determines if two nodes or tokens overlap. | ||
@@ -152,2 +215,112 @@ * @param {ASTNode|Token} first The first node or token to check. | ||
//----------------------------------------------------------------------------- | ||
// Directive Comments | ||
//----------------------------------------------------------------------------- | ||
/** | ||
* Extract the directive and the justification from a given directive comment and trim them. | ||
* @param {string} value The comment text to extract. | ||
* @returns {{directivePart: string, justificationPart: string}} The extracted directive and justification. | ||
*/ | ||
function extractDirectiveComment(value) { | ||
const match = /\s-{2,}\s/u.exec(value); | ||
if (!match) { | ||
return { directivePart: value.trim(), justificationPart: "" }; | ||
} | ||
const directive = value.slice(0, match.index).trim(); | ||
const justification = value.slice(match.index + match[0].length).trim(); | ||
return { directivePart: directive, justificationPart: justification }; | ||
} | ||
/** | ||
* Ensures that variables representing built-in properties of the Global Object, | ||
* and any globals declared by special block comments, are present in the global | ||
* scope. | ||
* @param {Scope} globalScope The global scope. | ||
* @param {Object|undefined} configGlobals The globals declared in configuration | ||
* @param {Object|undefined} inlineGlobals The globals declared in the source code | ||
* @returns {void} | ||
*/ | ||
function addDeclaredGlobals(globalScope, configGlobals = {}, inlineGlobals = {}) { | ||
// Define configured global variables. | ||
for (const id of new Set([...Object.keys(configGlobals), ...Object.keys(inlineGlobals)])) { | ||
/* | ||
* `normalizeConfigGlobal` will throw an error if a configured global value is invalid. However, these errors would | ||
* typically be caught when validating a config anyway (validity for inline global comments is checked separately). | ||
*/ | ||
const configValue = configGlobals[id] === void 0 ? void 0 : normalizeConfigGlobal(configGlobals[id]); | ||
const commentValue = inlineGlobals[id] && inlineGlobals[id].value; | ||
const value = commentValue || configValue; | ||
const sourceComments = inlineGlobals[id] && inlineGlobals[id].comments; | ||
if (value === "off") { | ||
continue; | ||
} | ||
let variable = globalScope.set.get(id); | ||
if (!variable) { | ||
variable = new eslintScope.Variable(id, globalScope); | ||
globalScope.variables.push(variable); | ||
globalScope.set.set(id, variable); | ||
} | ||
variable.eslintImplicitGlobalSetting = configValue; | ||
variable.eslintExplicitGlobal = sourceComments !== void 0; | ||
variable.eslintExplicitGlobalComments = sourceComments; | ||
variable.writeable = (value === "writable"); | ||
} | ||
/* | ||
* "through" contains all references which definitions cannot be found. | ||
* Since we augment the global scope using configuration, we need to update | ||
* references and remove the ones that were added by configuration. | ||
*/ | ||
globalScope.through = globalScope.through.filter(reference => { | ||
const name = reference.identifier.name; | ||
const variable = globalScope.set.get(name); | ||
if (variable) { | ||
/* | ||
* Links the variable and the reference. | ||
* And this reference is removed from `Scope#through`. | ||
*/ | ||
reference.resolved = variable; | ||
variable.references.push(reference); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
} | ||
/** | ||
* Sets the given variable names as exported so they won't be triggered by | ||
* the `no-unused-vars` rule. | ||
* @param {eslint.Scope} globalScope The global scope to define exports in. | ||
* @param {Record<string,string>} variables An object whose keys are the variable | ||
* names to export. | ||
* @returns {void} | ||
*/ | ||
function markExportedVariables(globalScope, variables) { | ||
Object.keys(variables).forEach(name => { | ||
const variable = globalScope.set.get(name); | ||
if (variable) { | ||
variable.eslintUsed = true; | ||
variable.eslintExported = true; | ||
} | ||
}); | ||
} | ||
//------------------------------------------------------------------------------ | ||
@@ -195,3 +368,5 @@ // Public Interface | ||
this[caches] = new Map([ | ||
["scopes", new WeakMap()] | ||
["scopes", new WeakMap()], | ||
["vars", new Map()], | ||
["configNodes", void 0] | ||
]); | ||
@@ -275,3 +450,3 @@ | ||
// don't allow modification of this object | ||
// don't allow further modification of this object | ||
Object.freeze(this); | ||
@@ -734,4 +909,176 @@ Object.freeze(this.lines); | ||
/** | ||
* Returns an array of all inline configuration nodes found in the | ||
* source code. | ||
* @returns {Array<Token>} An array of all inline configuration nodes. | ||
*/ | ||
getInlineConfigNodes() { | ||
// check the cache first | ||
let configNodes = this[caches].get("configNodes"); | ||
if (configNodes) { | ||
return configNodes; | ||
} | ||
// calculate fresh config nodes | ||
configNodes = this.ast.comments.filter(comment => { | ||
// shebang comments are never directives | ||
if (comment.type === "Shebang") { | ||
return false; | ||
} | ||
const { directivePart } = extractDirectiveComment(comment.value); | ||
const directiveMatch = directivesPattern.exec(directivePart); | ||
if (!directiveMatch) { | ||
return false; | ||
} | ||
// only certain comment types are supported as line comments | ||
return comment.type !== "Line" || !!/^eslint-disable-(next-)?line$/u.test(directiveMatch[1]); | ||
}); | ||
this[caches].set("configNodes", configNodes); | ||
return configNodes; | ||
} | ||
/** | ||
* Applies language options sent in from the core. | ||
* @param {Object} languageOptions The language options for this run. | ||
* @returns {void} | ||
*/ | ||
applyLanguageOptions(languageOptions) { | ||
/* | ||
* Add configured globals and language globals | ||
* | ||
* Using Object.assign instead of object spread for performance reasons | ||
* https://github.com/eslint/eslint/issues/16302 | ||
*/ | ||
const configGlobals = Object.assign( | ||
{}, | ||
getGlobalsForEcmaVersion(languageOptions.ecmaVersion), | ||
languageOptions.sourceType === "commonjs" ? globals.commonjs : void 0, | ||
languageOptions.globals | ||
); | ||
const varsCache = this[caches].get("vars"); | ||
varsCache.set("configGlobals", configGlobals); | ||
} | ||
/** | ||
* Applies configuration found inside of the source code. This method is only | ||
* called when ESLint is running with inline configuration allowed. | ||
* @returns {{problems:Array<Problem>,configs:{config:FlatConfigArray,node:ASTNode}}} Information | ||
* that ESLint needs to further process the inline configuration. | ||
*/ | ||
applyInlineConfig() { | ||
const problems = []; | ||
const configs = []; | ||
const exportedVariables = {}; | ||
const inlineGlobals = Object.create(null); | ||
this.getInlineConfigNodes().forEach(comment => { | ||
const { directivePart } = extractDirectiveComment(comment.value); | ||
const match = directivesPattern.exec(directivePart); | ||
const directiveText = match[1]; | ||
const directiveValue = directivePart.slice(match.index + directiveText.length); | ||
switch (directiveText) { | ||
case "exported": | ||
Object.assign(exportedVariables, commentParser.parseStringConfig(directiveValue, comment)); | ||
break; | ||
case "globals": | ||
case "global": | ||
for (const [id, { value }] of Object.entries(commentParser.parseStringConfig(directiveValue, comment))) { | ||
let normalizedValue; | ||
try { | ||
normalizedValue = normalizeConfigGlobal(value); | ||
} catch (err) { | ||
problems.push({ | ||
ruleId: null, | ||
loc: comment.loc, | ||
message: err.message | ||
}); | ||
continue; | ||
} | ||
if (inlineGlobals[id]) { | ||
inlineGlobals[id].comments.push(comment); | ||
inlineGlobals[id].value = normalizedValue; | ||
} else { | ||
inlineGlobals[id] = { | ||
comments: [comment], | ||
value: normalizedValue | ||
}; | ||
} | ||
} | ||
break; | ||
case "eslint": { | ||
const parseResult = commentParser.parseJsonConfig(directiveValue, comment.loc); | ||
if (parseResult.success) { | ||
configs.push({ | ||
config: { | ||
rules: parseResult.config | ||
}, | ||
node: comment | ||
}); | ||
} else { | ||
problems.push(parseResult.error); | ||
} | ||
break; | ||
} | ||
// no default | ||
} | ||
}); | ||
// save all the new variables for later | ||
const varsCache = this[caches].get("vars"); | ||
varsCache.set("inlineGlobals", inlineGlobals); | ||
varsCache.set("exportedVariables", exportedVariables); | ||
return { | ||
configs, | ||
problems | ||
}; | ||
} | ||
/** | ||
* Called by ESLint core to indicate that it has finished providing | ||
* information. We now add in all the missing variables and ensure that | ||
* state-changing methods cannot be called by rules. | ||
* @returns {void} | ||
*/ | ||
finalize() { | ||
// Step 1: ensure that all of the necessary variables are up to date | ||
const varsCache = this[caches].get("vars"); | ||
const globalScope = this.scopeManager.scopes[0]; | ||
const configGlobals = varsCache.get("configGlobals"); | ||
const inlineGlobals = varsCache.get("inlineGlobals"); | ||
const exportedVariables = varsCache.get("exportedVariables"); | ||
addDeclaredGlobals(globalScope, configGlobals, inlineGlobals); | ||
if (exportedVariables) { | ||
markExportedVariables(globalScope, exportedVariables); | ||
} | ||
} | ||
} | ||
module.exports = SourceCode; |
@@ -17,2 +17,3 @@ /** | ||
const FlatRuleTester = require("./rule-tester/flat-rule-tester"); | ||
const { ESLint } = require("./eslint/eslint"); | ||
@@ -28,3 +29,4 @@ //----------------------------------------------------------------------------- | ||
FlatRuleTester, | ||
FileEnumerator | ||
FileEnumerator, | ||
LegacyESLint: ESLint | ||
}; |
{ | ||
"name": "eslint", | ||
"version": "8.42.0", | ||
"version": "8.55.0", | ||
"author": "Nicholas C. Zakas <nicholas+npm@nczconsulting.com>", | ||
@@ -22,2 +22,3 @@ "description": "An AST-based pattern checker for JavaScript.", | ||
"lint:docs:js": "node Makefile.js lintDocsJS", | ||
"lint:docs:rule-examples": "node Makefile.js checkRuleExamples", | ||
"lint:fix": "node Makefile.js lint -- fix", | ||
@@ -46,2 +47,3 @@ "lint:fix:docs:js": "node Makefile.js lintDocsJS -- fix", | ||
"docs/src/rules/*.md": [ | ||
"node tools/check-rule-examples.js", | ||
"node tools/fetch-docs-links.js", | ||
@@ -66,9 +68,10 @@ "git add docs/src/_data/further_reading_links.json" | ||
"@eslint-community/eslint-utils": "^4.2.0", | ||
"@eslint-community/regexpp": "^4.4.0", | ||
"@eslint/eslintrc": "^2.0.3", | ||
"@eslint/js": "8.42.0", | ||
"@humanwhocodes/config-array": "^0.11.10", | ||
"@eslint-community/regexpp": "^4.6.1", | ||
"@eslint/eslintrc": "^2.1.4", | ||
"@eslint/js": "8.55.0", | ||
"@humanwhocodes/config-array": "^0.11.13", | ||
"@humanwhocodes/module-importer": "^1.0.1", | ||
"@nodelib/fs.walk": "^1.2.8", | ||
"ajv": "^6.10.0", | ||
"@ungap/structured-clone": "^1.2.0", | ||
"ajv": "^6.12.4", | ||
"chalk": "^4.0.0", | ||
@@ -79,5 +82,5 @@ "cross-spawn": "^7.0.2", | ||
"escape-string-regexp": "^4.0.0", | ||
"eslint-scope": "^7.2.0", | ||
"eslint-visitor-keys": "^3.4.1", | ||
"espree": "^9.5.2", | ||
"eslint-scope": "^7.2.2", | ||
"eslint-visitor-keys": "^3.4.3", | ||
"espree": "^9.6.1", | ||
"esquery": "^1.4.2", | ||
@@ -92,3 +95,2 @@ "esutils": "^2.0.2", | ||
"ignore": "^5.2.0", | ||
"import-fresh": "^3.0.0", | ||
"imurmurhash": "^0.1.4", | ||
@@ -103,5 +105,4 @@ "is-glob": "^4.0.0", | ||
"natural-compare": "^1.4.0", | ||
"optionator": "^0.9.1", | ||
"optionator": "^0.9.3", | ||
"strip-ansi": "^6.0.1", | ||
"strip-json-comments": "^3.1.0", | ||
"text-table": "^0.2.0" | ||
@@ -112,2 +113,7 @@ }, | ||
"@babel/preset-env": "^7.4.3", | ||
"@wdio/browser-runner": "^8.14.6", | ||
"@wdio/cli": "^8.14.6", | ||
"@wdio/concise-reporter": "^8.14.0", | ||
"@wdio/globals": "^8.14.6", | ||
"@wdio/mocha-framework": "^8.14.0", | ||
"babel-loader": "^8.0.5", | ||
@@ -123,6 +129,6 @@ "c8": "^7.12.0", | ||
"eslint-plugin-eslint-comments": "^3.2.0", | ||
"eslint-plugin-eslint-plugin": "^4.4.0", | ||
"eslint-plugin-eslint-plugin": "^5.1.0", | ||
"eslint-plugin-internal-rules": "file:tools/internal-rules", | ||
"eslint-plugin-jsdoc": "^38.1.6", | ||
"eslint-plugin-n": "^15.2.4", | ||
"eslint-plugin-jsdoc": "^46.2.5", | ||
"eslint-plugin-n": "^16.0.0", | ||
"eslint-plugin-unicorn": "^42.0.0", | ||
@@ -137,11 +143,8 @@ "eslint-release": "^3.2.0", | ||
"gray-matter": "^4.0.3", | ||
"karma": "^6.1.1", | ||
"karma-chrome-launcher": "^3.1.0", | ||
"karma-mocha": "^2.0.1", | ||
"karma-mocha-reporter": "^2.2.5", | ||
"karma-webpack": "^5.0.0", | ||
"lint-staged": "^11.0.0", | ||
"load-perf": "^0.2.0", | ||
"markdownlint": "^0.25.1", | ||
"markdownlint-cli": "^0.31.1", | ||
"markdown-it": "^12.2.0", | ||
"markdown-it-container": "^3.0.0", | ||
"markdownlint": "^0.31.1", | ||
"markdownlint-cli": "^0.37.0", | ||
"marked": "^4.0.8", | ||
@@ -162,9 +165,10 @@ "memfs": "^3.0.1", | ||
"proxyquire": "^2.0.1", | ||
"puppeteer": "^13.7.0", | ||
"recast": "^0.20.4", | ||
"regenerator-runtime": "^0.13.2", | ||
"semver": "^7.3.5", | ||
"recast": "^0.23.0", | ||
"regenerator-runtime": "^0.14.0", | ||
"rollup-plugin-node-polyfills": "^0.2.1", | ||
"semver": "^7.5.3", | ||
"shelljs": "^0.8.2", | ||
"sinon": "^11.0.0", | ||
"temp": "^0.9.0", | ||
"vite-plugin-commonjs": "^0.10.0", | ||
"webdriverio": "^8.14.6", | ||
"webpack": "^5.23.0", | ||
@@ -171,0 +175,0 @@ "webpack-cli": "^4.5.0", |
@@ -120,3 +120,3 @@ [![npm version](https://img.shields.io/npm/v/eslint.svg)](https://www.npmjs.com/package/eslint) | ||
ESLint has full support for ECMAScript 3, 5 (default), 2015, 2016, 2017, 2018, 2019, 2020, 2021 and 2022. You can set your desired ECMAScript syntax (and other settings, like global variables or your target environments) through [configuration](https://eslint.org/docs/latest/use/configure). | ||
ESLint has full support for ECMAScript 3, 5 (default), 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, and 2023. You can set your desired ECMAScript syntax (and other settings, like global variables or your target environments) through [configuration](https://eslint.org/docs/latest/use/configure). | ||
@@ -253,2 +253,12 @@ ### What about experimental features? | ||
</a> | ||
</td><td align="center" valign="top" width="11%"> | ||
<a href="https://github.com/ota-meshi"> | ||
<img src="https://github.com/ota-meshi.png?s=75" width="75" height="75"><br /> | ||
Yosuke Ota | ||
</a> | ||
</td><td align="center" valign="top" width="11%"> | ||
<a href="https://github.com/Tanujkanti4441"> | ||
<img src="https://github.com/Tanujkanti4441.png?s=75" width="75" height="75"><br /> | ||
Tanuj Kanti | ||
</a> | ||
</td></tr></tbody></table> | ||
@@ -288,4 +298,4 @@ | ||
<p><a href="https://engineering.salesforce.com"><img src="https://images.opencollective.com/salesforce/ca8f997/logo.png" alt="Salesforce" height="96"></a> <a href="https://www.airbnb.com/"><img src="https://images.opencollective.com/airbnb/d327d66/logo.png" alt="Airbnb" height="96"></a></p><h3>Silver Sponsors</h3> | ||
<p><a href="https://sentry.io"><img src="https://avatars.githubusercontent.com/u/1396951?v=4" alt="Sentry" height="64"></a> <a href="https://liftoff.io/"><img src="https://images.opencollective.com/liftoff/5c4fa84/logo.png" alt="Liftoff" height="64"></a> <a href="https://americanexpress.io"><img src="https://avatars.githubusercontent.com/u/3853301?v=4" alt="American Express" height="64"></a></p><h3>Bronze Sponsors</h3> | ||
<p><a href="#"><img src="https://images.opencollective.com/king-billy-slots1/c30c2aa/avatar.png" alt="King Billy Slots" height="32"></a> <a href="https://themeisle.com"><img src="https://images.opencollective.com/themeisle/d5592fe/logo.png" alt="ThemeIsle" height="32"></a> <a href="https://nx.dev"><img src="https://images.opencollective.com/nx/0efbe42/logo.png" alt="Nx (by Nrwl)" height="32"></a> <a href="https://www.crosswordsolver.org/anagram-solver/"><img src="https://images.opencollective.com/anagram-solver/2666271/logo.png" alt="Anagram Solver" height="32"></a> <a href="https://icons8.com"><img src="https://images.opencollective.com/icons8/7fa1641/logo.png" alt="Icons8: free icons, photos, illustrations, and music" height="32"></a> <a href="https://discord.com"><img src="https://images.opencollective.com/discordapp/f9645d9/logo.png" alt="Discord" height="32"></a> <a href="https://transloadit.com/"><img src="https://avatars.githubusercontent.com/u/125754?v=4" alt="Transloadit" height="32"></a> <a href="https://www.ignitionapp.com"><img src="https://avatars.githubusercontent.com/u/5753491?v=4" alt="Ignition" height="32"></a> <a href="https://herocoders.com"><img src="https://avatars.githubusercontent.com/u/37549774?v=4" alt="HeroCoders" height="32"></a> <a href="https://quickbookstoolhub.com"><img src="https://avatars.githubusercontent.com/u/95090305?u=e5bc398ef775c9ed19f955c675cdc1fb6abf01df&v=4" alt="QuickBooks Tool hub" height="32"></a></p> | ||
<p><a href="https://liftoff.io/"><img src="https://images.opencollective.com/liftoff/5c4fa84/logo.png" alt="Liftoff" height="64"></a> <a href="https://americanexpress.io"><img src="https://avatars.githubusercontent.com/u/3853301?v=4" alt="American Express" height="64"></a></p><h3>Bronze Sponsors</h3> | ||
<p><a href="https://themeisle.com"><img src="https://images.opencollective.com/themeisle/d5592fe/logo.png" alt="ThemeIsle" height="32"></a> <a href="https://www.crosswordsolver.org/anagram-solver/"><img src="https://images.opencollective.com/anagram-solver/2666271/logo.png" alt="Anagram Solver" height="32"></a> <a href="https://icons8.com/"><img src="https://images.opencollective.com/icons8/7fa1641/logo.png" alt="Icons8" height="32"></a> <a href="https://discord.com"><img src="https://images.opencollective.com/discordapp/f9645d9/logo.png" alt="Discord" height="32"></a> <a href="https://transloadit.com/"><img src="https://avatars.githubusercontent.com/u/125754?v=4" alt="Transloadit" height="32"></a> <a href="https://www.ignitionapp.com"><img src="https://avatars.githubusercontent.com/u/5753491?v=4" alt="Ignition" height="32"></a> <a href="https://herocoders.com"><img src="https://avatars.githubusercontent.com/u/37549774?v=4" alt="HeroCoders" height="32"></a></p> | ||
<!--sponsorsend--> | ||
@@ -292,0 +302,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3031350
38
407
71479
305
62
+ Added@eslint/js@8.55.0(transitive)
+ Added@ungap/structured-clone@1.2.0(transitive)
- Removedimport-fresh@^3.0.0
- Removedstrip-json-comments@^3.1.0
- Removed@eslint/js@8.42.0(transitive)
Updated@eslint/eslintrc@^2.1.4
Updated@eslint/js@8.55.0
Updatedajv@^6.12.4
Updatedeslint-scope@^7.2.2
Updatedeslint-visitor-keys@^3.4.3
Updatedespree@^9.6.1
Updatedoptionator@^0.9.3