From a75a3d0b315ac8da803a60d907838b235c4dc04e Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 26 Sep 2014 13:26:11 +0200 Subject: [PATCH] refactor(js2dart): use the parent functionalities as mush as possible Closes #18 --- tools/js2dart/src/compiler.js | 30 +++++++++++++++++------------- tools/js2dart/src/parser.js | 14 ++++++-------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/tools/js2dart/src/compiler.js b/tools/js2dart/src/compiler.js index 8cf53fc126..2724c44619 100644 --- a/tools/js2dart/src/compiler.js +++ b/tools/js2dart/src/compiler.js @@ -9,26 +9,30 @@ import { } from 'traceur/src/Options'; export class Compiler extends TraceurCompiler { + constructor(options, sourceRoot) { super(options, sourceRoot); } - compile(source, filename) { - var parsed = this.parse(source, filename || ''); - if (this.options_.outputLanguage === 'dart') { - return this.writeDart(this.transformDart(parsed), filename); + + transform(tree, moduleName = undefined) { + if (this.options_.outputLanguage.toLowerCase() === 'dart') { + var transformer = new ClassTransformer(); + return transformer.transformAny(tree); } else { - return this.write(this.transform(parsed)); + return super(tree, moduleName); } } - transformDart(tree) { - var transformer = new ClassTransformer(); - return transformer.transformAny(tree); - } - writeDart(tree, filename) { - var writer = new DartTreeWriter(this.resolveModuleName(filename)); - writer.visitAny(tree); - return writer.toString(); + + write(tree, outputName = undefined, sourceRoot = undefined) { + if (this.options_.outputLanguage.toLowerCase() === 'dart') { + var writer = new DartTreeWriter(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) { diff --git a/tools/js2dart/src/parser.js b/tools/js2dart/src/parser.js index 0a4ad1482d..259a8ac3b4 100644 --- a/tools/js2dart/src/parser.js +++ b/tools/js2dart/src/parser.js @@ -9,14 +9,10 @@ export class Parser extends TraceurParser { constructor(file, errorReporter = new SyntaxErrorReporter()) { super(file, errorReporter); } + parseTypeName_() { // Copy of original implementation - var start = this.getTreeStartLocation_(); - var typeName = new TypeName(this.getTreeLocation_(start), null, this.eatId_()); - while (this.eatIf_(PERIOD)) { - var memberName = this.eatIdName_(); - typeName = new TypeName(this.getTreeLocation_(start), typeName, memberName); - } + var typeName = super.parseTypeName_(); var next = this.peekType_(); // Generics support if (this.eatIf_(OPEN_ANGLE)) { @@ -29,6 +25,7 @@ export class Parser extends TraceurParser { } return typeName; } + parseImportSpecifier_() { // Copy of original implementation var start = this.getTreeStartLocation_(); @@ -38,7 +35,7 @@ export class Parser extends TraceurParser { var name = this.eatIdName_(); // Support for wraps keywoard if (this.peekToken_().value === WRAPS) { - var token = this.nextToken_(); + token = this.nextToken_(); var wrappedIdentifier = this.eatId_(); // TODO: Save the fact that this is a wrapper type and // also the wrapped type @@ -53,9 +50,10 @@ export class Parser extends TraceurParser { } return new ImportSpecifier(this.getTreeLocation_(start), binding, name); } + parseObjectType_() { //TODO(misko): save the type information - this.eat_(OPEN_CURLY) + this.eat_(OPEN_CURLY); do { var identifier = this.eatId_(); this.eat_(COLON);