Quick fix to ignore a specific error (#25915).

See: https://github.com/dart-lang/sdk/issues/25915

BUG=
R=scheglov@google.com

Review URL: https://codereview.chromium.org/1764853002 .
This commit is contained in:
pq 2016-03-04 11:11:06 -08:00
parent 40ebbcfb7c
commit 1f0a0b5a5c
4 changed files with 46 additions and 1 deletions

View file

@ -158,6 +158,8 @@ class DartFixKind {
'CREATE_MISSING_OVERRIDES', 49, "Create {0} missing override(s)");
static const CREATE_NO_SUCH_METHOD = const FixKind(
'CREATE_NO_SUCH_METHOD', 51, "Create 'noSuchMethod' method");
static const IGNORE_ERROR =
const FixKind('IGNORE_ERROR', 60, "Ignore error with code '{0}'");
static const IMPORT_LIBRARY_PREFIX = const FixKind('IMPORT_LIBRARY_PREFIX',
51, "Use imported library '{0}' with prefix '{1}'");
static const IMPORT_LIBRARY_PROJECT =

View file

@ -143,6 +143,15 @@ class FixProcessor {
new NodeLocator2(errorOffset, errorEnd - 1).searchWithin(unit);
// analyze ErrorCode
ErrorCode errorCode = error.errorCode;
// add ignore fix for ignorable errors.
// note that this fix needs to be added before fixes that side-effect
// the utils instance.
if (errorCode is StaticWarningCode ||
errorCode is StaticTypeWarningCode ||
errorCode is HintCode ||
errorCode is LintCode) {
_addFix_ignore(errorCode);
}
if (errorCode == StaticWarningCode.UNDEFINED_CLASS_BOOLEAN) {
_addFix_boolInsteadOfBoolean();
}
@ -1383,6 +1392,18 @@ class FixProcessor {
}
}
void _addFix_ignore(ErrorCode errorCode) {
int offset = node.offset;
int lineOffset = utils.getLineThis(offset);
exitPosition = new Position(file, lineOffset - 1);
String indent = utils.getLinePrefix(offset);
String errorCodeName = errorCode.name.toLowerCase();
String content = '$indent//ignore: $errorCodeName$eol';
_addReplaceEdit(rf.rangeStartLength(lineOffset, 0), content);
_addFix(DartFixKind.IGNORE_ERROR, [errorCodeName]);
}
void _addFix_illegalAsyncReturnType() {
// prepare the existing type
TypeName typeName = node.getAncestor((n) => n is TypeName);

View file

@ -9,6 +9,7 @@ import 'dart:async';
import 'package:analysis_server/plugin/protocol/protocol.dart';
import 'package:analysis_server/src/domain_analysis.dart';
import 'package:analysis_server/src/edit/edit_domain.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:plugin/manager.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'package:unittest/unittest.dart' hide ERROR;
@ -37,6 +38,7 @@ class FixesTest extends AbstractAnalysisTest {
addTestFile('''
main() {
Future<String> x = null;
print(x);
}
''');
await waitForTasksFinished();
@ -46,9 +48,11 @@ main() {
expect(error.severity, AnalysisErrorSeverity.WARNING);
expect(error.type, AnalysisErrorType.STATIC_WARNING);
List<SourceChange> fixes = errorFixes[0].fixes;
expect(fixes, hasLength(2));
expect(fixes, hasLength(3));
expect(fixes[0].message, matches('Import library'));
expect(fixes[1].message, matches('Create class'));
expect(
fixes[2].message, matches("Ignore error with code 'undefined_class'"));
}
test_hasFixes() async {

View file

@ -252,6 +252,22 @@ class Test {
''');
}
test_addIgnoreWarning() async {
resolveTestUnit('''
main() {
int x = ''; //invalid_assignment
}
''');
await assertHasFix(
DartFixKind.IGNORE_ERROR,
'''
main() {
//ignore: invalid_assignment
int x = ''; //invalid_assignment
}
''');
}
test_addMissingParameter_function_positional_hasNamed() async {
resolveTestUnit('''
test({int a}) {}
@ -473,6 +489,8 @@ main() {
}
if (error.message == message2) {
List<Fix> fixes = await _computeFixes(error);
// remove ignore fix
fixes.removeWhere((Fix fix) => fix.kind == DartFixKind.IGNORE_ERROR);
// has exactly one fix
expect(fixes, hasLength(1));
Fix fix = fixes[0];