Skip to content

use node kind checking more strict variant #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 27 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Config = {
name: string;
enable: boolean;
};
function init(modules: {
function init({ typescript: ts }: {
typescript: typeof import("typescript/lib/tsserverlibrary");
}) {
try {
Expand Down Expand Up @@ -35,9 +35,20 @@ function init(modules: {
return;
}
if (settings.enable) {
prior.definitions = prior.definitions?.filter(({ fileName }) => {
const matches = settings.modules.some((t) => fileName.includes(t));
if (matches) return false;
prior.definitions = prior.definitions?.filter(({ fileName, textSpan, kind, name }) => {
if (kind === 'index' && name === '__index') {
const definitionNode = findNodeAtPosition(ts, info.languageService.getProgram()!.getSourceFile(fileName)!, textSpan.start)
let moduleDeclaration: ts.ModuleDeclaration | undefined
ts.findAncestor(definitionNode, node => {
if (ts.isModuleDeclaration(node)) {
Copy link
Owner

@Viijay-Kr Viijay-Kr Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far , this method filter out results from nextjs declaration and react-scripts declaration files. which has a clear declaration type ancestor

However for vite based apps , the definition result points to this location in the declaration file.
So this specific node in the AST doesn't have any Module Declaration ancestor.

Therefore

if (ts.isModuleDeclaration(node)) {

}

this block never gets executed.

Do you have any other ideas to solve this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, didn't know of these cases, and have no ideas for clean solution.

Do you have any other ideas to solve this ?

Maybe we can include some additional pattern checks as in case of Vite these names are straightforwad:

  if (kind === ts.ScriptElementKind.indexSignatureElement && name === '__index') {
              if (containerName === 'CSSModule' || containerName === 'CSSModuleClasses') return false
              // ... findNodeAtPosition

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, didn't know of these cases, and have no ideas for clean solution.
In theory a library author could emit their declarations however they want.

Maybe we can include some additional pattern checks as in case of Vite these names are straightforwad:

Can try this out

moduleDeclaration = node
return 'quit'
}
return false
})
const cssModules = ['*.module.css', '*.module.scss', '*.module.sass', '*.module.less', '*.module.styl']
if (moduleDeclaration?.name.text && cssModules.includes(moduleDeclaration.name.text)) return false
}
return true;
});
}
Expand All @@ -52,8 +63,20 @@ function init(modules: {
}
return { create, onConfigurationChanged };
} catch (e) {
console.error(e)
throw new Error("Cannot load `typescript-cleanup-definitions`");
}
}

const findNodeAtPosition = (ts: typeof import("typescript/lib/tsserverlibrary"), sourceFile: ts.SourceFile, position: number) => {
function find(node: ts.Node): ts.Node | undefined {
if (position >= node.getStart() && position <= node.getEnd()) {
return ts.forEachChild(node, find) || node
}

return
}
return find(sourceFile)
}

export = init;