Skip to content

Commit

Permalink
fix: disable projectService if and only if necessary
Browse files Browse the repository at this point in the history
The previous implementation accidentally enabled it when `mayHaveJsx` is
`false`, which is totally unnecessary and dragged down the performance.
  • Loading branch information
haoqunjiang committed Oct 4, 2024
1 parent a51bf76 commit 52fbcc0
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,37 @@ export default function createConfig({
extends: configNamesToExtend = ['recommended'],
supportedScriptLangs = { ts: true, tsx: false, js: false, jsx: false },
}: ConfigOptions = {}): ConfigArray {
const mayHaveJsx = supportedScriptLangs.jsx || supportedScriptLangs.tsx
const mayHaveJsxInSfc = supportedScriptLangs.jsx || supportedScriptLangs.tsx
const needsTypeAwareLinting = configNamesToExtend.some(name =>
name.endsWith('-type-checked'),
)

// Type-aware linting is in conflict with JSX syntax in `.vue` files
// [!NOTE TO MYSELF] There's room for improvement here.
// We could disable type-aware linting *only* for `.vue` files with JSX syntax.
// Then the following error can be changed to a warning.
if (needsTypeAwareLinting && mayHaveJsxInSfc) {
throw new Error(
'Type-aware linting is not supported in Vue SFCs with JSX syntax. ' +
'Please disable type-aware linting or set `supportedScriptLangs.jsx` ' +
'and `supportedScriptLangs.tsx` to `false`.',
)
}

const noProjectServiceForVue = mayHaveJsxInSfc
const projectServiceConfigs: ConfigArray = []

if (noProjectServiceForVue) {
projectServiceConfigs.push({
name: 'vue-typescript/project-service-for-vue',
files: ['*.vue', '**/*.vue'],
languageOptions: {
parserOptions: {
projectService: false,
},
},
})
}

return tseslint.config(
...configNamesToExtend
Expand All @@ -42,21 +72,19 @@ export default function createConfig({
parser: {
// Fallback to espree for js/jsx scripts, as well as SFCs without scripts
// for better performance.
'js': 'espree',
'jsx': 'espree',
js: 'espree',
jsx: 'espree',

'ts': tseslintParser,
'tsx': tseslintParser,
ts: tseslintParser,
tsx: tseslintParser,

// Leave the template parser unspecified,
// so that it could be determined by `<script lang="...">`
},
ecmaFeatures: {
jsx: mayHaveJsx,
jsx: mayHaveJsxInSfc,
},
extraFileExtensions: ['vue'],
// type-aware linting is in conflict with jsx syntax in `.vue` files
projectService: !mayHaveJsx,
},
},
rules: {
Expand All @@ -73,5 +101,7 @@ export default function createConfig({
],
},
},

...projectServiceConfigs,
)
}

0 comments on commit 52fbcc0

Please sign in to comment.