Rename parameters in massageAstNode (#16024)
This commit is contained in:
1 parent
71b9cdec54
commit
76b8f95061
14 files changed
+361
-261
No files matched your search
@@ -7,6 +7,7 @@ module.exports = {
|
||||
"directly-loc-start-end": require("./directly-loc-start-end.js"),
|
||||
"flat-ast-path-call": require("./flat-ast-path-call.js"),
|
||||
"jsx-identifier-case": require("./jsx-identifier-case.js"),
|
||||
"massage-ast-parameter-names": require("./massage-ast-parameter-names.js"),
|
||||
"no-conflicting-comment-check-flags": require("./no-conflicting-comment-check-flags.js"),
|
||||
"no-doc-public-import": require("./no-doc-public-import.js"),
|
||||
"no-empty-flat-contents-for-if-break": require("./no-empty-flat-contents-for-if-break.js"),
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
"use strict";
|
||||
|
||||
const MESSAGE_ID = "massage-ast-parameter-names";
|
||||
|
||||
const massageAstFunctionSelector = [
|
||||
"FunctionDeclaration",
|
||||
"[async!=true]",
|
||||
"[generator!=true]",
|
||||
'[id.type="Identifier"]',
|
||||
'[id.name="clean"]',
|
||||
].join("");
|
||||
|
||||
const getVariableIdentifiers = ({ identifiers, references }) => [
|
||||
...new Set([
|
||||
...identifiers,
|
||||
...references.map(({ identifier }) => identifier),
|
||||
]),
|
||||
];
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
type: "suggestion",
|
||||
docs: {
|
||||
url: "https://github.com/prettier/prettier/blob/main/scripts/tools/eslint-plugin-prettier-internal-rules/massage-ast-parameter-names.js",
|
||||
},
|
||||
messages: {
|
||||
[MESSAGE_ID]:
|
||||
"The {{name}} node parameter '{{original}}' should be named '{{name}}'.",
|
||||
},
|
||||
fixable: "code",
|
||||
schema: {
|
||||
type: "array",
|
||||
uniqueItems: true,
|
||||
},
|
||||
},
|
||||
create: (context) =>
|
||||
Object.fromEntries(
|
||||
["original", "cloned"].map((name, index) => [
|
||||
`${massageAstFunctionSelector} > Identifier[name!="${name}"].params:nth-child(${index + 1})`,
|
||||
(parameter) => {
|
||||
const variables = context.sourceCode.getDeclaredVariables(
|
||||
parameter.parent,
|
||||
);
|
||||
const variable = variables.find(
|
||||
(variable) => variable.name === parameter.name,
|
||||
);
|
||||
if (!variable) {
|
||||
throw new Error("Unexpected error.");
|
||||
}
|
||||
|
||||
context.report({
|
||||
node: parameter,
|
||||
messageId: MESSAGE_ID,
|
||||
data: { original: parameter.name, name },
|
||||
// Good enough for our use case
|
||||
// A prefect fix should be https://github.com/sindresorhus/eslint-plugin-unicorn/blob/702d51bed176a9c2c93bc4a2ca52e700dd0c2339/rules/fix/rename-variable.js#L5
|
||||
fix: (fixer) =>
|
||||
getVariableIdentifiers(variable).map((node) =>
|
||||
fixer.replaceText(node, name),
|
||||
),
|
||||
});
|
||||
},
|
||||
]),
|
||||
),
|
||||
};
|
||||
@@ -970,3 +970,22 @@ test("prefer-ast-path-getters", {
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
test("massage-ast-parameter-names", {
|
||||
valid: [
|
||||
"function notNamedClean(a, b) {}",
|
||||
"function clean(original, cloned) {}",
|
||||
],
|
||||
invalid: [
|
||||
{
|
||||
code: "function clean(theOriginalNode, cloned) {delete theOriginalNode.property}",
|
||||
output: "function clean(original, cloned) {delete original.property}",
|
||||
errors: 1,
|
||||
},
|
||||
{
|
||||
code: "function clean(original, theClonedNode) {delete theClonedNode.property}",
|
||||
output: "function clean(original, cloned) {delete cloned.property}",
|
||||
errors: 1,
|
||||
},
|
||||
],
|
||||
});
|
||||
Reference in new issue
Block a user