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 = options.releaseMode ? transform.logger : |
| 42 new WrappedLogger(transform, convertErrorsToWarnings: true); |
| 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 && logger is WrappedLogger) { |
| 51 return logger.writeOutput(); |
| 52 } |
42 }); | 53 }); |
43 }); | 54 }); |
44 } | 55 } |
45 | 56 |
46 /// Collect into [elements] any data about each polymer-element defined in | 57 /// 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]. | 58 /// [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 | 59 /// Elements are added in the order they appear, transitive imports are added |
49 /// first. | 60 /// first. |
50 Future<Map<String, _ElementSummary>> _collectElements( | 61 Future<Map<String, _ElementSummary>> _collectElements( |
51 Document document, AssetId sourceId, Transform transform, | 62 Document document, AssetId sourceId, Transform transform, |
52 Set<AssetId> seen, [Map<String, _ElementSummary> elements]) { | 63 TransformLogger logger, Set<AssetId> seen, |
| 64 [Map<String, _ElementSummary> elements]) { |
53 if (elements == null) elements = <String, _ElementSummary>{}; | 65 if (elements == null) elements = <String, _ElementSummary>{}; |
54 return _getImportedIds(document, sourceId, transform) | 66 return _getImportedIds(document, sourceId, transform, logger) |
55 // Note: the import order is relevant, so we visit in that order. | 67 // Note: the import order is relevant, so we visit in that order. |
56 .then((ids) => Future.forEach(ids, | 68 .then((ids) => Future.forEach(ids, |
57 (id) => _readAndCollectElements(id, transform, seen, elements))) | 69 (id) => _readAndCollectElements( |
| 70 id, transform, logger, seen, elements))) |
58 .then((_) { | 71 .then((_) { |
59 if (sourceId.package == 'polymer' && | 72 if (sourceId.package == 'polymer' && |
60 sourceId.path == 'lib/src/js/polymer/polymer.html' && | 73 sourceId.path == 'lib/src/js/polymer/polymer.html' && |
61 elements['polymer-element'] == null) { | 74 elements['polymer-element'] == null) { |
62 elements['polymer-element'] = | 75 elements['polymer-element'] = |
63 new _ElementSummary('polymer-element', null, null); | 76 new _ElementSummary('polymer-element', null, null); |
64 } | 77 } |
65 return _addElements(document, transform.logger, elements); | 78 return _addElements(document, logger, elements); |
66 }) | 79 }) |
67 .then((_) => elements); | 80 .then((_) => elements); |
68 } | 81 } |
69 | 82 |
70 Future _readAndCollectElements(AssetId id, Transform transform, | 83 Future _readAndCollectElements(AssetId id, Transform transform, |
71 Set<AssetId> seen, Map<String, _ElementSummary> elements) { | 84 TransformLogger logger, Set<AssetId> seen, |
| 85 Map<String, _ElementSummary> elements) { |
72 if (id == null || seen.contains(id)) return new Future.value(null); | 86 if (id == null || seen.contains(id)) return new Future.value(null); |
73 seen.add(id); | 87 seen.add(id); |
74 return readAsHtml(id, transform, showWarnings: false).then( | 88 return readAsHtml(id, transform, showWarnings: false).then( |
75 (doc) => _collectElements(doc, id, transform, seen, elements)); | 89 (doc) => _collectElements(doc, id, transform, logger, seen, elements)); |
76 } | 90 } |
77 | 91 |
78 Future<List<AssetId>> _getImportedIds( | 92 Future<List<AssetId>> _getImportedIds( |
79 Document document, AssetId sourceId, Transform transform) { | 93 Document document, AssetId sourceId, Transform transform, |
| 94 TransformLogger logger) { |
80 var importIds = []; | 95 var importIds = []; |
81 var logger = transform.logger; | |
82 for (var tag in document.querySelectorAll('link')) { | 96 for (var tag in document.querySelectorAll('link')) { |
83 if (tag.attributes['rel'] != 'import') continue; | 97 if (tag.attributes['rel'] != 'import') continue; |
84 var href = tag.attributes['href']; | 98 var href = tag.attributes['href']; |
85 var span = tag.sourceSpan; | 99 var span = tag.sourceSpan; |
86 var id = uriToAssetId(sourceId, href, logger, span); | 100 var id = uriToAssetId(sourceId, href, logger, span); |
87 if (id == null) continue; | 101 if (id == null) continue; |
88 importIds.add(assetExists(id, transform).then((exists) { | 102 importIds.add(assetExists(id, transform).then((exists) { |
89 if (exists) return id; | 103 if (exists) return id; |
90 if (sourceId == transform.primaryInput.id) { | 104 if (sourceId == transform.primaryInput.id) { |
91 logger.warning('couldn\'t find imported asset "${id.path}" in package' | 105 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)"; | 160 String toString() => "($tagName <: $extendsTag)"; |
147 } | 161 } |
148 | 162 |
149 class _LinterVisitor extends TreeVisitor { | 163 class _LinterVisitor extends TreeVisitor { |
150 TransformLogger _logger; | 164 TransformLogger _logger; |
151 AssetId _sourceId; | 165 AssetId _sourceId; |
152 bool _inPolymerElement = false; | 166 bool _inPolymerElement = false; |
153 bool _dartTagSeen = false; | 167 bool _dartTagSeen = false; |
154 bool _polymerHtmlSeen = false; | 168 bool _polymerHtmlSeen = false; |
155 bool _polymerExperimentalHtmlSeen = false; | 169 bool _polymerExperimentalHtmlSeen = false; |
156 bool _isEntrypoint; | 170 bool _isEntryPoint; |
157 Map<String, _ElementSummary> _elements; | 171 Map<String, _ElementSummary> _elements; |
158 | 172 |
159 _LinterVisitor( | 173 _LinterVisitor( |
160 this._sourceId, this._logger, this._elements, this._isEntrypoint) { | 174 this._sourceId, this._logger, this._elements, this._isEntryPoint) { |
161 // We normalize the map, so each element has a direct reference to any | 175 // We normalize the map, so each element has a direct reference to any |
162 // element it extends from. | 176 // element it extends from. |
163 for (var tag in _elements.values) { | 177 for (var tag in _elements.values) { |
164 var extendsTag = tag.extendsTag; | 178 var extendsTag = tag.extendsTag; |
165 if (extendsTag == null) continue; | 179 if (extendsTag == null) continue; |
166 tag.extendsType = _elements[extendsTag]; | 180 tag.extendsType = _elements[extendsTag]; |
167 } | 181 } |
168 } | 182 } |
169 | 183 |
170 void visitElement(Element node) { | 184 void visitElement(Element node) { |
171 switch (node.localName) { | 185 switch (node.localName) { |
172 case 'link': _validateLinkElement(node); break; | 186 case 'link': _validateLinkElement(node); break; |
173 case 'element': _validateElementElement(node); break; | 187 case 'element': _validateElementElement(node); break; |
174 case 'polymer-element': _validatePolymerElement(node); break; | 188 case 'polymer-element': _validatePolymerElement(node); break; |
175 case 'script': _validateScriptElement(node); break; | 189 case 'script': _validateScriptElement(node); break; |
176 default: | 190 default: |
177 _validateNormalElement(node); | 191 _validateNormalElement(node); |
178 super.visitElement(node); | 192 super.visitElement(node); |
179 break; | 193 break; |
180 } | 194 } |
181 } | 195 } |
182 | 196 |
183 void run(Document doc) { | 197 void run(Document doc) { |
184 visit(doc); | 198 visit(doc); |
185 | 199 |
186 if (_isEntrypoint && !_dartTagSeen && !_polymerExperimentalHtmlSeen) { | 200 if (_isEntryPoint && !_dartTagSeen && !_polymerExperimentalHtmlSeen) { |
187 _logger.warning(USE_INIT_DART, span: doc.body.sourceSpan); | 201 _logger.warning(USE_INIT_DART, span: doc.body.sourceSpan); |
188 } | 202 } |
189 } | 203 } |
190 | 204 |
191 /// Produce warnings for invalid link-rel tags. | 205 /// Produce warnings for invalid link-rel tags. |
192 void _validateLinkElement(Element node) { | 206 void _validateLinkElement(Element node) { |
193 var rel = node.attributes['rel']; | 207 var rel = node.attributes['rel']; |
194 if (rel != 'import' && rel != 'stylesheet') return; | 208 if (rel != 'import' && rel != 'stylesheet') return; |
195 | 209 |
196 if (rel == 'import' && _dartTagSeen) { | 210 if (rel == 'import' && _dartTagSeen) { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 } | 288 } |
275 | 289 |
276 /// Checks for multiple Dart script tags in the same page, which is invalid. | 290 /// Checks for multiple Dart script tags in the same page, which is invalid. |
277 void _validateScriptElement(Element node) { | 291 void _validateScriptElement(Element node) { |
278 var scriptType = node.attributes['type']; | 292 var scriptType = node.attributes['type']; |
279 var isDart = scriptType == 'application/dart'; | 293 var isDart = scriptType == 'application/dart'; |
280 var src = node.attributes['src']; | 294 var src = node.attributes['src']; |
281 | 295 |
282 if (isDart) { | 296 if (isDart) { |
283 if (_dartTagSeen) _logger.warning(ONLY_ONE_TAG, span: node.sourceSpan); | 297 if (_dartTagSeen) _logger.warning(ONLY_ONE_TAG, span: node.sourceSpan); |
284 if (_isEntrypoint && _polymerExperimentalHtmlSeen) { | 298 if (_isEntryPoint && _polymerExperimentalHtmlSeen) { |
285 _logger.warning(NO_DART_SCRIPT_AND_EXPERIMENTAL, span: node.sourceSpan); | 299 _logger.warning(NO_DART_SCRIPT_AND_EXPERIMENTAL, span: node.sourceSpan); |
286 } | 300 } |
287 _dartTagSeen = true; | 301 _dartTagSeen = true; |
288 } | 302 } |
289 | 303 |
290 var isEmpty = node.innerHtml.trim() == ''; | 304 var isEmpty = node.innerHtml.trim() == ''; |
291 | 305 |
292 if (src == null) { | 306 if (src == null) { |
293 if (isDart && isEmpty) { | 307 if (isDart && isEmpty) { |
294 _logger.warning('script tag seems empty.', span: node.sourceSpan); | 308 _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:' | 463 'either include a generic script tag that does this for you:' |
450 '\'<script type="application/dart">export "package:polymer/init.dart";' | 464 '\'<script type="application/dart">export "package:polymer/init.dart";' |
451 '</script>\' or add your own script tag and call that function. ' | 465 '</script>\' or add your own script tag and call that function. ' |
452 'Make sure the script tag is placed after all HTML imports.'; | 466 'Make sure the script tag is placed after all HTML imports.'; |
453 | 467 |
454 const String NO_DART_SCRIPT_AND_EXPERIMENTAL = | 468 const String NO_DART_SCRIPT_AND_EXPERIMENTAL = |
455 'The experimental bootstrap feature doesn\'t support script tags on ' | 469 'The experimental bootstrap feature doesn\'t support script tags on ' |
456 'the main document (for now).'; | 470 'the main document (for now).'; |
457 | 471 |
458 const List<String> INTERNALLY_DEFINED_ELEMENTS = | 472 const List<String> INTERNALLY_DEFINED_ELEMENTS = |
459 const ['auto-binding-dart', 'polymer-element']; | 473 const ['auto-binding-dart', 'polymer-element']; |
OLD | NEW |