Skip to content

Commit 03d0629

Browse files
srawlinsCommit Queue
authored andcommitted
DAS plugins: provide a WorkspacePackage to RuleContext
Fixes #61489 Change-Id: I50dc5a0649ef9f77e37efb27430cbb3736814313 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/449360 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 0a47e56 commit 03d0629

File tree

3 files changed

+83
-14
lines changed

3 files changed

+83
-14
lines changed

pkg/analysis_server_plugin/lib/src/plugin_server.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,16 @@ class PluginServer {
390390
// TODO(srawlins): Enable timing similar to what the linter package's
391391
// `benchmark.dart` script does.
392392
var nodeRegistry = RuleVisitorRegistryImpl(enableTiming: false);
393+
var package = analysisContext.contextRoot.workspace.findPackageFor(
394+
libraryPath,
395+
);
393396

394397
var context = RuleContextWithResolvedResults(
395398
allUnits,
396399
definingContextUnit,
397400
libraryResult.element.typeProvider,
398401
libraryResult.element.typeSystem as TypeSystemImpl,
399-
// TODO(srawlins): Support 'package' parameter.
400-
null,
402+
package,
401403
);
402404

403405
// A mapping from each diagnostic code to its corresponding plugin.

pkg/analysis_server_plugin/test/src/lint_rules.dart

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ import 'package:analyzer/dart/ast/ast.dart';
99
import 'package:analyzer/dart/ast/visitor.dart';
1010
import 'package:analyzer/error/error.dart';
1111

12-
class NoBoolsRule extends AnalysisRule {
13-
static const LintCode code = LintCode('no_bools', 'No bools message');
12+
class NeedsPackageRule extends AnalysisRule {
13+
static const LintCode code = LintCode(
14+
'needs_package',
15+
'Needs Package at {0}',
16+
);
1417

15-
NoBoolsRule() : super(name: 'no_bools', description: 'No bools desc');
18+
NeedsPackageRule()
19+
: super(name: 'needs_package', description: 'This rule needs package info');
1620

1721
@override
1822
DiagnosticCode get diagnosticCode => code;
@@ -22,16 +26,17 @@ class NoBoolsRule extends AnalysisRule {
2226
RuleVisitorRegistry registry,
2327
RuleContext context,
2428
) {
25-
var visitor = _NoBoolsVisitor(this);
26-
registry.addBooleanLiteral(this, visitor);
29+
if (context.isInLibDir) {
30+
var visitor = _NeedsPackageVisitor(this, context);
31+
registry.addIntegerLiteral(this, visitor);
32+
}
2733
}
2834
}
2935

30-
class NoDoublesRule extends AnalysisRule {
31-
static const LintCode code = LintCode('no_doubles', 'No doubles message');
36+
class NoBoolsRule extends AnalysisRule {
37+
static const LintCode code = LintCode('no_bools', 'No bools message');
3238

33-
NoDoublesRule()
34-
: super(name: 'no_doubles', description: 'No doubles message');
39+
NoBoolsRule() : super(name: 'no_bools', description: 'No bools desc');
3540

3641
@override
3742
DiagnosticCode get diagnosticCode => code;
@@ -41,8 +46,8 @@ class NoDoublesRule extends AnalysisRule {
4146
RuleVisitorRegistry registry,
4247
RuleContext context,
4348
) {
44-
var visitor = _NoDoublesVisitor(this);
45-
registry.addDoubleLiteral(this, visitor);
49+
var visitor = _NoBoolsVisitor(this);
50+
registry.addBooleanLiteral(this, visitor);
4651
}
4752
}
4853

@@ -72,6 +77,25 @@ class NoDoublesCustomSeverityRule extends AnalysisRule {
7277
}
7378
}
7479

80+
class NoDoublesRule extends AnalysisRule {
81+
static const LintCode code = LintCode('no_doubles', 'No doubles message');
82+
83+
NoDoublesRule()
84+
: super(name: 'no_doubles', description: 'No doubles message');
85+
86+
@override
87+
DiagnosticCode get diagnosticCode => code;
88+
89+
@override
90+
void registerNodeProcessors(
91+
RuleVisitorRegistry registry,
92+
RuleContext context,
93+
) {
94+
var visitor = _NoDoublesVisitor(this);
95+
registry.addDoubleLiteral(this, visitor);
96+
}
97+
}
98+
7599
class NoReferencesToStringsRule extends AnalysisRule {
76100
static const LintCode code = LintCode(
77101
'no_references_to_strings',
@@ -97,6 +121,19 @@ class NoReferencesToStringsRule extends AnalysisRule {
97121
}
98122
}
99123

124+
class _NeedsPackageVisitor extends SimpleAstVisitor<void> {
125+
final AnalysisRule rule;
126+
127+
final RuleContext context;
128+
129+
_NeedsPackageVisitor(this.rule, this.context);
130+
131+
@override
132+
void visitIntegerLiteral(IntegerLiteral node) {
133+
rule.reportAtNode(node, arguments: ['"${context.package!.root.path}"']);
134+
}
135+
}
136+
100137
class _NoBoolsVisitor extends SimpleAstVisitor<void> {
101138
final AnalysisRule rule;
102139

pkg/analysis_server_plugin/test/src/plugin_server_test.dart

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,21 @@ class PluginServerTest extends PluginServerTestBase {
3636

3737
String get file2Path => join(packagePath, 'lib', 'test2.dart');
3838

39+
String get testFilePath => join(packagePath, 'test', 'test.dart');
40+
3941
String get packagePath => convertPath('/package1');
4042

4143
StreamQueue<protocol.AnalysisErrorsParams> get _analysisErrorsParams {
4244
return StreamQueue(
4345
channel.notifications
4446
.where((n) => n.event == protocol.ANALYSIS_NOTIFICATION_ERRORS)
4547
.map((n) => protocol.AnalysisErrorsParams.fromNotification(n))
46-
.where((p) => p.file == filePath || p.file == file2Path),
48+
.where(
49+
(p) =>
50+
p.file == filePath ||
51+
p.file == file2Path ||
52+
p.file == testFilePath,
53+
),
4754
);
4855
}
4956

@@ -260,6 +267,7 @@ bool b = false;
260267
expect(
261268
details.lintRules,
262269
unorderedEquals([
270+
'needs_package',
263271
'no_doubles',
264272
'no_doubles_custom_severity',
265273
'no_references_to_strings',
@@ -277,6 +285,27 @@ bool b = false;
277285
expect(assist.message, 'Invert Boolean value');
278286
}
279287

288+
Future<void> test_rulesHaveAccessToPackage() async {
289+
writeAnalysisOptionsWithPlugin({'needs_package': 'enable'});
290+
newFile(filePath, 'var x = 1;');
291+
newFile(testFilePath, 'var x = 1;');
292+
await channel.sendRequest(
293+
protocol.AnalysisSetContextRootsParams([contextRoot]),
294+
);
295+
var paramsQueue = _analysisErrorsParams;
296+
var params = await paramsQueue.next;
297+
expect(params.file, filePath);
298+
expect(params.errors, hasLength(1));
299+
_expectAnalysisError(
300+
params.errors.single,
301+
message: 'Needs Package at "$packagePath"',
302+
);
303+
304+
params = await paramsQueue.next;
305+
expect(params.file, testFilePath);
306+
expect(params.errors, isEmpty);
307+
}
308+
280309
Future<void> test_unsupportedRequest() async {
281310
writeAnalysisOptionsWithPlugin();
282311
newFile(filePath, 'bool b = false;');
@@ -626,6 +655,7 @@ class _NoLiteralsPlugin extends Plugin {
626655

627656
@override
628657
void register(PluginRegistry registry) {
658+
registry.registerLintRule(NeedsPackageRule());
629659
registry.registerWarningRule(NoBoolsRule());
630660
registry.registerLintRule(NoDoublesRule());
631661
registry.registerLintRule(NoDoublesCustomSeverityRule());

0 commit comments

Comments
 (0)