Skip to content

Commit

Permalink
Merge pull request #138 from ktsn/vue-fix-blank-line
Browse files Browse the repository at this point in the history
Avoid false positive of no-consecutive-blank-lines TSLint rule in Vue file
  • Loading branch information
johnnyreilly authored Aug 8, 2018
2 parents eca9b1e + aa8d482 commit 68ec1b6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/VueProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class VueProgram {
}
}

private static resolveScriptBlock(content: string): ResolvedScript {
static resolveScriptBlock(content: string): ResolvedScript {
// We need to import vue-template-compiler lazily because it cannot be included it
// as direct dependency because it is an optional dependency of fork-ts-checker-webpack-plugin.
// Since its version must not mismatch with user-installed Vue.js,
Expand All @@ -209,7 +209,7 @@ class VueProgram {
}

const { script } = parser.parseComponent(content, {
pad: 'line'
pad: 'space'
});

// No <script> block
Expand Down Expand Up @@ -240,9 +240,15 @@ class VueProgram {
};
}

// Pad blank lines to retain diagnostics location
// We need to prepend `//` for each line to avoid
// false positive of no-consecutive-blank-lines TSLint rule
const offset = content.slice(0, script.start).split(/\r?\n/g).length;
const paddedContent = Array(offset).join('//\n') + script.content.slice(script.start);

return {
scriptKind,
content: script.content
content: paddedContent
};
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/integration/vue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ describe('[INTEGRATION] vue', function () {
});
});

it('should not report no-consecutive-blank-lines tslint rule', function (callback) {
createCompiler({ tslint: true, vue: true });

compiler.run(function(error, stats) {
stats.compilation.warnings.forEach(function(warning) {
expect(warning.rawMessage).to.not.match(/no-consecutive-blank-lines/);
});
callback();
});
});

it('should find syntactic errors when checkSyntacticErrors is true', function (callback) {
createCompiler({ tslint: true, vue: true, checkSyntacticErrors: true });

Expand Down
46 changes: 46 additions & 0 deletions test/unit/VueProgram.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var ts = require('typescript');
var describe = require('mocha').describe;
var it = require('mocha').it;
var expect = require('chai').expect;
Expand Down Expand Up @@ -64,4 +65,49 @@ describe('[UNIT] VueProgram', function () {
resolvedModuleName = VueProgram.resolveNonTsModuleName(moduleName, containingFile, basedir, options);
expect(resolvedModuleName).to.be.equal('/baseurl3/src1/src2/test.vue');
});

it('should extract script block', function() {
var content = [
'<script lang="ts">',
'import Vue from "vue";',
'export default Vue.extend({});',
'</script>'
].join('\n');

var result = VueProgram.resolveScriptBlock(content);

expect(result.scriptKind).to.be.equal(ts.ScriptKind.TS);
expect(result.content).to.be.equal([
'',
'import Vue from "vue";',
'export default Vue.extend({});',
''
].join('\n'));
});

it('should pad lines', function() {
var content = [
'<template>',
' <p>Hello</p>',
'</template>',
'',
'<script lang="ts">',
'import Vue from "vue";',
'export default Vue.extend({});',
'</script>'
].join('\n');

var result = VueProgram.resolveScriptBlock(content);

expect(result.content).to.be.equal([
'//',
'//',
'//',
'//',
'',
'import Vue from "vue";',
'export default Vue.extend({});',
''
].join('\n'));
});
});

0 comments on commit 68ec1b6

Please sign in to comment.