Add internal ESLint rule no-legacy-format-test-fixtures (#12044)
This commit is contained in:
1 parent
6bd81c23b6
commit
7290ff2dc7
4 files changed
+95
No files matched your search
@@ -184,6 +184,12 @@ module.exports = {
|
||||
"jest/prefer-to-be": "error",
|
||||
},
|
||||
},
|
||||
{
|
||||
files: ["tests/format/**/*.js"],
|
||||
rules: {
|
||||
"prettier-internal-rules/no-legacy-format-test-fixtures": "error",
|
||||
},
|
||||
},
|
||||
{
|
||||
files: ["tests/integration/**/*.js"],
|
||||
rules: {
|
||||
|
||||
@@ -12,6 +12,7 @@ module.exports = {
|
||||
"no-doc-builder-concat": require("./no-doc-builder-concat.js"),
|
||||
"no-empty-flat-contents-for-if-break": require("./no-empty-flat-contents-for-if-break.js"),
|
||||
"no-identifier-n": require("./no-identifier-n.js"),
|
||||
"no-legacy-format-test-fixtures": require("./no-legacy-format-test-fixtures.js"),
|
||||
"no-node-comments": require("./no-node-comments.js"),
|
||||
"no-unnecessary-ast-path-call": require("./no-unnecessary-ast-path-call.js"),
|
||||
"prefer-ast-path-each": require("./prefer-ast-path-each.js"),
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
"use strict";
|
||||
|
||||
const dirnameArgumentSelector = [
|
||||
"CallExpression",
|
||||
'[callee.type="Identifier"]',
|
||||
'[callee.name="run_spec"]',
|
||||
" > ",
|
||||
"Identifier.arguments:first-child",
|
||||
'[name="__dirname"]',
|
||||
].join("");
|
||||
|
||||
const dirnamePropertySelector = [
|
||||
"CallExpression",
|
||||
'[callee.type="Identifier"]',
|
||||
'[callee.name="run_spec"]',
|
||||
" > ",
|
||||
"ObjectExpression.arguments:first-child",
|
||||
" > ",
|
||||
"Property.properties",
|
||||
'[key.type="Identifier"]',
|
||||
'[key.name="dirname"]',
|
||||
'[value.type="Identifier"]',
|
||||
'[value.name="__dirname"]',
|
||||
].join("");
|
||||
|
||||
const MESSAGE_ID_ARGUMENT = "dirname-argument";
|
||||
const MESSAGE_ID_PROPERTY = "dirname-property";
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
type: "suggestion",
|
||||
docs: {
|
||||
url: "https://github.com/prettier/prettier/blob/main/scripts/tools/eslint-plugin-prettier-internal-rules/no-legacy-format-test-fixtures.js",
|
||||
},
|
||||
messages: {
|
||||
[MESSAGE_ID_ARGUMENT]: "Use `import.meta` instead of `__dirname`.",
|
||||
[MESSAGE_ID_PROPERTY]:
|
||||
"Use `importMeta: import.meta` instead of `dirname: __dirname`.",
|
||||
},
|
||||
fixable: "code",
|
||||
hasSuggestions: true,
|
||||
},
|
||||
create(context) {
|
||||
return {
|
||||
[dirnameArgumentSelector](node) {
|
||||
context.report({
|
||||
node,
|
||||
messageId: MESSAGE_ID_ARGUMENT,
|
||||
fix: (fixer) => fixer.replaceText(node, "import.meta"),
|
||||
});
|
||||
},
|
||||
[dirnamePropertySelector](node) {
|
||||
context.report({
|
||||
node,
|
||||
messageId: MESSAGE_ID_PROPERTY,
|
||||
fix: (fixer) => [
|
||||
fixer.replaceText(node.key, "importMeta"),
|
||||
fixer.replaceText(node.value, "import.meta"),
|
||||
],
|
||||
});
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -331,6 +331,30 @@ test("no-identifier-n", {
|
||||
],
|
||||
});
|
||||
|
||||
test("no-legacy-format-test-fixtures", {
|
||||
valid: [
|
||||
"run_spec(import.meta, ['babel'])",
|
||||
"run_spec({importMeta: import.meta}, ['babel'])",
|
||||
].map((code) => ({ code, parserOptions: { sourceType: "module" } })),
|
||||
invalid: [
|
||||
{
|
||||
code: "run_spec(__dirname, ['babel'])",
|
||||
errors: [{ message: "Use `import.meta` instead of `__dirname`." }],
|
||||
output: "run_spec(import.meta, ['babel'])",
|
||||
},
|
||||
{
|
||||
code: "run_spec({snippets: ['x'], dirname: __dirname}, ['babel'])",
|
||||
errors: [
|
||||
{
|
||||
message:
|
||||
"Use `importMeta: import.meta` instead of `dirname: __dirname`.",
|
||||
},
|
||||
],
|
||||
output: "run_spec({snippets: ['x'], importMeta: import.meta}, ['babel'])",
|
||||
},
|
||||
].map((test) => ({ ...test, parserOptions: { sourceType: "module" } })),
|
||||
});
|
||||
|
||||
test("no-node-comments", {
|
||||
valid: [
|
||||
"const comments = node.notComments",
|
||||
|
||||
Reference in new issue
Block a user