Skip to content

Commit 27b0a69

Browse files
authored
Merge pull request dart-archive#47 from derekxu16/master
Prevented lib.dom.d.ts files from being compiled when the --generate-html flag is set
2 parents 6defeee + a877ec3 commit 27b0a69

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

lib/main.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class Transpiler {
151151
let moduleLocation = <ts.StringLiteral>exportDecl.moduleSpecifier;
152152
let location = moduleLocation.text;
153153
let resolvedPath = host.resolveModuleNames(
154-
[location], f.fileName, undefined, undefined, COMPILER_OPTIONS);
154+
[location], f.fileName, undefined, undefined, this.getCompilerOptions());
155155
resolvedPath.forEach((p) => {
156156
if (p.isExternalLibraryImport) return;
157157
let exportedFile = sourceFileMap[p.resolvedFileName];
@@ -192,21 +192,26 @@ export class Transpiler {
192192
return paths;
193193
}
194194

195-
private getCompilerOptions() {
195+
getCompilerOptions() {
196196
let opts: ts.CompilerOptions = {};
197197
for (let k of Object.keys(COMPILER_OPTIONS)) opts[k] = COMPILER_OPTIONS[k];
198198
opts.rootDir = this.options.basePath;
199+
if (this.options.generateHTML) {
200+
// Prevent the TypeScript DOM library files from being compiled
201+
opts.lib = ['lib.es2015.d.ts', 'lib.scripthost.d.ts'];
202+
}
199203
return opts;
200204
}
201205

202206
private createCompilerHost(): ts.CompilerHost {
203-
let compilerHost = ts.createCompilerHost(COMPILER_OPTIONS);
204-
let defaultLibFileName = ts.getDefaultLibFileName(COMPILER_OPTIONS);
207+
const compilerOptions = this.getCompilerOptions();
208+
let compilerHost = ts.createCompilerHost(compilerOptions);
209+
let defaultLibFileName = ts.getDefaultLibFileName(compilerOptions);
205210
defaultLibFileName = this.normalizeSlashes(defaultLibFileName);
206211
compilerHost.getSourceFile = (sourceName) => {
207212
let sourcePath = sourceName;
208213
if (sourceName === defaultLibFileName) {
209-
sourcePath = ts.getDefaultLibFilePath(COMPILER_OPTIONS);
214+
sourcePath = ts.getDefaultLibFilePath(compilerOptions);
210215
}
211216
if (!fs.existsSync(sourcePath)) return undefined;
212217
let contents = fs.readFileSync(sourcePath, 'utf-8');

test/declaration_test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,31 @@ abstract class XStatic {
494494
// Constructors on anonymous interfaces are not yet supported.
495495
/*external factory XStatic(String|bool a, b);*/
496496
external foo();
497+
}`);
498+
});
499+
500+
// If the lib.dom.d.ts file is compiled, it creates conflicting interface definitions which causes
501+
// a problem for variable declarations that we have merged into classes. The main problem was that
502+
// when the declaration transpiler performed the notSimpleBagOfProperties check, it was checking
503+
// the lib.dom.d.ts definition of the interface, which made it think the class didn't have a
504+
// constructor defined when in reality it did.
505+
it('does not compile DOM library files when the --generate-html flag is set', () => {
506+
const generateHTMLOpts = {failFast: true, generateHTML: true};
507+
expectTranslate(
508+
`declare var AbstractRange: {prototype: AbstractRange; new (): AbstractRange;};
509+
510+
declare interface AbstractRange {
511+
readonly collapsed: boolean;
512+
readonly endOffset: number;
513+
readonly startOffset: number;
514+
}`,
515+
generateHTMLOpts)
516+
.to.equal(`@JS("AbstractRange")
517+
abstract class AbstractRange {
518+
external bool get collapsed;
519+
external num get endOffset;
520+
external num get startOffset;
521+
external factory AbstractRange();
497522
}`);
498523
});
499524
});

test/facade_converter_test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ abstract class XMLHttpRequest {
6969
external num get LOADING;
7070
external num get OPENED;
7171
external num get UNSENT;
72+
external factory XMLHttpRequest(
73+
{num readyState,
74+
dynamic response,
75+
String responseText,
76+
num DONE,
77+
num HEADERS_RECEIVED,
78+
num LOADING,
79+
num OPENED,
80+
num UNSENT});
7281
}
7382
7483
@JS()

test/test_support.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ let compilerOptions = main.COMPILER_OPTIONS;
2424
let defaultLibFileName = ts.getDefaultLibFileName(compilerOptions);
2525
let libSourceFiles: Map<string, ts.SourceFile> = new Map();
2626

27-
export function parseFiles(nameToContent: StringMap): ts.Program {
27+
export function parseFiles(
28+
nameToContent: StringMap, customCompilerOptions: ts.CompilerOptions): ts.Program {
2829
let result: string;
29-
let compilerHost = ts.createCompilerHost(compilerOptions);
30+
let compilerHost = ts.createCompilerHost(customCompilerOptions);
3031
compilerHost.getSourceFile = (sourceName) => {
3132
let sourcePath = sourceName;
3233
if (sourcePath === defaultLibFileName) {
33-
sourcePath = ts.getDefaultLibFilePath(compilerOptions);
34+
sourcePath = ts.getDefaultLibFilePath(customCompilerOptions);
3435
} else if (nameToContent.hasOwnProperty(sourcePath)) {
3536
return ts.createSourceFile(
36-
sourcePath, nameToContent[sourcePath], compilerOptions.target, true);
37+
sourcePath, nameToContent[sourcePath], customCompilerOptions.target, true);
3738
} else if (!fs.existsSync(sourcePath)) {
3839
return undefined;
3940
}
@@ -43,7 +44,7 @@ export function parseFiles(nameToContent: StringMap): ts.Program {
4344
// Cache to avoid excessive test times.
4445
libSourceFiles.set(
4546
sourcePath,
46-
ts.createSourceFile(sourcePath, contents, main.COMPILER_OPTIONS.target, true));
47+
ts.createSourceFile(sourcePath, contents, customCompilerOptions.target, true));
4748
}
4849
return libSourceFiles.get(sourcePath);
4950
};
@@ -61,7 +62,7 @@ export function parseFiles(nameToContent: StringMap): ts.Program {
6162

6263
// Create a program from inputs
6364
let entryPoints = Object.keys(nameToContent);
64-
let program: ts.Program = ts.createProgram(entryPoints, compilerOptions, compilerHost);
65+
let program: ts.Program = ts.createProgram(entryPoints, customCompilerOptions, compilerHost);
6566
if (program.getSyntacticDiagnostics().length > 0) {
6667
// Throw first error.
6768
let first = program.getSyntacticDiagnostics()[0];
@@ -76,6 +77,7 @@ export function translateSources(contents: Input, options?: main.TranspilerOptio
7677
options = options || {};
7778
// Default to quick stack traces.
7879
if (!options.hasOwnProperty('failFast')) options.failFast = true;
80+
7981
let namesToContent: StringMap;
8082
if (typeof contents === 'string') {
8183
namesToContent = {};
@@ -85,7 +87,7 @@ export function translateSources(contents: Input, options?: main.TranspilerOptio
8587
}
8688
options.enforceUnderscoreConventions = true;
8789
let transpiler = new main.Transpiler(options);
88-
let program = parseFiles(namesToContent);
90+
let program = parseFiles(namesToContent, transpiler.getCompilerOptions());
8991
return transpiler.translateProgram(program);
9092
}
9193

0 commit comments

Comments
 (0)