Skip to content

Commit

Permalink
fix(eslint): fix --no-fix flag when linting with typescript plugin (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensche authored and yyx990803 committed Apr 26, 2018
1 parent 65ee2fa commit 83171e4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
32 changes: 31 additions & 1 deletion packages/@vue/cli-plugin-eslint/__tests__/eslintPlugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jest.setTimeout(30000)
jest.setTimeout(35000)

const path = require('path')
const { linkBin } = require('@vue/cli/lib/util/linkBin')
Expand Down Expand Up @@ -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(';')
})
4 changes: 0 additions & 4 deletions packages/@vue/cli-plugin-eslint/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(';')
})
2 changes: 1 addition & 1 deletion packages/@vue/cli-plugin-typescript/lib/tslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down

0 comments on commit 83171e4

Please sign in to comment.