Skip to content

Commit

Permalink
fix: eslint + airbnb compat with TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 16, 2018
1 parent 9f5d0b9 commit d391e47
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"env": {
"mocha": true,
"cypress/globals": true
},
"rules": {
"strict": "off"
}
}
<%_ } _%>
5 changes: 3 additions & 2 deletions packages/@vue/cli-plugin-pwa/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ module.exports = api => {
api.render('./template')

api.postProcessFiles(files => {
const file = files['src/main.ts']
const isTS = 'src/main.ts' in files
const file = isTS
? 'src/main.ts'
: 'src/main.js'
const main = files[file]
if (main) {
// inject import for registerServiceWorker script into main.js
const lines = main.split(/\r?\n/g).reverse()
const lastImportIndex = lines.findIndex(line => line.match(/^import/))
lines[lastImportIndex] += `\nimport './registerServiceWorker'`
lines[lastImportIndex] += `\nimport './registerServiceWorker${isTS ? `.ts` : ``}'`
files[file] = lines.reverse().join('\n')
}
})
Expand Down
3 changes: 2 additions & 1 deletion packages/@vue/cli-plugin-typescript/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ module.exports = (api, {
const jsRE = /\.js$/
const excludeRE = /^test\/e2e\/|\.config\.js$/
const convertLintFlags = require('../lib/convertLintFlags')
const convertImports = require('../lib/convertImports')
api.postProcessFiles(files => {
for (const file in files) {
if (jsRE.test(file) && !excludeRE.test(file)) {
const tsFile = file.replace(jsRE, '.ts')
if (!files[tsFile]) {
files[tsFile] = convertLintFlags(files[file])
files[tsFile] = convertLintFlags(convertImports(files[file]))
}
delete files[file]
}
Expand Down
16 changes: 16 additions & 0 deletions packages/@vue/cli-plugin-typescript/lib/convertImports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = function convertImports (file) {
return file
.replace(/import (.*from )?('([^']+)'|"([^"]+)")/g, ($1, $2, $3, $4) => {
const isRelative = $4.charAt(0) === '.'
const isExtensionless = !/\.\w+$/.test($4)
const isJS = /\.js$/.test($4)
const replaced = isRelative
? isExtensionless
? $3.replace($4, `${$4}.ts`)
: isJS
? $3.replace(/\.js('|")$/, '.ts$1')
: $3
: $3
return `import ${$2 || ''}${replaced}`
})
}
5 changes: 4 additions & 1 deletion packages/@vue/cli-plugin-unit-jest/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ module.exports = api => {
if (api.hasPlugin('eslint')) {
api.render(files => {
files['test/unit/.eslintrc'] = JSON.stringify({
env: { jest: true }
env: { jest: true },
rules: {
'import/no-extraneous-dependencies': 'off'
}
}, null, 2)
})
}
Expand Down
5 changes: 4 additions & 1 deletion packages/@vue/cli-plugin-unit-mocha/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ module.exports = api => {
if (api.hasPlugin('eslint')) {
api.render(files => {
files['test/unit/.eslintrc'] = JSON.stringify({
env: { mocha: true }
env: { mocha: true },
rules: {
'import/no-extraneous-dependencies': 'off'

This comment has been minimized.

Copy link
@wanecek

wanecek Jun 26, 2018

@yyx990803 Is there something I can read to understand why this is needed? :) What's the reason for wanting extraneous dependencies in tests? Is it to allow importing typescript declarations, or something entirely different? :)

}
}, null, 2)
})
}
Expand Down

0 comments on commit d391e47

Please sign in to comment.