Microsoft/TypeScript

SourceFile.ambientModuleNames is undefined after transform()

Open

#19,950 创建于 2017年11月12日

在 GitHub 查看
 (4 评论) (0 反应) (0 负责人)TypeScript (48,455 star) (6,726 fork)batch import
BugDomain: APIDomain: API: TransformsHelp Wanted

描述

TypeScript Version: 2.4.2 (stuck to this TypeScript version because of Angular 5.0.1)

Code

import * as ts from 'typescript';

  // transform typescript AST prior to compilation
  const transformationResult: ts.TransformationResult<ts.SourceFile> = ts.transform(
    tsProgram.getSourceFiles(),
    [ /* customTransformers */ ],
    options
  );

  // Wraps a CompilerHost reading SourceFile's from the previous TransformationResult
  function wrapCompilerHost({transformation, options}): ts.CompilerHost {
    const wrapped = ts.createCompilerHost(options);

    return {
      ...wrapped,
      getSourceFile: (fileName, version) => {
        const inTransformation = transformation.transformed.find((file) => file.fileName === fileName);

        if (inTransformation) {
          return inTransformation;
        } else {
          return wrapped.getSourceFile(fileName, version);
        }
      }
    }
  }

  // then, do another compilation
  const wrappingCompilerHost = wrapCompilerHost(transformationResult, options);
  ts.createProgram(/*..*/, wrappingCompilerHost).emit(/* .. */);

Expected behavior:

Should "ambientModuleNames" not be copied/applied to the transformed SourceFile?

Actual behavior:

After ts.transform(..), the "ambientModuleNames" of SourceFile is undefined, resulting in the following error:

"TypeError: Cannot read property 'length' of undefined
    at resolveModuleNamesReusingOldState (~ng-packagr/node_modules/typescript/lib/typescript.js:69409:80)
    at processImportedModules (~ng-packagr/node_modules/typescript/lib/typescript.js:70364:35)
    at findSourceFile (~ng-packagr/node_modules/typescript/lib/typescript.js:70274:17)
    at processImportedModules (~ng-packagr/node_modules/typescript/lib/typescript.js:70393:25)
    at findSourceFile (~ng-packagr/node_modules/typescript/lib/typescript.js:70274:17)
    at args (~ng-packagr/node_modules/typescript/lib/typescript.js:70200:85)
    at getSourceFileFromReferenceWorker (~ng-packagr/node_modules/typescript/lib/typescript.js:70173:34)
    at processSourceFile (~ng-packagr/node_modules/typescript/lib/typescript.js:70200:13)
    at processRootFile (~ng-packagr/node_modules/typescript/lib/typescript.js:70055:13)
    at ~ng-packagr/node_modules/typescript/lib/typescript.js:69312:60"

Original SourceFile has ambientModuleName. The transformed source file hasn't:

screen shot 2017-11-12 at 21 27 38

贡献者指南

SourceFile.ambientModuleNames is undefined after transform() · Microsoft/TypeScript#19950 | Good First Issue