Skip to content

Commit

Permalink
Macro. Check for valid parts in Code.fromParts(), not just assert().
Browse files Browse the repository at this point in the history
Bug: #55441
Change-Id: I27fd2cbdd750317a40583788690447d54df6a037
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/362440
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Jake Macdonald <jakemac@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Apr 16, 2024
1 parent f15bcc9 commit b08dae5
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 16 deletions.
4 changes: 4 additions & 0 deletions pkg/_macros/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.3

- Validate parts in `Code.fromParts()`.

## 0.1.2

- Add caching for `typeDeclarationOf` results.
Expand Down
15 changes: 12 additions & 3 deletions pkg/_macros/lib/src/api/code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ sealed class Code {

Code.fromString(String code) : parts = [code];

Code.fromParts(this.parts)
: assert(parts.every((element) =>
element is String || element is Code || element is Identifier));
Code.fromParts(this.parts) {
for (final part in parts) {
switch (part) {
case Code():
case Identifier():
case String():
break; // OK
default:
throw StateError('Unrecognized code part ${part.runtimeType}');
}
}
}
}

/// An arbitrary chunk of code, which does not have to be syntactically valid
Expand Down
2 changes: 1 addition & 1 deletion pkg/_macros/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: _macros
version: 0.1.2
version: 0.1.3
description: >-
This is a private SDK vendored package, which is re-exported by the public
`macros` package, which is a pub package. Every change to this package is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ holepunch:pkg/_macros/lib/src/api/builders.dart:
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

holepunch:pkg/_macros/lib/src/api/code.dart:
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:460:6)
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:469:6)
- _Enum. (from org-dartlang-sdk:///sdk/lib/core/enum.dart)
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ holepunch:pkg/_macros/lib/src/api/builders.dart:
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

holepunch:pkg/_macros/lib/src/api/code.dart:
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:460:6)
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:469:6)
- _Enum. (from org-dartlang-sdk:///sdk/lib/core/enum.dart)
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ holepunch:pkg/_macros/lib/src/api/builders.dart:
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

holepunch:pkg/_macros/lib/src/api/code.dart:
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:460:6)
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:469:6)
- _Enum. (from org-dartlang-sdk:///sdk/lib/core/enum.dart)
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ holepunch:pkg/_macros/lib/src/api/builders.dart:
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

holepunch:pkg/_macros/lib/src/api/code.dart:
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:460:6)
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:469:6)
- _Enum. (from org-dartlang-sdk:///sdk/lib/core/enum.dart)
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ holepunch:pkg/_macros/lib/src/api/builders.dart:
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

holepunch:pkg/_macros/lib/src/api/code.dart:
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:460:6)
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:469:6)
- _Enum. (from org-dartlang-sdk:///sdk/lib/core/enum.dart)
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ holepunch:pkg/_macros/lib/src/api/builders.dart:
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

holepunch:pkg/_macros/lib/src/api/code.dart:
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:460:6)
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:469:6)
- _Enum. (from org-dartlang-sdk:///sdk/lib/core/enum.dart)
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ holepunch:pkg/_macros/lib/src/api/builders.dart:
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

holepunch:pkg/_macros/lib/src/api/code.dart:
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:460:6)
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:469:6)
- _Enum. (from org-dartlang-sdk:///sdk/lib/core/enum.dart)
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ holepunch:pkg/_macros/lib/src/api/builders.dart:
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

holepunch:pkg/_macros/lib/src/api/code.dart:
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:460:6)
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:469:6)
- _Enum. (from org-dartlang-sdk:///sdk/lib/core/enum.dart)
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ holepunch:pkg/_macros/lib/src/api/builders.dart:
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

holepunch:pkg/_macros/lib/src/api/code.dart:
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:460:6)
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:469:6)
- _Enum. (from org-dartlang-sdk:///sdk/lib/core/enum.dart)
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ holepunch:pkg/_macros/lib/src/api/builders.dart:
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

holepunch:pkg/_macros/lib/src/api/code.dart:
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:460:6)
- CodeKind. (from holepunch:pkg/_macros/lib/src/api/code.dart:469:6)
- _Enum. (from org-dartlang-sdk:///sdk/lib/core/enum.dart)
- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart)

Expand Down
4 changes: 4 additions & 0 deletions pkg/macros/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.0-main.3

- Validate parts in `Code.fromParts()`.

## 0.1.0-main.2

- Add caching for `typeDeclarationOf` results.
Expand Down
4 changes: 2 additions & 2 deletions pkg/macros/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: macros
version: 0.1.0-main.2
version: 0.1.0-main.3
description: >-
This package is for macro authors, and exposes the APIs necessary to write
a macro. It exports the APIs from the private `_macros` SDK vendored package.
Expand All @@ -11,4 +11,4 @@ environment:
dependencies:
_macros:
sdk: dart
version: 0.1.2
version: 0.1.3

0 comments on commit b08dae5

Please sign in to comment.