fix(eslint): fix --no-fix flag when linting with typescript plugin (#1115)

This commit is contained in:
Derek Henscheid
2018-04-26 14:20:07 -05:00
committed by Evan You
parent 65ee2fad24
commit 83171e4029
4 changed files with 67 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
jest.setTimeout(30000)
jest.setTimeout(35000)
const path = require('path')
const { linkBin } = require('@vue/cli/lib/util/linkBin')
@@ -90,3 +90,33 @@ test('should work', async () => {
await donePromise
})
test('should not fix with --no-fix option', async () => {
const project = await create('eslint-nofix', {
plugins: {
'@vue/cli-plugin-babel': {},
'@vue/cli-plugin-eslint': {
config: 'airbnb',
lintOn: 'commit'
}
}
})
const { read, write, run } = project
// should've applied airbnb autofix
const main = await read('src/main.js')
expect(main).toMatch(';')
// remove semicolons
const updatedMain = main.replace(/;/g, '')
await write('src/main.js', updatedMain)
// lint with no fix should fail
try {
await run('vue-cli-service lint --no-fix')
} catch (e) {
expect(e.code).toBe(1)
expect(e.failed).toBeTruthy()
}
// files should not have been fixed
expect(await read('src/main.js')).not.toMatch(';')
})

View File

@@ -7,10 +7,6 @@ module.exports = function lint (args = {}, api) {
const { log, done } = require('@vue/cli-shared-utils')
const files = args._ && args._.length ? args._ : ['src', 'tests', '*.js']
if (args['no-fix']) {
args.fix = false
delete args['no-fix']
}
const config = Object.assign({}, options, {
fix: true,
cwd

View File

@@ -32,3 +32,38 @@ test('should work', async () => {
// test if tslint is fixing vue files properly
expect(lintedApp).toBe(app)
})
test('should not fix with --no-fix option', async () => {
const project = await create('ts-lint-nofix', {
plugins: {
'@vue/cli-plugin-typescript': {
tsLint: true
}
}
})
const { read, write, run } = project
const main = await read('src/main.ts')
expect(main).toMatch(';')
const app = await read('src/App.vue')
expect(main).toMatch(';')
// remove semicolons
const updatedMain = main.replace(/;/g, '')
await write('src/main.ts', updatedMain)
// for Vue file, only remove semis in script section
const updatedApp = app.replace(/<script(.|\n)*\/script>/, $ => {
return $.replace(/;/g, '')
})
await write('src/App.vue', updatedApp)
// lint with no fix should fail
try {
await run('vue-cli-service lint --no-fix')
} catch (e) {
expect(e.code).toBe(1)
expect(e.failed).toBeTruthy()
}
// files should not have been fixed
expect(await read('src/main.ts')).not.toMatch(';')
expect((await read('src/App.vue')).match(/<script(.|\n)*\/script>/)[1]).not.toMatch(';')
})

View File

@@ -8,7 +8,7 @@ module.exports = function lint (args = {}, api, silent) {
const vueCompiler = require('vue-template-compiler')
const options = {
fix: !args['no-fix'],
fix: args['fix'] !== false,
formatter: args.format || 'codeFrame',
formattersDirectory: args['formatters-dir'],
rulesDirectory: args['rules-dir']