-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support `getParent` * Fix * Fix * Refactor * Rename rule and add tests * Fix by rule * Fix error message * Add error message tests * Run Prettier! * Address review * Improve error message * Refactor * Stricter check & refactor * Minor tweak Co-authored-by: fisker Cheung <lionkay@gmail.com>
- Loading branch information
1 parent
4a8de7f
commit 92a6af2
Showing
32 changed files
with
349 additions
and
132 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
92 changes: 92 additions & 0 deletions
92
scripts/tools/eslint-plugin-prettier-internal-rules/prefer-ast-path-getters.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,92 @@ | ||
"use strict"; | ||
|
||
const selector = [ | ||
"CallExpression", | ||
"[optional=false]", | ||
"[arguments.length<2]", | ||
'[callee.type="MemberExpression"]', | ||
"[callee.computed=false]", | ||
"[callee.optional=false]", | ||
'[callee.object.type="Identifier"]', | ||
"[callee.object.name=/[pP]ath$/]", | ||
'[callee.property.type="Identifier"]', | ||
].join(""); | ||
|
||
const messageId = "prefer-ast-path-getters"; | ||
|
||
function getReplacement(callExpression) { | ||
const method = callExpression.callee.property.name; | ||
const description = `${method}()`; | ||
switch (method) { | ||
case "getValue": | ||
case "getNode": | ||
if (callExpression.arguments.length === 0) { | ||
return { getter: "node", description }; | ||
} | ||
break; | ||
|
||
case "getParentNode": { | ||
// `path.getParentNode()` | ||
if (callExpression.arguments.length === 0) { | ||
return { getter: "parent", description }; | ||
} | ||
|
||
// `path.getParentNode(count)` | ||
const [countNode] = callExpression.arguments; | ||
if (countNode.type !== "Literal") { | ||
return; | ||
} | ||
|
||
const count = countNode.value; | ||
if (count === 0) { | ||
return { getter: "parent", description: `${method}(0)` }; | ||
} | ||
|
||
if (count === 1) { | ||
return { getter: "grandparent", description: `${method}(1)` }; | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
docs: { | ||
url: "https://github.com/prettier/prettier/blob/main/scripts/tools/eslint-plugin-prettier-internal-rules/prefer-ast-path-getters.js", | ||
}, | ||
fixable: "code", | ||
messages: { | ||
[messageId]: | ||
"Prefer `AstPath#{{ getter }}` over `AstPath#{{ description }}`.", | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
[selector](callExpression) { | ||
const replacement = getReplacement(callExpression); | ||
if (!replacement) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: callExpression.callee, | ||
messageId, | ||
data: { | ||
getter: replacement.getter, | ||
description: replacement.description, | ||
}, | ||
fix: (fixer) => | ||
fixer.replaceTextRange( | ||
[ | ||
callExpression.callee.property.range[0], | ||
callExpression.range[1], | ||
], | ||
replacement.getter | ||
), | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
50 changes: 0 additions & 50 deletions
50
scripts/tools/eslint-plugin-prettier-internal-rules/prefer-ast-path-node.js
This file was deleted.
Oops, something went wrong.
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.