OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library dart2js.analyze_helpers.test; | 5 library dart2js.analyze_helpers.test; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:async_helper/async_helper.dart'; | 9 import 'package:async_helper/async_helper.dart'; |
10 import 'package:compiler/compiler_new.dart' show | 10 import 'package:compiler/compiler_new.dart' show |
(...skipping 30 matching lines...) Expand all Loading... | |
41 Flags.analyzeMain, | 41 Flags.analyzeMain, |
42 '--categories=Client,Server']; | 42 '--categories=Client,Server']; |
43 if (verbose) { | 43 if (verbose) { |
44 options.add(Flags.verbose); | 44 options.add(Flags.verbose); |
45 } | 45 } |
46 asyncTest(() async { | 46 asyncTest(() async { |
47 CompilerImpl compiler = compilerFor( | 47 CompilerImpl compiler = compilerFor( |
48 options: options, showDiagnostics: verbose); | 48 options: options, showDiagnostics: verbose); |
49 FormattingDiagnosticHandler diagnostics = | 49 FormattingDiagnosticHandler diagnostics = |
50 new FormattingDiagnosticHandler(compiler.provider); | 50 new FormattingDiagnosticHandler(compiler.provider); |
51 HelperAnalyzer analyzer = new HelperAnalyzer(diagnostics); | |
52 Directory dir = | 51 Directory dir = |
53 new Directory.fromUri(Uri.base.resolve('pkg/compiler/lib/')); | 52 new Directory.fromUri(Uri.base.resolve('pkg/compiler/lib/')); |
53 String helpersUriPrefix = dir.uri.resolve('src/helpers/').toString(); | |
54 HelperAnalyzer analyzer = new HelperAnalyzer(diagnostics, helpersUriPrefix); | |
55 LibraryElement helperLibrary; | |
54 for (FileSystemEntity entity in dir.listSync(recursive: true)) { | 56 for (FileSystemEntity entity in dir.listSync(recursive: true)) { |
55 if (entity is File && entity.path.endsWith('.dart')) { | 57 if (entity is File && entity.path.endsWith('.dart')) { |
56 Uri file = Uri.base.resolve(nativeToUriPath(entity.path)); | 58 Uri file = Uri.base.resolve(nativeToUriPath(entity.path)); |
57 if (verbose) { | 59 if (verbose) { |
58 print('---- analyzing $file ----'); | 60 print('---- analyzing $file ----'); |
59 } | 61 } |
60 LibraryElement library = await compiler.analyzeUri(file); | 62 LibraryElement library = await compiler.analyzeUri(file); |
61 if (library != null) { | 63 if (library != null) { |
64 if (library.libraryName == 'dart2js.helpers') { | |
65 helperLibrary = library; | |
66 } | |
62 library.forEachLocalMember((Element element) { | 67 library.forEachLocalMember((Element element) { |
63 if (element is ClassElement) { | 68 if (element is ClassElement) { |
64 element.forEachLocalMember((AstElement member) { | 69 element.forEachLocalMember((AstElement member) { |
65 analyzer.analyze(member.resolvedAst); | 70 analyzer.analyze(member.resolvedAst); |
66 }); | 71 }); |
67 } else if (element is MemberElement) { | 72 } else if (element is MemberElement) { |
68 analyzer.analyze(element.resolvedAst); | 73 analyzer.analyze(element.resolvedAst); |
69 } | 74 } |
70 }); | 75 }); |
71 } | 76 } |
72 } | 77 } |
73 } | 78 } |
79 Expect.isNotNull(helperLibrary, 'Helper library not found'); | |
80 Expect.isTrue(analyzer.isHelper(helperLibrary), | |
81 "Helper library $helperLibrary is not considered a helper."); | |
74 Expect.isTrue(analyzer.errors.isEmpty, "Errors found."); | 82 Expect.isTrue(analyzer.errors.isEmpty, "Errors found."); |
75 }); | 83 }); |
76 } | 84 } |
77 | 85 |
78 class HelperAnalyzer extends TraversalVisitor { | 86 class HelperAnalyzer extends TraversalVisitor { |
79 final FormattingDiagnosticHandler diagnostics; | 87 final FormattingDiagnosticHandler diagnostics; |
88 final String helpersUriPrefix; | |
80 List<SourceSpan> errors = <SourceSpan>[]; | 89 List<SourceSpan> errors = <SourceSpan>[]; |
81 | 90 |
82 ResolvedAst resolvedAst; | 91 ResolvedAst resolvedAst; |
83 | 92 |
84 @override | 93 @override |
85 TreeElements get elements => resolvedAst.elements; | 94 TreeElements get elements => resolvedAst.elements; |
86 | 95 |
87 AnalyzableElement get analyzedElement => resolvedAst.element; | 96 AnalyzableElement get analyzedElement => resolvedAst.element; |
88 | 97 |
89 HelperAnalyzer(this.diagnostics) : super(null); | 98 HelperAnalyzer(this.diagnostics, this.helpersUriPrefix) : super(null); |
90 | 99 |
91 @override | 100 @override |
92 void apply(Node node, [_]) { | 101 void apply(Node node, [_]) { |
93 node.accept(this); | 102 node.accept(this); |
94 } | 103 } |
95 | 104 |
96 void analyze(ResolvedAst resolvedAst) { | 105 void analyze(ResolvedAst resolvedAst) { |
97 if (resolvedAst.kind != ResolvedAstKind.PARSED) { | 106 if (resolvedAst.kind != ResolvedAstKind.PARSED) { |
98 // Skip synthesized members. | 107 // Skip synthesized members. |
99 return; | 108 return; |
100 } | 109 } |
101 this.resolvedAst = resolvedAst; | 110 this.resolvedAst = resolvedAst; |
102 apply(resolvedAst.node); | 111 apply(resolvedAst.node); |
103 this.resolvedAst = null; | 112 this.resolvedAst = null; |
104 } | 113 } |
105 | 114 |
106 bool isHelper(Element element) { | 115 bool isHelper(Element element) { |
107 Uri uri = element.library.canonicalUri; | 116 Uri uri = element.library.canonicalUri; |
108 return '$uri'.startsWith('package:compiler/src/helpers/'); | 117 return '$uri'.startsWith(helpersUriPrefix); |
Johnni Winther
2016/05/26 12:13:21
With the change to use `compiler.analyzeUri` some
| |
109 } | 118 } |
110 | 119 |
111 void checkAccess(Node node, MemberElement element) { | 120 void checkAccess(Node node, MemberElement element) { |
112 if (isHelper(element) && !isHelper(analyzedElement)) { | 121 if (isHelper(element) && !isHelper(analyzedElement)) { |
113 Uri uri = analyzedElement.implementation.sourcePosition.uri; | 122 Uri uri = analyzedElement.implementation.sourcePosition.uri; |
114 SourceSpan span = new SourceSpan.fromNode(uri, node); | 123 SourceSpan span = new SourceSpan.fromNode(uri, node); |
115 diagnostics.report(null, span.uri, span.begin, span.end, | 124 diagnostics.report(null, span.uri, span.begin, span.end, |
116 "Helper used in production code.", | 125 "Helper used in production code.", |
117 Diagnostic.ERROR); | 126 Diagnostic.ERROR); |
118 errors.add(span); | 127 errors.add(span); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 } | 236 } |
228 | 237 |
229 @override | 238 @override |
230 void visitConstConstructorInvoke( | 239 void visitConstConstructorInvoke( |
231 NewExpression node, | 240 NewExpression node, |
232 ConstructedConstantExpression constant, | 241 ConstructedConstantExpression constant, |
233 _) { | 242 _) { |
234 checkAccess(node, constant.target); | 243 checkAccess(node, constant.target); |
235 } | 244 } |
236 } | 245 } |
OLD | NEW |