Skip to content
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

How to I get the moduleName for each SourceFile? #14

Closed
ctaggart opened this issue Nov 19, 2017 · 5 comments
Closed

How to I get the moduleName for each SourceFile? #14

ctaggart opened this issue Nov 19, 2017 · 5 comments

Comments

@ctaggart
Copy link

ctaggart commented Nov 19, 2017

Is there a way to get the moduleNames for a SourceFile? They are all showing up as uknown when I do this?
http://www.typescriptlang.org/docs/handbook/module-resolution.html

import * as ts from "typescript";
import * as fs from "fs";
// import {isTypeReference, isClassDeclaration} from "tsutils";

const tsPath = "node_modules/@types/mocha/index.d.ts"
const options: ts.CompilerOptions = { target: ts.ScriptTarget.ES2015 }
const host = ts.createCompilerHost(options, true);
const program = ts.createProgram([tsPath], options, host)
const checker = program.getTypeChecker()

for (const sf of program.getSourceFiles()){
    console.log(sf.fileName + ": " + sf.moduleName)
}
C:\Users\camer\ts\TsAst [moduleName +1 ~0 -0 !]> node .\modulename.js
C:/Users/camer/ts/TsAst/node_modules/typescript/lib/lib.es6.d.ts: undefined
node_modules/@types/mocha/index.d.ts: undefined
C:/Users/camer/ts/TsAst/node_modules/@types/node/inspector.d.ts: undefined
C:/Users/camer/ts/TsAst/node_modules/@types/node/index.d.ts: undefined
@ajafff
Copy link
Owner

ajafff commented Nov 19, 2017

SourceFile.moduleName is only preset for AMD modules.

Can you describe which information you are trying to get from this property?

@ctaggart
Copy link
Author

ctaggart commented Nov 19, 2017

I want to generate import statements for each of the exports. To correctly generate imports, I need to know the module name and the name of the item being exported. Here, I'm trying to get mocha as the moduleName.

@ctaggart
Copy link
Author

I can string split on /, I just thought there may be a way to get it from the API.

@ajafff
Copy link
Owner

ajafff commented Nov 19, 2017

I'm afraid there is no ready-to-use solution. You need to use fileName and try to find an algorithm that works for all cases.

I'd suggest:

  • find the last node_modules in the name
  • if the directory after node_modules starts with an @, it's a scoped package -> use the next directory name, too
  • if the scope is @types, remove it
  • you probably need some special handling for submodule imports
    • for example @angular/common/http is not exported from @angular/common
    • you probably need to read the package.json's main property to get this right

@ctaggart
Copy link
Author

Thanks, I'll give that a shot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants