Skip to content

Commit bc60f5d

Browse files
committed
Ignore require calls and logging calls in string literals mutator
1 parent 13c2d06 commit bc60f5d

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

example-module/src/killed-dep.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'hello'

example-module/src/killed.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const string = require('./killed-dep')
2+
13
module.exports = {
24
deletion () {
35
return true
@@ -47,7 +49,7 @@ module.exports = {
4749
},
4850
stringLiterals: {
4951
hello () {
50-
return 'hello'
52+
return string
5153
},
5254
empty () {
5355
return ''

example-module/src/survived.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function other (a = 0) {
22
for (let i = 0; false; ) {
33
}
4-
console.log()
4+
console.log('')
55
a++
66
a--
77
}

src/mutators/stringLiteralsMutator.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,27 @@ const lineDiff = require('../util/lineDiff')
1313
module.exports = async function stringLiteralsMutator ({mutodeInstance, filePath, lines, queue, ast}) {
1414
debug('Running string literals mutator on %s', filePath)
1515

16-
walk.simple(ast, {
17-
StringLiteral (node) {
16+
walk.ancestor(ast, {
17+
StringLiteral (node, state, ancestors) {
1818
const line = node.loc.start.line
1919
const lineContent = lines[line - 1]
2020

21+
if (ancestors.length >= 2) {
22+
const ancestor = ancestors[ancestors.length - 2]
23+
if (ancestor.type && ancestor.type === 'CallExpression' && ancestor.callee) {
24+
if (ancestor.callee.type === 'MemberExpression' && ancestor.callee.object.name === 'console') return
25+
if (ancestor.callee.name) {
26+
switch (ancestor.callee.name) {
27+
case 'require':
28+
case 'debug':
29+
return
30+
default:
31+
break
32+
}
33+
}
34+
}
35+
}
36+
2137
if (node.value.length !== 0) {
2238
const mutantLineContent = lineContent.substr(0, node.loc.start.column) +
2339
node.extra.raw.replace(node.value, '') +

test/exampleModule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if (process.env.MUTODE_CONCURRENCY) opts.concurrency = process.env.MUTODE_CONCUR
1212

1313
test.serial('Exmaple module - killed', async t => {
1414
const testOpts = Object.assign({
15-
paths: 'src/killed.js'
15+
paths: ['src/killed.js', 'src/killed-dep.js']
1616
}, opts)
1717
let mutode = new Mutode(testOpts)
1818
await mutode.run()

0 commit comments

Comments
 (0)