OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'package:args/args.dart' show ArgParser, ArgResults; | 5 import 'package:args/args.dart' show ArgParser, ArgResults; |
6 import 'package:path/path.dart' as path; | 6 import 'package:path/path.dart' as path; |
7 | 7 |
8 import '../js_ast/js_ast.dart'; | 8 import '../js_ast/js_ast.dart'; |
9 import 'js_names.dart'; | 9 import 'js_names.dart'; |
10 | 10 |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 "Imports", new ArrayInitializer(importNames, multiline: true)), | 205 "Imports", new ArrayInitializer(importNames, multiline: true)), |
206 resultModule | 206 resultModule |
207 ]); | 207 ]); |
208 return new Program(<ModuleItem>[moduleDef]); | 208 return new Program(<ModuleItem>[moduleDef]); |
209 } | 209 } |
210 } | 210 } |
211 | 211 |
212 /// Generates CommonJS modules (used by Node.js). | 212 /// Generates CommonJS modules (used by Node.js). |
213 class CommonJSModuleBuilder extends _ModuleBuilder { | 213 class CommonJSModuleBuilder extends _ModuleBuilder { |
214 Program build(Program module) { | 214 Program build(Program module) { |
215 var importStatements = [js.statement("'use strict';"),]; | 215 var importStatements = [ |
| 216 js.statement("'use strict';"), |
| 217 ]; |
216 | 218 |
217 // Collect imports/exports/statements. | 219 // Collect imports/exports/statements. |
218 visitProgram(module); | 220 visitProgram(module); |
219 | 221 |
220 for (var import in imports) { | 222 for (var import in imports) { |
221 // TODO(jmesserly): we could use destructuring here. | 223 // TODO(jmesserly): we could use destructuring here. |
222 var moduleVar = | 224 var moduleVar = |
223 new TemporaryId(pathToJSIdentifier(import.from.valueWithoutQuotes)); | 225 new TemporaryId(pathToJSIdentifier(import.from.valueWithoutQuotes)); |
224 importStatements | 226 importStatements |
225 .add(js.statement('const # = require(#);', [moduleVar, import.from])); | 227 .add(js.statement('const # = require(#);', [moduleVar, import.from])); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 // Ensure the identifier first character is not numeric and that the whole | 342 // Ensure the identifier first character is not numeric and that the whole |
341 // identifier is not a keyword. | 343 // identifier is not a keyword. |
342 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { | 344 if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) { |
343 return '\$$result'; | 345 return '\$$result'; |
344 } | 346 } |
345 return result; | 347 return result; |
346 } | 348 } |
347 | 349 |
348 // Invalid characters for identifiers, which would need to be escaped. | 350 // Invalid characters for identifiers, which would need to be escaped. |
349 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); | 351 final _invalidCharInIdentifier = new RegExp(r'[^A-Za-z_$0-9]'); |
OLD | NEW |