microsoft / microsoft/TypeScript

Expose `resolvedModules` property of `SourceFile`

Open
#33,994 3 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Domain: API Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.6.4

Code

import * as ts from 'typescript'

function createCompilerHost(): ts.CompilerHost {
  function fileExists(fileName: string): boolean {
    return ts.sys.fileExists(fileName)
  }

  function readFile(fileName: string): string | undefined {
    return ts.sys.readFile(fileName)
  }

  function getSourceFile(
    fileName: string,
    languageVersion: ts.ScriptTarget,
  ) {
    const sourceText = ts.sys.readFile(fileName)
    return sourceText !== undefined
      ? ts.createSourceFile(fileName, sourceText, languageVersion)
      : undefined
  }

  function resolveModuleNames(moduleNames) {
    return moduleNames.map(() => undefined)
  }

  return {
    getSourceFile,
    getDefaultLibFileName: () => 'lib.d.ts',
    writeFile: (fileName, content) => ts.sys.writeFile(fileName, content),
    getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
    getDirectories: path => ts.sys.getDirectories(path),
    getCanonicalFileName: fileName =>
      ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(),
    getNewLine: () => ts.sys.newLine,
    useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
    fileExists,
    readFile,
    resolveModuleNames,
  }

}

const options = {
  module: ts.ModuleKind.ES2015,
  target: ts.ScriptTarget.ES5,
}
const host = createCompilerHost()
const sourceFiles = ts.createProgram(['./test.ts'], options, host).getSourceFiles()
const matchsFile = sourceFiles
  .filter(s => {
    return s.resolvedModules.has('typescript')
  })

Expected behavior:
Property 'resolvedModules' should exist on type 'SourceFile'

Actual behavior:
image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the public SourceFile type and the createProgram().getSourceFiles() path shown in the reproduction. Check how resolvedModules is represented internally, then verify that the property is exposed on SourceFile and that the provided TypeScript example can access it.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.