Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-module: Use suggestions for 'use strict' directive #1470

Merged
merged 2 commits into from Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 16 additions & 9 deletions rules/prefer-module.js
Expand Up @@ -10,6 +10,7 @@ const {replaceReferenceIdentifier, removeSpacesAfter} = require('./fix/index.js'
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 = 'suggestion/import';
Expand All @@ -18,6 +19,7 @@ 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]: 'Switch to `import`.',
Expand Down Expand Up @@ -212,24 +214,29 @@ function fixModuleExports(node, sourceCode) {
}

function create(context) {
const filename = context.getFilename();
const filename = context.getFilename().toLowerCase();

if (filename.toLowerCase().endsWith('.cjs')) {
if (filename.endsWith('.cjs')) {
return {};
}

const sourceCode = context.getSourceCode();

return {
'ExpressionStatement[directive="use strict"]'(node) {
return {
node,
messageId: ERROR_USE_STRICT_DIRECTIVE,
* fix(fixer) {
yield fixer.remove(node);
yield removeSpacesAfter(node, sourceCode, fixer);
},
const problem = {node, messageId: ERROR_USE_STRICT_DIRECTIVE};
const fix = function * (fixer) {
yield fixer.remove(node);
yield removeSpacesAfter(node, sourceCode, fixer);
};

if (filename.endsWith('.mjs')) {
problem.fix = fix;
} else {
problem.suggest = [{messageId: SUGGESTION_USE_STRICT_DIRECTIVE, fix}];
}

return problem;
},
'ReturnStatement:not(:function ReturnStatement)'(node) {
return {
Expand Down
8 changes: 8 additions & 0 deletions test/prefer-module.mjs
Expand Up @@ -29,6 +29,14 @@ test.snapshot({
console.log(1);
}
`,
{
code: outdent`
'use strict';

console.log(1);
`,
filename: 'example.mjs',
},
],
});

Expand Down
56 changes: 38 additions & 18 deletions test/snapshots/prefer-module.mjs.md
Expand Up @@ -9,39 +9,35 @@ Generated by [AVA](https://avajs.dev).
2 |
3 | console.log(1);

> Output

`␊
1 | console.log(1);␊
`

> Error 1/1

`␊
> 1 | 'use strict';␊
| ^^^^^^^^^^^^^ Do not use "use strict" directive.␊
2 |␊
3 | console.log(1);␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Remove "use strict" directive.␊
1 | console.log(1);␊
`

## Invalid #2
1 | "use strict";
2 |
3 | console.log(1);

> Output

`␊
1 | console.log(1);␊
`

> Error 1/1

`␊
> 1 | "use strict";␊
| ^^^^^^^^^^^^^ Do not use "use strict" directive.␊
2 |␊
3 | console.log(1);␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Remove "use strict" directive.␊
1 | console.log(1);␊
`

## Invalid #3
Expand All @@ -50,22 +46,46 @@ Generated by [AVA](https://avajs.dev).
3 | console.log(1);
4 | }

> Output
> Error 1/1

`␊
1 | function foo () {␊
> 2 | "use strict";␊
| ^^^^^^^^^^^^^ Do not use "use strict" directive.␊
3 | console.log(1);␊
4 | }␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Remove "use strict" directive.␊
1 | function foo () {␊
2 | console.log(1);␊
3 | }␊
`

## Invalid #4
1 | 'use strict';
2 |
3 | console.log(1);

> Filename

`␊
example.mjs␊
`

> Output

`␊
1 | console.log(1);␊
`

> Error 1/1

`␊
1 | function foo () {␊
> 2 | "use strict";␊
| ^^^^^^^^^^^^^ Do not use "use strict" directive.␊
3 | console.log(1);␊
4 | }␊
> 1 | 'use strict';␊
| ^^^^^^^^^^^^^ Do not use "use strict" directive.␊
2 |␊
3 | console.log(1);␊
`

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