OLD | NEW |
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 /// Logic to validate that developers are correctly using Polymer constructs. | 5 /// Logic to validate that developers are correctly using Polymer constructs. |
6 /// This is mainly used to produce warnings for feedback in the editor. | 6 /// This is mainly used to produce warnings for feedback in the editor. |
7 library polymer.src.build.linter; | 7 library polymer.src.build.linter; |
8 | 8 |
9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 import 'dart:convert'; |
10 | 11 |
11 import 'package:barback/barback.dart'; | 12 import 'package:barback/barback.dart'; |
12 import 'package:code_transformers/assets.dart'; | 13 import 'package:code_transformers/assets.dart'; |
13 import 'package:html5lib/dom.dart'; | 14 import 'package:html5lib/dom.dart'; |
14 import 'package:html5lib/dom_parsing.dart'; | 15 import 'package:html5lib/dom_parsing.dart'; |
15 import 'package:source_span/source_span.dart'; | 16 import 'package:source_span/source_span.dart'; |
16 | 17 |
17 import 'common.dart'; | 18 import 'common.dart'; |
18 import 'utils.dart'; | 19 import 'utils.dart'; |
| 20 import 'wrapped_logger.dart'; |
19 | 21 |
20 /// A linter that checks for common Polymer errors and produces warnings to | 22 /// A linter that checks for common Polymer errors and produces warnings to |
21 /// show on the editor or the command line. Leaves sources unchanged, but | 23 /// show on the editor or the command line. Leaves sources unchanged, but |
22 /// creates a new asset containing all the warnings. | 24 /// creates a new asset containing all the warnings. |
23 class Linter extends Transformer with PolymerTransformer { | 25 class Linter extends Transformer with PolymerTransformer { |
24 final TransformOptions options; | 26 final TransformOptions options; |
25 | 27 |
26 /// Only run on .html files. | 28 /// Only run on .html files. |
27 final String allowedExtensions = '.html'; | 29 final String allowedExtensions = '.html'; |
28 | 30 |
29 Linter(this.options); | 31 Linter(this.options); |
30 | 32 |
31 Future apply(Transform transform) { | 33 Future apply(Transform transform) { |
32 var seen = new Set<AssetId>(); | 34 var seen = new Set<AssetId>(); |
33 var primary = transform.primaryInput; | 35 var primary = transform.primaryInput; |
34 var id = primary.id; | 36 var id = primary.id; |
35 transform.addOutput(primary); // this phase is analysis only | 37 transform.addOutput(primary); // this phase is analysis only |
36 seen.add(id); | 38 seen.add(id); |
| 39 bool isEntryPoint = options.isHtmlEntryPoint(id); |
| 40 |
| 41 var logger = new WrappedLogger(transform, |
| 42 convertErrorsToWarnings: !options.releaseMode); |
| 43 |
37 return readPrimaryAsHtml(transform).then((document) { | 44 return readPrimaryAsHtml(transform).then((document) { |
38 return _collectElements(document, id, transform, seen).then((elements) { | 45 return _collectElements(document, id, transform, logger, seen) |
39 bool isEntrypoint = options.isHtmlEntryPoint(id); | 46 .then((elements) { |
40 new _LinterVisitor(id, transform.logger, elements, isEntrypoint) | 47 new _LinterVisitor(id, logger, elements, isEntryPoint).run(document); |
41 .run(document); | 48 |
| 49 // Write out the logs collected by our [WrappedLogger]. |
| 50 if (options.injectBuildLogsInOutput) return logger.writeOutput(); |
42 }); | 51 }); |
43 }); | 52 }); |
44 } | 53 } |
45 | 54 |
46 /// Collect into [elements] any data about each polymer-element defined in | 55 /// Collect into [elements] any data about each polymer-element defined in |
47 /// [document] or any of it's imports, unless they have already been [seen]. | 56 /// [document] or any of it's imports, unless they have already been [seen]. |
48 /// Elements are added in the order they appear, transitive imports are added | 57 /// Elements are added in the order they appear, transitive imports are added |
49 /// first. | 58 /// first. |
50 Future<Map<String, _ElementSummary>> _collectElements( | 59 Future<Map<String, _ElementSummary>> _collectElements( |
51 Document document, AssetId sourceId, Transform transform, | 60 Document document, AssetId sourceId, Transform transform, |
52 Set<AssetId> seen, [Map<String, _ElementSummary> elements]) { | 61 TransformLogger logger, Set<AssetId> seen, |
| 62 [Map<String, _ElementSummary> elements]) { |
53 if (elements == null) elements = <String, _ElementSummary>{}; | 63 if (elements == null) elements = <String, _ElementSummary>{}; |
54 return _getImportedIds(document, sourceId, transform) | 64 return _getImportedIds(document, sourceId, transform, logger) |
55 // Note: the import order is relevant, so we visit in that order. | 65 // Note: the import order is relevant, so we visit in that order. |
56 .then((ids) => Future.forEach(ids, | 66 .then((ids) => Future.forEach(ids, |
57 (id) => _readAndCollectElements(id, transform, seen, elements))) | 67 (id) => _readAndCollectElements( |
| 68 id, transform, logger, seen, elements))) |
58 .then((_) { | 69 .then((_) { |
59 if (sourceId.package == 'polymer' && | 70 if (sourceId.package == 'polymer' && |
60 sourceId.path == 'lib/src/js/polymer/polymer.html' && | 71 sourceId.path == 'lib/src/js/polymer/polymer.html' && |
61 elements['polymer-element'] == null) { | 72 elements['polymer-element'] == null) { |
62 elements['polymer-element'] = | 73 elements['polymer-element'] = |
63 new _ElementSummary('polymer-element', null, null); | 74 new _ElementSummary('polymer-element', null, null); |
64 } | 75 } |
65 return _addElements(document, transform.logger, elements); | 76 return _addElements(document, logger, elements); |
66 }) | 77 }) |
67 .then((_) => elements); | 78 .then((_) => elements); |
68 } | 79 } |
69 | 80 |
70 Future _readAndCollectElements(AssetId id, Transform transform, | 81 Future _readAndCollectElements(AssetId id, Transform transform, |
71 Set<AssetId> seen, Map<String, _ElementSummary> elements) { | 82 TransformLogger logger, Set<AssetId> seen, |
| 83 Map<String, _ElementSummary> elements) { |
72 if (id == null || seen.contains(id)) return new Future.value(null); | 84 if (id == null || seen.contains(id)) return new Future.value(null); |
73 seen.add(id); | 85 seen.add(id); |
74 return readAsHtml(id, transform, showWarnings: false).then( | 86 return readAsHtml(id, transform, showWarnings: false).then( |
75 (doc) => _collectElements(doc, id, transform, seen, elements)); | 87 (doc) => _collectElements(doc, id, transform, logger, seen, elements)); |
76 } | 88 } |
77 | 89 |
78 Future<List<AssetId>> _getImportedIds( | 90 Future<List<AssetId>> _getImportedIds( |
79 Document document, AssetId sourceId, Transform transform) { | 91 Document document, AssetId sourceId, Transform transform, |
| 92 TransformLogger logger) { |
80 var importIds = []; | 93 var importIds = []; |
81 var logger = transform.logger; | |
82 for (var tag in document.querySelectorAll('link')) { | 94 for (var tag in document.querySelectorAll('link')) { |
83 if (tag.attributes['rel'] != 'import') continue; | 95 if (tag.attributes['rel'] != 'import') continue; |
84 var href = tag.attributes['href']; | 96 var href = tag.attributes['href']; |
85 var span = tag.sourceSpan; | 97 var span = tag.sourceSpan; |
86 var id = uriToAssetId(sourceId, href, logger, span); | 98 var id = uriToAssetId(sourceId, href, logger, span); |
87 if (id == null) continue; | 99 if (id == null) continue; |
88 importIds.add(assetExists(id, transform).then((exists) { | 100 importIds.add(assetExists(id, transform).then((exists) { |
89 if (exists) return id; | 101 if (exists) return id; |
90 if (sourceId == transform.primaryInput.id) { | 102 if (sourceId == transform.primaryInput.id) { |
91 logger.warning('couldn\'t find imported asset "${id.path}" in package' | 103 logger.warning('couldn\'t find imported asset "${id.path}" in package' |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 String toString() => "($tagName <: $extendsTag)"; | 158 String toString() => "($tagName <: $extendsTag)"; |
147 } | 159 } |
148 | 160 |
149 class _LinterVisitor extends TreeVisitor { | 161 class _LinterVisitor extends TreeVisitor { |
150 TransformLogger _logger; | 162 TransformLogger _logger; |
151 AssetId _sourceId; | 163 AssetId _sourceId; |
152 bool _inPolymerElement = false; | 164 bool _inPolymerElement = false; |
153 bool _dartTagSeen = false; | 165 bool _dartTagSeen = false; |
154 bool _polymerHtmlSeen = false; | 166 bool _polymerHtmlSeen = false; |
155 bool _polymerExperimentalHtmlSeen = false; | 167 bool _polymerExperimentalHtmlSeen = false; |
156 bool _isEntrypoint; | 168 bool _isEntryPoint; |
157 Map<String, _ElementSummary> _elements; | 169 Map<String, _ElementSummary> _elements; |
158 | 170 |
159 _LinterVisitor( | 171 _LinterVisitor( |
160 this._sourceId, this._logger, this._elements, this._isEntrypoint) { | 172 this._sourceId, this._logger, this._elements, this._isEntryPoint) { |
161 // We normalize the map, so each element has a direct reference to any | 173 // We normalize the map, so each element has a direct reference to any |
162 // element it extends from. | 174 // element it extends from. |
163 for (var tag in _elements.values) { | 175 for (var tag in _elements.values) { |
164 var extendsTag = tag.extendsTag; | 176 var extendsTag = tag.extendsTag; |
165 if (extendsTag == null) continue; | 177 if (extendsTag == null) continue; |
166 tag.extendsType = _elements[extendsTag]; | 178 tag.extendsType = _elements[extendsTag]; |
167 } | 179 } |
168 } | 180 } |
169 | 181 |
170 void visitElement(Element node) { | 182 void visitElement(Element node) { |
171 switch (node.localName) { | 183 switch (node.localName) { |
172 case 'link': _validateLinkElement(node); break; | 184 case 'link': _validateLinkElement(node); break; |
173 case 'element': _validateElementElement(node); break; | 185 case 'element': _validateElementElement(node); break; |
174 case 'polymer-element': _validatePolymerElement(node); break; | 186 case 'polymer-element': _validatePolymerElement(node); break; |
175 case 'script': _validateScriptElement(node); break; | 187 case 'script': _validateScriptElement(node); break; |
176 default: | 188 default: |
177 _validateNormalElement(node); | 189 _validateNormalElement(node); |
178 super.visitElement(node); | 190 super.visitElement(node); |
179 break; | 191 break; |
180 } | 192 } |
181 } | 193 } |
182 | 194 |
183 void run(Document doc) { | 195 void run(Document doc) { |
184 visit(doc); | 196 visit(doc); |
185 | 197 |
186 if (_isEntrypoint && !_dartTagSeen && !_polymerExperimentalHtmlSeen) { | 198 if (_isEntryPoint && !_dartTagSeen && !_polymerExperimentalHtmlSeen) { |
187 _logger.warning(USE_INIT_DART, span: doc.body.sourceSpan); | 199 _logger.warning(USE_INIT_DART, span: doc.body.sourceSpan); |
188 } | 200 } |
189 } | 201 } |
190 | 202 |
191 /// Produce warnings for invalid link-rel tags. | 203 /// Produce warnings for invalid link-rel tags. |
192 void _validateLinkElement(Element node) { | 204 void _validateLinkElement(Element node) { |
193 var rel = node.attributes['rel']; | 205 var rel = node.attributes['rel']; |
194 if (rel != 'import' && rel != 'stylesheet') return; | 206 if (rel != 'import' && rel != 'stylesheet') return; |
195 | 207 |
196 if (rel == 'import' && _dartTagSeen) { | 208 if (rel == 'import' && _dartTagSeen) { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 } | 286 } |
275 | 287 |
276 /// Checks for multiple Dart script tags in the same page, which is invalid. | 288 /// Checks for multiple Dart script tags in the same page, which is invalid. |
277 void _validateScriptElement(Element node) { | 289 void _validateScriptElement(Element node) { |
278 var scriptType = node.attributes['type']; | 290 var scriptType = node.attributes['type']; |
279 var isDart = scriptType == 'application/dart'; | 291 var isDart = scriptType == 'application/dart'; |
280 var src = node.attributes['src']; | 292 var src = node.attributes['src']; |
281 | 293 |
282 if (isDart) { | 294 if (isDart) { |
283 if (_dartTagSeen) _logger.warning(ONLY_ONE_TAG, span: node.sourceSpan); | 295 if (_dartTagSeen) _logger.warning(ONLY_ONE_TAG, span: node.sourceSpan); |
284 if (_isEntrypoint && _polymerExperimentalHtmlSeen) { | 296 if (_isEntryPoint && _polymerExperimentalHtmlSeen) { |
285 _logger.warning(NO_DART_SCRIPT_AND_EXPERIMENTAL, span: node.sourceSpan); | 297 _logger.warning(NO_DART_SCRIPT_AND_EXPERIMENTAL, span: node.sourceSpan); |
286 } | 298 } |
287 _dartTagSeen = true; | 299 _dartTagSeen = true; |
288 } | 300 } |
289 | 301 |
290 var isEmpty = node.innerHtml.trim() == ''; | 302 var isEmpty = node.innerHtml.trim() == ''; |
291 | 303 |
292 if (src == null) { | 304 if (src == null) { |
293 if (isDart && isEmpty) { | 305 if (isDart && isEmpty) { |
294 _logger.warning('script tag seems empty.', span: node.sourceSpan); | 306 _logger.warning('script tag seems empty.', span: node.sourceSpan); |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 'either include a generic script tag that does this for you:' | 461 'either include a generic script tag that does this for you:' |
450 '\'<script type="application/dart">export "package:polymer/init.dart";' | 462 '\'<script type="application/dart">export "package:polymer/init.dart";' |
451 '</script>\' or add your own script tag and call that function. ' | 463 '</script>\' or add your own script tag and call that function. ' |
452 'Make sure the script tag is placed after all HTML imports.'; | 464 'Make sure the script tag is placed after all HTML imports.'; |
453 | 465 |
454 const String NO_DART_SCRIPT_AND_EXPERIMENTAL = | 466 const String NO_DART_SCRIPT_AND_EXPERIMENTAL = |
455 'The experimental bootstrap feature doesn\'t support script tags on ' | 467 'The experimental bootstrap feature doesn\'t support script tags on ' |
456 'the main document (for now).'; | 468 'the main document (for now).'; |
457 | 469 |
458 const List<String> INTERNALLY_DEFINED_ELEMENTS = | 470 const List<String> INTERNALLY_DEFINED_ELEMENTS = |
459 const ['auto-binding-dart', 'polymer-element']; | 471 const ['auto-binding-dart', 'polymer-element']; |
OLD | NEW |