From 2cc1a4c35420e62829d886f1f84c2731d0f7ef33 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 26 Sep 2014 09:23:11 +0200 Subject: [PATCH] refactor(js2dart): refactor dart library statement fixes #12 closes #16 --- tools/js2dart/spec/0-subfolder/library_spec.js | 3 +++ tools/js2dart/src/compiler.js | 9 ++------- tools/js2dart/src/dart_writer.js | 12 ++++++++++++ 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 tools/js2dart/spec/0-subfolder/library_spec.js diff --git a/tools/js2dart/spec/0-subfolder/library_spec.js b/tools/js2dart/spec/0-subfolder/library_spec.js new file mode 100644 index 0000000000..c155700ced --- /dev/null +++ b/tools/js2dart/spec/0-subfolder/library_spec.js @@ -0,0 +1,3 @@ +function main() { + assert(true); +} \ No newline at end of file diff --git a/tools/js2dart/src/compiler.js b/tools/js2dart/src/compiler.js index ebbdf4c46d..8cf53fc126 100644 --- a/tools/js2dart/src/compiler.js +++ b/tools/js2dart/src/compiler.js @@ -25,14 +25,9 @@ export class Compiler extends TraceurCompiler { return transformer.transformAny(tree); } writeDart(tree, filename) { - var writer = new DartTreeWriter(); + var writer = new DartTreeWriter(this.resolveModuleName(filename)); writer.visitAny(tree); - // TODO: Do this in the writer... - var library = this.resolveModuleName(filename) - .replace(/\//g, '.') - .replace('.lib.', '.') - .replace(/\.dart$/, ''); - return 'library ' + library + ';\n' + writer.toString(); + return writer.toString(); } // Copy of the original method to use our custom Parser parse(content, sourceName) { diff --git a/tools/js2dart/src/dart_writer.js b/tools/js2dart/src/dart_writer.js index b11e53eebb..79ffdbc106 100644 --- a/tools/js2dart/src/dart_writer.js +++ b/tools/js2dart/src/dart_writer.js @@ -4,6 +4,14 @@ import {EQUAL_EQUAL_EQUAL, OPEN_PAREN, CLOSE_PAREN, IMPORT, SEMI_COLON, STAR, OP import {ParseTreeWriter as JavaScriptParseTreeWriter} from 'traceur/src/outputgeneration/ParseTreeWriter'; export class DartTreeWriter extends JavaScriptParseTreeWriter { + constructor(moduleName) { + super(); + this.libName = moduleName + .replace(/\//g, '.') + .replace(/[^\w.]/, '_') + .replace('.lib.', '.') + .replace(/\.dart$/, ''); + } // VARIABLES - types // ``` @@ -237,5 +245,9 @@ export class DartTreeWriter extends JavaScriptParseTreeWriter { this.writeSpace_() } + + toString() { + return "library " + this.libName + ";\n" + super.toString(); + } }