Skip to content

Commit

Permalink
Update nomenclature in ErrorCode and related classes.
Browse files Browse the repository at this point in the history
This change standardizes most of the analyzer to refer to problem
messages and correction messages using the names `problemMessage` and
`correctionMessage` (consistent with the naming convention used in the
analyzer and CFE `messages.yaml` files).

Change-Id: I72f078a368c65b346626f560cc721fcff4836452
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/215151
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
stereotype441 authored and commit-bot@chromium.org committed Oct 4, 2021
1 parent 04e184d commit 757d179
Show file tree
Hide file tree
Showing 22 changed files with 844 additions and 776 deletions.
32 changes: 19 additions & 13 deletions pkg/_fe_analyzer_shared/lib/src/base/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ abstract class ErrorCode {
*/
final String uniqueName;

final String _message;
final String _problemMessage;

final String? _correction;
final String? _correctionMessage;

/**
* Return `true` if diagnostics with this code have documentation for them
Expand All @@ -36,30 +36,34 @@ abstract class ErrorCode {

/**
* Initialize a newly created error code to have the given [name]. The message
* associated with the error will be created from the given [message]
* associated with the error will be created from the given [problemMessage]
* template. The correction associated with the error will be created from the
* given [correction] template.
* given [correctionMessage] template.
*/
const ErrorCode({
String? correction,
String? correctionMessage,
this.hasPublishedDocs = false,
this.isUnresolvedIdentifier: false,
required String message,
required this.name,
@Deprecated('Please use problemMessage') String? message,
String? problemMessage,
required this.uniqueName,
}) : _correction = correction,
_message = message,
}) : _correctionMessage = correctionMessage,
_problemMessage = problemMessage ?? message ?? 'NO MESSAGE',
// ignore: unnecessary_null_comparison
assert(hasPublishedDocs != null),
// ignore: unnecessary_null_comparison
assert(isUnresolvedIdentifier != null);
assert(isUnresolvedIdentifier != null),
assert((message == null) != (problemMessage == null),
'Either problemMessage or message must be provided (not both)');

/**
* The template used to create the correction to be displayed for this error,
* or `null` if there is no correction information for this error. The
* correction should indicate how the user can fix the error.
*/
String? get correction => customizedCorrections[uniqueName] ?? _correction;
String? get correctionMessage =>
customizedCorrections[uniqueName] ?? _correctionMessage;

/**
* The severity of the error.
Expand All @@ -71,10 +75,12 @@ abstract class ErrorCode {
bool get isIgnorable => errorSeverity != ErrorSeverity.ERROR;

/**
* The template used to create the message to be displayed for this error. The
* message should indicate what is wrong and why it is wrong.
* The template used to create the problem message to be displayed for this
* error. The problem message should indicate what is wrong and why it is
* wrong.
*/
String get message => customizedMessages[uniqueName] ?? _message;
String get problemMessage =>
customizedMessages[uniqueName] ?? _problemMessage;

/**
* The type of the error.
Expand Down
15 changes: 8 additions & 7 deletions pkg/_fe_analyzer_shared/lib/src/scanner/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class ScannerErrorCode extends ErrorCode {
'UNEXPECTED_DOLLAR_IN_STRING',
"A '\$' has special meaning inside a string, and must be followed by "
"an identifier or an expression in curly braces ({}).",
correction: "Try adding a backslash (\\) to escape the '\$'.");
correctionMessage: "Try adding a backslash (\\) to escape the '\$'.");

/**
* Parameters:
Expand All @@ -167,7 +167,7 @@ class ScannerErrorCode extends ErrorCode {
static const ScannerErrorCode UNTERMINATED_MULTI_LINE_COMMENT =
const ScannerErrorCode(
'UNTERMINATED_MULTI_LINE_COMMENT', "Unterminated multi-line comment.",
correction: "Try terminating the comment with '*/', or "
correctionMessage: "Try terminating the comment with '*/', or "
"removing any unbalanced occurrences of '/*'"
" (because comments nest in Dart).");

Expand All @@ -177,14 +177,15 @@ class ScannerErrorCode extends ErrorCode {

/**
* Initialize a newly created error code to have the given [name]. The message
* associated with the error will be created from the given [message]
* associated with the error will be created from the given [problemMessage]
* template. The correction associated with the error will be created from the
* given [correction] template.
* given [correctionMessage] template.
*/
const ScannerErrorCode(String name, String message, {String? correction})
const ScannerErrorCode(String name, String problemMessage,
{String? correctionMessage})
: super(
correction: correction,
message: message,
correctionMessage: correctionMessage,
problemMessage: problemMessage,
name: name,
uniqueName: 'ScannerErrorCode.$name',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ class TransformSetErrorCode extends ErrorCode {
/// Initialize a newly created error code.
const TransformSetErrorCode(
String name,
String message, {
String? correction,
String problemMessage, {
String? correctionMessage,
bool hasPublishedDocs = false,
}) : super(
correction: correction,
correctionMessage: correctionMessage,
hasPublishedDocs: hasPublishedDocs,
message: message,
name: name,
problemMessage: problemMessage,
uniqueName: 'TransformSetErrorCode.$name',
);

Expand Down
11 changes: 6 additions & 5 deletions pkg/analysis_server/test/protocol_server_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ class AnalysisErrorTest {
void test_fromEngine_lint() {
engineError = MockAnalysisError(
source: source,
errorCode: LintCode('my_lint', 'my message', correction: 'correction'),
errorCode:
LintCode('my_lint', 'my message', correctionMessage: 'correction'),
offset: 10,
length: 20,
message: 'my message',
Expand Down Expand Up @@ -390,8 +391,8 @@ class MockErrorCode implements engine.ErrorCode {
this.url});

@override
String get correction {
throw StateError('Unexpected invocation of correction');
String get correctionMessage {
throw StateError('Unexpected invocation of correctionMessage');
}

@override
Expand All @@ -404,8 +405,8 @@ class MockErrorCode implements engine.ErrorCode {
bool get isUnresolvedIdentifier => false;

@override
String get message {
throw StateError('Unexpected invocation of message');
String get problemMessage {
throw StateError('Unexpected invocation of problemMessage');
}

@override
Expand Down
16 changes: 8 additions & 8 deletions pkg/analyzer/lib/error/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ class AnalysisError implements Diagnostic {

/// The correction to be displayed for this error, or `null` if there is no
/// correction information for this error.
String? _correction;
String? _correctionMessage;

/// The source in which the error occurred, or `null` if unknown.
final Source source;
Expand All @@ -982,22 +982,22 @@ class AnalysisError implements Diagnostic {
[List<Object?>? arguments,
List<DiagnosticMessage> contextMessages = const []])
: _contextMessages = contextMessages {
String message = formatList(errorCode.message, arguments);
String? correctionTemplate = errorCode.correction;
String problemMessage = formatList(errorCode.problemMessage, arguments);
String? correctionTemplate = errorCode.correctionMessage;
if (correctionTemplate != null) {
_correction = formatList(correctionTemplate, arguments);
_correctionMessage = formatList(correctionTemplate, arguments);
}
_problemMessage = DiagnosticMessageImpl(
filePath: source.fullName,
length: length,
message: message,
message: problemMessage,
offset: offset,
url: null);
}

/// Initialize a newly created analysis error with given values.
AnalysisError.forValues(this.source, int offset, int length, this.errorCode,
String message, this._correction,
String message, this._correctionMessage,
{List<DiagnosticMessage> contextMessages = const []})
: _contextMessages = contextMessages {
_problemMessage = DiagnosticMessageImpl(
Expand Down Expand Up @@ -1029,10 +1029,10 @@ class AnalysisError implements Diagnostic {
/// Return the template used to create the correction to be displayed for this
/// error, or `null` if there is no correction information for this error. The
/// correction should indicate how the user can fix the error.
String? get correction => _correction;
String? get correction => _correctionMessage;

@override
String? get correctionMessage => _correction;
String? get correctionMessage => _correctionMessage;

@override
int get hashCode {
Expand Down
37 changes: 19 additions & 18 deletions pkg/analyzer/lib/src/analysis_options/error/option_codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ class AnalysisOptionsErrorCode extends ErrorCode {
/// Initialize a newly created error code to have the given [name].
const AnalysisOptionsErrorCode(
String name,
String message, {
String? correction,
String problemMessage, {
String? correctionMessage,
bool hasPublishedDocs = false,
bool isUnresolvedIdentifier = false,
String? uniqueName,
}) : super(
correction: correction,
correctionMessage: correctionMessage,
hasPublishedDocs: hasPublishedDocs,
isUnresolvedIdentifier: isUnresolvedIdentifier,
message: message,
name: name,
problemMessage: problemMessage,
uniqueName: 'AnalysisOptionsErrorCode.${uniqueName ?? name}',
);

Expand All @@ -74,7 +74,7 @@ class AnalysisOptionsHintCode extends ErrorCode {
AnalysisOptionsHintCode(
'PREVIEW_DART_2_SETTING_DEPRECATED',
"The 'enablePreviewDart2' setting is deprecated.",
correction: "It is no longer necessary to explicitly enable Dart 2.",
correctionMessage: "It is no longer necessary to explicitly enable Dart 2.",
);

/**
Expand All @@ -84,7 +84,8 @@ class AnalysisOptionsHintCode extends ErrorCode {
AnalysisOptionsHintCode(
'STRONG_MODE_SETTING_DEPRECATED',
"The 'strong-mode: true' setting is deprecated.",
correction: "It is no longer necessary to explicitly enable strong mode.",
correctionMessage:
"It is no longer necessary to explicitly enable strong mode.",
);

/**
Expand All @@ -95,24 +96,24 @@ class AnalysisOptionsHintCode extends ErrorCode {
AnalysisOptionsHintCode(
'SUPER_MIXINS_SETTING_DEPRECATED',
"The 'enableSuperMixins' setting is deprecated.",
correction:
correctionMessage:
"Support has been added to the language for 'mixin' based mixins.",
);

/// Initialize a newly created error code to have the given [name].
const AnalysisOptionsHintCode(
String name,
String message, {
String? correction,
String problemMessage, {
String? correctionMessage,
bool hasPublishedDocs = false,
bool isUnresolvedIdentifier = false,
String? uniqueName,
}) : super(
correction: correction,
correctionMessage: correctionMessage,
hasPublishedDocs: hasPublishedDocs,
isUnresolvedIdentifier: isUnresolvedIdentifier,
message: message,
name: name,
problemMessage: problemMessage,
uniqueName: 'AnalysisOptionsHintCode.${uniqueName ?? name}',
);

Expand Down Expand Up @@ -191,7 +192,7 @@ class AnalysisOptionsWarningCode extends ErrorCode {
AnalysisOptionsWarningCode(
'SPEC_MODE_REMOVED',
"The option 'strong-mode: false' is no longer supported.",
correction:
correctionMessage:
"It's recommended to remove the 'strong-mode:' setting (and make your code Dart 2 compliant).",
);

Expand Down Expand Up @@ -250,7 +251,7 @@ class AnalysisOptionsWarningCode extends ErrorCode {
AnalysisOptionsWarningCode(
'UNSUPPORTED_OPTION_WITH_LEGAL_VALUES',
"The option '{1}' isn't supported by '{0}'.",
correction: "Try using one of the supported options: {2}.",
correctionMessage: "Try using one of the supported options: {2}.",
);

/**
Expand All @@ -266,23 +267,23 @@ class AnalysisOptionsWarningCode extends ErrorCode {
AnalysisOptionsWarningCode(
'UNSUPPORTED_VALUE',
"The value '{1}' isn't supported by '{0}'.",
correction: "Try using one of the supported options: {2}.",
correctionMessage: "Try using one of the supported options: {2}.",
);

/// Initialize a newly created error code to have the given [name].
const AnalysisOptionsWarningCode(
String name,
String message, {
String? correction,
String problemMessage, {
String? correctionMessage,
bool hasPublishedDocs = false,
bool isUnresolvedIdentifier = false,
String? uniqueName,
}) : super(
correction: correction,
correctionMessage: correctionMessage,
hasPublishedDocs: hasPublishedDocs,
isUnresolvedIdentifier: isUnresolvedIdentifier,
message: message,
name: name,
problemMessage: problemMessage,
uniqueName: 'AnalysisOptionsWarningCode.${uniqueName ?? name}',
);

Expand Down
Loading

0 comments on commit 757d179

Please sign in to comment.