From 3f00b372914837c17fdf0794c6b751082b17fc10 Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Fri, 1 Aug 2025 23:56:52 +0100 Subject: [PATCH] [add] add namespace import parsing --- src/tsCalls.mjs | 74 ++++++++++++++++++++++++++++++++++++++++++++-- test_src/index.mjs | 14 ++++----- 2 files changed, 77 insertions(+), 11 deletions(-) diff --git a/src/tsCalls.mjs b/src/tsCalls.mjs index 7c75968..a0c5bc2 100644 --- a/src/tsCalls.mjs +++ b/src/tsCalls.mjs @@ -71,11 +71,17 @@ export function getImportCallsAndArgumentTypes(importDecls, checker, mainFilePat } const defaultImportIdentifier = importDecl.getDefaultImport(); - console.log("Default import",defaultImportIdentifier); + // console.log("Default import",defaultImportIdentifier); if( defaultImportIdentifier !== undefined) { recordImportedIdentifierUsage(defaultImportIdentifier, mainFilePath, libraryCallsRecorder, importStringDecl, true); } - // console.log("Namespace import",importDecl.getNamespaceImport()); + + const namespaceImportIdentifier = importDecl.getNamespaceImport(); + // console.log("Namespace import",namespaceImportIdentifier); + if( namespaceImportIdentifier !== undefined) { + recordNamespaceImportIdentifierUsage(namespaceImportIdentifier, mainFilePath, libraryCallsRecorder, importStringDecl); + } + // recordImportedIdentifierUsage(defaultImportIdentifier, mainFilePath, libraryCallsRecorder, importStringDecl, true); console.log("STOP"); @@ -111,6 +117,69 @@ function handleImportForGivenImport(importStringLiteral,namedImport, mainFilePat recordImportedIdentifierUsage(importNode, mainFilePath, libraryCallsRecorder, importStringLiteral); } +/** + * + * @param {Identifier} importNode + * @param {string} mainFilePath + * @param {LibraryTypesRecorder} libraryCallsRecorder + * @param {StringLiteral} importStringLiteral + */ +function recordNamespaceImportIdentifierUsage(importNode, mainFilePath, libraryCallsRecorder, importStringLiteral) { + const importRefs = importNode.findReferences(); + for (const importRef of importRefs) { + const referenceSourceFile = importRef.getDefinition().getSourceFile(); + const comparePath = path.relative(mainFilePath, referenceSourceFile.getFilePath()); + if (comparePath !== '') { + console.warn("Skipping import reference from other file", referenceSourceFile.getFilePath()); + continue; + } + console.log("Compare path", comparePath === ''); + // const filePath = referenceSourceFile.getFilePath(); + // console.log("Refset for import",filePath); + for (const ref of importRef.getReferences()) { + if (ref.isDefinition()) { + continue; + } + // console.log("I am ",ref.isDefinition()); + const callExpression = ref.getNode().getFirstAncestorByKind(SyntaxKind.CallExpression); + + + /** + * @type {`.${string}`} + */ + let getImportSection; + + if(callExpression?.getExpression().getDescendantsOfKind(SyntaxKind.Identifier).some(id=>id===ref.getNode())){ + // asserted that the call expression is using the importNode + if(callExpression.getExpression().isKind(SyntaxKind.PropertyAccessExpression)){ + console.log("Used a submethod of import", ref.getNode().getText(),callExpression.getExpression().getText()); + ref.getNode().getText(); + const expressionImportSection = callExpression.getExpression().getText().split('.'); + expressionImportSection.shift(); + getImportSection = '.'+expressionImportSection.join('.'); + }else{ + console.warn("Call expression is not using the import node as property access", ref.getNode().getText()); + continue; + } + }else{ + console.warn("Call expression is not using the import node", callExpression?.getText()); + continue; + } + const callExpressionArguments = callExpression?.getArguments(); + if (callExpressionArguments === undefined || callExpressionArguments.length === 0) { + console.warn("No call expressions found for import reference", ref.getNode().getText()); + continue; + } + + // for(const argument of callExpressionArguments){ + // console.log(`Arg ${idx} is ${arg.getText()}, type is ${arg.getType()}`); + // } + // console.log("Noted call for namespace import", importStringLiteral.getLiteralValue(), getImportSection, callExpressionArguments.map(arg => arg.getType().getText())); + libraryCallsRecorder.pushToMap(importStringLiteral.getLiteralValue(), getImportSection, callExpressionArguments.map(arg => arg.getType())); + + } + } +} /** * * @param {Identifier} importNode @@ -150,7 +219,6 @@ function recordImportedIdentifierUsage(importNode, mainFilePath, libraryCallsRec const getImportSection = '.' + (isDefaultImport? 'default':importNode.getText()); libraryCallsRecorder.pushToMap(importStringLiteral.getLiteralValue(), getImportSection, callExpressionArguments.map(arg => arg.getType())); - console.log("I am ", callExpression?.getText()); } } } diff --git a/test_src/index.mjs b/test_src/index.mjs index 3e3bfb8..857c3e0 100644 --- a/test_src/index.mjs +++ b/test_src/index.mjs @@ -1,12 +1,10 @@ import fs,{readFile} from 'node:fs' -import classnames from 'classnames' -// import {neq} from 'semver' -import {sum, div} from './arithmetic.cjs'; +// import classnames from 'classnames' +import * as s from 'esprima' +// import {sum, div} from './arithmetic.cjs'; -readFile('a',(err)=>{ - if(err){return;} -}) -console.log(classnames({a:true,b:true})); // $ExpectType string -console.log(sum(2, 3)); +console.log(s.parseScript("const a = 5;")); // $ExpectType boolean +// console.log(classnames({a:true,b:true})); // $ExpectType string +// console.log(sum(2, 3)); // console.log(neq('1.0.0', '1.0.1')); // $ExpectType boolean \ No newline at end of file