Skip to content

Commit

Permalink
fix(vue): only mutate Vue files with <script> blocks (#1540)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo van Rijswijk authored and simondel committed May 16, 2019
1 parent d281e42 commit ee4d27c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
26 changes: 14 additions & 12 deletions packages/vue-mutator/src/VueMutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ export default class VueMutator implements Mutator {
inputFiles.forEach(file => {
if (file.name.endsWith('.vue')) {
const script = compiler.parseComponent(file.textContent).script;
const { mutator, extension } = this.getVueScriptMutatorAndExtension(script);
const vueFile = new File(
file.name + extension,
file.textContent.substring(script.start, script.end)
);
const vueMutants = mutator.mutate([vueFile]);
vueMutants.forEach(mutant => {
mutant.fileName = file.name;
mutant.range[0] += script.start;
mutant.range[1] += script.start;
});
mutants.push(...vueMutants);
if (script) { // Vue file must have <script></script> tag to be mutated
const { mutator, extension } = this.getVueScriptMutatorAndExtension(script);
const vueFile = new File(
file.name + extension,
file.textContent.substring(script.start, script.end)
);
const vueMutants = mutator.mutate([vueFile]);
vueMutants.forEach(mutant => {
mutant.fileName = file.name;
mutant.range[0] += script.start;
mutant.range[1] += script.start;
});
mutants.push(...vueMutants);
}
} else {
const mutator = this.getMutator(file);
mutants.push(...mutator.mutate([file]));
Expand Down
15 changes: 15 additions & 0 deletions packages/vue-mutator/test/unit/VueMutator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,21 @@ describe('VueMutator', () => {
expect(() => sut.mutate(files)).throws(`Found unsupported language attribute 'lang="coffeescript"' on a <script> block.`);
});

it('should not mutate a .vue file without a <script> block', () => {
mutators = { javascript: stubJavaScriptMutator };
const vueFile = new File('Component.vue',
`<template>
<span id="msg">{{ message }}</span>
</template>`);
const files = [vueFile];
const sut = createSut();

const result = sut.mutate(files);

expect(result).to.be.empty;
expect(stubJavaScriptMutator.mutate).not.calledWith([vueFile]);
});

it('should generate correct vue mutants', () => {
mutators = { javascript: stubJavaScriptMutator };

Expand Down

0 comments on commit ee4d27c

Please sign in to comment.