Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions web_generator/lib/src/interop_gen/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,58 @@

import 'dart:js_interop';

import '../js/filesystem_api.dart';
import '../js/node.dart';
import '../js/typescript.dart' as ts;

class PreProcessResult {
List<String> modules;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be final?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #446

List<String> referenceLibs;
List<String> referenceTypes;

PreProcessResult(
{this.modules = const [],
this.referenceLibs = const [],
this.referenceTypes = const []});
}

class ParserResult {
ts.TSProgram program;
Iterable<String> files;
Map<String, PreProcessResult> preprocessResult;

ParserResult({required this.program, required this.files});
ParserResult(
{required this.program,
required this.files,
this.preprocessResult = const {}});
}

/// Parses the given TypeScript declaration [files], provides any diagnostics,
/// if any, and generates a [ts.TSProgram] for transformation
ParserResult parseDeclarationFiles(Iterable<String> files) {
// preprocess file
final preProcessResultMap = <String, PreProcessResult>{};

for (final file in files) {
final contents = (fs.readFileSync(
file.toJS, JSReadFileOptions(encoding: 'utf8'.toJS)) as JSString)
.toDart;
final preProcessResult = ts.preProcessFile(contents);

preProcessResultMap[file] = PreProcessResult(
modules: preProcessResult.ambientExternalModules?.toDart
.map((t) => t.toDart)
.toList() ??
[],
referenceLibs: preProcessResult.libReferenceDirectives.toDart
.map((ref) => ref.fileName)
.toList(),
referenceTypes: preProcessResult.typeReferenceDirectives.toDart
.map((ref) => ref.fileName)
.toList(),
);
}

final program = ts.createProgram(files.jsify() as JSArray<JSString>,
ts.TSCompilerOptions(declaration: true));

Expand Down Expand Up @@ -44,5 +83,6 @@ ParserResult parseDeclarationFiles(Iterable<String> files) {
exit(1);
}

return ParserResult(program: program, files: files);
return ParserResult(
program: program, files: files, preprocessResult: preProcessResultMap);
}
17 changes: 17 additions & 0 deletions web_generator/lib/src/js/typescript.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ external TSLineAndCharacter getLineAndCharacterOfPosition(
external String flattenDiagnosticMessageText(JSAny? diag, String newLine,
[int indent]);

@JS()
external TSPreProcessedFileInfo preProcessFile(String sourceText,
[bool readImportFiles, bool detectJavaScriptImports]);

@JS('PreProcessedFileInfo')
extension type TSPreProcessedFileInfo._(JSObject _) implements JSObject {
external JSArray<TSFileReference> referencedFiles;
external JSArray<TSFileReference> typeReferenceDirectives;
external JSArray<TSFileReference> libReferenceDirectives;
external JSArray<JSString>? ambientExternalModules;
}

@JS('FileReference')
extension type TSFileReference._(JSObject _) implements JSObject {
external String fileName;
}

@JS('CompilerOptions')
extension type TSCompilerOptions._(JSObject _) implements JSObject {
external TSCompilerOptions({bool? allowJs, bool? declaration});
Expand Down