Skip to content

Commit 178f6a9

Browse files
authored
[api-extractor] Fix issue where "typescriptCompilerFolder" did… (microsoft#1341)
[api-extractor] Fix issue where "typescriptCompilerFolder" did not work with TypeScript 3.4
2 parents a44df82 + d988682 commit 178f6a9

2 files changed

Lines changed: 25 additions & 51 deletions

File tree

apps/api-extractor/src/api/CompilerState.ts

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import * as ts from 'typescript';
66
import colors = require('colors');
77

88
import {
9-
JsonFile,
10-
FileSystem
9+
JsonFile
1110
} from '@microsoft/node-core-library';
1211

1312
import { ExtractorConfig } from './ExtractorConfig';
@@ -71,8 +70,6 @@ export class CompilerState {
7170
));
7271
}
7372

74-
CompilerState._updateCommandLineForTypescriptPackage(commandLine, options);
75-
7673
const inputFilePaths: string[] = commandLine.fileNames.concat(extractorConfig.mainEntryPointFilePath);
7774
if (options && options.additionalEntryPoints) {
7875
inputFilePaths.push(...options.additionalEntryPoints);
@@ -81,7 +78,9 @@ export class CompilerState {
8178
// Append the entry points and remove any non-declaration files from the list
8279
const analysisFilePaths: string[] = CompilerState._generateFilePathsForAnalysis(inputFilePaths);
8380

84-
const program: ts.Program = ts.createProgram(analysisFilePaths, commandLine.options);
81+
const compilerHost: ts.CompilerHost = CompilerState._createCompilerHost(commandLine, options);
82+
83+
const program: ts.Program = ts.createProgram(analysisFilePaths, commandLine.options, compilerHost);
8584

8685
if (commandLine.errors.length > 0) {
8786
const errorText: string = TypeScriptMessageFormatter.format(commandLine.errors[0].messageText);
@@ -130,54 +129,18 @@ export class CompilerState {
130129
return analysisFilePaths;
131130
}
132131

133-
/**
134-
* Update the parsed command line to use paths from the specified TS compiler folder, if
135-
* a TS compiler folder is specified.
136-
*/
137-
private static _updateCommandLineForTypescriptPackage(
138-
commandLine: ts.ParsedCommandLine,
139-
options?: IExtractorInvokeOptions
140-
): void {
141-
const DEFAULT_BUILTIN_LIBRARY: string = 'lib.d.ts';
142-
const OTHER_BUILTIN_LIBRARIES: string[] = ['lib.es5.d.ts', 'lib.es6.d.ts'];
143-
144-
if (options && options.typescriptCompilerFolder) {
145-
commandLine.options.noLib = true;
146-
const compilerLibFolder: string = path.join(options.typescriptCompilerFolder, 'lib');
147-
148-
let foundBaseLib: boolean = false;
149-
const filesToAdd: string[] = [];
150-
for (const libFilename of commandLine.options.lib || []) {
151-
if (libFilename === DEFAULT_BUILTIN_LIBRARY) {
152-
// Ignore the default lib - it'll get added later
153-
continue;
154-
}
155-
156-
if (OTHER_BUILTIN_LIBRARIES.indexOf(libFilename) !== -1) {
157-
foundBaseLib = true;
158-
}
159-
160-
const libPath: string = path.join(compilerLibFolder, libFilename);
161-
if (!FileSystem.exists(libPath)) {
162-
throw new Error(`lib ${libFilename} does not exist in the compiler specified in typescriptLibPackage`);
163-
}
164-
165-
filesToAdd.push(libPath);
166-
}
167-
168-
if (!foundBaseLib) {
169-
// If we didn't find another version of the base lib library, include the default
170-
filesToAdd.push(path.join(compilerLibFolder, 'lib.d.ts'));
171-
}
132+
private static _createCompilerHost(commandLine: ts.ParsedCommandLine,
133+
options: IExtractorInvokeOptions | undefined): ts.CompilerHost {
172134

173-
if (!commandLine.fileNames) {
174-
commandLine.fileNames = [];
175-
}
176-
177-
commandLine.fileNames.push(...filesToAdd);
135+
// Create a default CompilerHost that we can override
136+
const compilerHost: ts.CompilerHost = ts.createCompilerHost(commandLine.options);
178137

179-
commandLine.options.lib = undefined;
138+
if (options && options.typescriptCompilerFolder) {
139+
// Prevent a closure parameter
140+
const typescriptCompilerLibFolder: string = path.join(options.typescriptCompilerFolder, 'lib');
141+
compilerHost.getDefaultLibLocation = () => typescriptCompilerLibFolder;
180142
}
181-
}
182143

144+
return compilerHost;
145+
}
183146
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor",
5+
"comment": "Fix GitHub issue #1304 where \"IExtractorInvokeOptions.typescriptCompilerFolder\" did not work with TypeScript 3.4",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor",
10+
"email": "4673363+octogonz@users.noreply.github.com"
11+
}

0 commit comments

Comments
 (0)