From 6c718af090c1b5dd25d74a2ecfc65fbee0c00716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Thu, 28 Apr 2022 08:45:57 +0800 Subject: [PATCH] fix: "use strict" should not trigger strict mode in es3. (#547) * fix: "use strict" should not trigger strict mode in es3. fixes https://github.com/eslint/eslint/issues/15456 * chore: add func strict --- package.json | 4 +- .../3/strict/func-strict.module-result.js | 3 + .../3/strict/func-strict.result.js | 424 ++++++++++++++++++ .../ecma-version/3/strict/func-strict.src.js | 4 + .../3/strict/use-strict.module-result.js | 3 + .../3/strict/use-strict.result.js | 259 +++++++++++ .../ecma-version/3/strict/use-strict.src.js | 7 + 7 files changed, 702 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/ecma-version/3/strict/func-strict.module-result.js create mode 100644 tests/fixtures/ecma-version/3/strict/func-strict.result.js create mode 100644 tests/fixtures/ecma-version/3/strict/func-strict.src.js create mode 100644 tests/fixtures/ecma-version/3/strict/use-strict.module-result.js create mode 100644 tests/fixtures/ecma-version/3/strict/use-strict.result.js create mode 100644 tests/fixtures/ecma-version/3/strict/use-strict.src.js diff --git a/package.json b/package.json index 78005f5c..c4257e32 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,8 @@ }, "license": "BSD-2-Clause", "dependencies": { - "acorn": "^8.7.0", - "acorn-jsx": "^5.3.1", + "acorn": "^8.7.1", + "acorn-jsx": "^5.3.2", "eslint-visitor-keys": "^3.3.0" }, "devDependencies": { diff --git a/tests/fixtures/ecma-version/3/strict/func-strict.module-result.js b/tests/fixtures/ecma-version/3/strict/func-strict.module-result.js new file mode 100644 index 00000000..52aa8f04 --- /dev/null +++ b/tests/fixtures/ecma-version/3/strict/func-strict.module-result.js @@ -0,0 +1,3 @@ +export default { + "message": "sourceType 'module' is not supported when ecmaVersion < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options." +}; \ No newline at end of file diff --git a/tests/fixtures/ecma-version/3/strict/func-strict.result.js b/tests/fixtures/ecma-version/3/strict/func-strict.result.js new file mode 100644 index 00000000..bf3ba9e1 --- /dev/null +++ b/tests/fixtures/ecma-version/3/strict/func-strict.result.js @@ -0,0 +1,424 @@ +export default { + "type": "Program", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "range": [ + 0, + 51 + ], + "body": [ + { + "type": "FunctionDeclaration", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "range": [ + 0, + 51 + ], + "id": { + "type": "Identifier", + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "range": [ + 9, + 12 + ], + "name": "foo" + }, + "params": [], + "body": { + "type": "BlockStatement", + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "range": [ + 14, + 51 + ], + "body": [ + { + "type": "ExpressionStatement", + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "range": [ + 20, + 33 + ], + "expression": { + "type": "Literal", + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "range": [ + 20, + 32 + ], + "value": "use strict", + "raw": "\"use strict\"" + }, + "directive": "use strict" + }, + { + "type": "VariableDeclaration", + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 15 + } + }, + "range": [ + 38, + 49 + ], + "declarations": [ + { + "type": "VariableDeclarator", + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 14 + } + }, + "range": [ + 42, + 48 + ], + "id": { + "type": "Identifier", + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "range": [ + 42, + 43 + ], + "name": "i" + }, + "init": { + "type": "Literal", + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 14 + } + }, + "range": [ + 46, + 48 + ], + "value": 0, + "raw": "00" + } + } + ], + "kind": "var" + } + ] + }, + "expression": false, + "generator": false + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "function", + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "range": [ + 0, + 8 + ] + }, + { + "type": "Identifier", + "value": "foo", + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "range": [ + 9, + 12 + ] + }, + { + "type": "Punctuator", + "value": "(", + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "range": [ + 12, + 13 + ] + }, + { + "type": "Punctuator", + "value": ")", + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "range": [ + 13, + 14 + ] + }, + { + "type": "Punctuator", + "value": "{", + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "range": [ + 14, + 15 + ] + }, + { + "type": "String", + "value": "\"use strict\"", + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "range": [ + 20, + 32 + ] + }, + { + "type": "Punctuator", + "value": ";", + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "range": [ + 32, + 33 + ] + }, + { + "type": "Keyword", + "value": "var", + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 7 + } + }, + "range": [ + 38, + 41 + ] + }, + { + "type": "Identifier", + "value": "i", + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "range": [ + 42, + 43 + ] + }, + { + "type": "Punctuator", + "value": "=", + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 11 + } + }, + "range": [ + 44, + 45 + ] + }, + { + "type": "Numeric", + "value": "00", + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 14 + } + }, + "range": [ + 46, + 48 + ] + }, + { + "type": "Punctuator", + "value": ";", + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 15 + } + }, + "range": [ + 48, + 49 + ] + }, + { + "type": "Punctuator", + "value": "}", + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "range": [ + 50, + 51 + ] + } + ] +}; \ No newline at end of file diff --git a/tests/fixtures/ecma-version/3/strict/func-strict.src.js b/tests/fixtures/ecma-version/3/strict/func-strict.src.js new file mode 100644 index 00000000..3bc3a81a --- /dev/null +++ b/tests/fixtures/ecma-version/3/strict/func-strict.src.js @@ -0,0 +1,4 @@ +function foo(){ + "use strict"; + var i = 00; +} diff --git a/tests/fixtures/ecma-version/3/strict/use-strict.module-result.js b/tests/fixtures/ecma-version/3/strict/use-strict.module-result.js new file mode 100644 index 00000000..52aa8f04 --- /dev/null +++ b/tests/fixtures/ecma-version/3/strict/use-strict.module-result.js @@ -0,0 +1,3 @@ +export default { + "message": "sourceType 'module' is not supported when ecmaVersion < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options." +}; \ No newline at end of file diff --git a/tests/fixtures/ecma-version/3/strict/use-strict.result.js b/tests/fixtures/ecma-version/3/strict/use-strict.result.js new file mode 100644 index 00000000..becb3f8d --- /dev/null +++ b/tests/fixtures/ecma-version/3/strict/use-strict.result.js @@ -0,0 +1,259 @@ +export default { + "type": "Program", + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 7, + "column": 13 + } + }, + "range": [ + 183, + 210 + ], + "body": [ + { + "type": "ExpressionStatement", + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 13 + } + }, + "range": [ + 183, + 196 + ], + "expression": { + "type": "Literal", + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 12 + } + }, + "range": [ + 183, + 195 + ], + "value": "use strict", + "raw": "\"use strict\"" + }, + "directive": "use strict" + }, + { + "type": "VariableDeclaration", + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 13 + } + }, + "range": [ + 197, + 210 + ], + "declarations": [ + { + "type": "VariableDeclarator", + "loc": { + "start": { + "line": 7, + "column": 4 + }, + "end": { + "line": 7, + "column": 12 + } + }, + "range": [ + 201, + 209 + ], + "id": { + "type": "Identifier", + "loc": { + "start": { + "line": 7, + "column": 4 + }, + "end": { + "line": 7, + "column": 7 + } + }, + "range": [ + 201, + 204 + ], + "name": "foo" + }, + "init": { + "type": "Literal", + "loc": { + "start": { + "line": 7, + "column": 10 + }, + "end": { + "line": 7, + "column": 12 + } + }, + "range": [ + 207, + 209 + ], + "value": 0, + "raw": "00" + } + } + ], + "kind": "var" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "String", + "value": "\"use strict\"", + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 12 + } + }, + "range": [ + 183, + 195 + ] + }, + { + "type": "Punctuator", + "value": ";", + "loc": { + "start": { + "line": 6, + "column": 12 + }, + "end": { + "line": 6, + "column": 13 + } + }, + "range": [ + 195, + 196 + ] + }, + { + "type": "Keyword", + "value": "var", + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 3 + } + }, + "range": [ + 197, + 200 + ] + }, + { + "type": "Identifier", + "value": "foo", + "loc": { + "start": { + "line": 7, + "column": 4 + }, + "end": { + "line": 7, + "column": 7 + } + }, + "range": [ + 201, + 204 + ] + }, + { + "type": "Punctuator", + "value": "=", + "loc": { + "start": { + "line": 7, + "column": 8 + }, + "end": { + "line": 7, + "column": 9 + } + }, + "range": [ + 205, + 206 + ] + }, + { + "type": "Numeric", + "value": "00", + "loc": { + "start": { + "line": 7, + "column": 10 + }, + "end": { + "line": 7, + "column": 12 + } + }, + "range": [ + 207, + 209 + ] + }, + { + "type": "Punctuator", + "value": ";", + "loc": { + "start": { + "line": 7, + "column": 12 + }, + "end": { + "line": 7, + "column": 13 + } + }, + "range": [ + 209, + 210 + ] + } + ] +}; \ No newline at end of file diff --git a/tests/fixtures/ecma-version/3/strict/use-strict.src.js b/tests/fixtures/ecma-version/3/strict/use-strict.src.js new file mode 100644 index 00000000..1699516b --- /dev/null +++ b/tests/fixtures/ecma-version/3/strict/use-strict.src.js @@ -0,0 +1,7 @@ +/** + * "use strict" should not trigger strict mode in es3. + * in acorn