Chromium Code Reviews| 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 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 | 30 |
| 31 Future apply(Transform transform) { | 31 Future apply(Transform transform) { |
| 32 var seen = new Set<AssetId>(); | 32 var seen = new Set<AssetId>(); |
| 33 var primary = transform.primaryInput; | 33 var primary = transform.primaryInput; |
| 34 var id = primary.id; | 34 var id = primary.id; |
| 35 transform.addOutput(primary); // this phase is analysis only | 35 transform.addOutput(primary); // this phase is analysis only |
| 36 seen.add(id); | 36 seen.add(id); |
| 37 return readPrimaryAsHtml(transform).then((document) { | 37 return readPrimaryAsHtml(transform).then((document) { |
| 38 return _collectElements(document, id, transform, seen).then((elements) { | 38 return _collectElements(document, id, transform, seen).then((elements) { |
| 39 bool isEntrypoint = options.isHtmlEntryPoint(id); | 39 bool isEntrypoint = options.isHtmlEntryPoint(id); |
| 40 new _LinterVisitor(transform.logger, elements, isEntrypoint) | 40 new _LinterVisitor(id, transform.logger, elements, isEntrypoint) |
| 41 .run(document); | 41 .run(document); |
| 42 }); | 42 }); |
| 43 }); | 43 }); |
| 44 } | 44 } |
| 45 | 45 |
| 46 /// Collect into [elements] any data about each polymer-element defined in | 46 /// 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]. | 47 /// [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 | 48 /// Elements are added in the order they appear, transitive imports are added |
| 49 /// first. | 49 /// first. |
| 50 Future<Map<String, _ElementSummary>> _collectElements( | 50 Future<Map<String, _ElementSummary>> _collectElements( |
| 51 Document document, AssetId sourceId, Transform transform, | 51 Document document, AssetId sourceId, Transform transform, |
| 52 Set<AssetId> seen, [Map<String, _ElementSummary> elements]) { | 52 Set<AssetId> seen, [Map<String, _ElementSummary> elements]) { |
| 53 if (elements == null) elements = <String, _ElementSummary>{}; | 53 if (elements == null) elements = <String, _ElementSummary>{}; |
| 54 return _getImportedIds(document, sourceId, transform) | 54 return _getImportedIds(document, sourceId, transform) |
| 55 // Note: the import order is relevant, so we visit in that order. | 55 // Note: the import order is relevant, so we visit in that order. |
| 56 .then((ids) => Future.forEach(ids, | 56 .then((ids) => Future.forEach(ids, |
| 57 (id) => _readAndCollectElements(id, transform, seen, elements))) | 57 (id) => _readAndCollectElements(id, transform, seen, elements))) |
| 58 .then((_) => _addElements(document, transform.logger, elements)) | 58 .then((_) { |
| 59 if (sourceId.package == 'polymer' && | |
| 60 sourceId.path == 'lib/src/js/polymer/polymer.html' && | |
| 61 elements['polymer-element'] == null) { | |
| 62 elements['polymer-element'] = | |
| 63 new _ElementSummary('polymer-element', null, null); | |
| 64 } | |
| 65 return _addElements(document, transform.logger, elements); | |
| 66 }) | |
| 59 .then((_) => elements); | 67 .then((_) => elements); |
| 60 } | 68 } |
| 61 | 69 |
| 62 Future _readAndCollectElements(AssetId id, Transform transform, | 70 Future _readAndCollectElements(AssetId id, Transform transform, |
| 63 Set<AssetId> seen, Map<String, _ElementSummary> elements) { | 71 Set<AssetId> seen, Map<String, _ElementSummary> elements) { |
| 64 if (id == null || seen.contains(id)) return new Future.value(null); | 72 if (id == null || seen.contains(id)) return new Future.value(null); |
| 65 seen.add(id); | 73 seen.add(id); |
| 66 return readAsHtml(id, transform).then( | 74 return readAsHtml(id, transform).then( |
| 67 (doc) => _collectElements(doc, id, transform, seen, elements)); | 75 (doc) => _collectElements(doc, id, transform, seen, elements)); |
| 68 } | 76 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 String get baseExtendsTag => extendsType == null | 138 String get baseExtendsTag => extendsType == null |
| 131 ? extendsTag : extendsType.baseExtendsTag; | 139 ? extendsTag : extendsType.baseExtendsTag; |
| 132 | 140 |
| 133 _ElementSummary(this.tagName, this.extendsTag, this.span); | 141 _ElementSummary(this.tagName, this.extendsTag, this.span); |
| 134 | 142 |
| 135 String toString() => "($tagName <: $extendsTag)"; | 143 String toString() => "($tagName <: $extendsTag)"; |
| 136 } | 144 } |
| 137 | 145 |
| 138 class _LinterVisitor extends TreeVisitor { | 146 class _LinterVisitor extends TreeVisitor { |
| 139 TransformLogger _logger; | 147 TransformLogger _logger; |
| 148 AssetId _sourceId; | |
| 140 bool _inPolymerElement = false; | 149 bool _inPolymerElement = false; |
| 141 bool _dartTagSeen = false; | 150 bool _dartTagSeen = false; |
| 142 bool _polymerHtmlSeen = false; | 151 bool _polymerHtmlSeen = false; |
| 143 bool _polymerExperimentalHtmlSeen = false; | 152 bool _polymerExperimentalHtmlSeen = false; |
| 144 bool _isEntrypoint; | 153 bool _isEntrypoint; |
| 145 Map<String, _ElementSummary> _elements; | 154 Map<String, _ElementSummary> _elements; |
| 146 | 155 |
| 147 _LinterVisitor(this._logger, this._elements, this._isEntrypoint) { | 156 _LinterVisitor( |
| 157 this._sourceId, this._logger, this._elements, this._isEntrypoint) { | |
| 148 // We normalize the map, so each element has a direct reference to any | 158 // We normalize the map, so each element has a direct reference to any |
| 149 // element it extends from. | 159 // element it extends from. |
| 150 for (var tag in _elements.values) { | 160 for (var tag in _elements.values) { |
| 151 var extendsTag = tag.extendsTag; | 161 var extendsTag = tag.extendsTag; |
| 152 if (extendsTag == null) continue; | 162 if (extendsTag == null) continue; |
| 153 tag.extendsType = _elements[extendsTag]; | 163 tag.extendsType = _elements[extendsTag]; |
| 154 } | 164 } |
| 155 } | 165 } |
| 156 | 166 |
| 157 void visitElement(Element node) { | 167 void visitElement(Element node) { |
| 158 switch (node.localName) { | 168 switch (node.localName) { |
| 159 case 'link': _validateLinkElement(node); break; | 169 case 'link': _validateLinkElement(node); break; |
| 160 case 'element': _validateElementElement(node); break; | 170 case 'element': _validateElementElement(node); break; |
| 161 case 'polymer-element': _validatePolymerElement(node); break; | 171 case 'polymer-element': _validatePolymerElement(node); break; |
| 162 case 'script': _validateScriptElement(node); break; | 172 case 'script': _validateScriptElement(node); break; |
| 163 default: | 173 default: |
| 164 _validateNormalElement(node); | 174 _validateNormalElement(node); |
| 165 super.visitElement(node); | 175 super.visitElement(node); |
| 166 break; | 176 break; |
| 167 } | 177 } |
| 168 } | 178 } |
| 169 | 179 |
| 170 void run(Document doc) { | 180 void run(Document doc) { |
| 171 visit(doc); | 181 visit(doc); |
| 172 | 182 |
| 173 if (_isEntrypoint && !_polymerHtmlSeen && !_polymerExperimentalHtmlSeen) { | |
| 174 _logger.warning(USE_POLYMER_HTML, span: doc.body.sourceSpan); | |
| 175 } | |
| 176 | |
| 177 if (_isEntrypoint && !_dartTagSeen && !_polymerExperimentalHtmlSeen) { | 183 if (_isEntrypoint && !_dartTagSeen && !_polymerExperimentalHtmlSeen) { |
| 178 _logger.warning(USE_INIT_DART, span: doc.body.sourceSpan); | 184 _logger.warning(USE_INIT_DART, span: doc.body.sourceSpan); |
| 179 } | 185 } |
| 180 } | 186 } |
| 181 | 187 |
| 182 /// Produce warnings for invalid link-rel tags. | 188 /// Produce warnings for invalid link-rel tags. |
| 183 void _validateLinkElement(Element node) { | 189 void _validateLinkElement(Element node) { |
| 184 var rel = node.attributes['rel']; | 190 var rel = node.attributes['rel']; |
| 185 if (rel != 'import' && rel != 'stylesheet') return; | 191 if (rel != 'import' && rel != 'stylesheet') return; |
| 186 | 192 |
| 187 if (rel == 'import' && _dartTagSeen) { | 193 if (rel == 'import' && _dartTagSeen) { |
| 188 _logger.warning("Move HTML imports above your Dart script tag.", | 194 _logger.warning("Move HTML imports above your Dart script tag.", |
| 189 span: node.sourceSpan); | 195 span: node.sourceSpan); |
| 190 } | 196 } |
| 191 | 197 |
| 192 var href = node.attributes['href']; | 198 var href = node.attributes['href']; |
| 193 if (href == null || href == '') { | 199 if (href == null || href == '') { |
| 194 _logger.warning('link rel="$rel" missing href.', span: node.sourceSpan); | 200 _logger.warning('link rel="$rel" missing href.', span: node.sourceSpan); |
| 195 return; | 201 return; |
| 196 } | 202 } |
| 197 | 203 |
| 198 if (href == 'packages/polymer/polymer.html') { | 204 if (rel != 'import') return; |
| 199 _polymerHtmlSeen = true; | 205 |
| 200 } else if (href == POLYMER_EXPERIMENTAL_HTML) { | 206 if (_inPolymerElement) { |
| 207 _logger.error(NO_IMPORT_WITHIN_ELEMENT, span: node.sourceSpan); | |
|
Siggi Cherem (dart-lang)
2014/06/13 20:08:37
this is unrelated to the rest of the changes, but
| |
| 208 return; | |
| 209 } | |
| 210 | |
| 211 if (href == POLYMER_EXPERIMENTAL_HTML) { | |
| 201 _polymerExperimentalHtmlSeen = true; | 212 _polymerExperimentalHtmlSeen = true; |
| 202 } | 213 } |
| 203 // TODO(sigmund): warn also if href can't be resolved. | 214 // TODO(sigmund): warn also if href can't be resolved. |
| 204 } | 215 } |
| 205 | 216 |
| 206 /// Produce warnings if using `<element>` instead of `<polymer-element>`. | 217 /// Produce warnings if using `<element>` instead of `<polymer-element>`. |
| 207 void _validateElementElement(Element node) { | 218 void _validateElementElement(Element node) { |
| 208 _logger.warning('<element> elements are not supported, use' | 219 _logger.warning('<element> elements are not supported, use' |
| 209 ' <polymer-element> instead', span: node.sourceSpan); | 220 ' <polymer-element> instead', span: node.sourceSpan); |
| 210 } | 221 } |
| 211 | 222 |
| 212 /// Produce warnings if using `<polymer-element>` in the wrong place or if the | 223 /// Produce warnings if using `<polymer-element>` in the wrong place or if the |
| 213 /// definition is not complete. | 224 /// definition is not complete. |
| 214 void _validatePolymerElement(Element node) { | 225 void _validatePolymerElement(Element node) { |
| 226 if (!_elements.containsKey('polymer-element')) { | |
| 227 _logger.warning(usePolymerHtmlMessageFrom(_sourceId), | |
| 228 span: node.sourceSpan); | |
| 229 } | |
| 230 | |
| 215 if (_inPolymerElement) { | 231 if (_inPolymerElement) { |
| 216 _logger.error('Nested polymer element definitions are not allowed.', | 232 _logger.error('Nested polymer element definitions are not allowed.', |
| 217 span: node.sourceSpan); | 233 span: node.sourceSpan); |
| 218 return; | 234 return; |
| 219 } | 235 } |
| 220 | 236 |
| 221 var tagName = node.attributes['name']; | 237 var tagName = node.attributes['name']; |
| 222 var extendsTag = node.attributes['extends']; | 238 var extendsTag = node.attributes['extends']; |
| 223 | 239 |
| 224 if (tagName == null) { | 240 if (tagName == null) { |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 'in your custom element "void handlerName(event, detail, target)" ' | 410 'in your custom element "void handlerName(event, detail, target)" ' |
| 395 'and use the form $name="{{handlerName}}".', | 411 'and use the form $name="{{handlerName}}".', |
| 396 span: node.attributeSpans[name]); | 412 span: node.attributeSpans[name]); |
| 397 } | 413 } |
| 398 } | 414 } |
| 399 } | 415 } |
| 400 | 416 |
| 401 const String ONLY_ONE_TAG = | 417 const String ONLY_ONE_TAG = |
| 402 'Only one "application/dart" script tag per document is allowed.'; | 418 'Only one "application/dart" script tag per document is allowed.'; |
| 403 | 419 |
| 404 const String USE_POLYMER_HTML = | 420 String usePolymerHtmlMessageFrom(AssetId id) { |
| 405 'Besides the initPolymer invocation, to run a polymer application you need ' | 421 var segments = id.path.split('/'); |
| 406 'to include the following HTML import: ' | 422 var upDirCount = 0; |
| 407 '<link rel="import" href="packages/polymer/polymer.html">. This will ' | 423 if (segments[0] == 'lib') { |
| 408 'include the common polymer logic needed to boostrap your application.'; | 424 // lib/foo.html => ../../packages/ |
| 425 upDirCount = segments.length; | |
| 426 } else if (segments.length > 2) { | |
| 427 // web/a/foo.html => ../packages/ | |
| 428 upDirCount = segments.length - 2; | |
| 429 } | |
| 430 return usePolymerHtmlMessage(upDirCount); | |
| 431 } | |
| 432 | |
| 433 String usePolymerHtmlMessage(int upDirCount) { | |
| 434 var reachOutPrefix = '../' * upDirCount; | |
| 435 return 'Missing definition for <polymer-element>, please add the following ' | |
| 436 'HTML import at the top of this file: <link rel="import" ' | |
| 437 'href="${reachOutPrefix}packages/polymer/polymer.html">.'; | |
| 438 } | |
| 439 | |
| 440 const String NO_IMPORT_WITHIN_ELEMENT = 'HTML imports are not supported ' | |
|
Jennifer Messerly
2014/06/13 21:07:47
maybe improve the wording so it's clearly an issue
Siggi Cherem (dart-lang)
2014/06/13 22:15:28
Done.
| |
| 441 'within polymer element definitions, yet. Please move the import out of ' | |
| 442 'this <polymer-element>.'; | |
| 409 | 443 |
| 410 const String USE_INIT_DART = | 444 const String USE_INIT_DART = |
| 411 'To run a polymer application, you need to call "initPolymer". You can ' | 445 'To run a polymer application, you need to call "initPolymer". You can ' |
| 412 'either include a generic script tag that does this for you:' | 446 'either include a generic script tag that does this for you:' |
| 413 '\'<script type="application/dart">export "package:polymer/init.dart";' | 447 '\'<script type="application/dart">export "package:polymer/init.dart";' |
| 414 '</script>\' or add your own script tag and call that function. ' | 448 '</script>\' or add your own script tag and call that function. ' |
| 415 'Make sure the script tag is placed after all HTML imports.'; | 449 'Make sure the script tag is placed after all HTML imports.'; |
| 416 | 450 |
| 417 const String NO_DART_SCRIPT_AND_EXPERIMENTAL = | 451 const String NO_DART_SCRIPT_AND_EXPERIMENTAL = |
| 418 'The experimental bootstrap feature doesn\'t support script tags on ' | 452 'The experimental bootstrap feature doesn\'t support script tags on ' |
| 419 'the main document (for now).'; | 453 'the main document (for now).'; |
| OLD | NEW |