-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(instrumenter): add parsers (#2222)
Add parsers to the instrumenter package. Add support for: * Parsing `*.js`, `*.tsx`, `*.html` and `*.vue` files. * Load custom babel config with `babel.config` format and `.babelrc` format.
- Loading branch information
Showing
44 changed files
with
8,287 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { types } from '@babel/core'; | ||
import generate from '@babel/generator'; | ||
|
||
import { isLineBreak } from './util/char-helpers'; | ||
|
||
export interface NodeMutation { | ||
replacement: types.Node; | ||
original: types.Node; | ||
} | ||
export interface NamedNodeMutation extends NodeMutation { | ||
mutatorName: string; | ||
} | ||
|
||
export class Mutant { | ||
constructor(public id: number, public original: types.Node, public replacement: types.Node, public fileName: string, public mutatorName: string) {} | ||
|
||
public originalLines(originalText: string): string { | ||
const [startIndex, endIndex] = this.getMutationLineIndexes(originalText); | ||
return originalText.substring(startIndex, endIndex); | ||
} | ||
|
||
private getMutationLineIndexes(originalText: string): [number, number] { | ||
let startIndexLines = this.original.start!; | ||
let endIndexLines = this.original.end!; | ||
while (startIndexLines > 0 && !isLineBreak(originalText.charCodeAt(startIndexLines - 1))) { | ||
startIndexLines--; | ||
} | ||
while (endIndexLines < originalText.length && !isLineBreak(originalText.charCodeAt(endIndexLines))) { | ||
endIndexLines++; | ||
} | ||
return [startIndexLines, endIndexLines]; | ||
} | ||
|
||
public mutatedLines(originalText: string): string { | ||
const [startIndex, endIndex] = this.getMutationLineIndexes(originalText); | ||
return `${originalText.substring(startIndex, this.original.start!)}${generate(this.replacement).code}${originalText.substring( | ||
this.original.end!, | ||
endIndex | ||
)}`; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
packages/instrumenter/src/mutators/arithmetic-operator-mutator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { NodePath, types } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from './node-mutator'; | ||
|
||
type Operator = | ||
| '+' | ||
| '-' | ||
| '/' | ||
| '%' | ||
| '*' | ||
| '**' | ||
| '&' | ||
| '|' | ||
| '>>' | ||
| '>>>' | ||
| '<<' | ||
| '^' | ||
| '==' | ||
| '===' | ||
| '!=' | ||
| '!==' | ||
| 'in' | ||
| 'instanceof' | ||
| '>' | ||
| '<' | ||
| '>=' | ||
| '<='; | ||
|
||
export class ArithmeticOperatorMutator implements NodeMutator { | ||
private readonly operators: { | ||
[op: string]: Operator | undefined; | ||
} = Object.freeze({ | ||
'+': '-', | ||
'-': '+', | ||
'*': '/', | ||
'/': '*', | ||
'%': '*', | ||
} as const); | ||
|
||
public name = 'ArithmeticOperator'; | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
if (types.isBinaryExpression(path.node)) { | ||
const mutatedOperator = this.operators[path.node.operator]; | ||
if (mutatedOperator) { | ||
const replacement = types.cloneNode(path.node, false); | ||
replacement.operator = mutatedOperator; | ||
return [{ original: path.node, replacement }]; | ||
} | ||
} | ||
return []; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/instrumenter/src/mutators/block-statement-mutator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { types, NodePath } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from './node-mutator'; | ||
|
||
export class BlockStatementMutator implements NodeMutator { | ||
public name = 'BlockStatement'; | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
if (path.isBlockStatement()) { | ||
const replacement = types.cloneNode(path.node, false); | ||
replacement.body = []; | ||
return [{ original: path.node, replacement }]; | ||
} else { | ||
return []; | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
packages/instrumenter/src/mutators/conditional-expression-mutator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { NodePath, types } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from './node-mutator'; | ||
|
||
export class ConditionalExpressionMutator implements NodeMutator { | ||
private readonly validOperators: string[] = ['!=', '!==', '&&', '<', '<=', '==', '===', '>', '>=', '||']; | ||
|
||
public name = 'ConditionalExpression'; | ||
|
||
private hasValidParent(node: NodePath): boolean { | ||
return ( | ||
!node.parent || | ||
!( | ||
types.isForStatement(node.parent) || | ||
types.isWhileStatement(node.parent) || | ||
types.isIfStatement(node.parent) || | ||
types.isDoWhileStatement(node.parent) | ||
) | ||
); | ||
} | ||
|
||
private isValidOperator(operator: string): boolean { | ||
return this.validOperators.includes(operator); | ||
} | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
if ((path.isBinaryExpression() || path.isLogicalExpression()) && this.hasValidParent(path) && this.isValidOperator(path.node.operator)) { | ||
return [ | ||
{ original: path.node, replacement: types.booleanLiteral(true) }, | ||
{ original: path.node, replacement: types.booleanLiteral(false) }, | ||
]; | ||
} else if (path.isDoWhileStatement() || path.isWhileStatement()) { | ||
return [{ original: path.node.test, replacement: types.booleanLiteral(false) }]; | ||
} else if (path.isForStatement()) { | ||
if (!path.node.test) { | ||
const replacement = types.cloneNode(path.node, /* deep */ false); | ||
replacement.test = types.booleanLiteral(false); | ||
return [{ original: path.node, replacement }]; | ||
} else { | ||
return [{ original: path.node.test, replacement: types.booleanLiteral(false) }]; | ||
} | ||
} else if (path.isIfStatement()) { | ||
return [ | ||
// raw string mutations in the `if` condition | ||
{ original: path.node.test, replacement: types.booleanLiteral(true) }, | ||
{ original: path.node.test, replacement: types.booleanLiteral(false) }, | ||
]; | ||
} else if ( | ||
path.isSwitchCase() && | ||
// if not a fallthrough case | ||
path.node.consequent.length > 0 | ||
) { | ||
const replacement = types.cloneNode(path.node); | ||
replacement.consequent = []; | ||
return [{ original: path.node, replacement }]; | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NodePath } from '@babel/core'; | ||
import { flatMap } from '@stryker-mutator/util'; | ||
|
||
import { NamedNodeMutation } from '../mutant'; | ||
|
||
import { ArithmeticOperatorMutator } from './arithmetic-operator-mutator'; | ||
import { NodeMutator } from './node-mutator'; | ||
import { BlockStatementMutator } from './block-statement-mutator'; | ||
import { ConditionalExpressionMutator } from './conditional-expression-mutator'; | ||
import { StringLiteralMutator } from './string-literal-mutator'; | ||
|
||
export * from './node-mutator'; | ||
export const mutators: NodeMutator[] = [ | ||
new ArithmeticOperatorMutator(), | ||
new BlockStatementMutator(), | ||
new ConditionalExpressionMutator(), | ||
new StringLiteralMutator(), | ||
]; | ||
export const mutate = (node: NodePath): NamedNodeMutation[] => { | ||
return flatMap(mutators, (mutator) => mutator.mutate(node).map((nodeMutation) => ({ ...nodeMutation, mutatorName: mutator.name }))); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NodePath } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
export interface NodeMutator { | ||
mutate(path: NodePath): NodeMutation[]; | ||
readonly name: string; | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/instrumenter/src/mutators/string-literal-mutator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { types, NodePath } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from './node-mutator'; | ||
|
||
export class StringLiteralMutator implements NodeMutator { | ||
public name = 'StringLiteral'; | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
if (path.isTemplateLiteral()) { | ||
if (path.node.quasis.length === 1 && path.node.quasis[0].value.raw.length === 0) { | ||
return [ | ||
{ | ||
original: path.node, | ||
replacement: types.templateLiteral([types.templateElement({ raw: 'Stryker was here!' })], []), | ||
}, | ||
]; | ||
} else { | ||
return [ | ||
{ | ||
original: path.node, | ||
replacement: types.templateLiteral([types.templateElement({ raw: '' })], []), | ||
}, | ||
]; | ||
} | ||
} else if (this.isDeclarationOrJSX(path.parent) && path.isStringLiteral()) { | ||
return [ | ||
{ | ||
original: path.node, | ||
replacement: types.stringLiteral(path.node.value.length === 0 ? 'Stryker was here!' : ''), | ||
}, | ||
]; | ||
} else { | ||
return []; | ||
} | ||
} | ||
|
||
private isDeclarationOrJSX(parent?: types.Node): boolean { | ||
return !types.isImportDeclaration(parent) && !types.isExportDeclaration(parent) && !types.isJSXAttribute(parent); | ||
} | ||
} |
Oops, something went wrong.