Skip to content

Commit 6f463b2

Browse files
authored
Fix CompilerHost file name resolution (dart-archive#59)
Added error to make it clearer when source file paths are missing from the command line arguments Updated README
1 parent c1762b4 commit 6f463b2

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
11
[![Build Status](https://travis-ci.org/dart-lang/js_facade_gen.svg?branch=master)](https://travis-ci.org/dart-lang/js_facade_gen)
22

3-
Generates `package:js` Javascript interop facades for arbitrary TypeScript
4-
libraries.
3+
Generates `package:js` JavaScript interop facades for arbitrary TypeScript libraries.
54

65
## Installation
76

87
- [Install Node.js](https://docs.npmjs.com/getting-started/installing-node)
9-
- We depend on Node.js so that we can analyze TypeScript files using the [TypeScript language services](https://www.npmjs.com/package/typescript-services) package. This ensures we parse `d.ts` consistently with other tools.
8+
- We depend on Node.js so that we can analyze TypeScript files using the [TypeScript Compiler API](https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API) in the [TypeScript](https://www.npmjs.com/package/typescript) package. This ensures we parse `d.ts` consistently with other tools.
109
- Execute `npm install -g dart_js_facade_gen` to install.
1110

1211
## Usage
1312

1413
### Basic
15-
`dart_js_facade_gen <input d.ts file>`
14+
`dart_js_facade_gen <input d.ts file>`<br/>
1615
Dart interop facade file is written to stdout.
1716

1817
### Advanced
19-
`dart_js_facade_gen --destination=<destination-dir> --basePath=<input d.ts file directory> <input d.ts file> <input d.ts file> ...`
18+
`dart_js_facade_gen --destination=<destination-dir> --base-path=<input d.ts file directory> <input d.ts file> <input d.ts file> ...`
2019

2120
#### Flags
22-
`--destination=<destination-dir>`: Output generated code to destination-dir
23-
`--generate-html`: Generate facades for dart:html types rather than importing them
24-
`--explicit-static`: Disables default assumption that properties declared on the anonymous types of top level variable declarations are static
21+
`--destination=<destination-dir>`: output generated code to destination-dir<br/>
22+
`--base-path=<input d.ts file directory>`: specify the directory that contains the input d.ts files<br/>
23+
`--generate-html`: generate facades for dart:html types rather than importing them<br/>
24+
`--explicit-static`: disables default assumption that properties declared on the anonymous types of top level variable declarations are static
2525

2626
### Example
27-
`dart_js_facade_gen --destination=/usr/foo/tmp/chartjs/lib --basePath=/usr/foo/git/DefinitelyTyped/chartjs /usr/foo/git/DefinitelyTyped/chartjs/chart.d.ts`
28-
29-
## Development
30-
31-
- The Dart SDK must be available to run end to end tests.
27+
`dart_js_facade_gen --destination=/usr/foo/tmp/chartjs/lib --base-path=/usr/foo/git/DefinitelyTyped/chartjs /usr/foo/git/DefinitelyTyped/chartjs/chart.d.ts`
3228

3329
### Gulp tasks
3430

3531
- `gulp watch` executes the unit tests in watch mode (use `gulp test.unit` for a single run),
3632
- `gulp test.check-format` checks the source code formatting using `clang-format`,
37-
- `gulp test` runs unit tests, e2e tests and checks the source code formatting.
33+
- `gulp test` runs unit tests and checks the source code formatting.
3834

3935
### Publish
4036

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var args = require('minimist')(process.argv.slice(2), {
66
base: 'string',
77
boolean: ['semantic-diagnostics', 'generate-html', 'explicit-static'],
88
alias: {
9+
'base-path': 'basePath',
910
'semantic-diagnostics': 'semanticDiagnostics',
1011
'generate-html': 'generateHTML',
1112
'explicit-static': 'explicitStatic'

lib/main.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,22 @@ export class Transpiler {
127127
this.options.basePath = this.normalizeSlashes(path.resolve(this.options.basePath));
128128
}
129129
fileNames = fileNames.map((f) => this.normalizeSlashes(f));
130-
let host = this.createCompilerHost();
131-
let program = ts.createProgram(fileNames, this.getCompilerOptions(), host);
130+
const host = this.createCompilerHost();
131+
const program = ts.createProgram(fileNames, this.getCompilerOptions(), host);
132132
this.fc.setTypeChecker(program.getTypeChecker());
133133
this.declarationTranspiler.setTypeChecker(program.getTypeChecker());
134134

135135
// Only write files that were explicitly passed in.
136136
const fileSet = new Set(fileNames);
137-
let sourceFiles =
138-
program.getSourceFiles().filter((sourceFile) => fileSet.has(sourceFile.fileName));
137+
const sourceFiles = program.getSourceFiles().filter((sourceFile) => {
138+
return fileSet.has(sourceFile.fileName);
139+
});
139140

140141
this.errors = [];
141142

142-
let sourceFileMap: {[s: string]: ts.SourceFile} = {};
143+
const sourceFileMap: Map<string, ts.SourceFile> = new Map();
143144
sourceFiles.forEach((f: ts.SourceFile) => {
144-
sourceFileMap[f.fileName] = f;
145+
sourceFileMap.set(f.fileName, f);
145146
});
146147

147148
// Check for global module export declarations and propogate them to all modules they export.
@@ -154,6 +155,7 @@ export class Transpiler {
154155
let globalModuleName = base.ident(n.name);
155156
f.moduleName = globalModuleName;
156157

158+
const missingFiles: string[] = [];
157159
f.statements.forEach((e: ts.Node) => {
158160
if (!ts.isExportDeclaration(e)) return;
159161
let exportDecl = e;
@@ -164,10 +166,25 @@ export class Transpiler {
164166
[location], f.fileName, undefined, undefined, this.getCompilerOptions());
165167
resolvedPath.forEach((p) => {
166168
if (p.isExternalLibraryImport) return;
167-
let exportedFile = sourceFileMap[p.resolvedFileName];
168-
exportedFile.moduleName = globalModuleName;
169+
const exportedFile = sourceFileMap.get(p.resolvedFileName);
170+
if (exportedFile) {
171+
exportedFile.moduleName = globalModuleName;
172+
} else {
173+
missingFiles.push(p.resolvedFileName);
174+
}
169175
});
170176
});
177+
if (missingFiles.length) {
178+
const error = new Error();
179+
error.message =
180+
'The following files were referenced but were not supplied as a command line arguments. Reference the README for usage instructions.';
181+
for (const file of missingFiles) {
182+
error.message += '\n';
183+
error.message += file;
184+
}
185+
error.name = 'DartFacadeError';
186+
throw error;
187+
}
171188
});
172189
});
173190

@@ -215,7 +232,7 @@ export class Transpiler {
215232

216233
private createCompilerHost(): ts.CompilerHost {
217234
const compilerOptions = this.getCompilerOptions();
218-
let compilerHost = ts.createCompilerHost(compilerOptions);
235+
const compilerHost = ts.createCompilerHost(compilerOptions);
219236
let defaultLibFileName = ts.getDefaultLibFileName(compilerOptions);
220237
defaultLibFileName = this.normalizeSlashes(defaultLibFileName);
221238
compilerHost.getSourceFile = (sourceName) => {
@@ -232,6 +249,7 @@ export class Transpiler {
232249
};
233250
compilerHost.useCaseSensitiveFileNames = () => true;
234251
compilerHost.getCanonicalFileName = (filename) => filename;
252+
compilerHost.getCurrentDirectory = () => '';
235253
compilerHost.getNewLine = () => '\n';
236254
compilerHost.resolveModuleNames = getModuleResolver(compilerHost);
237255

@@ -339,7 +357,7 @@ export class Transpiler {
339357
if (diagnosticErrs.length) errors = errors.concat(diagnosticErrs);
340358

341359
if (errors.length) {
342-
let e = new Error(errors.join('\n'));
360+
const e = new Error(errors.join('\n'));
343361
e.name = 'DartFacadeError';
344362
throw e;
345363
}

0 commit comments

Comments
 (0)