Skip to content

Commit 19ffd54

Browse files
authored
prefer-module: Suggest import.meta.{dirname,filename} (#2261)
1 parent eb5af8b commit 19ffd54

File tree

3 files changed

+78
-19
lines changed

3 files changed

+78
-19
lines changed

rules/prefer-module.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,54 @@ const ERROR_USE_STRICT_DIRECTIVE = 'error/use-strict-directive';
1313
const ERROR_GLOBAL_RETURN = 'error/global-return';
1414
const ERROR_IDENTIFIER = 'error/identifier';
1515
const SUGGESTION_USE_STRICT_DIRECTIVE = 'suggestion/use-strict-directive';
16-
const SUGGESTION_DIRNAME = 'suggestion/dirname';
17-
const SUGGESTION_FILENAME = 'suggestion/filename';
16+
const SUGGESTION_IMPORT_META_DIRNAME = 'suggestion/import-meta-dirname';
17+
const SUGGESTION_IMPORT_META_URL_TO_DIRNAME = 'suggestion/import-meta-url-to-dirname';
18+
const SUGGESTION_IMPORT_META_FILENAME = 'suggestion/import-meta-filename';
19+
const SUGGESTION_IMPORT_META_URL_TO_FILENAME = 'suggestion/import-meta-url-to-filename';
1820
const SUGGESTION_IMPORT = 'suggestion/import';
1921
const SUGGESTION_EXPORT = 'suggestion/export';
2022
const messages = {
2123
[ERROR_USE_STRICT_DIRECTIVE]: 'Do not use "use strict" directive.',
2224
[ERROR_GLOBAL_RETURN]: '"return" should be used inside a function.',
2325
[ERROR_IDENTIFIER]: 'Do not use "{{name}}".',
2426
[SUGGESTION_USE_STRICT_DIRECTIVE]: 'Remove "use strict" directive.',
25-
[SUGGESTION_DIRNAME]: 'Replace "__dirname" with `"…(import.meta.url)"`.',
26-
[SUGGESTION_FILENAME]: 'Replace "__filename" with `"…(import.meta.url)"`.',
27+
[SUGGESTION_IMPORT_META_DIRNAME]: 'Replace `__dirname` with `import.meta.dirname`.',
28+
[SUGGESTION_IMPORT_META_URL_TO_DIRNAME]: 'Replace `__dirname` with `…(import.meta.url)`.',
29+
[SUGGESTION_IMPORT_META_FILENAME]: 'Replace `__dirname` with `import.meta.filename`.',
30+
[SUGGESTION_IMPORT_META_URL_TO_FILENAME]: 'Replace `__filename` with `…(import.meta.url)`.',
2731
[SUGGESTION_IMPORT]: 'Switch to `import`.',
2832
[SUGGESTION_EXPORT]: 'Switch to `export`.',
2933
};
3034

35+
const suggestions = new Map([
36+
[
37+
'__dirname',
38+
[
39+
{
40+
messageId: SUGGESTION_IMPORT_META_DIRNAME,
41+
replacement: 'import.meta.dirname',
42+
},
43+
{
44+
messageId: SUGGESTION_IMPORT_META_URL_TO_DIRNAME,
45+
replacement: 'path.dirname(url.fileURLToPath(import.meta.url))',
46+
},
47+
],
48+
],
49+
[
50+
'__filename',
51+
[
52+
{
53+
messageId: SUGGESTION_IMPORT_META_FILENAME,
54+
replacement: 'import.meta.filename',
55+
},
56+
{
57+
messageId: SUGGESTION_IMPORT_META_URL_TO_FILENAME,
58+
replacement: 'url.fileURLToPath(import.meta.url)',
59+
},
60+
],
61+
],
62+
]);
63+
3164
function fixRequireCall(node, sourceCode) {
3265
if (!isStaticRequire(node.parent) || node.parent.callee !== node) {
3366
return;
@@ -277,14 +310,12 @@ function create(context) {
277310
switch (name) {
278311
case '__filename':
279312
case '__dirname': {
280-
const messageId = node.name === '__dirname' ? SUGGESTION_DIRNAME : SUGGESTION_FILENAME;
281-
const replacement = node.name === '__dirname'
282-
? 'path.dirname(url.fileURLToPath(import.meta.url))'
283-
: 'url.fileURLToPath(import.meta.url)';
284-
problem.suggest = [{
285-
messageId,
286-
fix: fixer => replaceReferenceIdentifier(node, replacement, fixer),
287-
}];
313+
problem.suggest = suggestions.get(node.name)
314+
.map(({messageId, replacement}) => ({
315+
messageId,
316+
fix: fixer => replaceReferenceIdentifier(node, replacement, fixer),
317+
}));
318+
288319
return problem;
289320
}
290321

test/snapshots/prefer-module.mjs.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ Generated by [AVA](https://avajs.dev).
123123
| ^^^^^^^^^ Do not use "__dirname".␊
124124
125125
--------------------------------------------------------------------------------␊
126-
Suggestion 1/1: Replace "__dirname" with \`"…(import.meta.url)"\`.␊
126+
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.dirname\`.␊
127+
1 | const dirname = import.meta.dirname;␊
128+
129+
--------------------------------------------------------------------------------␊
130+
Suggestion 2/2: Replace \`__dirname\` with \`…(import.meta.url)\`.␊
127131
1 | const dirname = path.dirname(url.fileURLToPath(import.meta.url));␊
128132
`
129133

@@ -142,7 +146,11 @@ Generated by [AVA](https://avajs.dev).
142146
| ^^^^^^^^^^ Do not use "__filename".␊
143147
144148
--------------------------------------------------------------------------------␊
145-
Suggestion 1/1: Replace "__filename" with \`"…(import.meta.url)"\`.␊
149+
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.filename\`.␊
150+
1 | const dirname = import.meta.filename;␊
151+
152+
--------------------------------------------------------------------------------␊
153+
Suggestion 2/2: Replace \`__filename\` with \`…(import.meta.url)\`.␊
146154
1 | const dirname = url.fileURLToPath(import.meta.url);␊
147155
`
148156

@@ -161,7 +169,11 @@ Generated by [AVA](https://avajs.dev).
161169
| ^^^^^^^^^ Do not use "__dirname".␊
162170
163171
--------------------------------------------------------------------------------␊
164-
Suggestion 1/1: Replace "__dirname" with \`"…(import.meta.url)"\`.␊
172+
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.dirname\`.␊
173+
1 | const foo = { __dirname: import.meta.dirname};␊
174+
175+
--------------------------------------------------------------------------------␊
176+
Suggestion 2/2: Replace \`__dirname\` with \`…(import.meta.url)\`.␊
165177
1 | const foo = { __dirname: path.dirname(url.fileURLToPath(import.meta.url))};␊
166178
`
167179

@@ -180,7 +192,11 @@ Generated by [AVA](https://avajs.dev).
180192
| ^^^^^^^^^^ Do not use "__filename".␊
181193
182194
--------------------------------------------------------------------------------␊
183-
Suggestion 1/1: Replace "__filename" with \`"…(import.meta.url)"\`.␊
195+
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.filename\`.␊
196+
1 | const foo = {__filename: import.meta.filename, };␊
197+
198+
--------------------------------------------------------------------------------␊
199+
Suggestion 2/2: Replace \`__filename\` with \`…(import.meta.url)\`.␊
184200
1 | const foo = {__filename: url.fileURLToPath(import.meta.url), };␊
185201
`
186202

@@ -199,7 +215,11 @@ Generated by [AVA](https://avajs.dev).
199215
| ^^^^^^^^^ Do not use "__dirname".␊
200216
201217
--------------------------------------------------------------------------------␊
202-
Suggestion 1/1: Replace "__dirname" with \`"…(import.meta.url)"\`.␊
218+
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.dirname\`.␊
219+
1 | if (import.meta.dirname.startsWith("/project/src/")) {}␊
220+
221+
--------------------------------------------------------------------------------␊
222+
Suggestion 2/2: Replace \`__dirname\` with \`…(import.meta.url)\`.␊
203223
1 | if (path.dirname(url.fileURLToPath(import.meta.url)).startsWith("/project/src/")) {}␊
204224
`
205225

@@ -218,7 +238,11 @@ Generated by [AVA](https://avajs.dev).
218238
| ^^^^^^^^^^ Do not use "__filename".␊
219239
220240
--------------------------------------------------------------------------------␊
221-
Suggestion 1/1: Replace "__filename" with \`"…(import.meta.url)"\`.␊
241+
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.filename\`.␊
242+
1 | if (import.meta.filename.endsWith(".js")) {}␊
243+
244+
--------------------------------------------------------------------------------␊
245+
Suggestion 2/2: Replace \`__filename\` with \`…(import.meta.url)\`.␊
222246
1 | if (url.fileURLToPath(import.meta.url).endsWith(".js")) {}␊
223247
`
224248

@@ -1833,7 +1857,11 @@ Generated by [AVA](https://avajs.dev).
18331857
| ^^^^^^^^^^ Do not use "__filename".␊
18341858
18351859
--------------------------------------------------------------------------------␊
1836-
Suggestion 1/1: Replace "__filename" with \`"…(import.meta.url)"\`.␊
1860+
Suggestion 1/2: Replace \`__dirname\` with \`import.meta.filename\`.␊
1861+
1 | import.meta.filename␊
1862+
1863+
--------------------------------------------------------------------------------␊
1864+
Suggestion 2/2: Replace \`__filename\` with \`…(import.meta.url)\`.␊
18371865
1 | url.fileURLToPath(import.meta.url)␊
18381866
`
18391867

test/snapshots/prefer-module.mjs.snap

111 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)