Skip to content

Commit c30a984

Browse files
feat(eslint-plugin): add new rule to disallow 'use strict' in es modules
1 parent c406ccc commit c30a984

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

projects/eslint-plugin/configs/general.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const config = {
107107
'@liferay/no-it-should': 'error',
108108
'@liferay/no-length-jsx-expression': 'error',
109109
'@liferay/no-require-and-call': 'error',
110+
'@liferay/no-use-strict-in-module': 'error',
110111
'@liferay/padded-test-blocks': 'error',
111112
'@liferay/ref-name-suffix': 'error',
112113
'@liferay/sort-import-destructures': 'error',
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Disallow 'use strict' in es modules (no-use-strict-in-module)
2+
3+
You never need `'use strict'` in an ES module ([spec](http://www.ecma-international.org/ecma-262/6.0/#sec-strict-mode-code)). We can infer if file is an esmodule if it uses export/import syntax.
4+
5+
## Rule Details
6+
7+
Examples of **incorrect** code for this rule:
8+
9+
```js
10+
'use strict';
11+
12+
export default () => {};
13+
```
14+
15+
```js
16+
'use strict';
17+
18+
import foo from 'bar';
19+
```
20+
21+
Examples of **correct** code for this rule:
22+
23+
```js
24+
import foo from 'bar';
25+
//...
26+
```
27+
28+
```js
29+
//...
30+
export default foo;
31+
```
32+
33+
```js
34+
//...
35+
export foo;
36+
```
37+
38+
## Further Reading
39+
40+
- [Initial motivation for this rule](https://github.com/liferay/liferay-frontend-projects/issues/20).

projects/eslint-plugin/rules/general/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = {
2121
'no-it-should': require('./lib/rules/no-it-should'),
2222
'no-length-jsx-expression': require('./lib/rules/no-length-jsx-expression'),
2323
'no-require-and-call': require('./lib/rules/no-require-and-call'),
24+
'no-use-strict-in-module': require('./lib/rules/no-use-strict-in-module'),
2425
'padded-test-blocks': require('./lib/rules/padded-test-blocks'),
2526
'ref-name-suffix': require('./lib/rules/ref-name-suffix'),
2627
'sort-class-names': require('./lib/rules/sort-class-names'),
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com>
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
6+
const message = `'use strict' is unnecessary inside of modules`;
7+
8+
module.exports = {
9+
create(context) {
10+
const useStrictExpressions = [];
11+
let esModule;
12+
13+
const checkEsModule = () => {
14+
if (!esModule) {
15+
esModule = true;
16+
}
17+
};
18+
19+
return {
20+
'ExportDefaultDeclaration': checkEsModule,
21+
'ExportNamedDeclaration': checkEsModule,
22+
'ExpressionStatement'(node) {
23+
if (
24+
node.expression.type === 'Literal' &&
25+
node.expression.value === 'use strict'
26+
) {
27+
useStrictExpressions.push(node.expression);
28+
}
29+
},
30+
'ImportDeclaration': checkEsModule,
31+
'ImportNamespaceSpecifier': checkEsModule,
32+
'Program:exit': () => {
33+
if (esModule) {
34+
useStrictExpressions.forEach((expression) => {
35+
context.report({
36+
message,
37+
node: expression,
38+
});
39+
});
40+
}
41+
},
42+
};
43+
},
44+
45+
meta: {
46+
docs: {
47+
category: 'Best Practices',
48+
description: message,
49+
recommended: false,
50+
url:
51+
'https://github.com/liferay/liferay-frontend-projects/issues/20',
52+
},
53+
fixable: 'code',
54+
schema: [],
55+
type: 'problem',
56+
},
57+
};
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
6+
const MultiTester = require('../../../../../scripts/MultiTester');
7+
const rule = require('../../../lib/rules/no-use-strict-in-module');
8+
9+
const parserOptions = {
10+
parserOptions: {
11+
ecmaVersion: 6,
12+
sourceType: 'module',
13+
},
14+
};
15+
16+
const ruleTester = new MultiTester(parserOptions);
17+
18+
ruleTester.run('no-use-strict-in-module', rule, {
19+
invalid: [
20+
{
21+
code: `
22+
'use strict'
23+
24+
import {test} from 'test';
25+
`,
26+
errors: [
27+
{
28+
message: `'use strict' is unnecessary inside of modules`,
29+
type: 'Literal',
30+
},
31+
],
32+
},
33+
{
34+
code: `
35+
'use strict'
36+
37+
function test() {
38+
'use strict'
39+
40+
return 'test';
41+
}
42+
export default test;
43+
`,
44+
errors: [
45+
{
46+
message: `'use strict' is unnecessary inside of modules`,
47+
type: 'Literal',
48+
},
49+
{
50+
message: `'use strict' is unnecessary inside of modules`,
51+
type: 'Literal',
52+
},
53+
],
54+
},
55+
{
56+
code: `
57+
'use strict'
58+
59+
function test() {}
60+
export {test};
61+
`,
62+
errors: [
63+
{
64+
message: `'use strict' is unnecessary inside of modules`,
65+
type: 'Literal',
66+
},
67+
],
68+
},
69+
],
70+
71+
valid: [
72+
{
73+
code: `
74+
import {test} from 'test';
75+
`,
76+
},
77+
{
78+
code: `
79+
'use strict'
80+
81+
module.export = function test() {}
82+
`,
83+
},
84+
{
85+
code: `
86+
'use strict'
87+
88+
console.log('foo');
89+
`,
90+
},
91+
],
92+
});

0 commit comments

Comments
 (0)