angular-cn/tools/js2dart/src/compiler.js
Vojta Jina c3b442ea53 chore: karma with JS, Dart
Note: karma with dart is still not working
because of how `karma-dart` loads `package:…` dependencies.

Usage:
```
karma start karma-js.conf.js
karma start karma-dart.conf.js
```

Make sure to set `DARTIUM_BIN` env variable.

Refactors `js2dart`:
- live outside of the traceur module (`tools/js2dart/index.js`)
  so it can be reused by gulp and karma
- automatically build the sources in memory,
  so that `js2dart` can be used without running `gulp build` first
- provide a way to specify the moduleName of a compilation run
  independently of the input filename. This helps error messages
  and source maps (not yet enabled) to report the correct file name

Changes project setup:
- add module `test_lib` that contains the primitives for tests
  (e.g. `describe`, `it`, …)
- clean up some sources that had errors in them
- module names in transpiled js and dart files don’t contain
  `lib`, `test` nor `src` any more (e.g. `di/di`).
2014-09-26 16:53:54 -07:00

58 lines
1.9 KiB
JavaScript

import {Compiler as TraceurCompiler} from 'traceur/src/Compiler';
import {ClassTransformer} from './dart_class_transformer';
import {DartTreeWriter} from './dart_writer';
import {CollectingErrorReporter} from 'traceur/src/util/CollectingErrorReporter';
import {Parser} from './parser';
import {SourceFile} from 'traceur/src/syntax/SourceFile';
import {
options as traceurOptions
} from 'traceur/src/Options';
export class Compiler extends TraceurCompiler {
constructor(options, sourceRoot) {
super(options, sourceRoot);
}
transform(tree, moduleName = undefined) {
if (this.options_.outputLanguage.toLowerCase() === 'dart') {
var transformer = new ClassTransformer();
return transformer.transformAny(tree);
} else {
return super(tree, moduleName);
}
}
write(tree, outputName = undefined, sourceRoot = undefined) {
if (this.options_.outputLanguage.toLowerCase() === 'dart') {
var writer = new DartTreeWriter(this.options_.moduleName, outputName);
writer.visitAny(tree);
return writer.toString();
} else {
return super.write(tree, outputName, sourceRoot);
}
}
// Copy of the original method to use our custom Parser
parse(content, sourceName) {
if (!content) {
throw new Error('Compiler: no content to compile.');
} else if (!sourceName) {
throw new Error('Compiler: no source name for content.');
}
this.sourceMapGenerator_ = null;
// Here we mutate the global/module options object to be used in parsing.
traceurOptions.setFromObject(this.options_);
var errorReporter = new CollectingErrorReporter();
sourceName = this.sourceName(sourceName);
var sourceFile = new SourceFile(sourceName, content);
var parser = new Parser(sourceFile, errorReporter);
var tree =
this.options_.script ? parser.parseScript() : parser.parseModule();
this.throwIfErrors(errorReporter);
return tree;
}
}