+ do_not_use_environment (#2100)
* + do_not_use_environment
* nit && fix
diff --git a/example/all.yaml b/example/all.yaml
index ff6885b..6a94df1 100644
--- a/example/all.yaml
+++ b/example/all.yaml
@@ -57,6 +57,7 @@
- curly_braces_in_flow_control_structures
- diagnostic_describe_all_properties
- directives_ordering
+ - do_not_use_environment
- empty_catches
- empty_constructor_bodies
- empty_statements
diff --git a/lib/src/rules.dart b/lib/src/rules.dart
index cbd3b50..f303ac9 100644
--- a/lib/src/rules.dart
+++ b/lib/src/rules.dart
@@ -58,6 +58,7 @@
import 'rules/curly_braces_in_flow_control_structures.dart';
import 'rules/diagnostic_describe_all_properties.dart';
import 'rules/directives_ordering.dart';
+import 'rules/do_not_use_environment.dart';
import 'rules/empty_catches.dart';
import 'rules/empty_constructor_bodies.dart';
import 'rules/empty_statements.dart';
@@ -233,6 +234,7 @@
..register(CurlyBracesInFlowControlStructures())
..register(DiagnosticsDescribeAllProperties())
..register(DirectivesOrdering())
+ ..register(DoNotUseEnvironment())
..register(EmptyCatches())
..register(EmptyConstructorBodies())
..register(EmptyStatements())
diff --git a/lib/src/rules/do_not_use_environment.dart b/lib/src/rules/do_not_use_environment.dart
new file mode 100644
index 0000000..e65e12c
--- /dev/null
+++ b/lib/src/rules/do_not_use_environment.dart
@@ -0,0 +1,69 @@
+// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:analyzer/dart/ast/ast.dart';
+import 'package:analyzer/dart/ast/visitor.dart';
+
+import '../analyzer.dart';
+
+const _desc = r'Do not use environment declared variables.';
+
+const _details = r'''
+Using values derived from the environment at compile-time, creates
+hidden global state and makes applications hard to understand and maintain.
+
+**DO NOT** use `fromEnvironment` or `hasEnvironment` factory constructors.
+
+**BAD:**
+```
+const loggingLevel =
+ bool.hasEnvironment('logging') ? String.fromEnvironment('logging') : null;
+```
+''';
+
+class DoNotUseEnvironment extends LintRule implements NodeLintRule {
+ DoNotUseEnvironment()
+ : super(
+ name: 'do_not_use_environment',
+ description: _desc,
+ details: _details,
+ group: Group.style);
+
+ @override
+ void registerNodeProcessors(NodeLintRegistry registry,
+ [LinterContext context]) {
+ final visitor = _Visitor(this);
+ registry.addInstanceCreationExpression(this, visitor);
+ }
+}
+
+class _Visitor extends SimpleAstVisitor {
+ final LintRule rule;
+
+ _Visitor(this.rule);
+
+ @override
+ void visitInstanceCreationExpression(InstanceCreationExpression node) {
+ if (node.staticElement?.isFactory != true) {
+ return;
+ }
+
+ var staticType = node.staticType;
+ if (staticType == null) {
+ return;
+ }
+ var constructorName = node.constructorName?.name?.name;
+ if (constructorName == null) {
+ return;
+ }
+
+ if (((staticType.isDartCoreBool ||
+ staticType.isDartCoreInt ||
+ staticType.isDartCoreString) &&
+ constructorName == 'fromEnvironment') ||
+ (staticType.isDartCoreBool && constructorName == 'hasEnvironment')) {
+ rule.reportLint(node.constructorName);
+ }
+ }
+}
diff --git a/test/rules/do_not_use_environment.dart b/test/rules/do_not_use_environment.dart
new file mode 100644
index 0000000..26bacb0
--- /dev/null
+++ b/test/rules/do_not_use_environment.dart
@@ -0,0 +1,12 @@
+// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// test w/ `pub run test -N do_not_use_environment`
+
+void f() {
+ int.fromEnvironment('key'); // LINT
+ bool.fromEnvironment('key'); // LINT
+ String.fromEnvironment('key'); //LINT
+ bool.hasEnvironment('key'); //LINT
+}