Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename parameters in
massageAstNode
(#16024)
- Loading branch information
Showing
14 changed files
with
357 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
scripts/tools/eslint-plugin-prettier-internal-rules/massage-ast-parameter-names.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
), | ||
}); | ||
}, | ||
]), | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.