@d-es-ign/stryker-js-instrumenter 9.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/dist/src/create-instrumenter.d.ts +16 -0
- package/dist/src/create-instrumenter.d.ts.map +1 -0
- package/dist/src/create-instrumenter.js +15 -0
- package/dist/src/create-instrumenter.js.map +1 -0
- package/dist/src/disable-type-checks.d.ts +11 -0
- package/dist/src/disable-type-checks.d.ts.map +1 -0
- package/dist/src/disable-type-checks.js +125 -0
- package/dist/src/disable-type-checks.js.map +1 -0
- package/dist/src/file.d.ts +6 -0
- package/dist/src/file.d.ts.map +1 -0
- package/dist/src/file.js +2 -0
- package/dist/src/file.js.map +1 -0
- package/dist/src/frameworks/angular-ignorer.d.ts +16 -0
- package/dist/src/frameworks/angular-ignorer.d.ts.map +1 -0
- package/dist/src/frameworks/angular-ignorer.js +85 -0
- package/dist/src/frameworks/angular-ignorer.js.map +1 -0
- package/dist/src/frameworks/index.d.ts +4 -0
- package/dist/src/frameworks/index.d.ts.map +1 -0
- package/dist/src/frameworks/index.js +7 -0
- package/dist/src/frameworks/index.js.map +1 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +8 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/instrument-result.d.ts +7 -0
- package/dist/src/instrument-result.d.ts.map +1 -0
- package/dist/src/instrument-result.js +2 -0
- package/dist/src/instrument-result.js.map +1 -0
- package/dist/src/instrumenter-options.d.ts +5 -0
- package/dist/src/instrumenter-options.d.ts.map +1 -0
- package/dist/src/instrumenter-options.js +2 -0
- package/dist/src/instrumenter-options.js.map +1 -0
- package/dist/src/instrumenter-tokens.d.ts +6 -0
- package/dist/src/instrumenter-tokens.d.ts.map +1 -0
- package/dist/src/instrumenter-tokens.js +6 -0
- package/dist/src/instrumenter-tokens.js.map +1 -0
- package/dist/src/instrumenter.d.ts +24 -0
- package/dist/src/instrumenter.d.ts.map +1 -0
- package/dist/src/instrumenter.js +76 -0
- package/dist/src/instrumenter.js.map +1 -0
- package/dist/src/mutant-placers/expression-mutant-placer.d.ts +10 -0
- package/dist/src/mutant-placers/expression-mutant-placer.d.ts.map +1 -0
- package/dist/src/mutant-placers/expression-mutant-placer.js +144 -0
- package/dist/src/mutant-placers/expression-mutant-placer.js.map +1 -0
- package/dist/src/mutant-placers/index.d.ts +5 -0
- package/dist/src/mutant-placers/index.d.ts.map +1 -0
- package/dist/src/mutant-placers/index.js +11 -0
- package/dist/src/mutant-placers/index.js.map +1 -0
- package/dist/src/mutant-placers/mutant-placer.d.ts +8 -0
- package/dist/src/mutant-placers/mutant-placer.d.ts.map +1 -0
- package/dist/src/mutant-placers/mutant-placer.js +2 -0
- package/dist/src/mutant-placers/mutant-placer.js.map +1 -0
- package/dist/src/mutant-placers/statement-mutant-placer.d.ts +8 -0
- package/dist/src/mutant-placers/statement-mutant-placer.d.ts.map +1 -0
- package/dist/src/mutant-placers/statement-mutant-placer.js +24 -0
- package/dist/src/mutant-placers/statement-mutant-placer.js.map +1 -0
- package/dist/src/mutant-placers/switch-case-mutant-placer.d.ts +14 -0
- package/dist/src/mutant-placers/switch-case-mutant-placer.d.ts.map +1 -0
- package/dist/src/mutant-placers/switch-case-mutant-placer.js +29 -0
- package/dist/src/mutant-placers/switch-case-mutant-placer.js.map +1 -0
- package/dist/src/mutant-placers/throw-placement-error.d.ts +5 -0
- package/dist/src/mutant-placers/throw-placement-error.d.ts.map +1 -0
- package/dist/src/mutant-placers/throw-placement-error.js +17 -0
- package/dist/src/mutant-placers/throw-placement-error.js.map +1 -0
- package/dist/src/mutant.d.ts +27 -0
- package/dist/src/mutant.d.ts.map +1 -0
- package/dist/src/mutant.js +80 -0
- package/dist/src/mutant.js.map +1 -0
- package/dist/src/mutators/arithmetic-operator-mutator.d.ts +3 -0
- package/dist/src/mutators/arithmetic-operator-mutator.d.ts.map +1 -0
- package/dist/src/mutators/arithmetic-operator-mutator.js +34 -0
- package/dist/src/mutators/arithmetic-operator-mutator.js.map +1 -0
- package/dist/src/mutators/array-declaration-mutator.d.ts +3 -0
- package/dist/src/mutators/array-declaration-mutator.d.ts.map +1 -0
- package/dist/src/mutators/array-declaration-mutator.js +26 -0
- package/dist/src/mutators/array-declaration-mutator.js.map +1 -0
- package/dist/src/mutators/arrow-function-mutator.d.ts +3 -0
- package/dist/src/mutators/arrow-function-mutator.d.ts.map +1 -0
- package/dist/src/mutators/arrow-function-mutator.js +14 -0
- package/dist/src/mutators/arrow-function-mutator.js.map +1 -0
- package/dist/src/mutators/assignment-operator-mutator.d.ts +3 -0
- package/dist/src/mutators/assignment-operator-mutator.d.ts.map +1 -0
- package/dist/src/mutators/assignment-operator-mutator.js +42 -0
- package/dist/src/mutators/assignment-operator-mutator.js.map +1 -0
- package/dist/src/mutators/block-statement-mutator.d.ts +3 -0
- package/dist/src/mutators/block-statement-mutator.d.ts.map +1 -0
- package/dist/src/mutators/block-statement-mutator.js +63 -0
- package/dist/src/mutators/block-statement-mutator.js.map +1 -0
- package/dist/src/mutators/boolean-literal-mutator.d.ts +3 -0
- package/dist/src/mutators/boolean-literal-mutator.d.ts.map +1 -0
- package/dist/src/mutators/boolean-literal-mutator.js +17 -0
- package/dist/src/mutators/boolean-literal-mutator.js.map +1 -0
- package/dist/src/mutators/conditional-expression-mutator.d.ts +3 -0
- package/dist/src/mutators/conditional-expression-mutator.d.ts.map +1 -0
- package/dist/src/mutators/conditional-expression-mutator.js +81 -0
- package/dist/src/mutators/conditional-expression-mutator.js.map +1 -0
- package/dist/src/mutators/equality-operator-mutator.d.ts +3 -0
- package/dist/src/mutators/equality-operator-mutator.d.ts.map +1 -0
- package/dist/src/mutators/equality-operator-mutator.js +28 -0
- package/dist/src/mutators/equality-operator-mutator.js.map +1 -0
- package/dist/src/mutators/index.d.ts +4 -0
- package/dist/src/mutators/index.d.ts.map +1 -0
- package/dist/src/mutators/index.js +4 -0
- package/dist/src/mutators/index.js.map +1 -0
- package/dist/src/mutators/logical-operator-mutator.d.ts +3 -0
- package/dist/src/mutators/logical-operator-mutator.d.ts.map +1 -0
- package/dist/src/mutators/logical-operator-mutator.js +21 -0
- package/dist/src/mutators/logical-operator-mutator.js.map +1 -0
- package/dist/src/mutators/method-expression-mutator.d.ts +3 -0
- package/dist/src/mutators/method-expression-mutator.d.ts.map +1 -0
- package/dist/src/mutators/method-expression-mutator.js +65 -0
- package/dist/src/mutators/method-expression-mutator.js.map +1 -0
- package/dist/src/mutators/mutate.d.ts +3 -0
- package/dist/src/mutators/mutate.d.ts.map +1 -0
- package/dist/src/mutators/mutate.js +35 -0
- package/dist/src/mutators/mutate.js.map +1 -0
- package/dist/src/mutators/mutator-options.d.ts +5 -0
- package/dist/src/mutators/mutator-options.d.ts.map +1 -0
- package/dist/src/mutators/mutator-options.js +2 -0
- package/dist/src/mutators/mutator-options.js.map +1 -0
- package/dist/src/mutators/node-mutator.d.ts +6 -0
- package/dist/src/mutators/node-mutator.d.ts.map +1 -0
- package/dist/src/mutators/node-mutator.js +2 -0
- package/dist/src/mutators/node-mutator.js.map +1 -0
- package/dist/src/mutators/object-literal-mutator.d.ts +3 -0
- package/dist/src/mutators/object-literal-mutator.d.ts.map +1 -0
- package/dist/src/mutators/object-literal-mutator.js +11 -0
- package/dist/src/mutators/object-literal-mutator.js.map +1 -0
- package/dist/src/mutators/optional-chaining-mutator.d.ts +15 -0
- package/dist/src/mutators/optional-chaining-mutator.d.ts.map +1 -0
- package/dist/src/mutators/optional-chaining-mutator.js +28 -0
- package/dist/src/mutators/optional-chaining-mutator.js.map +1 -0
- package/dist/src/mutators/regex-mutator.d.ts +3 -0
- package/dist/src/mutators/regex-mutator.d.ts.map +1 -0
- package/dist/src/mutators/regex-mutator.js +53 -0
- package/dist/src/mutators/regex-mutator.js.map +1 -0
- package/dist/src/mutators/string-literal-mutator.d.ts +3 -0
- package/dist/src/mutators/string-literal-mutator.d.ts.map +1 -0
- package/dist/src/mutators/string-literal-mutator.js +36 -0
- package/dist/src/mutators/string-literal-mutator.js.map +1 -0
- package/dist/src/mutators/unary-operator-mutator.d.ts +3 -0
- package/dist/src/mutators/unary-operator-mutator.d.ts.map +1 -0
- package/dist/src/mutators/unary-operator-mutator.js +27 -0
- package/dist/src/mutators/unary-operator-mutator.js.map +1 -0
- package/dist/src/mutators/update-operator-mutator.d.ts +3 -0
- package/dist/src/mutators/update-operator-mutator.d.ts.map +1 -0
- package/dist/src/mutators/update-operator-mutator.js +17 -0
- package/dist/src/mutators/update-operator-mutator.js.map +1 -0
- package/dist/src/parsers/create-parser.d.ts +5 -0
- package/dist/src/parsers/create-parser.d.ts.map +1 -0
- package/dist/src/parsers/create-parser.js +58 -0
- package/dist/src/parsers/create-parser.js.map +1 -0
- package/dist/src/parsers/html-parser.d.ts +4 -0
- package/dist/src/parsers/html-parser.d.ts.map +1 -0
- package/dist/src/parsers/html-parser.js +96 -0
- package/dist/src/parsers/html-parser.js.map +1 -0
- package/dist/src/parsers/index.d.ts +5 -0
- package/dist/src/parsers/index.d.ts.map +1 -0
- package/dist/src/parsers/index.js +3 -0
- package/dist/src/parsers/index.js.map +1 -0
- package/dist/src/parsers/js-parser.d.ts +4 -0
- package/dist/src/parsers/js-parser.d.ts.map +1 -0
- package/dist/src/parsers/js-parser.js +50 -0
- package/dist/src/parsers/js-parser.js.map +1 -0
- package/dist/src/parsers/parse-error.d.ts +5 -0
- package/dist/src/parsers/parse-error.d.ts.map +1 -0
- package/dist/src/parsers/parse-error.js +6 -0
- package/dist/src/parsers/parse-error.js.map +1 -0
- package/dist/src/parsers/parser-context.d.ts +6 -0
- package/dist/src/parsers/parser-context.d.ts.map +1 -0
- package/dist/src/parsers/parser-context.js +2 -0
- package/dist/src/parsers/parser-context.js.map +1 -0
- package/dist/src/parsers/parser-options.d.ts +4 -0
- package/dist/src/parsers/parser-options.d.ts.map +1 -0
- package/dist/src/parsers/parser-options.js +2 -0
- package/dist/src/parsers/parser-options.js.map +1 -0
- package/dist/src/parsers/svelte-parser.d.ts +4 -0
- package/dist/src/parsers/svelte-parser.d.ts.map +1 -0
- package/dist/src/parsers/svelte-parser.js +169 -0
- package/dist/src/parsers/svelte-parser.js.map +1 -0
- package/dist/src/parsers/ts-parser.d.ts +9 -0
- package/dist/src/parsers/ts-parser.d.ts.map +1 -0
- package/dist/src/parsers/ts-parser.js +54 -0
- package/dist/src/parsers/ts-parser.js.map +1 -0
- package/dist/src/printers/html-printer.d.ts +4 -0
- package/dist/src/printers/html-printer.d.ts.map +1 -0
- package/dist/src/printers/html-printer.js +15 -0
- package/dist/src/printers/html-printer.js.map +1 -0
- package/dist/src/printers/index.d.ts +7 -0
- package/dist/src/printers/index.d.ts.map +1 -0
- package/dist/src/printers/index.js +23 -0
- package/dist/src/printers/index.js.map +1 -0
- package/dist/src/printers/js-printer.d.ts +4 -0
- package/dist/src/printers/js-printer.d.ts.map +1 -0
- package/dist/src/printers/js-printer.js +6 -0
- package/dist/src/printers/js-printer.js.map +1 -0
- package/dist/src/printers/svelte-printer.d.ts +4 -0
- package/dist/src/printers/svelte-printer.d.ts.map +1 -0
- package/dist/src/printers/svelte-printer.js +28 -0
- package/dist/src/printers/svelte-printer.js.map +1 -0
- package/dist/src/printers/ts-printer.d.ts +4 -0
- package/dist/src/printers/ts-printer.d.ts.map +1 -0
- package/dist/src/printers/ts-printer.js +9 -0
- package/dist/src/printers/ts-printer.js.map +1 -0
- package/dist/src/syntax/index.d.ts +86 -0
- package/dist/src/syntax/index.d.ts.map +1 -0
- package/dist/src/syntax/index.js +9 -0
- package/dist/src/syntax/index.js.map +1 -0
- package/dist/src/transformers/babel-transformer.d.ts +4 -0
- package/dist/src/transformers/babel-transformer.d.ts.map +1 -0
- package/dist/src/transformers/babel-transformer.js +142 -0
- package/dist/src/transformers/babel-transformer.js.map +1 -0
- package/dist/src/transformers/directive-bookkeeper.d.ts +19 -0
- package/dist/src/transformers/directive-bookkeeper.d.ts.map +1 -0
- package/dist/src/transformers/directive-bookkeeper.js +108 -0
- package/dist/src/transformers/directive-bookkeeper.js.map +1 -0
- package/dist/src/transformers/html-transformer.d.ts +4 -0
- package/dist/src/transformers/html-transformer.d.ts.map +1 -0
- package/dist/src/transformers/html-transformer.js +6 -0
- package/dist/src/transformers/html-transformer.js.map +1 -0
- package/dist/src/transformers/ignorer-bookkeeper.d.ts +18 -0
- package/dist/src/transformers/ignorer-bookkeeper.d.ts.map +1 -0
- package/dist/src/transformers/ignorer-bookkeeper.js +29 -0
- package/dist/src/transformers/ignorer-bookkeeper.js.map +1 -0
- package/dist/src/transformers/index.d.ts +6 -0
- package/dist/src/transformers/index.d.ts.map +1 -0
- package/dist/src/transformers/index.js +4 -0
- package/dist/src/transformers/index.js.map +1 -0
- package/dist/src/transformers/mutant-collector.d.ts +19 -0
- package/dist/src/transformers/mutant-collector.d.ts.map +1 -0
- package/dist/src/transformers/mutant-collector.js +25 -0
- package/dist/src/transformers/mutant-collector.js.map +1 -0
- package/dist/src/transformers/svelte-transformer.d.ts +4 -0
- package/dist/src/transformers/svelte-transformer.d.ts.map +1 -0
- package/dist/src/transformers/svelte-transformer.js +46 -0
- package/dist/src/transformers/svelte-transformer.js.map +1 -0
- package/dist/src/transformers/transformer-options.d.ts +6 -0
- package/dist/src/transformers/transformer-options.d.ts.map +1 -0
- package/dist/src/transformers/transformer-options.js +2 -0
- package/dist/src/transformers/transformer-options.js.map +1 -0
- package/dist/src/transformers/transformer.d.ts +22 -0
- package/dist/src/transformers/transformer.d.ts.map +1 -0
- package/dist/src/transformers/transformer.js +30 -0
- package/dist/src/transformers/transformer.js.map +1 -0
- package/dist/src/util/binary-operator.d.ts +2 -0
- package/dist/src/util/binary-operator.d.ts.map +1 -0
- package/dist/src/util/binary-operator.js +2 -0
- package/dist/src/util/binary-operator.js.map +1 -0
- package/dist/src/util/index.d.ts +4 -0
- package/dist/src/util/index.d.ts.map +1 -0
- package/dist/src/util/index.js +4 -0
- package/dist/src/util/index.js.map +1 -0
- package/dist/src/util/position-converter.d.ts +16 -0
- package/dist/src/util/position-converter.d.ts.map +1 -0
- package/dist/src/util/position-converter.js +126 -0
- package/dist/src/util/position-converter.js.map +1 -0
- package/dist/src/util/syntax-helpers.d.ts +60 -0
- package/dist/src/util/syntax-helpers.d.ts.map +1 -0
- package/dist/src/util/syntax-helpers.js +217 -0
- package/dist/src/util/syntax-helpers.js.map +1 -0
- package/package.json +64 -0
- package/src/create-instrumenter.ts +30 -0
- package/src/disable-type-checks.ts +153 -0
- package/src/file.ts +6 -0
- package/src/frameworks/angular-ignorer.ts +129 -0
- package/src/frameworks/index.ts +9 -0
- package/src/index.ts +8 -0
- package/src/instrument-result.ts +8 -0
- package/src/instrumenter-options.ts +5 -0
- package/src/instrumenter-tokens.ts +5 -0
- package/src/instrumenter.ts +100 -0
- package/src/mutant-placers/expression-mutant-placer.ts +210 -0
- package/src/mutant-placers/index.ts +12 -0
- package/src/mutant-placers/mutant-placer.ts +9 -0
- package/src/mutant-placers/statement-mutant-placer.ts +39 -0
- package/src/mutant-placers/switch-case-mutant-placer.ts +41 -0
- package/src/mutant-placers/throw-placement-error.ts +34 -0
- package/src/mutant.ts +101 -0
- package/src/mutators/arithmetic-operator-mutator.ts +51 -0
- package/src/mutators/array-declaration-mutator.ts +36 -0
- package/src/mutators/arrow-function-mutator.ts +22 -0
- package/src/mutators/assignment-operator-mutator.ts +59 -0
- package/src/mutators/block-statement-mutator.ts +89 -0
- package/src/mutators/boolean-literal-mutator.ts +24 -0
- package/src/mutators/conditional-expression-mutator.ts +92 -0
- package/src/mutators/equality-operator-mutator.ts +35 -0
- package/src/mutators/index.ts +3 -0
- package/src/mutators/logical-operator-mutator.ts +29 -0
- package/src/mutators/method-expression-mutator.ts +99 -0
- package/src/mutators/mutate.ts +36 -0
- package/src/mutators/mutator-options.ts +4 -0
- package/src/mutators/node-mutator.ts +6 -0
- package/src/mutators/object-literal-mutator.ts +15 -0
- package/src/mutators/optional-chaining-mutator.ts +39 -0
- package/src/mutators/regex-mutator.ts +69 -0
- package/src/mutators/string-literal-mutator.ts +49 -0
- package/src/mutators/unary-operator-mutator.ts +39 -0
- package/src/mutators/update-operator-mutator.ts +26 -0
- package/src/parsers/create-parser.ts +78 -0
- package/src/parsers/html-parser.ts +138 -0
- package/src/parsers/index.ts +5 -0
- package/src/parsers/js-parser.ts +58 -0
- package/src/parsers/parse-error.ts +9 -0
- package/src/parsers/parser-context.ts +15 -0
- package/src/parsers/parser-options.ts +3 -0
- package/src/parsers/svelte-parser.ts +255 -0
- package/src/parsers/ts-parser.ts +69 -0
- package/src/printers/html-printer.ts +20 -0
- package/src/printers/index.ts +33 -0
- package/src/printers/js-printer.ts +11 -0
- package/src/printers/svelte-printer.ts +34 -0
- package/src/printers/ts-printer.ts +14 -0
- package/src/syntax/index.ts +97 -0
- package/src/transformers/babel-transformer.ts +217 -0
- package/src/transformers/directive-bookkeeper.ts +173 -0
- package/src/transformers/html-transformer.ts +13 -0
- package/src/transformers/ignorer-bookkeeper.ts +41 -0
- package/src/transformers/index.ts +6 -0
- package/src/transformers/mutant-collector.ts +44 -0
- package/src/transformers/svelte-transformer.ts +57 -0
- package/src/transformers/transformer-options.ts +7 -0
- package/src/transformers/transformer.ts +54 -0
- package/src/util/binary-operator.ts +23 -0
- package/src/util/index.ts +3 -0
- package/src/util/position-converter.ts +145 -0
- package/src/util/syntax-helpers.ts +301 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import babel from '@babel/core';
|
|
2
|
+
|
|
3
|
+
import { deepCloneNode } from '../util/syntax-helpers.js';
|
|
4
|
+
|
|
5
|
+
import { NodeMutator } from './node-mutator.js';
|
|
6
|
+
|
|
7
|
+
const { types } = babel;
|
|
8
|
+
|
|
9
|
+
const replacements = new Map([
|
|
10
|
+
['charAt', null],
|
|
11
|
+
['endsWith', 'startsWith'],
|
|
12
|
+
['every', 'some'],
|
|
13
|
+
['filter', null],
|
|
14
|
+
['reverse', null],
|
|
15
|
+
['slice', null],
|
|
16
|
+
['sort', null],
|
|
17
|
+
['substr', null],
|
|
18
|
+
['substring', null],
|
|
19
|
+
['toLocaleLowerCase', 'toLocaleUpperCase'],
|
|
20
|
+
['toLowerCase', 'toUpperCase'],
|
|
21
|
+
['trim', null],
|
|
22
|
+
['trimEnd', 'trimStart'],
|
|
23
|
+
['min', 'max'],
|
|
24
|
+
['setDate', 'setTime'],
|
|
25
|
+
['setFullYear', 'setMonth'],
|
|
26
|
+
['setHours', 'setMinutes'],
|
|
27
|
+
['setSeconds', 'setMilliseconds'],
|
|
28
|
+
['setUTCDate', 'setTime'],
|
|
29
|
+
['setUTCFullYear', 'setUTCMonth'],
|
|
30
|
+
['setUTCHours', 'setUTCMinutes'],
|
|
31
|
+
['setUTCSeconds', 'setUTCMilliseconds'],
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
const noReverseRemplacements = ['getUTCDate', 'setUTCDate'];
|
|
35
|
+
|
|
36
|
+
for (const [key, value] of Array.from(replacements)) {
|
|
37
|
+
if (value && !noReverseRemplacements.includes(key)) {
|
|
38
|
+
replacements.set(value, key);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const methodExpressionMutator: NodeMutator = {
|
|
43
|
+
name: 'MethodExpression',
|
|
44
|
+
|
|
45
|
+
*mutate(path) {
|
|
46
|
+
if (!(path.isCallExpression() || path.isOptionalCallExpression())) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const { callee } = path.node;
|
|
51
|
+
if (
|
|
52
|
+
!(
|
|
53
|
+
types.isMemberExpression(callee) ||
|
|
54
|
+
types.isOptionalMemberExpression(callee)
|
|
55
|
+
) ||
|
|
56
|
+
!types.isIdentifier(callee.property)
|
|
57
|
+
) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const newName = replacements.get(callee.property.name);
|
|
62
|
+
if (newName === undefined) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (newName === null) {
|
|
67
|
+
// Remove the method expression. I.e. `foo.trim()` => `foo`
|
|
68
|
+
yield deepCloneNode(callee.object);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Replace the method expression. I.e. `foo.toLowerCase()` => `foo.toUpperCase`
|
|
73
|
+
const nodeArguments = path.node.arguments.map((argumentNode) =>
|
|
74
|
+
deepCloneNode(argumentNode),
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const mutatedCallee = types.isMemberExpression(callee)
|
|
78
|
+
? types.memberExpression(
|
|
79
|
+
deepCloneNode(callee.object),
|
|
80
|
+
types.identifier(newName),
|
|
81
|
+
false,
|
|
82
|
+
callee.optional,
|
|
83
|
+
)
|
|
84
|
+
: types.optionalMemberExpression(
|
|
85
|
+
deepCloneNode(callee.object),
|
|
86
|
+
types.identifier(newName),
|
|
87
|
+
false,
|
|
88
|
+
callee.optional,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
yield types.isCallExpression(path.node)
|
|
92
|
+
? types.callExpression(mutatedCallee, nodeArguments)
|
|
93
|
+
: types.optionalCallExpression(
|
|
94
|
+
mutatedCallee,
|
|
95
|
+
nodeArguments,
|
|
96
|
+
path.node.optional,
|
|
97
|
+
);
|
|
98
|
+
},
|
|
99
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { arithmeticOperatorMutator } from './arithmetic-operator-mutator.js';
|
|
2
|
+
import { NodeMutator } from './node-mutator.js';
|
|
3
|
+
import { blockStatementMutator } from './block-statement-mutator.js';
|
|
4
|
+
import { conditionalExpressionMutator } from './conditional-expression-mutator.js';
|
|
5
|
+
import { stringLiteralMutator } from './string-literal-mutator.js';
|
|
6
|
+
import { arrayDeclarationMutator } from './array-declaration-mutator.js';
|
|
7
|
+
import { arrowFunctionMutator } from './arrow-function-mutator.js';
|
|
8
|
+
import { booleanLiteralMutator } from './boolean-literal-mutator.js';
|
|
9
|
+
import { equalityOperatorMutator } from './equality-operator-mutator.js';
|
|
10
|
+
import { methodExpressionMutator } from './method-expression-mutator.js';
|
|
11
|
+
import { logicalOperatorMutator } from './logical-operator-mutator.js';
|
|
12
|
+
import { objectLiteralMutator } from './object-literal-mutator.js';
|
|
13
|
+
import { unaryOperatorMutator } from './unary-operator-mutator.js';
|
|
14
|
+
import { updateOperatorMutator } from './update-operator-mutator.js';
|
|
15
|
+
import { regexMutator } from './regex-mutator.js';
|
|
16
|
+
import { optionalChainingMutator } from './optional-chaining-mutator.js';
|
|
17
|
+
import { assignmentOperatorMutator } from './assignment-operator-mutator.js';
|
|
18
|
+
|
|
19
|
+
export const allMutators: NodeMutator[] = [
|
|
20
|
+
arithmeticOperatorMutator,
|
|
21
|
+
arrayDeclarationMutator,
|
|
22
|
+
arrowFunctionMutator,
|
|
23
|
+
blockStatementMutator,
|
|
24
|
+
booleanLiteralMutator,
|
|
25
|
+
conditionalExpressionMutator,
|
|
26
|
+
equalityOperatorMutator,
|
|
27
|
+
logicalOperatorMutator,
|
|
28
|
+
methodExpressionMutator,
|
|
29
|
+
objectLiteralMutator,
|
|
30
|
+
stringLiteralMutator,
|
|
31
|
+
unaryOperatorMutator,
|
|
32
|
+
updateOperatorMutator,
|
|
33
|
+
regexMutator,
|
|
34
|
+
optionalChainingMutator,
|
|
35
|
+
assignmentOperatorMutator,
|
|
36
|
+
];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import babel from '@babel/core';
|
|
2
|
+
|
|
3
|
+
import { NodeMutator } from './index.js';
|
|
4
|
+
|
|
5
|
+
const { types } = babel;
|
|
6
|
+
|
|
7
|
+
export const objectLiteralMutator: NodeMutator = {
|
|
8
|
+
name: 'ObjectLiteral',
|
|
9
|
+
|
|
10
|
+
*mutate(path) {
|
|
11
|
+
if (path.isObjectExpression() && path.node.properties.length > 0) {
|
|
12
|
+
yield types.objectExpression([]);
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import babel from '@babel/core';
|
|
2
|
+
|
|
3
|
+
import { NodeMutator } from './index.js';
|
|
4
|
+
|
|
5
|
+
const { types: t } = babel;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Mutates optional chaining operators
|
|
9
|
+
* Note that the AST for optional chaining might not be what you expect. Nodes of type `OptionalMemberExpression` can be either optional or not-optional
|
|
10
|
+
*
|
|
11
|
+
* For example: In this expression: `foo?.bar.baz` the `.baz` member expression is of type `OptionalMemberExpression`, because it is part of an optional chain, but is is _not_ optional.
|
|
12
|
+
* Only the `.bar` optional member expression is optional.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* foo?.bar -> foo.bar
|
|
16
|
+
* foo?.[1] -> foo[1]
|
|
17
|
+
* foo?.() -> foo()
|
|
18
|
+
*/
|
|
19
|
+
export const optionalChainingMutator: NodeMutator = {
|
|
20
|
+
name: 'OptionalChaining',
|
|
21
|
+
|
|
22
|
+
*mutate(path) {
|
|
23
|
+
if (path.isOptionalMemberExpression() && path.node.optional) {
|
|
24
|
+
yield t.optionalMemberExpression(
|
|
25
|
+
t.cloneNode(path.node.object, true),
|
|
26
|
+
t.cloneNode(path.node.property, true),
|
|
27
|
+
path.node.computed,
|
|
28
|
+
/*optional*/ false,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
if (path.isOptionalCallExpression() && path.node.optional) {
|
|
32
|
+
yield t.optionalCallExpression(
|
|
33
|
+
t.cloneNode(path.node.callee, true),
|
|
34
|
+
path.node.arguments.map((arg) => t.cloneNode(arg, true)),
|
|
35
|
+
/*optional*/ false,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import babel, { NodePath, type types as t } from '@babel/core';
|
|
2
|
+
import * as weaponRegex from 'weapon-regex';
|
|
3
|
+
|
|
4
|
+
import { NodeMutator } from './index.js';
|
|
5
|
+
|
|
6
|
+
const { types } = babel;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks that a string literal is an obvious regex string literal
|
|
10
|
+
* @param path The string literal to checks
|
|
11
|
+
* @example
|
|
12
|
+
* new RegExp("\\d{4}");
|
|
13
|
+
*/
|
|
14
|
+
function isObviousRegexString(path: NodePath<t.StringLiteral>) {
|
|
15
|
+
return (
|
|
16
|
+
path.parentPath.isNewExpression() &&
|
|
17
|
+
types.isIdentifier(path.parentPath.node.callee) &&
|
|
18
|
+
path.parentPath.node.callee.name === RegExp.name &&
|
|
19
|
+
path.parentPath.node.arguments[0] === path.node
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function getFlags(path: NodePath<t.NewExpression>): string | undefined {
|
|
24
|
+
if (types.isStringLiteral(path.node.arguments[1])) {
|
|
25
|
+
return path.node.arguments[1].value;
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const weaponRegexOptions: weaponRegex.MutationOptions = { mutationLevels: [1] };
|
|
31
|
+
|
|
32
|
+
export const regexMutator: NodeMutator = {
|
|
33
|
+
name: 'Regex',
|
|
34
|
+
|
|
35
|
+
*mutate(path) {
|
|
36
|
+
if (path.isRegExpLiteral()) {
|
|
37
|
+
for (const replacementPattern of mutatePattern(
|
|
38
|
+
path.node.pattern,
|
|
39
|
+
path.node.flags,
|
|
40
|
+
)) {
|
|
41
|
+
const replacement = types.regExpLiteral(
|
|
42
|
+
replacementPattern,
|
|
43
|
+
path.node.flags,
|
|
44
|
+
);
|
|
45
|
+
yield replacement;
|
|
46
|
+
}
|
|
47
|
+
} else if (path.isStringLiteral() && isObviousRegexString(path)) {
|
|
48
|
+
const flags = getFlags(path.parentPath as NodePath<t.NewExpression>);
|
|
49
|
+
for (const replacementPattern of mutatePattern(path.node.value, flags)) {
|
|
50
|
+
yield types.stringLiteral(replacementPattern);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
function mutatePattern(pattern: string, flags: string | undefined): string[] {
|
|
57
|
+
if (pattern.length) {
|
|
58
|
+
try {
|
|
59
|
+
return weaponRegex
|
|
60
|
+
.mutate(pattern, flags, weaponRegexOptions)
|
|
61
|
+
.map((mutant) => mutant.pattern);
|
|
62
|
+
} catch (err: any) {
|
|
63
|
+
console.error(
|
|
64
|
+
`[RegexMutator]: The Regex parser of weapon-regex couldn't parse this regex pattern: "${pattern}". Please report this issue at https://github.com/stryker-mutator/weapon-regex/issues. Inner error: ${err.message}`,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import babel, { type NodePath } from '@babel/core';
|
|
2
|
+
|
|
3
|
+
import { NodeMutator } from './node-mutator.js';
|
|
4
|
+
|
|
5
|
+
const { types } = babel;
|
|
6
|
+
|
|
7
|
+
export const stringLiteralMutator: NodeMutator = {
|
|
8
|
+
name: 'StringLiteral',
|
|
9
|
+
|
|
10
|
+
*mutate(path) {
|
|
11
|
+
if (path.isTemplateLiteral()) {
|
|
12
|
+
const replacement =
|
|
13
|
+
path.node.quasis.length === 1 &&
|
|
14
|
+
path.node.quasis[0].value.raw.length === 0
|
|
15
|
+
? 'Stryker was here!'
|
|
16
|
+
: '';
|
|
17
|
+
yield types.templateLiteral(
|
|
18
|
+
[types.templateElement({ raw: replacement })],
|
|
19
|
+
[],
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
if (path.isStringLiteral() && isValidParent(path)) {
|
|
23
|
+
yield types.stringLiteral(
|
|
24
|
+
path.node.value.length === 0 ? 'Stryker was here!' : '',
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function isValidParent(child: NodePath<babel.types.StringLiteral>): boolean {
|
|
31
|
+
const { parent } = child;
|
|
32
|
+
return !(
|
|
33
|
+
types.isImportDeclaration(parent) ||
|
|
34
|
+
types.isExportDeclaration(parent) ||
|
|
35
|
+
types.isImportOrExportDeclaration(parent) ||
|
|
36
|
+
types.isTSExternalModuleReference(parent) ||
|
|
37
|
+
types.isJSXAttribute(parent) ||
|
|
38
|
+
types.isExpressionStatement(parent) ||
|
|
39
|
+
types.isTSLiteralType(parent) ||
|
|
40
|
+
types.isObjectMethod(parent) ||
|
|
41
|
+
(types.isObjectProperty(parent) && parent.key === child.node) ||
|
|
42
|
+
(types.isClassProperty(parent) && parent.key === child.node) ||
|
|
43
|
+
(types.isCallExpression(parent) &&
|
|
44
|
+
types.isIdentifier(parent.callee, { name: 'require' })) ||
|
|
45
|
+
(types.isCallExpression(parent) &&
|
|
46
|
+
types.isIdentifier(parent.callee, { name: 'Symbol' })) ||
|
|
47
|
+
(types.isCallExpression(parent) && types.isImport(parent.callee))
|
|
48
|
+
);
|
|
49
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import babel from '@babel/core';
|
|
2
|
+
|
|
3
|
+
import { deepCloneNode } from '../util/index.js';
|
|
4
|
+
|
|
5
|
+
import { NodeMutator } from './index.js';
|
|
6
|
+
|
|
7
|
+
const { types } = babel;
|
|
8
|
+
|
|
9
|
+
enum UnaryOperator {
|
|
10
|
+
'+' = '-',
|
|
11
|
+
'-' = '+',
|
|
12
|
+
'~' = '',
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const unaryOperatorMutator: NodeMutator = {
|
|
16
|
+
name: 'UnaryOperator',
|
|
17
|
+
|
|
18
|
+
*mutate(path) {
|
|
19
|
+
if (
|
|
20
|
+
path.isUnaryExpression() &&
|
|
21
|
+
isSupported(path.node.operator) &&
|
|
22
|
+
path.node.prefix
|
|
23
|
+
) {
|
|
24
|
+
const mutatedOperator = UnaryOperator[path.node.operator];
|
|
25
|
+
const replacement = mutatedOperator.length
|
|
26
|
+
? types.unaryExpression(
|
|
27
|
+
mutatedOperator as '-' | '+',
|
|
28
|
+
deepCloneNode(path.node.argument),
|
|
29
|
+
)
|
|
30
|
+
: deepCloneNode(path.node.argument);
|
|
31
|
+
|
|
32
|
+
yield replacement;
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
function isSupported(operator: string): operator is keyof typeof UnaryOperator {
|
|
38
|
+
return Object.keys(UnaryOperator).includes(operator);
|
|
39
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import babel from '@babel/core';
|
|
2
|
+
|
|
3
|
+
import { deepCloneNode } from '../util/index.js';
|
|
4
|
+
|
|
5
|
+
import { NodeMutator } from './index.js';
|
|
6
|
+
|
|
7
|
+
const { types } = babel;
|
|
8
|
+
|
|
9
|
+
enum UpdateOperators {
|
|
10
|
+
'++' = '--',
|
|
11
|
+
'--' = '++',
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const updateOperatorMutator: NodeMutator = {
|
|
15
|
+
name: 'UpdateOperator',
|
|
16
|
+
|
|
17
|
+
*mutate(path) {
|
|
18
|
+
if (path.isUpdateExpression()) {
|
|
19
|
+
yield types.updateExpression(
|
|
20
|
+
UpdateOperators[path.node.operator],
|
|
21
|
+
deepCloneNode(path.node.argument),
|
|
22
|
+
path.node.prefix,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
|
|
3
|
+
import { AstByFormat, AstFormat } from '../syntax/index.js';
|
|
4
|
+
|
|
5
|
+
import { createParser as createJSParser } from './js-parser.js';
|
|
6
|
+
import { parseTS, parseTsx } from './ts-parser.js';
|
|
7
|
+
import { parse as htmlParse } from './html-parser.js';
|
|
8
|
+
import { parse as svelteParse } from './svelte-parser.js';
|
|
9
|
+
import { ParserOptions } from './parser-options.js';
|
|
10
|
+
|
|
11
|
+
export function createParser(
|
|
12
|
+
parserOptions: ParserOptions,
|
|
13
|
+
): <T extends AstFormat = AstFormat>(
|
|
14
|
+
code: string,
|
|
15
|
+
fileName: string,
|
|
16
|
+
formatOverride?: T,
|
|
17
|
+
) => Promise<AstByFormat[T]> {
|
|
18
|
+
const jsParse = createJSParser(parserOptions);
|
|
19
|
+
return async function parse<T extends AstFormat = AstFormat>(
|
|
20
|
+
code: string,
|
|
21
|
+
fileName: string,
|
|
22
|
+
formatOverride?: T,
|
|
23
|
+
): Promise<AstByFormat[T]> {
|
|
24
|
+
const format = getFormat(fileName, formatOverride);
|
|
25
|
+
if (!format) {
|
|
26
|
+
const ext = path.extname(fileName).toLowerCase();
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Unable to parse ${fileName}. No parser registered for ${ext}!`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
switch (format) {
|
|
32
|
+
case AstFormat.JS:
|
|
33
|
+
return jsParse(code, fileName) as Promise<AstByFormat[T]>;
|
|
34
|
+
case AstFormat.Tsx:
|
|
35
|
+
return parseTsx(code, fileName) as Promise<AstByFormat[T]>;
|
|
36
|
+
case AstFormat.TS:
|
|
37
|
+
return parseTS(code, fileName) as Promise<AstByFormat[T]>;
|
|
38
|
+
case AstFormat.Html:
|
|
39
|
+
return htmlParse(code, fileName, { parse }) as Promise<AstByFormat[T]>;
|
|
40
|
+
case AstFormat.Svelte:
|
|
41
|
+
return svelteParse(code, fileName, { parse }) as Promise<
|
|
42
|
+
AstByFormat[T]
|
|
43
|
+
>;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function getFormat(
|
|
49
|
+
fileName: string,
|
|
50
|
+
override?: AstFormat,
|
|
51
|
+
): AstFormat | undefined {
|
|
52
|
+
if (override) {
|
|
53
|
+
return override;
|
|
54
|
+
} else {
|
|
55
|
+
const ext = path.extname(fileName).toLowerCase();
|
|
56
|
+
switch (ext) {
|
|
57
|
+
case '.js':
|
|
58
|
+
case '.jsx':
|
|
59
|
+
case '.mjs':
|
|
60
|
+
case '.cjs':
|
|
61
|
+
return AstFormat.JS;
|
|
62
|
+
case '.mts':
|
|
63
|
+
case '.cts':
|
|
64
|
+
case '.ts':
|
|
65
|
+
return AstFormat.TS;
|
|
66
|
+
case '.tsx':
|
|
67
|
+
return AstFormat.Tsx;
|
|
68
|
+
case '.vue':
|
|
69
|
+
case '.html':
|
|
70
|
+
case '.htm':
|
|
71
|
+
return AstFormat.Html;
|
|
72
|
+
case '.svelte':
|
|
73
|
+
return AstFormat.Svelte;
|
|
74
|
+
default:
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HtmlAst,
|
|
3
|
+
AstFormat,
|
|
4
|
+
HtmlRootNode,
|
|
5
|
+
ScriptFormat,
|
|
6
|
+
AstByFormat,
|
|
7
|
+
ScriptAst,
|
|
8
|
+
} from '../syntax/index.js';
|
|
9
|
+
import type { Ast as NGAst } from 'angular-html-parser';
|
|
10
|
+
import { ParserContext } from './parser-context.js';
|
|
11
|
+
import { ParseError } from './parse-error.js';
|
|
12
|
+
|
|
13
|
+
const TSX_SCRIPT_TYPES = Object.freeze(['tsx', 'text/tsx']);
|
|
14
|
+
const TS_SCRIPT_TYPES = Object.freeze(['ts', 'text/typescript', 'typescript']);
|
|
15
|
+
const JS_SCRIPT_TYPES = Object.freeze([
|
|
16
|
+
'js',
|
|
17
|
+
'text/javascript',
|
|
18
|
+
'javascript',
|
|
19
|
+
'module',
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
/*
|
|
23
|
+
The parser implementation in this file is heavily based on prettier's html parser
|
|
24
|
+
https://github.com/prettier/prettier/blob/5a7162d0636a82c5862b9101b845af40918d22d1/src/language-html/parser-html.js
|
|
25
|
+
*/
|
|
26
|
+
export async function parse(
|
|
27
|
+
text: string,
|
|
28
|
+
originFileName: string,
|
|
29
|
+
context: ParserContext,
|
|
30
|
+
): Promise<HtmlAst> {
|
|
31
|
+
const root = await ngHtmlParser(text, originFileName, context);
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
originFileName,
|
|
35
|
+
rawContent: text,
|
|
36
|
+
format: AstFormat.Html,
|
|
37
|
+
root,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function ngHtmlParser(
|
|
42
|
+
text: string,
|
|
43
|
+
fileName: string,
|
|
44
|
+
parserContext: ParserContext,
|
|
45
|
+
): Promise<HtmlRootNode> {
|
|
46
|
+
const ngParser = await import('angular-html-parser');
|
|
47
|
+
|
|
48
|
+
const { rootNodes, errors } = ngParser.parse(text, {
|
|
49
|
+
canSelfClose: true,
|
|
50
|
+
allowHtmComponentClosingTags: true,
|
|
51
|
+
isTagNameCaseSensitive: true,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
if (errors.length !== 0) {
|
|
55
|
+
throw new ParseError(
|
|
56
|
+
errors[0].msg,
|
|
57
|
+
fileName,
|
|
58
|
+
toSourceLocation(errors[0].span.start),
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
const scriptsAsPromised: Array<Promise<ScriptAst>> = [];
|
|
62
|
+
ngParser.visitAll(
|
|
63
|
+
new (class extends ngParser.RecursiveVisitor {
|
|
64
|
+
public override visitElement(el: NGAst.Element, context: unknown): void {
|
|
65
|
+
const scriptFormat = getScriptType(el);
|
|
66
|
+
if (scriptFormat) {
|
|
67
|
+
scriptsAsPromised.push(parseScript(el, scriptFormat));
|
|
68
|
+
}
|
|
69
|
+
super.visitElement(el, context);
|
|
70
|
+
}
|
|
71
|
+
})(),
|
|
72
|
+
rootNodes,
|
|
73
|
+
);
|
|
74
|
+
const scripts = await Promise.all(scriptsAsPromised);
|
|
75
|
+
const root: HtmlRootNode = {
|
|
76
|
+
scripts,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return root;
|
|
80
|
+
|
|
81
|
+
async function parseScript<T extends ScriptFormat>(
|
|
82
|
+
el: NGAst.Element,
|
|
83
|
+
scriptFormat: T,
|
|
84
|
+
): Promise<AstByFormat[T]> {
|
|
85
|
+
const content = text.substring(
|
|
86
|
+
el.startSourceSpan.end.offset,
|
|
87
|
+
el.endSourceSpan!.start.offset,
|
|
88
|
+
);
|
|
89
|
+
const ast = await parserContext.parse(content, fileName, scriptFormat);
|
|
90
|
+
if (ast) {
|
|
91
|
+
const offset = el.startSourceSpan.end;
|
|
92
|
+
ast.root.start! += offset.offset;
|
|
93
|
+
ast.root.end! += offset.offset;
|
|
94
|
+
return {
|
|
95
|
+
...ast,
|
|
96
|
+
offset: {
|
|
97
|
+
column: offset.offset,
|
|
98
|
+
line: offset.line,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
return ast;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function toSourceLocation({ line, col }: { line: number; col: number }): {
|
|
107
|
+
line: number;
|
|
108
|
+
column: number;
|
|
109
|
+
} {
|
|
110
|
+
// Offset line with 1, since ngHtmlParser is 0-based
|
|
111
|
+
return { line: line + 1, column: col };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function getScriptType(element: NGAst.Element): ScriptFormat | undefined {
|
|
115
|
+
if (element.name === 'script') {
|
|
116
|
+
const containsSrc = element.attrs.some((attr) => attr.name === 'src');
|
|
117
|
+
if (!containsSrc) {
|
|
118
|
+
const type =
|
|
119
|
+
element.attrs.find((attr) => attr.name === 'type') ??
|
|
120
|
+
element.attrs.find((attr) => attr.name === 'lang');
|
|
121
|
+
if (type) {
|
|
122
|
+
const typeToLower = type.value.toLowerCase();
|
|
123
|
+
if (TSX_SCRIPT_TYPES.includes(typeToLower)) {
|
|
124
|
+
return AstFormat.Tsx;
|
|
125
|
+
}
|
|
126
|
+
if (TS_SCRIPT_TYPES.includes(typeToLower)) {
|
|
127
|
+
return AstFormat.TS;
|
|
128
|
+
}
|
|
129
|
+
if (JS_SCRIPT_TYPES.includes(typeToLower)) {
|
|
130
|
+
return AstFormat.JS;
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
return AstFormat.JS;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ParserPlugin } from '@babel/parser';
|
|
2
|
+
import babel from '@babel/core';
|
|
3
|
+
|
|
4
|
+
import { AstFormat, JSAst } from '../syntax/index.js';
|
|
5
|
+
|
|
6
|
+
import { ParserOptions } from './parser-options.js';
|
|
7
|
+
|
|
8
|
+
const { types, parseAsync } = babel;
|
|
9
|
+
|
|
10
|
+
const defaultPlugins: ParserPlugin[] = [
|
|
11
|
+
'doExpressions',
|
|
12
|
+
'objectRestSpread',
|
|
13
|
+
'classProperties',
|
|
14
|
+
'exportDefaultFrom',
|
|
15
|
+
'exportNamespaceFrom',
|
|
16
|
+
'asyncGenerators',
|
|
17
|
+
'functionBind',
|
|
18
|
+
'functionSent',
|
|
19
|
+
'dynamicImport',
|
|
20
|
+
'numericSeparator',
|
|
21
|
+
'importMeta',
|
|
22
|
+
'optionalCatchBinding',
|
|
23
|
+
'optionalChaining',
|
|
24
|
+
'classPrivateProperties',
|
|
25
|
+
['pipelineOperator', { proposal: 'minimal' }],
|
|
26
|
+
'nullishCoalescingOperator',
|
|
27
|
+
'bigInt',
|
|
28
|
+
'throwExpressions',
|
|
29
|
+
'logicalAssignment',
|
|
30
|
+
'classPrivateMethods',
|
|
31
|
+
'v8intrinsic',
|
|
32
|
+
'partialApplication',
|
|
33
|
+
['decorators', { decoratorsBeforeExport: false }],
|
|
34
|
+
'jsx',
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
export function createParser({ plugins: pluginsOverride }: ParserOptions) {
|
|
38
|
+
return async function parse(text: string, fileName: string): Promise<JSAst> {
|
|
39
|
+
const ast = await parseAsync(text, {
|
|
40
|
+
parserOpts: {
|
|
41
|
+
plugins: [...(pluginsOverride ?? defaultPlugins)] as ParserPlugin[],
|
|
42
|
+
},
|
|
43
|
+
filename: fileName,
|
|
44
|
+
sourceType: 'module',
|
|
45
|
+
});
|
|
46
|
+
if (types.isProgram(ast)) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
`Expected ${fileName} to contain a babel.types.file, but was a program`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
originFileName: fileName,
|
|
53
|
+
rawContent: text,
|
|
54
|
+
format: AstFormat.JS,
|
|
55
|
+
root: ast!,
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Position } from '@stryker-mutator/api/core';
|
|
2
|
+
|
|
3
|
+
export class ParseError extends Error {
|
|
4
|
+
constructor(message: string, fileName: string, location: Position) {
|
|
5
|
+
super(
|
|
6
|
+
`Parse error in ${fileName} (${location.line}:${location.column}) ${message}`,
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
}
|