Microsoft/TypeScript
GitHub で見るSourceFile.ambientModuleNames is undefined after transform()
Open
#19,950 opened on 2017年11月12日
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:
