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

Side by Side Diff: pkg/polymer/lib/src/transform/code_extractor.dart

Issue 23452010: Add a polymer validator: a linter/analysis that will replace the old (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 /** Transfomer that extracts inlined script code into separate assets. */ 5 /** Transfomer that extracts inlined script code into separate assets. */
6 library polymer.src.transformers; 6 library polymer.src.transformers;
7 7
8 import 'dart:async'; 8 import 'dart:async';
9 9
10 import 'package:analyzer_experimental/src/generated/ast.dart'; 10 import 'package:analyzer_experimental/src/generated/ast.dart';
11 import 'package:analyzer_experimental/src/generated/error.dart'; 11 import 'package:analyzer_experimental/src/generated/error.dart';
12 import 'package:analyzer_experimental/src/generated/parser.dart'; 12 import 'package:analyzer_experimental/src/generated/parser.dart';
13 import 'package:analyzer_experimental/src/generated/scanner.dart'; 13 import 'package:analyzer_experimental/src/generated/scanner.dart';
14 import 'package:barback/barback.dart'; 14 import 'package:barback/barback.dart';
15 import 'package:path/path.dart' as path; 15 import 'package:path/path.dart' as path;
16 16
17 import 'common.dart'; 17 import 'common.dart';
18 18
19 /** 19 /**
20 * Transformer that extracts Dart code inlined in HTML script tags and outputs a 20 * Transformer that extracts Dart code inlined in HTML script tags and outputs a
21 * separate file for each. 21 * separate file for each.
22 */ 22 */
23 class InlineCodeExtractor extends Transformer { 23 class InlineCodeExtractor extends Transformer {
24 /** Only run this transformer on .html files. */ 24 /** Only run this transformer on .html files. */
25 final String allowedExtensions = ".html"; 25 final String allowedExtensions = ".html";
26 26
27 Future apply(Transform transform) { 27 Future apply(Transform transform) {
28 var inputId = transform.primaryInput.id; 28 var input = transform.primaryInput;
29 return transform.primaryInput.readAsString().then((content) { 29 var id = transform.primaryInput.id;
30 var document = parseHtml(content, inputId.path, transform.logger, 30 return readPrimaryAsHtml(transform).then((document) {
31 checkDocType: false);
32 int count = 0; 31 int count = 0;
33 bool htmlChanged = false; 32 bool htmlChanged = false;
34 for (var tag in document.queryAll('script')) { 33 for (var tag in document.queryAll('script')) {
35 // Only process tags that have inline Dart code 34 // Only process tags that have inline Dart code
36 if (tag.attributes['type'] != 'application/dart' || 35 if (tag.attributes['type'] != 'application/dart' ||
37 tag.attributes.containsKey('src')) { 36 tag.attributes.containsKey('src')) {
38 continue; 37 continue;
39 } 38 }
40 htmlChanged = true; 39 htmlChanged = true;
41 40
42 // Remove empty tags 41 // Remove empty tags
43 if (tag.nodes.length == 0) { 42 if (tag.nodes.length == 0) {
44 tag.remove(); 43 tag.remove();
45 continue; 44 continue;
46 } 45 }
47 46
48 var filename = path.url.basename(inputId.path); 47 var filename = path.url.basename(id.path);
49 // TODO(sigmund): ensure this filename is unique (dartbug.com/12618). 48 // TODO(sigmund): ensure this filename is unique (dartbug.com/12618).
50 tag.attributes['src'] = '$filename.$count.dart'; 49 tag.attributes['src'] = '$filename.$count.dart';
51 var textContent = tag.nodes.first; 50 var textContent = tag.nodes.first;
52 var code = textContent.value; 51 var code = textContent.value;
53 var id = inputId.addExtension('.$count.dart'); 52 var newId = id.addExtension('.$count.dart');
54 if (!_hasLibraryDirective(code)) { 53 if (!_hasLibraryDirective(code)) {
55 var libname = path.withoutExtension(id.path) 54 var libname = path.withoutExtension(newId.path)
56 .replaceAll(new RegExp('[-./]'), '_'); 55 .replaceAll(new RegExp('[-./]'), '_');
57 code = "library $libname;\n$code"; 56 code = "library $libname;\n$code";
58 } 57 }
59 transform.addOutput(new Asset.fromString(id, code)); 58 transform.addOutput(new Asset.fromString(newId, code));
60 textContent.remove(); 59 textContent.remove();
61 count++; 60 count++;
62 } 61 }
63 transform.addOutput(new Asset.fromString(inputId, 62 transform.addOutput(
64 htmlChanged ? document.outerHtml : content)); 63 htmlChanged ? new Asset.fromString(id, document.outerHtml) : input);
65 }); 64 });
66 } 65 }
67 } 66 }
68 67
69 /** Parse [code] and determine whether it has a library directive. */ 68 /** Parse [code] and determine whether it has a library directive. */
70 bool _hasLibraryDirective(String code) { 69 bool _hasLibraryDirective(String code) {
71 var errorListener = new _ErrorCollector(); 70 var errorListener = new _ErrorCollector();
72 var token = new StringScanner(null, code, errorListener).tokenize(); 71 var token = new StringScanner(null, code, errorListener).tokenize();
73 var unit = new Parser(null, errorListener).parseCompilationUnit(token); 72 var unit = new Parser(null, errorListener).parseCompilationUnit(token);
74 return unit.directives.any((d) => d is LibraryDirective); 73 return unit.directives.any((d) => d is LibraryDirective);
75 } 74 }
76 75
77 class _ErrorCollector extends AnalysisErrorListener { 76 class _ErrorCollector extends AnalysisErrorListener {
78 final errors = <AnalysisError>[]; 77 final errors = <AnalysisError>[];
79 onError(error) => errors.add(error); 78 onError(error) => errors.add(error);
80 } 79 }
OLDNEW
« no previous file with comments | « no previous file | pkg/polymer/lib/src/transform/common.dart » ('j') | pkg/polymer/lib/src/transform/common.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698