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

Side by Side Diff: pkg/polymer/lib/src/build/linter.dart

Issue 427623002: Polymer transformer logs now show on the frontend for pub serve. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Moved some logic to the isPrimary function Created 6 years, 4 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 /// 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_maps/span.dart'; 16 import 'package:source_maps/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 {
26
Siggi Cherem (dart-lang) 2014/08/01 21:31:51 nit: remove
jakemac 2014/08/04 19:49:58 Done.
24 final TransformOptions options; 27 final TransformOptions options;
25 28
26 /// Only run on .html files. 29 /// Only run on .html files.
27 final String allowedExtensions = '.html'; 30 final String allowedExtensions = '.html';
28 31
29 Linter(this.options); 32 Linter(this.options);
30 33
31 Future apply(Transform transform) { 34 Future apply(Transform transform) {
32 var seen = new Set<AssetId>(); 35 var seen = new Set<AssetId>();
33 var primary = transform.primaryInput; 36 var primary = transform.primaryInput;
34 var id = primary.id; 37 var id = primary.id;
35 transform.addOutput(primary); // this phase is analysis only 38 transform.addOutput(primary); // this phase is analysis only
36 seen.add(id); 39 seen.add(id);
40 bool isEntryPoint = options.isHtmlEntryPoint(id);
41
42 var logger = transform.logger;
43 if (!options.releaseMode) {
44 logger = new WrappedLogger(transform, convertErrorsToWarnings: true);
45 }
46
37 return readPrimaryAsHtml(transform).then((document) { 47 return readPrimaryAsHtml(transform).then((document) {
38 return _collectElements(document, id, transform, seen).then((elements) { 48 return _collectElements(document, id, transform, logger, seen)
39 bool isEntrypoint = options.isHtmlEntryPoint(id); 49 .then((elements) {
40 new _LinterVisitor(id, transform.logger, elements, isEntrypoint) 50 new _LinterVisitor(id, logger, elements, isEntryPoint).run(document);
41 .run(document); 51
52 // Write out the logs collected by our [WrappedLogger].
53 if (logger is WrappedLogger) return logger.writeOutput();
42 }); 54 });
43 }); 55 });
44 } 56 }
45 57
46 /// Collect into [elements] any data about each polymer-element defined in 58 /// 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]. 59 /// [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 60 /// Elements are added in the order they appear, transitive imports are added
49 /// first. 61 /// first.
50 Future<Map<String, _ElementSummary>> _collectElements( 62 Future<Map<String, _ElementSummary>> _collectElements(
51 Document document, AssetId sourceId, Transform transform, 63 Document document, AssetId sourceId, Transform transform,
52 Set<AssetId> seen, [Map<String, _ElementSummary> elements]) { 64 TransformLogger logger, Set<AssetId> seen,
65 [Map<String, _ElementSummary> elements]) {
53 if (elements == null) elements = <String, _ElementSummary>{}; 66 if (elements == null) elements = <String, _ElementSummary>{};
54 return _getImportedIds(document, sourceId, transform) 67 return _getImportedIds(document, sourceId, transform, logger)
55 // Note: the import order is relevant, so we visit in that order. 68 // Note: the import order is relevant, so we visit in that order.
56 .then((ids) => Future.forEach(ids, 69 .then((ids) => Future.forEach(ids,
57 (id) => _readAndCollectElements(id, transform, seen, elements))) 70 (id) => _readAndCollectElements(
71 id, transform, logger, seen, elements)))
58 .then((_) { 72 .then((_) {
59 if (sourceId.package == 'polymer' && 73 if (sourceId.package == 'polymer' &&
60 sourceId.path == 'lib/src/js/polymer/polymer.html' && 74 sourceId.path == 'lib/src/js/polymer/polymer.html' &&
61 elements['polymer-element'] == null) { 75 elements['polymer-element'] == null) {
62 elements['polymer-element'] = 76 elements['polymer-element'] =
63 new _ElementSummary('polymer-element', null, null); 77 new _ElementSummary('polymer-element', null, null);
64 } 78 }
65 return _addElements(document, transform.logger, elements); 79 return _addElements(document, logger, elements);
66 }) 80 })
67 .then((_) => elements); 81 .then((_) => elements);
68 } 82 }
69 83
70 Future _readAndCollectElements(AssetId id, Transform transform, 84 Future _readAndCollectElements(AssetId id, Transform transform,
71 Set<AssetId> seen, Map<String, _ElementSummary> elements) { 85 TransformLogger logger, Set<AssetId> seen,
86 Map<String, _ElementSummary> elements) {
72 if (id == null || seen.contains(id)) return new Future.value(null); 87 if (id == null || seen.contains(id)) return new Future.value(null);
73 seen.add(id); 88 seen.add(id);
74 return readAsHtml(id, transform, showWarnings: false).then( 89 return readAsHtml(id, transform, showWarnings: false).then(
75 (doc) => _collectElements(doc, id, transform, seen, elements)); 90 (doc) => _collectElements(doc, id, transform, logger, seen, elements));
76 } 91 }
77 92
78 Future<List<AssetId>> _getImportedIds( 93 Future<List<AssetId>> _getImportedIds(
79 Document document, AssetId sourceId, Transform transform) { 94 Document document, AssetId sourceId, Transform transform,
95 TransformLogger logger) {
80 var importIds = []; 96 var importIds = [];
81 var logger = transform.logger;
82 for (var tag in document.querySelectorAll('link')) { 97 for (var tag in document.querySelectorAll('link')) {
83 if (tag.attributes['rel'] != 'import') continue; 98 if (tag.attributes['rel'] != 'import') continue;
84 var href = tag.attributes['href']; 99 var href = tag.attributes['href'];
85 var span = tag.sourceSpan; 100 var span = tag.sourceSpan;
86 var id = uriToAssetId(sourceId, href, logger, span); 101 var id = uriToAssetId(sourceId, href, logger, span);
87 if (id == null) continue; 102 if (id == null) continue;
88 importIds.add(assetExists(id, transform).then((exists) { 103 importIds.add(assetExists(id, transform).then((exists) {
89 if (exists) return id; 104 if (exists) return id;
90 if (sourceId == transform.primaryInput.id) { 105 if (sourceId == transform.primaryInput.id) {
91 logger.warning('couldn\'t find imported asset "${id.path}" in package' 106 logger.warning('couldn\'t find imported asset "${id.path}" in package'
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 String toString() => "($tagName <: $extendsTag)"; 161 String toString() => "($tagName <: $extendsTag)";
147 } 162 }
148 163
149 class _LinterVisitor extends TreeVisitor { 164 class _LinterVisitor extends TreeVisitor {
150 TransformLogger _logger; 165 TransformLogger _logger;
151 AssetId _sourceId; 166 AssetId _sourceId;
152 bool _inPolymerElement = false; 167 bool _inPolymerElement = false;
153 bool _dartTagSeen = false; 168 bool _dartTagSeen = false;
154 bool _polymerHtmlSeen = false; 169 bool _polymerHtmlSeen = false;
155 bool _polymerExperimentalHtmlSeen = false; 170 bool _polymerExperimentalHtmlSeen = false;
156 bool _isEntrypoint; 171 bool _isEntryPoint;
157 Map<String, _ElementSummary> _elements; 172 Map<String, _ElementSummary> _elements;
158 173
159 _LinterVisitor( 174 _LinterVisitor(
160 this._sourceId, this._logger, this._elements, this._isEntrypoint) { 175 this._sourceId, this._logger, this._elements, this._isEntryPoint) {
161 // We normalize the map, so each element has a direct reference to any 176 // We normalize the map, so each element has a direct reference to any
162 // element it extends from. 177 // element it extends from.
163 for (var tag in _elements.values) { 178 for (var tag in _elements.values) {
164 var extendsTag = tag.extendsTag; 179 var extendsTag = tag.extendsTag;
165 if (extendsTag == null) continue; 180 if (extendsTag == null) continue;
166 tag.extendsType = _elements[extendsTag]; 181 tag.extendsType = _elements[extendsTag];
167 } 182 }
168 } 183 }
169 184
170 void visitElement(Element node) { 185 void visitElement(Element node) {
171 switch (node.localName) { 186 switch (node.localName) {
172 case 'link': _validateLinkElement(node); break; 187 case 'link': _validateLinkElement(node); break;
173 case 'element': _validateElementElement(node); break; 188 case 'element': _validateElementElement(node); break;
174 case 'polymer-element': _validatePolymerElement(node); break; 189 case 'polymer-element': _validatePolymerElement(node); break;
175 case 'script': _validateScriptElement(node); break; 190 case 'script': _validateScriptElement(node); break;
176 default: 191 default:
177 _validateNormalElement(node); 192 _validateNormalElement(node);
178 super.visitElement(node); 193 super.visitElement(node);
179 break; 194 break;
180 } 195 }
181 } 196 }
182 197
183 void run(Document doc) { 198 void run(Document doc) {
184 visit(doc); 199 visit(doc);
185 200
186 if (_isEntrypoint && !_dartTagSeen && !_polymerExperimentalHtmlSeen) { 201 if (_isEntryPoint && !_dartTagSeen && !_polymerExperimentalHtmlSeen) {
187 _logger.warning(USE_INIT_DART, span: doc.body.sourceSpan); 202 _logger.warning(USE_INIT_DART, span: doc.body.sourceSpan);
188 } 203 }
189 } 204 }
190 205
191 /// Produce warnings for invalid link-rel tags. 206 /// Produce warnings for invalid link-rel tags.
192 void _validateLinkElement(Element node) { 207 void _validateLinkElement(Element node) {
193 var rel = node.attributes['rel']; 208 var rel = node.attributes['rel'];
194 if (rel != 'import' && rel != 'stylesheet') return; 209 if (rel != 'import' && rel != 'stylesheet') return;
195 210
196 if (rel == 'import' && _dartTagSeen) { 211 if (rel == 'import' && _dartTagSeen) {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 } 289 }
275 290
276 /// Checks for multiple Dart script tags in the same page, which is invalid. 291 /// Checks for multiple Dart script tags in the same page, which is invalid.
277 void _validateScriptElement(Element node) { 292 void _validateScriptElement(Element node) {
278 var scriptType = node.attributes['type']; 293 var scriptType = node.attributes['type'];
279 var isDart = scriptType == 'application/dart'; 294 var isDart = scriptType == 'application/dart';
280 var src = node.attributes['src']; 295 var src = node.attributes['src'];
281 296
282 if (isDart) { 297 if (isDart) {
283 if (_dartTagSeen) _logger.warning(ONLY_ONE_TAG, span: node.sourceSpan); 298 if (_dartTagSeen) _logger.warning(ONLY_ONE_TAG, span: node.sourceSpan);
284 if (_isEntrypoint && _polymerExperimentalHtmlSeen) { 299 if (_isEntryPoint && _polymerExperimentalHtmlSeen) {
285 _logger.warning(NO_DART_SCRIPT_AND_EXPERIMENTAL, span: node.sourceSpan); 300 _logger.warning(NO_DART_SCRIPT_AND_EXPERIMENTAL, span: node.sourceSpan);
286 } 301 }
287 _dartTagSeen = true; 302 _dartTagSeen = true;
288 } 303 }
289 304
290 var isEmpty = node.innerHtml.trim() == ''; 305 var isEmpty = node.innerHtml.trim() == '';
291 306
292 if (src == null) { 307 if (src == null) {
293 if (isDart && isEmpty) { 308 if (isDart && isEmpty) {
294 _logger.warning('script tag seems empty.', span: node.sourceSpan); 309 _logger.warning('script tag seems empty.', span: node.sourceSpan);
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 'either include a generic script tag that does this for you:' 464 'either include a generic script tag that does this for you:'
450 '\'<script type="application/dart">export "package:polymer/init.dart";' 465 '\'<script type="application/dart">export "package:polymer/init.dart";'
451 '</script>\' or add your own script tag and call that function. ' 466 '</script>\' or add your own script tag and call that function. '
452 'Make sure the script tag is placed after all HTML imports.'; 467 'Make sure the script tag is placed after all HTML imports.';
453 468
454 const String NO_DART_SCRIPT_AND_EXPERIMENTAL = 469 const String NO_DART_SCRIPT_AND_EXPERIMENTAL =
455 'The experimental bootstrap feature doesn\'t support script tags on ' 470 'The experimental bootstrap feature doesn\'t support script tags on '
456 'the main document (for now).'; 471 'the main document (for now).';
457 472
458 const List<String> INTERNALLY_DEFINED_ELEMENTS = 473 const List<String> INTERNALLY_DEFINED_ELEMENTS =
459 const ['auto-binding-dart', 'polymer-element']; 474 const ['auto-binding-dart', 'polymer-element'];
Siggi Cherem (dart-lang) 2014/08/01 21:31:52 not sure what the diff means here, is your editor
Siggi Cherem (dart-lang) 2014/08/01 21:48:29 If there isn't one, and this is adding it back, th
jakemac 2014/08/04 19:49:58 Huh weird, I do have a newline so maybe thats what
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698