Files
test-repo/scripts/tools/eslint-plugin-prettier-internal-rules/prefer-ast-path-node.js
T
Sosuke Suzuki e92dcbd1a5 Add new ESLint prefer-ast-path-node (#13470)
* Add new lint rule `prefer-path-get-value`

* Add new rule

* Apply fix

* Ingore projects in benchmark

* Add comment
2022-09-11 18:39:06 +09:00

51 lines
1.2 KiB
JavaScript

"use strict";
const selector = [
"CallExpression[arguments.length=0]",
"> MemberExpression",
].join("");
const messageId = "prefer-ast-path-node";
const getNodeFunctionNames = new Set(["getValue", "getNode"]);
module.exports = {
meta: {
type: "suggestion",
docs: {
url: "https://github.com/prettier/prettier/blob/main/scripts/tools/eslint-plugin-prettier-internal-rules/prefer-ast-path-node.js",
},
fixable: "code",
messages: {
[messageId]:
"Prefer `path.node` instead of `path.getValue()` and `path.getNode()`",
},
},
create(context) {
return {
[selector](node) {
if (!node.object?.name?.toLowerCase().endsWith("path")) {
return;
}
if (!getNodeFunctionNames.has(node.property?.name)) {
return;
}
context.report({
node,
messageId,
fix: (fixer) => [
fixer.replaceTextRange(
[
node.property.range[0],
// remove `()` for CallExpression
node.property.range[1] + 2,
],
"node"
),
],
});
},
};
},
};