Skip to content

Commit 4b61c79

Browse files
kallentuCommit Queue
authored andcommitted
[linter] Dot shorthands: Update do_not_use_environment lint.
Adds an extra case for `DotShorthandConstructorInvocation`s so that the lint fires on `.hasEnvironment(...)` and `.fromEnvironment(...)`. Tests added and passing. Fixes: #60903 Bug: #60893 Change-Id: I9e85c766c0009cb7a2807ce3b32d802dd47550fa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/434480 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent a54c46c commit 4b61c79

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

pkg/linter/lib/src/rules/do_not_use_environment.dart

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:analyzer/analysis_rule/rule_context.dart';
66
import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
8+
import 'package:analyzer/dart/element/type.dart';
89
import 'package:analyzer/error/error.dart';
910

1011
import '../analyzer.dart';
@@ -21,6 +22,7 @@ class DoNotUseEnvironment extends LintRule {
2122
@override
2223
void registerNodeProcessors(NodeLintRegistry registry, RuleContext context) {
2324
var visitor = _Visitor(this);
25+
registry.addDotShorthandConstructorInvocation(this, visitor);
2426
registry.addInstanceCreationExpression(this, visitor);
2527
}
2628
}
@@ -30,6 +32,32 @@ class _Visitor extends SimpleAstVisitor<void> {
3032

3133
_Visitor(this.rule);
3234

35+
void reportIfUsingEnvironment(
36+
AstNode node,
37+
String constructorName,
38+
DartType staticType,
39+
) {
40+
if (((staticType.isDartCoreBool ||
41+
staticType.isDartCoreInt ||
42+
staticType.isDartCoreString) &&
43+
constructorName == 'fromEnvironment') ||
44+
(staticType.isDartCoreBool && constructorName == 'hasEnvironment')) {
45+
rule.reportAtNode(node);
46+
}
47+
}
48+
49+
@override
50+
void visitDotShorthandConstructorInvocation(
51+
DotShorthandConstructorInvocation node,
52+
) {
53+
var nameNode = node.constructorName;
54+
var staticType = node.staticType;
55+
if (staticType == null) {
56+
return;
57+
}
58+
reportIfUsingEnvironment(nameNode, nameNode.name, staticType);
59+
}
60+
3361
@override
3462
void visitInstanceCreationExpression(InstanceCreationExpression node) {
3563
var constructorNameNode = node.constructorName;
@@ -44,13 +72,6 @@ class _Visitor extends SimpleAstVisitor<void> {
4472
if (constructorName == null) {
4573
return;
4674
}
47-
48-
if (((staticType.isDartCoreBool ||
49-
staticType.isDartCoreInt ||
50-
staticType.isDartCoreString) &&
51-
constructorName == 'fromEnvironment') ||
52-
(staticType.isDartCoreBool && constructorName == 'hasEnvironment')) {
53-
rule.reportAtNode(constructorNameNode);
54-
}
75+
reportIfUsingEnvironment(constructorNameNode, constructorName, staticType);
5576
}
5677
}

pkg/linter/test/rules/do_not_use_environment_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,28 @@ void f() {
2727
);
2828
}
2929

30+
test_dotShorthand_fromEnvironment() async {
31+
await assertDiagnostics(
32+
r'''
33+
void f() {
34+
bool x = .fromEnvironment('key');
35+
}
36+
''',
37+
[lint(23, 15)],
38+
);
39+
}
40+
41+
test_dotShorthand_hasEnvironment() async {
42+
await assertDiagnostics(
43+
r'''
44+
void f() {
45+
bool x = .hasEnvironment('key');
46+
}
47+
''',
48+
[lint(23, 14)],
49+
);
50+
}
51+
3052
test_hasEnvironment() async {
3153
await assertDiagnostics(
3254
r'''

0 commit comments

Comments
 (0)