Skip to content

Commit e1a1596

Browse files
committed
feat(definitions): exclude css modules index definition. It is included in miscDefinitionImprovement that is enabled by default.
1 parent c8302bb commit e1a1596

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

typescript/src/definitions.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { GetConfig } from './types'
22
import { findChildContainingExactPosition } from './utils'
33
import { join } from 'path-browserify'
4+
import { ModuleDeclaration } from 'typescript'
45

56
export default (proxy: ts.LanguageService, info: ts.server.PluginCreateInfo, c: GetConfig) => {
67
proxy.getDefinitionAndBoundSpan = (fileName, position) => {
@@ -35,7 +36,8 @@ export default (proxy: ts.LanguageService, info: ts.server.PluginCreateInfo, c:
3536
}
3637
}
3738

38-
// thoughts about type definition: no impl here, will be simpler to do this in core
39+
// partial fix for https://github.com/microsoft/TypeScript/issues/49033 (string literal in function call definition)
40+
// thoughts about type definition: no impl here, will be simpler to do this in core instead
3941
if (ts.isCallExpression(node.parent)) {
4042
const parameterIndex = node.parent.arguments.indexOf(node)
4143
const typeChecker = program.getTypeChecker()
@@ -116,12 +118,30 @@ export default (proxy: ts.LanguageService, info: ts.server.PluginCreateInfo, c:
116118
const isJsFileExist = info.languageServiceHost.fileExists?.(jsFileName)
117119
if (isJsFileExist) prior.definitions = [{ ...firstDef, fileName: jsFileName }]
118120
}
119-
if (c('miscDefinitionImprovement') && prior.definitions?.length === 2) {
120-
prior.definitions = prior.definitions.filter(({ fileName, containerName }) => {
121-
const isFcDef = fileName.endsWith('node_modules/@types/react/index.d.ts') && containerName === 'FunctionComponent'
122-
return !isFcDef
121+
if (c('miscDefinitionImprovement') && prior.definitions) {
122+
const filterOutReactFcDef = prior.definitions.length === 2
123+
prior.definitions = prior.definitions.filter(({ fileName, containerName, containerKind, kind, name, ...rest }) => {
124+
const isFcDef = filterOutReactFcDef && fileName.endsWith('node_modules/@types/react/index.d.ts') && containerName === 'FunctionComponent'
125+
if (isFcDef) return false
126+
// filter out css modules index definition
127+
if (containerName === 'classes' && containerKind === undefined && rest['isAmbient'] && kind === 'index' && name === '__index') {
128+
// ensure we don't filter out something important?
129+
const nodeAtDefinition = findChildContainingExactPosition(
130+
info.languageService.getProgram()!.getSourceFile(fileName)!,
131+
firstDef.textSpan.start,
132+
)
133+
let moduleDeclaration: ModuleDeclaration | undefined
134+
ts.findAncestor(nodeAtDefinition, node => {
135+
if (ts.isModuleDeclaration(node)) {
136+
moduleDeclaration = node
137+
return 'quit'
138+
}
139+
return false
140+
})
141+
if (moduleDeclaration?.name.getText() === '*.module.css') return false
142+
}
143+
return true
123144
})
124-
// 11
125145
}
126146

127147
if (

0 commit comments

Comments
 (0)