Skip to content

Commit

Permalink
feat: miscDefinitionImprovement for react
Browse files Browse the repository at this point in the history
feat: changeDtsFileDefinitionToJs to focus .d.ts -> .js
  • Loading branch information
zardoy committed Sep 11, 2022
1 parent f016cab commit 0765633
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/configurationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,16 @@ export type Configuration = {
* @default true
*/
removeDefinitionFromReferences: boolean
/**
* Small definition improvements by cleaning them out:
* - remove node_modules definition on React.FC component click
* @default true
*/
miscDefinitionImprovement: boolean
/**
* Experimental, feedback welcome
* If default, namespace import or import path click resolves to .d.ts file, try to resolve .js file instead with the same name
* @default false
*/
changeDtsFileDefinitionToJs: boolean
}
32 changes: 27 additions & 5 deletions typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,9 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
// description: '',
// changes: []
// })
// prior.codeActions = [{ description: '', changes: [{ fileName, textChanges: [{ span: { start: position, length: 0 }, newText: '()' }] }] }]
// formatOptions
// info.languageService.getDefinitionAtPosition(fileName, position)
return prior
}

// proxy.getCombinedCodeFix(scope, fixId, formatOptions, preferences)
proxy.getApplicableRefactors = (fileName, positionOrRange, preferences) => {
let prior = info.languageService.getApplicableRefactors(fileName, positionOrRange, preferences)

Expand Down Expand Up @@ -140,9 +136,35 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
return prior
}

proxy.getDefinitionAndBoundSpan = (fileName, position) => {
const prior = info.languageService.getDefinitionAndBoundSpan(fileName, position)
if (!prior) return
// used after check
const firstDef = prior.definitions![0]!
if (
c('changeDtsFileDefinitionToJs') &&
prior.definitions?.length === 1 &&
// default, namespace import or import path click
firstDef.containerName === '' &&
firstDef.fileName.endsWith('.d.ts')
) {
const jsFileName = `${firstDef.fileName.slice(0, -'.d.ts'.length)}.js`
const isJsFileExist = info.languageServiceHost.fileExists?.(jsFileName)
if (isJsFileExist) prior.definitions = [{ ...firstDef, fileName: jsFileName }]
}
if (c('miscDefinitionImprovement') && prior.definitions?.length === 2) {
prior.definitions = prior.definitions.filter(({ fileName, containerName }) => {
const isFcDef = fileName.endsWith('node_modules/@types/react/index.d.ts') && containerName === 'FunctionComponent'
return !isFcDef
})
}
return prior
}

proxy.findReferences = (fileName, position) => {
let prior = info.languageService.findReferences(fileName, position)
if (prior && c('removeDefinitionFromReferences')) {
if (!prior) return
if (c('removeDefinitionFromReferences')) {
prior = prior.map(({ references, ...other }) => ({ ...other, references: references.filter(({ isDefinition }) => !isDefinition) }))
}
return prior
Expand Down

0 comments on commit 0765633

Please sign in to comment.