Skip to content

Commit 83171e4

Browse files
dhenscheyyx990803
authored andcommitted
fix(eslint): fix --no-fix flag when linting with typescript plugin (#1115)
1 parent 65ee2fa commit 83171e4

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

packages/@vue/cli-plugin-eslint/__tests__/eslintPlugin.spec.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
jest.setTimeout(30000)
1+
jest.setTimeout(35000)
22

33
const path = require('path')
44
const { linkBin } = require('@vue/cli/lib/util/linkBin')
@@ -90,3 +90,33 @@ test('should work', async () => {
9090

9191
await donePromise
9292
})
93+
94+
test('should not fix with --no-fix option', async () => {
95+
const project = await create('eslint-nofix', {
96+
plugins: {
97+
'@vue/cli-plugin-babel': {},
98+
'@vue/cli-plugin-eslint': {
99+
config: 'airbnb',
100+
lintOn: 'commit'
101+
}
102+
}
103+
})
104+
const { read, write, run } = project
105+
// should've applied airbnb autofix
106+
const main = await read('src/main.js')
107+
expect(main).toMatch(';')
108+
// remove semicolons
109+
const updatedMain = main.replace(/;/g, '')
110+
await write('src/main.js', updatedMain)
111+
112+
// lint with no fix should fail
113+
try {
114+
await run('vue-cli-service lint --no-fix')
115+
} catch (e) {
116+
expect(e.code).toBe(1)
117+
expect(e.failed).toBeTruthy()
118+
}
119+
120+
// files should not have been fixed
121+
expect(await read('src/main.js')).not.toMatch(';')
122+
})

packages/@vue/cli-plugin-eslint/lint.js

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ module.exports = function lint (args = {}, api) {
77
const { log, done } = require('@vue/cli-shared-utils')
88

99
const files = args._ && args._.length ? args._ : ['src', 'tests', '*.js']
10-
if (args['no-fix']) {
11-
args.fix = false
12-
delete args['no-fix']
13-
}
1410
const config = Object.assign({}, options, {
1511
fix: true,
1612
cwd

packages/@vue/cli-plugin-typescript/__tests__/tsPluginTSLint.spec.js

+35
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,38 @@ test('should work', async () => {
3232
// test if tslint is fixing vue files properly
3333
expect(lintedApp).toBe(app)
3434
})
35+
36+
test('should not fix with --no-fix option', async () => {
37+
const project = await create('ts-lint-nofix', {
38+
plugins: {
39+
'@vue/cli-plugin-typescript': {
40+
tsLint: true
41+
}
42+
}
43+
})
44+
const { read, write, run } = project
45+
const main = await read('src/main.ts')
46+
expect(main).toMatch(';')
47+
const app = await read('src/App.vue')
48+
expect(main).toMatch(';')
49+
// remove semicolons
50+
const updatedMain = main.replace(/;/g, '')
51+
await write('src/main.ts', updatedMain)
52+
// for Vue file, only remove semis in script section
53+
const updatedApp = app.replace(/<script(.|\n)*\/script>/, $ => {
54+
return $.replace(/;/g, '')
55+
})
56+
await write('src/App.vue', updatedApp)
57+
58+
// lint with no fix should fail
59+
try {
60+
await run('vue-cli-service lint --no-fix')
61+
} catch (e) {
62+
expect(e.code).toBe(1)
63+
expect(e.failed).toBeTruthy()
64+
}
65+
66+
// files should not have been fixed
67+
expect(await read('src/main.ts')).not.toMatch(';')
68+
expect((await read('src/App.vue')).match(/<script(.|\n)*\/script>/)[1]).not.toMatch(';')
69+
})

packages/@vue/cli-plugin-typescript/lib/tslint.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = function lint (args = {}, api, silent) {
88
const vueCompiler = require('vue-template-compiler')
99

1010
const options = {
11-
fix: !args['no-fix'],
11+
fix: args['fix'] !== false,
1212
formatter: args.format || 'codeFrame',
1313
formattersDirectory: args['formatters-dir'],
1414
rulesDirectory: args['rules-dir']

0 commit comments

Comments
 (0)