Skip to content

Commit 45722db

Browse files
bwilkersonCommit Queue
authored and
Commit Queue
committed
Add documentation for dot shorthand diagnostics
Please let me know if there are other diagnostics that I missed. Change-Id: I87d29f62bcd1dad519c34212a2aa41d4422f1002 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/431949 Reviewed-by: Kallen Tu <[email protected]> Reviewed-by: Amanda Fitch <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 9df0000 commit 45722db

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

pkg/analyzer/lib/src/error/codes.g.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,8 @@ class CompileTimeErrorCode extends DiagnosticCode {
14171417
static const CompileTimeErrorCode
14181418
DOT_SHORTHAND_UNDEFINED_INVOCATION = CompileTimeErrorCode(
14191419
'DOT_SHORTHAND_UNDEFINED_MEMBER',
1420-
"The static method or constructor '{0}' isn't defined for the type '{1}'.",
1420+
"The static method or constructor '{0}' isn't defined for the context type "
1421+
"'{1}'.",
14211422
correctionMessage:
14221423
"Try correcting the name to the name of an existing static method or "
14231424
"constructor, or defining a static method or constructor named '{0}'.",

pkg/analyzer/messages.yaml

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3879,6 +3879,51 @@ CompileTimeErrorCode:
38793879
experiment: dot-shorthands
38803880
problemMessage: "A dot shorthand can't be used where there is no context type."
38813881
hasPublishedDocs: false
3882+
documentation: |-
3883+
#### Description
3884+
3885+
The analyzer produces this diagnostic when a dot shorthand is used where
3886+
there is no context type.
3887+
3888+
#### Example
3889+
3890+
The following code produces this diagnostic because there is no context
3891+
type for the expression `.a`:
3892+
3893+
```dart
3894+
void f() {
3895+
var e = [!.a!];
3896+
print(e);
3897+
}
3898+
3899+
enum E {a, b}
3900+
```
3901+
3902+
#### Common fixes
3903+
3904+
If you want to use a dot shorthand, then add a context type, which in this
3905+
example means adding the explicit type `E` to the local variable:
3906+
3907+
```dart
3908+
void f() {
3909+
E e = .a;
3910+
print(e);
3911+
}
3912+
3913+
enum E {a, b}
3914+
```
3915+
3916+
If you don't want to add a context type, then specify the name of the
3917+
type containing the member being referenced, which in this case is `E`:
3918+
3919+
```dart
3920+
void f() {
3921+
var e = E.a;
3922+
print(e);
3923+
}
3924+
3925+
enum E {a, b}
3926+
```
38823927
DOT_SHORTHAND_UNDEFINED_GETTER:
38833928
sharedName: DOT_SHORTHAND_UNDEFINED_MEMBER
38843929
experiment: dot-shorthands
@@ -3889,10 +3934,55 @@ CompileTimeErrorCode:
38893934
Parameters:
38903935
0: the name of the static getter
38913936
1: the name of the enclosing type where the getter is being looked for
3937+
documentation: |-
3938+
#### Description
3939+
3940+
The analyzer produces this diagnostic when a dot shorthand is used to
3941+
reference a member, but that member doesn't exist.
3942+
3943+
#### Example
3944+
3945+
The following code produces this diagnostic because the enum `E` doesn't
3946+
define a static member named `c`:
3947+
3948+
```dart
3949+
void f() {
3950+
E e = .[!c!];
3951+
print(e);
3952+
}
3953+
3954+
enum E {a, b}
3955+
```
3956+
3957+
#### Common fixes
3958+
3959+
If the name is correct, then define a member with that name in the context
3960+
type, which in this case is the enum `E`:
3961+
3962+
```dart
3963+
void f() {
3964+
E e = .c;
3965+
print(e);
3966+
}
3967+
3968+
enum E {a, b, c}
3969+
```
3970+
3971+
If the name is not correct, then replace the name with the name of an
3972+
existing member from the context type, which in this case is the enum `E`:
3973+
3974+
```dart
3975+
void f() {
3976+
E e = .b;
3977+
print(e);
3978+
}
3979+
3980+
enum E {a, b}
3981+
```
38923982
DOT_SHORTHAND_UNDEFINED_INVOCATION:
38933983
sharedName: DOT_SHORTHAND_UNDEFINED_MEMBER
38943984
experiment: dot-shorthands
3895-
problemMessage: "The static method or constructor '{0}' isn't defined for the type '{1}'."
3985+
problemMessage: "The static method or constructor '{0}' isn't defined for the context type '{1}'."
38963986
correctionMessage: "Try correcting the name to the name of an existing static method or constructor, or defining a static method or constructor named '{0}'."
38973987
hasPublishedDocs: false
38983988
comment: |-

0 commit comments

Comments
 (0)