Skip to content

Commit

Permalink
prefer-module: Suggest import.meta.{dirname,filename} (#2261)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Jan 18, 2024
1 parent eb5af8b commit 19ffd54
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
55 changes: 43 additions & 12 deletions rules/prefer-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,54 @@ const ERROR_USE_STRICT_DIRECTIVE = 'error/use-strict-directive';
const ERROR_GLOBAL_RETURN = 'error/global-return';
const ERROR_IDENTIFIER = 'error/identifier';
const SUGGESTION_USE_STRICT_DIRECTIVE = 'suggestion/use-strict-directive';
const SUGGESTION_DIRNAME = 'suggestion/dirname';
const SUGGESTION_FILENAME = 'suggestion/filename';
const SUGGESTION_IMPORT_META_DIRNAME = 'suggestion/import-meta-dirname';
const SUGGESTION_IMPORT_META_URL_TO_DIRNAME = 'suggestion/import-meta-url-to-dirname';
const SUGGESTION_IMPORT_META_FILENAME = 'suggestion/import-meta-filename';
const SUGGESTION_IMPORT_META_URL_TO_FILENAME = 'suggestion/import-meta-url-to-filename';
const SUGGESTION_IMPORT = 'suggestion/import';
const SUGGESTION_EXPORT = 'suggestion/export';
const messages = {
[ERROR_USE_STRICT_DIRECTIVE]: 'Do not use "use strict" directive.',
[ERROR_GLOBAL_RETURN]: '"return" should be used inside a function.',
[ERROR_IDENTIFIER]: 'Do not use "{{name}}".',
[SUGGESTION_USE_STRICT_DIRECTIVE]: 'Remove "use strict" directive.',
[SUGGESTION_DIRNAME]: 'Replace "__dirname" with `"…(import.meta.url)"`.',
[SUGGESTION_FILENAME]: 'Replace "__filename" with `"…(import.meta.url)"`.',
[SUGGESTION_IMPORT_META_DIRNAME]: 'Replace `__dirname` with `import.meta.dirname`.',
[SUGGESTION_IMPORT_META_URL_TO_DIRNAME]: 'Replace `__dirname` with `…(import.meta.url)`.',
[SUGGESTION_IMPORT_META_FILENAME]: 'Replace `__dirname` with `import.meta.filename`.',
[SUGGESTION_IMPORT_META_URL_TO_FILENAME]: 'Replace `__filename` with `…(import.meta.url)`.',
[SUGGESTION_IMPORT]: 'Switch to `import`.',
[SUGGESTION_EXPORT]: 'Switch to `export`.',
};

const suggestions = new Map([
[
'__dirname',
[
{
messageId: SUGGESTION_IMPORT_META_DIRNAME,
replacement: 'import.meta.dirname',
},
{
messageId: SUGGESTION_IMPORT_META_URL_TO_DIRNAME,
replacement: 'path.dirname(url.fileURLToPath(import.meta.url))',
},
],
],
[
'__filename',
[
{
messageId: SUGGESTION_IMPORT_META_FILENAME,
replacement: 'import.meta.filename',
},
{
messageId: SUGGESTION_IMPORT_META_URL_TO_FILENAME,
replacement: 'url.fileURLToPath(import.meta.url)',
},
],
],
]);

function fixRequireCall(node, sourceCode) {
if (!isStaticRequire(node.parent) || node.parent.callee !== node) {
return;
Expand Down Expand Up @@ -277,14 +310,12 @@ function create(context) {
switch (name) {
case '__filename':
case '__dirname': {
const messageId = node.name === '__dirname' ? SUGGESTION_DIRNAME : SUGGESTION_FILENAME;
const replacement = node.name === '__dirname'
? 'path.dirname(url.fileURLToPath(import.meta.url))'
: 'url.fileURLToPath(import.meta.url)';
problem.suggest = [{
messageId,
fix: fixer => replaceReferenceIdentifier(node, replacement, fixer),
}];
problem.suggest = suggestions.get(node.name)
.map(({messageId, replacement}) => ({
messageId,
fix: fixer => replaceReferenceIdentifier(node, replacement, fixer),
}));

return problem;
}

Expand Down
42 changes: 35 additions & 7 deletions test/snapshots/prefer-module.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^ Do not use "__dirname".␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace "__dirname" with \`"…(import.meta.url)"\`.␊
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.dirname\`.␊
1 | const dirname = import.meta.dirname;␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Replace \`__dirname\` with \`…(import.meta.url)\`.␊
1 | const dirname = path.dirname(url.fileURLToPath(import.meta.url));␊
`

Expand All @@ -142,7 +146,11 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^ Do not use "__filename".␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace "__filename" with \`"…(import.meta.url)"\`.␊
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.filename\`.␊
1 | const dirname = import.meta.filename;␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Replace \`__filename\` with \`…(import.meta.url)\`.␊
1 | const dirname = url.fileURLToPath(import.meta.url);␊
`

Expand All @@ -161,7 +169,11 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^ Do not use "__dirname".␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace "__dirname" with \`"…(import.meta.url)"\`.␊
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.dirname\`.␊
1 | const foo = { __dirname: import.meta.dirname};␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Replace \`__dirname\` with \`…(import.meta.url)\`.␊
1 | const foo = { __dirname: path.dirname(url.fileURLToPath(import.meta.url))};␊
`

Expand All @@ -180,7 +192,11 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^ Do not use "__filename".␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace "__filename" with \`"…(import.meta.url)"\`.␊
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.filename\`.␊
1 | const foo = {__filename: import.meta.filename, };␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Replace \`__filename\` with \`…(import.meta.url)\`.␊
1 | const foo = {__filename: url.fileURLToPath(import.meta.url), };␊
`

Expand All @@ -199,7 +215,11 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^ Do not use "__dirname".␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace "__dirname" with \`"…(import.meta.url)"\`.␊
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.dirname\`.␊
1 | if (import.meta.dirname.startsWith("/project/src/")) {}␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Replace \`__dirname\` with \`…(import.meta.url)\`.␊
1 | if (path.dirname(url.fileURLToPath(import.meta.url)).startsWith("/project/src/")) {}␊
`

Expand All @@ -218,7 +238,11 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^ Do not use "__filename".␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace "__filename" with \`"…(import.meta.url)"\`.␊
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.filename\`.␊
1 | if (import.meta.filename.endsWith(".js")) {}␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Replace \`__filename\` with \`…(import.meta.url)\`.␊
1 | if (url.fileURLToPath(import.meta.url).endsWith(".js")) {}␊
`

Expand Down Expand Up @@ -1833,7 +1857,11 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^ Do not use "__filename".␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace "__filename" with \`"…(import.meta.url)"\`.␊
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.filename\`.␊
1 | import.meta.filename␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Replace \`__filename\` with \`…(import.meta.url)\`.␊
1 | url.fileURLToPath(import.meta.url)␊
`

Expand Down
Binary file modified test/snapshots/prefer-module.mjs.snap
Binary file not shown.

0 comments on commit 19ffd54

Please sign in to comment.