Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: lib/src/codegen/js_printer.dart

Issue 1111893002: small refactor: use JS.* prefix for our other JS extensions (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/codegen/js_names.dart ('k') | lib/src/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 library dev_compiler.src.codegen.js_printer; 5 library dev_compiler.src.codegen.js_printer;
6 6
7 import 'dart:io' show Directory, File; 7 import 'dart:io' show Directory, File;
8 import 'package:analyzer/src/generated/ast.dart'; 8 import 'package:analyzer/src/generated/ast.dart';
9 import 'package:path/path.dart' as path; 9 import 'package:path/path.dart' as path;
10 import 'package:source_maps/source_maps.dart' as srcmaps show Printer; 10 import 'package:source_maps/source_maps.dart' as srcmaps show Printer;
11 import 'package:source_maps/source_maps.dart' show SourceMapSpan; 11 import 'package:source_maps/source_maps.dart' show SourceMapSpan;
12 import 'package:source_span/source_span.dart' show SourceLocation; 12 import 'package:source_span/source_span.dart' show SourceLocation;
13 13
14 import 'package:dev_compiler/src/js/js_ast.dart' as JS; 14 import 'package:dev_compiler/src/js/js_ast.dart' as JS;
15 import 'package:dev_compiler/src/utils.dart' 15 import 'package:dev_compiler/src/utils.dart'
16 show computeHash, locationForOffset; 16 show computeHash, locationForOffset;
17 17
18 import 'js_names.dart' show JSNamer; 18 import 'js_names.dart' show TemporaryNamer;
19 19
20 String writeJsLibrary(JS.Program jsTree, String outputPath, 20 String writeJsLibrary(JS.Program jsTree, String outputPath,
21 {bool emitSourceMaps: false}) { 21 {bool emitSourceMaps: false}) {
22 new Directory(path.dirname(outputPath)).createSync(recursive: true); 22 new Directory(path.dirname(outputPath)).createSync(recursive: true);
23 if (emitSourceMaps) { 23 if (emitSourceMaps) {
24 var outFilename = path.basename(outputPath); 24 var outFilename = path.basename(outputPath);
25 var printer = new srcmaps.Printer(outFilename); 25 var printer = new srcmaps.Printer(outFilename);
26 writeNodeToContext(jsTree, 26 writeNodeToContext(jsTree,
27 new SourceMapPrintingContext(printer, path.dirname(outputPath))); 27 new SourceMapPrintingContext(printer, path.dirname(outputPath)));
28 printer.add('//# sourceMappingURL=$outFilename.map'); 28 printer.add('//# sourceMappingURL=$outFilename.map');
29 // Write output file and source map 29 // Write output file and source map
30 var text = printer.text; 30 var text = printer.text;
31 new File(outputPath).writeAsStringSync(text); 31 new File(outputPath).writeAsStringSync(text);
32 new File('$outputPath.map').writeAsStringSync(printer.map); 32 new File('$outputPath.map').writeAsStringSync(printer.map);
33 return computeHash(text); 33 return computeHash(text);
34 } else { 34 } else {
35 var text = jsNodeToString(jsTree); 35 var text = jsNodeToString(jsTree);
36 new File(outputPath).writeAsStringSync(text); 36 new File(outputPath).writeAsStringSync(text);
37 return computeHash(text); 37 return computeHash(text);
38 } 38 }
39 } 39 }
40 40
41 void writeNodeToContext(JS.Node node, JS.JavaScriptPrintingContext context) { 41 void writeNodeToContext(JS.Node node, JS.JavaScriptPrintingContext context) {
42 var opts = new JS.JavaScriptPrintingOptions(allowKeywordsInProperties: true); 42 var opts = new JS.JavaScriptPrintingOptions(allowKeywordsInProperties: true);
43 node.accept(new JS.Printer(opts, context, localNamer: new JSNamer(node))); 43 node.accept(
44 new JS.Printer(opts, context, localNamer: new TemporaryNamer(node)));
44 } 45 }
45 46
46 String jsNodeToString(JS.Node node) { 47 String jsNodeToString(JS.Node node) {
47 var context = new JS.SimpleJavaScriptPrintingContext(); 48 var context = new JS.SimpleJavaScriptPrintingContext();
48 writeNodeToContext(node, context); 49 writeNodeToContext(node, context);
49 return context.getText(); 50 return context.getText();
50 } 51 }
51 52
52 class SourceMapPrintingContext extends JS.JavaScriptPrintingContext { 53 class SourceMapPrintingContext extends JS.JavaScriptPrintingContext {
53 final srcmaps.Printer printer; 54 final srcmaps.Printer printer;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 101
101 // TODO(jmesserly): in many cases marking the end will be unnecessary. 102 // TODO(jmesserly): in many cases marking the end will be unnecessary.
102 printer.mark(_location(node.end)); 103 printer.mark(_location(node.end));
103 } 104 }
104 105
105 String _getIdentifier(AstNode node) { 106 String _getIdentifier(AstNode node) {
106 if (node is SimpleIdentifier) return node.name; 107 if (node is SimpleIdentifier) return node.name;
107 return null; 108 return null;
108 } 109 }
109 } 110 }
OLDNEW
« no previous file with comments | « lib/src/codegen/js_names.dart ('k') | lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698