Skip to content

Commit

Permalink
Merge pull request #62 from Workiva/keying-constant-expressions
Browse files Browse the repository at this point in the history
Keying constant expressions
  • Loading branch information
joebingham-wk authored Jun 22, 2020
2 parents 74098fa + cfb242f commit 58833c7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/source/source_range.dart';
import 'package:over_react_analyzer_plugin/src/diagnostic_contributor.dart';
import 'package:over_react_analyzer_plugin/src/fluent_interface_util.dart';
import 'package:over_react_analyzer_plugin/src/util/ast_util.dart';

class SingleChildWithKey extends ComponentUsageDiagnosticContributor {
static final code = DiagnosticCode(
Expand Down Expand Up @@ -40,7 +41,7 @@ class SingleChildWithKey extends ComponentUsageDiagnosticContributor {

if ((isInAList && isSingleChild) || isVariadic) {
await forEachCascadedPropAsync(usage, (lhs, rhs) async {
if (lhs.propertyName.name == 'key' && rhs is SimpleStringLiteral) {
if (lhs.propertyName.name == 'key' && isAConstantValue(rhs)) {
await collector.addErrorWithFix(code, result.location(range: SourceRange(lhs.offset, rhs.end - lhs.offset)),
fixKind: fixKind,
computeFix: () => buildFileEdit(result, (builder) {
Expand Down
7 changes: 6 additions & 1 deletion over_react_analyzer_plugin/lib/src/util/ast_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
library over_react_analyzer_plugin.src.ast_util;

import 'dart:collection';

// ignore: deprecated_member_use
// This is necessary for `ConstantEvaluator`. If that API is removed, it can just
// be copied and pasted into this analyzer package (if still needed).
import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/source/line_info.dart';

Expand Down Expand Up @@ -37,3 +40,5 @@ int prevLine(int offset, LineInfo lineInfo) {
int nextLine(int offset, LineInfo lineInfo) {
return lineInfo.getOffsetOfLineAfter(offset);
}

bool isAConstantValue(Expression expr) => expr.accept(ConstantEvaluator()) != ConstantEvaluator.NOT_A_CONSTANT;
2 changes: 1 addition & 1 deletion playground/web/single_child_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestComponent extends UiComponent2<TestProps> {
@override
render(){
final children = [_renderDivs(), _renderDivs()];
return (Dom.div()..key='unnecessary')(children);
return (Dom.div()..key = 'constant' + 'expression')(children);
}

_returnListTest() {
Expand Down

0 comments on commit 58833c7

Please sign in to comment.