diff --git a/rules/prefer-module.js b/rules/prefer-module.js index 479a83102a..7888bcdcd2 100644 --- a/rules/prefer-module.js +++ b/rules/prefer-module.js @@ -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'; @@ -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`.', @@ -212,9 +214,9 @@ 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 {}; } @@ -222,14 +224,19 @@ function create(context) { 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 { diff --git a/test/prefer-module.mjs b/test/prefer-module.mjs index 96e3789348..2ee5cbaefa 100644 --- a/test/prefer-module.mjs +++ b/test/prefer-module.mjs @@ -29,6 +29,14 @@ test.snapshot({ console.log(1); } `, + { + code: outdent` + 'use strict'; + + console.log(1); + `, + filename: 'example.mjs', + }, ], }); diff --git a/test/snapshots/prefer-module.mjs.md b/test/snapshots/prefer-module.mjs.md index 3ab79063c3..62d7792a55 100644 --- a/test/snapshots/prefer-module.mjs.md +++ b/test/snapshots/prefer-module.mjs.md @@ -9,12 +9,6 @@ Generated by [AVA](https://avajs.dev). 2 | 3 | console.log(1); -> Output - - `␊ - 1 | console.log(1);␊ - ` - > Error 1/1 `␊ @@ -22,6 +16,10 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^^ Do not use "use strict" directive.␊ 2 |␊ 3 | console.log(1);␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/1: Remove "use strict" directive.␊ + 1 | console.log(1);␊ ` ## Invalid #2 @@ -29,12 +27,6 @@ Generated by [AVA](https://avajs.dev). 2 | 3 | console.log(1); -> Output - - `␊ - 1 | console.log(1);␊ - ` - > Error 1/1 `␊ @@ -42,6 +34,10 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^^ Do not use "use strict" directive.␊ 2 |␊ 3 | console.log(1);␊ + ␊ + --------------------------------------------------------------------------------␊ + Suggestion 1/1: Remove "use strict" directive.␊ + 1 | console.log(1);␊ ` ## Invalid #3 @@ -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 diff --git a/test/snapshots/prefer-module.mjs.snap b/test/snapshots/prefer-module.mjs.snap index 53cbbb0647..2e91adb24a 100644 Binary files a/test/snapshots/prefer-module.mjs.snap and b/test/snapshots/prefer-module.mjs.snap differ