Skip to content

Commit 1d0ffe9

Browse files
authored
Enums refactoring for Dart 2.17 (#15)
Refactor to use the new enum features in Dart 2.17.
1 parent 3672ebf commit 1d0ffe9

34 files changed

+403
-598
lines changed

_tests/lib/compiler.dart

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @dart=2.9
2-
31
import 'dart:io';
42

53
import 'package:build/build.dart';
@@ -56,9 +54,9 @@ final _ngFiles = Glob('lib/**.dart');
5654
Future<void> _testBuilder(
5755
Builder builder,
5856
Map<String, String> sourceAssets, {
59-
List<AssetId> runBuilderOn,
60-
void Function(LogRecord) onLog,
61-
String rootPackage,
57+
List<AssetId>? runBuilderOn,
58+
required void Function(LogRecord) onLog,
59+
String? rootPackage,
6260
}) async {
6361
// Setup the readers/writers for assets.
6462
final sources = InMemoryAssetReader(rootPackage: rootPackage);
@@ -135,13 +133,13 @@ Future<void> _testBuilder(
135133
/// Note that `package:ngdart/**.dart` is always included.
136134
Future<void> compilesExpecting(
137135
String input, {
138-
String inputSource,
139-
Set<AssetId> runBuilderOn,
140-
Map<String, String> include,
141-
Object /*Matcher|Iterable<Matcher>*/ errors,
142-
Object /*Matcher|Iterable<Matcher>*/ warnings,
143-
Object /*Matcher|Iterable<Matcher>*/ notices,
144-
Object /*Matcher|Map<String, Matcher>*/ outputs,
136+
String? inputSource,
137+
Set<AssetId>? runBuilderOn,
138+
Map<String, String>? include,
139+
Object? /*Matcher|Iterable<Matcher>*/ errors,
140+
Object? /*Matcher|Iterable<Matcher>*/ warnings,
141+
Object? /*Matcher|Iterable<Matcher>*/ notices,
142+
Object? /*Matcher|Map<String, Matcher>*/ outputs,
145143
}) async {
146144
// Default values.
147145
//
@@ -177,7 +175,7 @@ Future<void> compilesExpecting(
177175
}
178176
}
179177

180-
void expectLogRecords(List<LogRecord> logs, matcher, String reasonPrefix) {
178+
void expectLogRecords(List<LogRecord>? logs, matcher, String reasonPrefix) {
181179
if (matcher == null) {
182180
return;
183181
}
@@ -200,9 +198,9 @@ String formattedLogMessage(LogRecord record) {
200198
/// An alias [compilesExpecting] with `errors` and `warnings` asserting empty.
201199
Future<void> compilesNormally(
202200
String input, {
203-
String inputSource,
204-
Map<String, String> include,
205-
Set<AssetId> runBuilderOn,
201+
String? inputSource,
202+
Map<String, String>? include,
203+
Set<AssetId>? runBuilderOn,
206204
}) =>
207205
compilesExpecting(
208206
input,

_tests/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: _tests
22
description: Tests for AngularDart.
33
environment:
4-
sdk: '>=2.14.0 <3.0.0'
4+
sdk: '>=2.17.0 <3.0.0'
55

66
dependencies:
77
# No strict dependencies, we always use dependency_overrides.

_tests/test/compiler/ast_template_parser_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,7 +2622,8 @@ void main() {
26222622

26232623
CompileDirectiveMetadata createCompileDirectiveMetadata({
26242624
CompileTypeMetadata type,
2625-
CompileDirectiveMetadataType metadataType,
2625+
CompileDirectiveMetadataType metadataType =
2626+
CompileDirectiveMetadataType.Directive,
26262627
String selector,
26272628
String exportAs,
26282629
List<String> inputs,
@@ -2652,7 +2653,7 @@ CompileDirectiveMetadata createCompileDirectiveMetadata({
26522653

26532654
return CompileDirectiveMetadata(
26542655
type: type,
2655-
metadataType: metadataType ?? CompileDirectiveMetadataType.Directive,
2656+
metadataType: metadataType,
26562657
selector: selector,
26572658
exportAs: exportAs,
26582659
inputs: inputsMap,
Lines changed: 54 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -1,203 +1,65 @@
11
part of 'exception_handler.dart';
22

3-
@sealed
4-
class ParserErrorCode {
5-
static const cannotFindMatchingClose = ParserErrorCode._(
6-
'CANNOT_FIND_MATCHING_CLOSE',
7-
'Cannot find matching close element to this',
8-
);
9-
10-
static const danglingCloseElement = ParserErrorCode._(
11-
'DANGLING_CLOSE_ELEMENT',
12-
'Closing tag is dangling and no matching open tag can be found',
13-
);
14-
15-
static const duplicateStarDirective = ParserErrorCode._(
16-
'DUPLICATE_STAR_DIRECTIVE',
17-
'Already found a *-directive, limit 1 per element.',
18-
);
19-
20-
static const duplicateSelectDecorator = ParserErrorCode._(
21-
'DUPLICATE_SELECT_DECORATOR',
22-
"Only 1 'select' decorator can exist in <ng-content>, found duplicate",
23-
);
24-
25-
static const duplicateProjectAsDecorator = ParserErrorCode._(
26-
'DUPLICATE_PROJECT_AS_DECORATOR',
27-
"Only 1 'ngProjectAs' decorator can exist in <ng-content>, found duplicate",
28-
);
29-
30-
static const duplicateReferenceDecorator = ParserErrorCode._(
31-
'DUPLICATE_REFERENCE_DECORATOR',
32-
'Only 1 reference decorator can exist in <ng-content>, found duplicate',
33-
);
34-
35-
static const elementDecorator = ParserErrorCode._(
36-
'ELEMENT_DECORATOR',
37-
'Expected element decorator after whitespace',
38-
);
39-
40-
static const elementDecoratorAfterPrefix = ParserErrorCode._(
41-
'ELEMENT_DECORATOR_AFTER_PREFIX',
42-
'Expected element decorator identifier after prefix',
43-
);
44-
45-
static const elementDecoratorSuffixBeforePrefix = ParserErrorCode._(
46-
'ELEMENT_DECORATOR',
47-
'Found special decorator suffix before prefix',
48-
);
49-
50-
static const elementDecoratorValue = ParserErrorCode._(
51-
'ELEMENT_DECORATOR_VALUE',
52-
"Expected quoted value following '='",
53-
);
54-
55-
static const elementDecoratorValueMissingQuotes = ParserErrorCode._(
56-
'ELEMENT_DECORATOR_VALUE_MISSING_QUOTES',
57-
'Decorator values must contain quotes',
58-
);
59-
60-
static const elementIdentifier = ParserErrorCode._(
61-
'ELEMENT_IDENTIFIER',
62-
'Expected element tag name',
63-
);
64-
65-
static const expectedAfterElementIdentifier = ParserErrorCode._(
66-
'EXPECTED_AFTER_ELEMENT_IDENTIFIER',
67-
'Expected either whitespace or close tag end after element identifier',
68-
);
69-
70-
static const expectedEqualSign = ParserErrorCode._(
71-
'EXPECTED_EQUAL_SIGN',
72-
"Expected '=' between decorator and value",
73-
);
74-
75-
static const expectedStandalone = ParserErrorCode._(
76-
'EXPECTING_STANDALONE',
77-
'Expected standalone token',
78-
);
79-
80-
static const expectedTagClose = ParserErrorCode._(
81-
'EXPECTED_TAG_CLOSE',
82-
'Expected tag close.',
83-
);
84-
3+
enum ParserErrorCode {
4+
cannotFindMatchingClose('Cannot find matching close element to this'),
5+
danglingCloseElement(
6+
'Closing tag is dangling and no matching open tag can be found'),
7+
duplicateStarDirective('Already found a *-directive, limit 1 per element.'),
8+
duplicateSelectDecorator(
9+
"Only 1 'select' decorator can exist in <ng-content>, found duplicate"),
10+
duplicateProjectAsDecorator(
11+
"Only 1 'ngProjectAs' decorator can exist in <ng-content>, found duplicate"),
12+
duplicateReferenceDecorator(
13+
'Only 1 reference decorator can exist in <ng-content>, found duplicate'),
14+
elementDecorator('Expected element decorator after whitespace'),
15+
elementDecoratorAfterPrefix(
16+
'Expected element decorator identifier after prefix'),
17+
elementDecoratorSuffixBeforePrefix(
18+
'Found special decorator suffix before prefix'),
19+
elementDecoratorValue("Expected quoted value following '='"),
20+
elementDecoratorValueMissingQuotes('Decorator values must contain quotes'),
21+
elementIdentifier('Expected element tag name'),
22+
expectedAfterElementIdentifier(
23+
'Expected either whitespace or close tag end after element identifier'),
24+
expectedEqualSign("Expected '=' between decorator and value"),
25+
expectedStandalone('Expected standalone token'),
26+
expectedTagClose('Expected tag close.'),
8527
// 'Catch-all' error code.
86-
static const expectedToken = ParserErrorCode._(
87-
'UNEXPECTED_TOKEN',
88-
'Unexpected token',
89-
);
90-
91-
static const expectedWhitespaceBeforeNewDecorator = ParserErrorCode._(
92-
'EXPECTED_WHITESPACE_BEFORE_DECORATOR',
93-
'Expected whitespace before a new decorator',
94-
);
95-
96-
static const emptyInterpolation = ParserErrorCode._(
97-
'EMPTY_INTERPOLATION',
98-
'Interpolation expression cannot be empty',
99-
);
100-
101-
static const invalidDecoratorInNgContainer = ParserErrorCode._(
102-
'INVALID_DECORATOR_IN_NGCONTAINER',
103-
"Only '*' bindings are supported on <ng-container>",
104-
);
105-
106-
static const invalidDecoratorInNgContent = ParserErrorCode._(
107-
'INVALID_DECORATOR_IN_NGCONTENT',
108-
"Only 'select' is a valid attribute/decorate in <ng-content>",
109-
);
110-
111-
static const invalidDecoratorInTemplate = ParserErrorCode._(
112-
'INVALID_DECORATOR_IN_TEMPLATE',
113-
"Invalid decorator in 'template' element",
114-
);
115-
116-
static const invalidLetBindingInNoTemplate = ParserErrorCode._(
117-
'INVALID_LET_BINDING_IN_NONTEMPLATE',
118-
"'let-' binding can only be used in 'template' element",
119-
);
120-
121-
static const invalidMicroExpression = ParserErrorCode._(
122-
'INVALID_MICRO_EXPRESSION',
123-
'Failed parsing micro expression',
124-
);
125-
126-
static const nonVoidElementUsingVoidEnd = ParserErrorCode._(
127-
'NONVOID_ELEMENT_USING_VOID_END',
128-
'Element is not a void-element',
129-
);
130-
131-
static const ngContentMustCLoseImmediately = ParserErrorCode._(
132-
'NGCONTENT_MUST_CLOSE_IMMEDIATElY',
133-
"'<ng-content ...>' must be followed immediately by close '</ng-content>'",
134-
);
135-
136-
static const propertyNameTooManyFixes = ParserErrorCode._(
137-
'PROPERTY_NAME_TOO_MANY_FIXES',
138-
"Property name can only be in format: 'name[.postfix[.unit]]",
139-
);
140-
141-
static const referenceIdentifierFound = ParserErrorCode._(
142-
'REFERENCE_IDENTIFIER_FOUND',
143-
'Reference decorator only supports #<variable> on <ng-content>',
144-
);
145-
146-
static const suffixBanana = ParserErrorCode._(
147-
'SUFFIX_BANANA',
148-
"Expected closing banana ')]'",
149-
);
150-
151-
static const suffixEvent = ParserErrorCode._(
152-
'SUFFIX_EVENT',
153-
"Expected closing parenthesis ')'",
154-
);
155-
156-
static const suffixProperty = ParserErrorCode._(
157-
'SUFFIX_PROPERTY',
158-
"Expected closing bracket ']'",
159-
);
160-
161-
static const enclosedQuote = ParserErrorCode._(
162-
'UNCLOSED_QUOTE',
163-
'Expected close quote for element decorator value',
164-
);
165-
166-
static const unopenedMustache = ParserErrorCode._(
167-
'UNOPENED_MUSTACHE',
168-
'Unopened mustache',
169-
);
170-
171-
static const unterminatedComment = ParserErrorCode._(
172-
'UNTERMINATED COMMENT',
173-
'Unterminated comment',
174-
);
175-
176-
static const unterminatedMustache = ParserErrorCode._(
177-
'UNTERMINATED_MUSTACHE',
178-
'Unterminated mustache',
179-
);
180-
181-
static const voidElementInCloseTag = ParserErrorCode._(
182-
'VOID_ELEMENT_IN_CLOSE_TAG',
183-
'Void element identifiers cannot be used in close element tag',
184-
);
185-
186-
static const voidCloseInCloseTag = ParserErrorCode._(
187-
'VOID_CLOSE_IN_CLOSE_TAG',
188-
"Void close '/>' cannot be used in a close element",
189-
);
190-
191-
final String name;
28+
expectedToken('Unexpected token'),
29+
expectedWhitespaceBeforeNewDecorator(
30+
'Expected whitespace before a new decorator'),
31+
emptyInterpolation('Interpolation expression cannot be empty'),
32+
invalidDecoratorInNgContainer(
33+
"Only '*' bindings are supported on <ng-container>"),
34+
invalidDecoratorInNgContent(
35+
"Only 'select' is a valid attribute/decorate in <ng-content>"),
36+
invalidDecoratorInTemplate("Invalid decorator in 'template' element"),
37+
invalidLetBindingInNoTemplate(
38+
"'let-' binding can only be used in 'template' element"),
39+
invalidMicroExpression('Failed parsing micro expression'),
40+
nonVoidElementUsingVoidEnd('Element is not a void-element'),
41+
ngContentMustCLoseImmediately(
42+
"'<ng-content ...>' must be followed immediately by close '</ng-content>'"),
43+
propertyNameTooManyFixes(
44+
"Property name can only be in format: 'name[.postfix[.unit]]"),
45+
referenceIdentifierFound(
46+
'Reference decorator only supports #<variable> on <ng-content>'),
47+
suffixBanana("Expected closing banana ')]'"),
48+
suffixEvent("Expected closing parenthesis ')'"),
49+
suffixProperty("Expected closing bracket ']'"),
50+
enclosedQuote('Expected close quote for element decorator value'),
51+
unopenedMustache('Unopened mustache'),
52+
unterminatedComment('Unterminated comment'),
53+
unterminatedMustache('Unterminated mustache'),
54+
voidElementInCloseTag(
55+
'Void element identifiers cannot be used in close element tag'),
56+
voidCloseInCloseTag("Void close '/>' cannot be used in a close element");
19257

19358
final String message;
19459

19560
/// Initialize a newly created erorr code to have the given [name].
19661
/// The message associated with the error will be created from the
19762
/// given [message] template. The correction associated with the error
19863
/// will be created from the given [correction] template.
199-
const ParserErrorCode._(
200-
this.name,
201-
this.message,
202-
);
64+
const ParserErrorCode(this.message);
20365
}

ngast/lib/src/expression/micro/token.dart

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,17 @@ class NgMicroToken {
7878
final NgMicroTokenType type;
7979

8080
@override
81-
String toString() => '#$NgMicroToken(${type._name}) {$offset:$lexeme}';
81+
String toString() => '#$NgMicroToken(${type.name}) {$offset:$lexeme}';
8282
}
8383

84-
class NgMicroTokenType {
85-
static const endExpression = NgMicroTokenType._('endExpression');
86-
static const bindExpression = NgMicroTokenType._('bindExpression');
87-
static const bindExpressionBefore = NgMicroTokenType._(
88-
'bindExpressionBefore',
89-
);
90-
static const bindIdentifier = NgMicroTokenType._('bindIdentifier');
91-
static const letAssignment = NgMicroTokenType._('letAssignment');
92-
static const letAssignmentBefore = NgMicroTokenType._(
93-
'letAssignmentBefore',
94-
);
95-
static const letIdentifier = NgMicroTokenType._('letIdentifier');
96-
static const letKeyword = NgMicroTokenType._('letKeyword');
97-
static const letKeywordAfter = NgMicroTokenType._('letKeywordAfter');
98-
99-
final String _name;
100-
101-
const NgMicroTokenType._(this._name);
84+
enum NgMicroTokenType {
85+
endExpression,
86+
bindExpression,
87+
bindExpressionBefore,
88+
bindIdentifier,
89+
letAssignment,
90+
letAssignmentBefore,
91+
letIdentifier,
92+
letKeyword,
93+
letKeywordAfter
10294
}

0 commit comments

Comments
 (0)