├── .github ├── dependabot.yml └── workflows │ ├── publish.yaml │ └── test-package.yml ├── .gitignore ├── AUTHORS ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── analysis_options.yaml ├── example └── example.dart ├── lib ├── code_builder.dart └── src │ ├── allocator.dart │ ├── base.dart │ ├── emitter.dart │ ├── matchers.dart │ ├── mixins │ ├── annotations.dart │ ├── dartdoc.dart │ └── generics.dart │ ├── specs │ ├── class.dart │ ├── class.g.dart │ ├── code.dart │ ├── code.g.dart │ ├── constructor.dart │ ├── constructor.g.dart │ ├── directive.dart │ ├── directive.g.dart │ ├── enum.dart │ ├── enum.g.dart │ ├── expression.dart │ ├── expression │ │ ├── binary.dart │ │ ├── closure.dart │ │ ├── code.dart │ │ ├── invoke.dart │ │ ├── literal.dart │ │ └── parenthesized.dart │ ├── extension.dart │ ├── extension.g.dart │ ├── extension_type.dart │ ├── extension_type.g.dart │ ├── field.dart │ ├── field.g.dart │ ├── library.dart │ ├── library.g.dart │ ├── method.dart │ ├── method.g.dart │ ├── mixin.dart │ ├── mixin.g.dart │ ├── reference.dart │ ├── type_function.dart │ ├── type_function.g.dart │ ├── type_record.dart │ ├── type_record.g.dart │ ├── type_reference.dart │ ├── type_reference.g.dart │ ├── typedef.dart │ └── typedef.g.dart │ └── visitors.dart ├── pubspec.yaml ├── test ├── allocator_test.dart ├── common.dart ├── const_test.dart ├── directive_test.dart ├── e2e │ └── injection_test.dart ├── matcher_test.dart └── specs │ ├── class_test.dart │ ├── code │ ├── expression_test.dart │ └── statement_test.dart │ ├── enum_test.dart │ ├── extension_test.dart │ ├── extension_type_test.dart │ ├── field_test.dart │ ├── library_test.dart │ ├── method_test.dart │ ├── mixin_test.dart │ ├── record_type_test.dart │ └── type_reference_test.dart └── tool └── regenerate.sh /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # Set update schedule for GitHub Actions 2 | # See https://docs.github.com/en/github/administering-a-repository/keeping-your-actions-up-to-date-with-dependabot 3 | 4 | version: 2 5 | updates: 6 | 7 | - package-ecosystem: github-actions 8 | directory: / 9 | schedule: 10 | interval: monthly 11 | labels: 12 | - autosubmit 13 | groups: 14 | github-actions: 15 | patterns: 16 | - "*" 17 | -------------------------------------------------------------------------------- /.github/workflows/publish.yaml: -------------------------------------------------------------------------------- 1 | # A CI configuration to auto-publish pub packages. 2 | 3 | name: Publish 4 | 5 | on: 6 | pull_request: 7 | branches: [ master ] 8 | types: [opened, synchronize, reopened, labeled, unlabeled] 9 | push: 10 | tags: [ 'v[0-9]+.[0-9]+.[0-9]+*' ] 11 | 12 | jobs: 13 | publish: 14 | if: ${{ github.repository_owner == 'dart-lang' }} 15 | uses: dart-lang/ecosystem/.github/workflows/publish.yaml@main 16 | with: 17 | sdk: dev 18 | -------------------------------------------------------------------------------- /.github/workflows/test-package.yml: -------------------------------------------------------------------------------- 1 | name: Dart CI 2 | 3 | on: 4 | # Run on PRs and pushes to the default branch. 5 | push: 6 | branches: [ master ] 7 | pull_request: 8 | branches: [ master ] 9 | schedule: 10 | - cron: "0 0 * * 0" 11 | 12 | env: 13 | PUB_ENVIRONMENT: bot.github 14 | 15 | jobs: 16 | # Check code formatting and static analysis on a single OS (linux) 17 | # against Dart dev and an earlier stable version. 18 | analyze: 19 | runs-on: ubuntu-latest 20 | strategy: 21 | fail-fast: false 22 | matrix: 23 | sdk: [dev] 24 | steps: 25 | - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 26 | - uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672 27 | with: 28 | sdk: ${{ matrix.sdk }} 29 | - id: install 30 | name: Install dependencies 31 | run: dart pub get 32 | - name: Check formatting 33 | run: dart format --output=none --set-exit-if-changed . 34 | if: always() && steps.install.outcome == 'success' 35 | - name: Analyze code 36 | run: dart analyze --fatal-infos . 37 | if: always() && steps.install.outcome == 'success' 38 | 39 | test: 40 | needs: analyze 41 | runs-on: ${{ matrix.os }} 42 | strategy: 43 | fail-fast: false 44 | matrix: 45 | os: [ubuntu-latest] 46 | sdk: [3.5.0, dev] 47 | steps: 48 | - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 49 | - uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672 50 | with: 51 | sdk: ${{ matrix.sdk }} 52 | - id: install 53 | name: Install dependencies 54 | run: dart pub get 55 | - name: Run VM tests 56 | run: dart test --platform vm 57 | if: always() && steps.install.outcome == 'success' 58 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool 3 | .packages 4 | .pub 5 | pubspec.lock 6 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # Below is a list of people and organizations that have contributed 2 | # to the project. Names should be added to the list like so: 3 | # 4 | # Name/Organization 5 | 6 | Nikolas Rimikis 7 | Google Inc. 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Want to contribute? Great! First, read this page (including the small print at 2 | the end). 3 | 4 | ### Before you contribute 5 | Before we can use your code, you must sign the 6 | [Google Individual Contributor License Agreement](https://cla.developers.google.com/about/google-individual) 7 | (CLA), which you can do online. The CLA is necessary mainly because you own the 8 | copyright to your changes, even after your contribution becomes part of our 9 | codebase, so we need your permission to use and distribute your code. We also 10 | need to be sure of various other things—for instance that you'll tell us if you 11 | know that your code infringes on other people's patents. You don't have to sign 12 | the CLA until after you've submitted your code for review and a member has 13 | approved it, but you must do it before we can put your code into our codebase. 14 | 15 | Before you start working on a larger contribution, you should get in touch with 16 | us first through the issue tracker with your idea so that we can help out and 17 | possibly guide you. Coordinating up front makes it much easier to avoid 18 | frustration later on. 19 | 20 | ### Code reviews 21 | All submissions, including submissions by project members, require review. 22 | 23 | ### File headers 24 | All files in the project must start with the following header. 25 | 26 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 27 | // for details. All rights reserved. Use of this source code is governed by a 28 | // BSD-style license that can be found in the LICENSE file. 29 | 30 | ### The small print 31 | Contributions made by corporations are covered by a different agreement than the 32 | one above, the 33 | [Software Grant and Corporate Contributor License Agreement](https://developers.google.com/open-source/cla/corporate). -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2016, the Dart project authors. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following 11 | disclaimer in the documentation and/or other materials provided 12 | with the distribution. 13 | * Neither the name of Google LLC nor the names of its 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > [!IMPORTANT] 2 | > This repo has moved to https://github.com/dart-lang/tools/tree/main/pkgs/code_builder 3 | 4 | [![Dart CI](https://github.com/dart-lang/code_builder/actions/workflows/test-package.yml/badge.svg)](https://github.com/dart-lang/code_builder/actions/workflows/test-package.yml) 5 | [![Pub package](https://img.shields.io/pub/v/code_builder.svg)](https://pub.dev/packages/code_builder) 6 | [![package publisher](https://img.shields.io/pub/publisher/code_builder.svg)](https://pub.dev/packages/code_builder/publisher) 7 | [![Gitter chat](https://badges.gitter.im/dart-lang/build.svg)](https://gitter.im/dart-lang/build) 8 | 9 | A fluent, builder-based library for generating valid Dart code. 10 | 11 | ## Usage 12 | 13 | `code_builder` has a narrow and user-friendly API. 14 | 15 | See the `example` and `test` folders for additional examples. 16 | 17 | For example creating a class with a method: 18 | 19 | ```dart 20 | import 'package:code_builder/code_builder.dart'; 21 | import 'package:dart_style/dart_style.dart'; 22 | 23 | void main() { 24 | final animal = Class((b) => b 25 | ..name = 'Animal' 26 | ..extend = refer('Organism') 27 | ..methods.add(Method.returnsVoid((b) => b 28 | ..name = 'eat' 29 | ..body = const Code("print('Yum!');")))); 30 | final emitter = DartEmitter(); 31 | print( 32 | DartFormatter(languageVersion: DartFormatter.latestLanguageVersion) 33 | .format('${animal.accept(emitter)}'), 34 | ); 35 | } 36 | ``` 37 | 38 | Outputs: 39 | 40 | ```dart 41 | class Animal extends Organism { 42 | void eat() => print('Yum!'); 43 | } 44 | ``` 45 | 46 | Have a complicated set of dependencies for your generated code? `code_builder` 47 | supports automatic scoping of your ASTs to automatically use prefixes to avoid 48 | symbol conflicts: 49 | 50 | ```dart 51 | import 'package:code_builder/code_builder.dart'; 52 | import 'package:dart_style/dart_style.dart'; 53 | 54 | void main() { 55 | final library = Library((b) => b.body.addAll([ 56 | Method((b) => b 57 | ..body = const Code('') 58 | ..name = 'doThing' 59 | ..returns = refer('Thing', 'package:a/a.dart')), 60 | Method((b) => b 61 | ..body = const Code('') 62 | ..name = 'doOther' 63 | ..returns = refer('Other', 'package:b/b.dart')), 64 | ])); 65 | final emitter = DartEmitter.scoped(); 66 | print( 67 | DartFormatter(languageVersion: DartFormatter.latestLanguageVersion) 68 | .format('${library.accept(emitter)}'), 69 | ); 70 | } 71 | ``` 72 | 73 | Outputs: 74 | 75 | ```dart 76 | import 'package:a/a.dart' as _i1; 77 | import 'package:b/b.dart' as _i2; 78 | 79 | _i1.Thing doThing() {} 80 | _i2.Other doOther() {} 81 | ``` 82 | 83 | ## Contributing 84 | 85 | - Read and help us document common patterns over [at the wiki][wiki]. 86 | - Is there a _bug_ in the code? [File an issue][issue]. 87 | 88 | If a feature is missing (the Dart language is always evolving) or you'd like an 89 | easier or better way to do something, consider [opening a pull request][pull]. 90 | You can always [file an issue][issue], but generally speaking, feature requests 91 | will be on a best-effort basis. 92 | 93 | > **NOTE**: Due to the evolving Dart SDK the local `dartfmt` must be used to 94 | > format this repository. You can run it simply from the command-line: 95 | > 96 | > ```sh 97 | > $ dart run dart_style:format -w . 98 | > ``` 99 | 100 | [wiki]: https://github.com/dart-lang/code_builder/wiki 101 | [issue]: https://github.com/dart-lang/code_builder/issues 102 | [pull]: https://github.com/dart-lang/code_builder/pulls 103 | 104 | ### Updating generated (`.g.dart`) files 105 | 106 | > **NOTE**: There is currently a limitation in `build_runner` that requires a 107 | > workaround for developing this package since it is a dependency of the build 108 | > system. 109 | 110 | Make a snapshot of the generated [`build_runner`][build_runner] build script and 111 | run from the snapshot instead of from source to avoid problems with deleted 112 | files. These steps must be run without deleting the source files. 113 | 114 | ```bash 115 | ./tool/regenerate.sh 116 | ``` 117 | 118 | [build_runner]: https://pub.dev/packages/build_runner 119 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | include: package:dart_flutter_team_lints/analysis_options.yaml 2 | 3 | analyzer: 4 | language: 5 | strict-casts: true 6 | strict-inference: true 7 | strict-raw-types: true 8 | 9 | linter: 10 | rules: 11 | - avoid_private_typedef_functions 12 | - avoid_redundant_argument_values 13 | - avoid_unused_constructor_parameters 14 | - cancel_subscriptions 15 | - cascade_invocations 16 | - join_return_with_assignment 17 | - literal_only_boolean_expressions 18 | - missing_whitespace_between_adjacent_strings 19 | - no_adjacent_strings_in_list 20 | - no_runtimeType_toString 21 | - prefer_const_declarations 22 | - prefer_expression_function_bodies 23 | - prefer_final_locals 24 | - unnecessary_await_in_return 25 | - use_string_buffers 26 | -------------------------------------------------------------------------------- /example/example.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:dart_style/dart_style.dart'; 7 | 8 | final _dartfmt = DartFormatter( 9 | languageVersion: DartFormatter.latestLanguageVersion, 10 | ); 11 | 12 | void main() { 13 | print('animalClass():\n${'=' * 40}\n${animalClass()}'); 14 | print('scopedLibrary():\n${'=' * 40}\n${scopedLibrary()}'); 15 | print('jsonEnum():\n${'=' * 40}\n${jsonEnum()}'); 16 | } 17 | 18 | /// Outputs: 19 | /// 20 | /// ```dart 21 | /// class Animal extends Organism { 22 | /// void eat() => print('Yum!'); 23 | /// } 24 | /// ``` 25 | String animalClass() { 26 | final animal = Class((b) => b 27 | ..name = 'Animal' 28 | ..extend = refer('Organism') 29 | ..methods.add(Method.returnsVoid((b) => b 30 | ..name = 'eat' 31 | ..body = refer('print').call([literalString('Yum!')]).code))); 32 | return _dartfmt.format('${animal.accept(DartEmitter())}'); 33 | } 34 | 35 | /// Outputs: 36 | /// 37 | /// ```dart 38 | /// import 'package:a/a.dart' as _i1; 39 | /// import 'package:b/b.dart' as _i2; 40 | /// 41 | /// _i1.Thing doThing() {} 42 | /// _i2.Other doOther() {} 43 | /// ``` 44 | String scopedLibrary() { 45 | final methods = [ 46 | Method((b) => b 47 | ..body = const Code('') 48 | ..name = 'doThing' 49 | ..returns = refer('Thing', 'package:a/a.dart')), 50 | Method((b) => b 51 | ..body = const Code('') 52 | ..name = 'doOther' 53 | ..returns = refer('Other', 'package:b/b.dart')), 54 | ]; 55 | final library = Library((b) => b.body.addAll(methods)); 56 | return _dartfmt.format('${library.accept(DartEmitter.scoped())}'); 57 | } 58 | 59 | /// Outputs: 60 | /// 61 | /// ```dart 62 | /// enum Unit { 63 | /// @JsonKey('m') 64 | /// metric, 65 | /// @JsonKey('i') 66 | /// imperial 67 | /// } 68 | /// ``` 69 | String jsonEnum() { 70 | final values = [ 71 | EnumValue((b) => b 72 | ..name = 'metric' 73 | ..annotations.addAll([ 74 | refer('JsonKey').call([literalString('m')]) 75 | ])), 76 | EnumValue((b) => b 77 | ..name = 'imperial' 78 | ..annotations.addAll([ 79 | refer('JsonKey').call([literalString('i')]) 80 | ])), 81 | ]; 82 | final e = Enum((b) => b 83 | ..name = 'Unit' 84 | ..values.addAll(values)); 85 | return _dartfmt.format('${e.accept(DartEmitter())}'); 86 | } 87 | -------------------------------------------------------------------------------- /lib/code_builder.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | export 'src/allocator.dart' show Allocator; 6 | export 'src/base.dart' show Spec, lazySpec; 7 | export 'src/emitter.dart' show DartEmitter; 8 | export 'src/matchers.dart' show EqualsDart, equalsDart; 9 | export 'src/specs/class.dart' show Class, ClassBuilder, ClassModifier; 10 | export 'src/specs/code.dart' 11 | show Block, BlockBuilder, Code, ScopedCode, StaticCode, lazyCode; 12 | export 'src/specs/constructor.dart' show Constructor, ConstructorBuilder; 13 | export 'src/specs/directive.dart' 14 | show Directive, DirectiveBuilder, DirectiveType; 15 | export 'src/specs/enum.dart' 16 | show Enum, EnumBuilder, EnumValue, EnumValueBuilder; 17 | export 'src/specs/expression.dart' 18 | show 19 | BinaryExpression, 20 | CodeExpression, 21 | Expression, 22 | ExpressionEmitter, 23 | ExpressionVisitor, 24 | InvokeExpression, 25 | InvokeExpressionType, 26 | LiteralExpression, 27 | LiteralListExpression, 28 | ParenthesizedExpression, 29 | ToCodeExpression, 30 | declareConst, 31 | declareFinal, 32 | declareVar, 33 | literal, 34 | literalBool, 35 | literalConstList, 36 | literalConstMap, 37 | literalConstRecord, 38 | literalConstSet, 39 | literalFalse, 40 | literalList, 41 | literalMap, 42 | literalNull, 43 | literalNullSafeSpread, 44 | literalNum, 45 | literalRecord, 46 | literalSet, 47 | literalSpread, 48 | literalString, 49 | literalTrue; 50 | export 'src/specs/extension.dart' show Extension, ExtensionBuilder; 51 | export 'src/specs/extension_type.dart' 52 | show 53 | ExtensionType, 54 | ExtensionTypeBuilder, 55 | RepresentationDeclaration, 56 | RepresentationDeclarationBuilder; 57 | export 'src/specs/field.dart' show Field, FieldBuilder, FieldModifier; 58 | export 'src/specs/library.dart' show Library, LibraryBuilder; 59 | export 'src/specs/method.dart' 60 | show 61 | Method, 62 | MethodBuilder, 63 | MethodModifier, 64 | MethodType, 65 | Parameter, 66 | ParameterBuilder; 67 | export 'src/specs/mixin.dart' show Mixin, MixinBuilder; 68 | export 'src/specs/reference.dart' show Reference, refer; 69 | export 'src/specs/type_function.dart' show FunctionType, FunctionTypeBuilder; 70 | export 'src/specs/type_record.dart' show RecordType, RecordTypeBuilder; 71 | export 'src/specs/type_reference.dart' show TypeReference, TypeReferenceBuilder; 72 | export 'src/specs/typedef.dart' show TypeDef, TypeDefBuilder; 73 | -------------------------------------------------------------------------------- /lib/src/allocator.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'specs/directive.dart'; 6 | import 'specs/reference.dart'; 7 | 8 | /// Collects references and automatically allocates prefixes and imports. 9 | /// 10 | /// `Allocator` takes out the manual work of deciding whether a symbol will 11 | /// clash with other imports in your generated code, or what imports are needed 12 | /// to resolve all symbols in your generated code. 13 | abstract class Allocator { 14 | /// An allocator that does not prefix symbols nor collects imports. 15 | static const Allocator none = _NullAllocator(); 16 | 17 | /// Creates a new default allocator that applies no prefixing. 18 | factory Allocator() = _Allocator; 19 | 20 | /// Creates a new allocator that applies naive prefixing to avoid conflicts. 21 | /// 22 | /// This implementation is not optimized for any particular code generation 23 | /// style and instead takes a conservative approach of prefixing _every_ 24 | /// import except references to `dart:core` (which are considered always 25 | /// imported). 26 | /// 27 | /// The prefixes are not guaranteed to be stable and cannot be expected to 28 | /// have any particular value. 29 | factory Allocator.simplePrefixing() = _PrefixedAllocator; 30 | 31 | /// Returns a reference string given a [reference] object. 32 | /// 33 | /// For example, a no-op implementation: 34 | /// ```dart 35 | /// allocate(const Reference('List', 'dart:core')); // Returns 'List'. 36 | /// ``` 37 | /// 38 | /// Where-as an implementation that prefixes imports might output: 39 | /// ```dart 40 | /// allocate(const Reference('Foo', 'package:foo')); // Returns '_i1.Foo'. 41 | /// ``` 42 | String allocate(Reference reference); 43 | 44 | /// All imports that have so far been added implicitly via [allocate]. 45 | Iterable get imports; 46 | } 47 | 48 | class _Allocator implements Allocator { 49 | final _imports = {}; 50 | 51 | @override 52 | String allocate(Reference reference) { 53 | final url = reference.url; 54 | if (url != null) { 55 | _imports.add(url); 56 | } 57 | return reference.symbol!; 58 | } 59 | 60 | @override 61 | Iterable get imports => _imports.map(Directive.import); 62 | } 63 | 64 | class _NullAllocator implements Allocator { 65 | const _NullAllocator(); 66 | 67 | @override 68 | String allocate(Reference reference) => reference.symbol!; 69 | 70 | @override 71 | Iterable get imports => const []; 72 | } 73 | 74 | class _PrefixedAllocator implements Allocator { 75 | static const _doNotPrefix = ['dart:core']; 76 | 77 | final _imports = {}; 78 | var _keys = 1; 79 | 80 | @override 81 | String allocate(Reference reference) { 82 | final symbol = reference.symbol; 83 | final url = reference.url; 84 | if (url == null || _doNotPrefix.contains(url)) { 85 | return symbol!; 86 | } 87 | return '_i${_imports.putIfAbsent(url, _nextKey)}.$symbol'; 88 | } 89 | 90 | int _nextKey() => _keys++; 91 | 92 | @override 93 | Iterable get imports => _imports.keys.map( 94 | (u) => Directive.import(u, as: '_i${_imports[u]}'), 95 | ); 96 | } 97 | -------------------------------------------------------------------------------- /lib/src/base.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'visitors.dart'; 6 | 7 | abstract class Spec { 8 | R accept(SpecVisitor visitor, [R? context]); 9 | } 10 | 11 | /// Returns a generic [Spec] that is lazily generated when visited. 12 | Spec lazySpec(Spec Function() generate) => _LazySpec(generate); 13 | 14 | class _LazySpec implements Spec { 15 | final Spec Function() generate; 16 | 17 | const _LazySpec(this.generate); 18 | 19 | @override 20 | R accept(SpecVisitor visitor, [R? context]) => 21 | generate().accept(visitor, context); 22 | } 23 | -------------------------------------------------------------------------------- /lib/src/matchers.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:matcher/matcher.dart'; 6 | 7 | import 'base.dart'; 8 | import 'emitter.dart'; 9 | 10 | /// Encodes [spec] as Dart source code. 11 | String _dart(Spec spec, DartEmitter emitter) => 12 | EqualsDart._format(spec.accept(emitter).toString()); 13 | 14 | /// Returns a matcher for [Spec] objects that emit code matching [source]. 15 | /// 16 | /// Both [source] and the result emitted from the compared [Spec] are formatted 17 | /// with [EqualsDart.format]. A plain [DartEmitter] is used by default and may 18 | /// be overridden with [emitter]. 19 | Matcher equalsDart( 20 | String source, [ 21 | DartEmitter? emitter, 22 | ]) => 23 | EqualsDart._(EqualsDart._format(source), emitter ?? DartEmitter()); 24 | 25 | /// Implementation detail of using the [equalsDart] matcher. 26 | /// 27 | /// See [EqualsDart.format] to specify the default source code formatter. 28 | class EqualsDart extends Matcher { 29 | /// May override to provide a function to format Dart on [equalsDart]. 30 | /// 31 | /// By default, uses [collapseWhitespace], but it is recommended to instead 32 | /// use `dart_style` (dartfmt) where possible. See `test/common.dart` for an 33 | /// example. 34 | static String Function(String) format = collapseWhitespace; 35 | 36 | static String _format(String source) { 37 | try { 38 | return format(source).trim(); 39 | } catch (_) { 40 | // Ignored on purpose, probably not exactly valid Dart code. 41 | return collapseWhitespace(source).trim(); 42 | } 43 | } 44 | 45 | final DartEmitter _emitter; 46 | final String _expectedSource; 47 | 48 | const EqualsDart._(this._expectedSource, this._emitter); 49 | 50 | @override 51 | Description describe(Description description) => 52 | description.add(_expectedSource); 53 | 54 | @override 55 | Description describeMismatch( 56 | covariant Spec item, 57 | Description mismatchDescription, 58 | Map matchState, 59 | bool verbose, 60 | ) { 61 | final actualSource = _dart(item, _emitter); 62 | return equals(_expectedSource).describeMismatch( 63 | actualSource, 64 | mismatchDescription, 65 | matchState, 66 | verbose, 67 | ); 68 | } 69 | 70 | @override 71 | bool matches(covariant Spec item, Object? matchState) => 72 | _dart(item, _emitter) == _expectedSource; 73 | } 74 | -------------------------------------------------------------------------------- /lib/src/mixins/annotations.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | 7 | import '../specs/expression.dart'; 8 | 9 | /// A type of AST node that can have metadata [annotations]. 10 | abstract mixin class HasAnnotations { 11 | /// Annotations as metadata on the node. 12 | BuiltList get annotations; 13 | } 14 | 15 | /// Compliment to the [HasAnnotations] mixin for metadata [annotations]. 16 | abstract mixin class HasAnnotationsBuilder { 17 | /// Annotations as metadata on the node. 18 | abstract ListBuilder annotations; 19 | } 20 | -------------------------------------------------------------------------------- /lib/src/mixins/dartdoc.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | 7 | abstract mixin class HasDartDocs { 8 | /// Dart docs. 9 | BuiltList get docs; 10 | } 11 | 12 | abstract mixin class HasDartDocsBuilder { 13 | /// Dart docs. 14 | abstract ListBuilder docs; 15 | } 16 | -------------------------------------------------------------------------------- /lib/src/mixins/generics.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | 7 | import '../specs/reference.dart'; 8 | 9 | abstract mixin class HasGenerics { 10 | /// Generic type parameters. 11 | BuiltList get types; 12 | } 13 | 14 | abstract mixin class HasGenericsBuilder { 15 | /// Generic type parameters. 16 | abstract ListBuilder types; 17 | } 18 | -------------------------------------------------------------------------------- /lib/src/specs/class.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../mixins/annotations.dart'; 11 | import '../mixins/dartdoc.dart'; 12 | import '../mixins/generics.dart'; 13 | import '../visitors.dart'; 14 | import 'constructor.dart'; 15 | import 'expression.dart'; 16 | import 'field.dart'; 17 | import 'method.dart'; 18 | import 'reference.dart'; 19 | 20 | part 'class.g.dart'; 21 | 22 | @immutable 23 | abstract class Class extends Object 24 | with HasAnnotations, HasDartDocs, HasGenerics 25 | implements Built, Spec { 26 | factory Class([void Function(ClassBuilder) updates]) = _$Class; 27 | 28 | Class._(); 29 | 30 | /// Whether the class is `abstract`. 31 | bool get abstract; 32 | 33 | /// Whether the class is `sealed`. 34 | bool get sealed; 35 | 36 | /// Whether the class is a `mixin class`. 37 | bool get mixin; 38 | 39 | /// The class modifier, i.e. `base`, `final`, `interface`. 40 | ClassModifier? get modifier; 41 | 42 | @override 43 | BuiltList get annotations; 44 | 45 | @override 46 | BuiltList get docs; 47 | 48 | Reference? get extend; 49 | 50 | BuiltList get implements; 51 | 52 | BuiltList get mixins; 53 | 54 | @override 55 | BuiltList get types; 56 | 57 | BuiltList get constructors; 58 | BuiltList get methods; 59 | BuiltList get fields; 60 | 61 | /// Name of the class. 62 | String get name; 63 | 64 | @override 65 | R accept( 66 | SpecVisitor visitor, [ 67 | R? context, 68 | ]) => 69 | visitor.visitClass(this, context); 70 | } 71 | 72 | enum ClassModifier { 73 | base, 74 | final$, 75 | interface; 76 | 77 | String get name => switch (this) { 78 | ClassModifier.base => 'base', 79 | ClassModifier.final$ => 'final', 80 | ClassModifier.interface => 'interface' 81 | }; 82 | } 83 | 84 | abstract class ClassBuilder extends Object 85 | with HasAnnotationsBuilder, HasDartDocsBuilder, HasGenericsBuilder 86 | implements Builder { 87 | factory ClassBuilder() = _$ClassBuilder; 88 | 89 | ClassBuilder._(); 90 | 91 | @override 92 | void update(void Function(ClassBuilder)? updates) { 93 | updates?.call(this); 94 | } 95 | 96 | /// Whether the class is `abstract`. 97 | bool abstract = false; 98 | 99 | /// Whether the class is `sealed`. 100 | bool sealed = false; 101 | 102 | /// Whether the class is a `mixin class`. 103 | bool mixin = false; 104 | 105 | /// The class modifier, i.e. `base`, `final`, `interface`. 106 | ClassModifier? modifier; 107 | 108 | @override 109 | ListBuilder annotations = ListBuilder(); 110 | 111 | @override 112 | ListBuilder docs = ListBuilder(); 113 | 114 | Reference? extend; 115 | 116 | ListBuilder implements = ListBuilder(); 117 | ListBuilder mixins = ListBuilder(); 118 | 119 | @override 120 | ListBuilder types = ListBuilder(); 121 | 122 | ListBuilder constructors = ListBuilder(); 123 | ListBuilder methods = ListBuilder(); 124 | ListBuilder fields = ListBuilder(); 125 | 126 | /// Name of the class. 127 | String? name; 128 | } 129 | -------------------------------------------------------------------------------- /lib/src/specs/code.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../allocator.dart'; 10 | import '../base.dart'; 11 | import '../emitter.dart'; 12 | import '../visitors.dart'; 13 | import 'expression.dart'; 14 | import 'reference.dart'; 15 | 16 | part 'code.g.dart'; 17 | 18 | /// Returns a scoped symbol to [Reference], with an import prefix if needed. 19 | /// 20 | /// This is short-hand for [Allocator.allocate] in most implementations. 21 | typedef Allocate = String Function(Reference); 22 | 23 | /// Represents arbitrary Dart code (either expressions or statements). 24 | /// 25 | /// See the various constructors for details. 26 | abstract class Code implements Spec { 27 | /// Create a simple code body based on a static string. 28 | const factory Code(String code) = StaticCode._; 29 | 30 | /// Create a code based that may use a provided [Allocator] for scoping: 31 | /// 32 | /// ```dart 33 | /// // Emits `_i123.FooType()`, where `_i123` is the import prefix. 34 | /// 35 | /// Code.scope((a) { 36 | /// return '${a.allocate(fooType)}()' 37 | /// }); 38 | /// ``` 39 | const factory Code.scope( 40 | String Function(Allocate) scope, 41 | ) = ScopedCode._; 42 | 43 | @override 44 | R accept(covariant CodeVisitor visitor, [R? context]); 45 | } 46 | 47 | /// Represents blocks of statements of Dart code. 48 | abstract class Block implements Built, Code, Spec { 49 | factory Block([void Function(BlockBuilder) updates]) = _$Block; 50 | 51 | factory Block.of(Iterable statements) => 52 | Block((b) => b..statements.addAll(statements)); 53 | 54 | Block._(); 55 | 56 | @override 57 | R accept(covariant CodeVisitor visitor, [R? context]) => 58 | visitor.visitBlock(this, context); 59 | 60 | BuiltList get statements; 61 | } 62 | 63 | abstract class BlockBuilder implements Builder { 64 | factory BlockBuilder() = _$BlockBuilder; 65 | 66 | BlockBuilder._(); 67 | 68 | /// Adds an [expression] to [statements]. 69 | /// 70 | /// **NOTE**: Not all expressions are _useful_ statements. 71 | void addExpression(Expression expression) { 72 | statements.add(expression.statement); 73 | } 74 | 75 | ListBuilder statements = ListBuilder(); 76 | } 77 | 78 | /// Knowledge of different types of blocks of code in Dart. 79 | /// 80 | /// **INTERNAL ONLY**. 81 | abstract class CodeVisitor implements SpecVisitor { 82 | T visitBlock(Block code, [T? context]); 83 | 84 | T visitStaticCode(StaticCode code, [T? context]); 85 | 86 | T visitScopedCode(ScopedCode code, [T? context]); 87 | } 88 | 89 | /// Knowledge of how to write valid Dart code from [CodeVisitor]. 90 | abstract mixin class CodeEmitter implements CodeVisitor { 91 | @protected 92 | Allocator get allocator; 93 | 94 | @override 95 | StringSink visitBlock(Block block, [StringSink? output]) { 96 | output ??= StringBuffer(); 97 | return visitAll(block.statements, output, (statement) { 98 | statement.accept(this, output); 99 | }, '\n'); 100 | } 101 | 102 | @override 103 | StringSink visitStaticCode(StaticCode code, [StringSink? output]) { 104 | output ??= StringBuffer(); 105 | return output..write(code.code); 106 | } 107 | 108 | @override 109 | StringSink visitScopedCode(ScopedCode code, [StringSink? output]) { 110 | output ??= StringBuffer(); 111 | return output..write(code.code(allocator.allocate)); 112 | } 113 | } 114 | 115 | /// Represents a code block that requires lazy visiting. 116 | class LazyCode implements Code { 117 | final Spec Function(SpecVisitor) generate; 118 | 119 | const LazyCode._(this.generate); 120 | 121 | @override 122 | R accept(CodeVisitor visitor, [R? context]) => 123 | generate(visitor).accept(visitor, context); 124 | } 125 | 126 | /// Returns a generic [Code] that is lazily generated when visited. 127 | Code lazyCode(Code Function() generate) => _LazyCode(generate); 128 | 129 | class _LazyCode implements Code { 130 | final Code Function() generate; 131 | 132 | const _LazyCode(this.generate); 133 | 134 | @override 135 | R accept(CodeVisitor visitor, [R? context]) => 136 | generate().accept(visitor, context); 137 | } 138 | 139 | /// Represents a simple, literal code block to be inserted as-is. 140 | class StaticCode implements Code { 141 | final String code; 142 | 143 | const StaticCode._(this.code); 144 | 145 | @override 146 | R accept(CodeVisitor visitor, [R? context]) => 147 | visitor.visitStaticCode(this, context); 148 | 149 | @override 150 | String toString() => code; 151 | } 152 | 153 | /// Represents a code block that may require scoping. 154 | class ScopedCode implements Code { 155 | final String Function(Allocate) code; 156 | 157 | const ScopedCode._(this.code); 158 | 159 | @override 160 | R accept(CodeVisitor visitor, [R? context]) => 161 | visitor.visitScopedCode(this, context); 162 | 163 | @override 164 | String toString() => code((ref) => ref.symbol!); 165 | } 166 | -------------------------------------------------------------------------------- /lib/src/specs/code.g.dart: -------------------------------------------------------------------------------- 1 | // GENERATED CODE - DO NOT MODIFY BY HAND 2 | 3 | part of 'code.dart'; 4 | 5 | // ************************************************************************** 6 | // BuiltValueGenerator 7 | // ************************************************************************** 8 | 9 | class _$Block extends Block { 10 | @override 11 | final BuiltList statements; 12 | 13 | factory _$Block([void Function(BlockBuilder)? updates]) => 14 | (new BlockBuilder()..update(updates)).build() as _$Block; 15 | 16 | _$Block._({required this.statements}) : super._() { 17 | BuiltValueNullFieldError.checkNotNull(statements, r'Block', 'statements'); 18 | } 19 | 20 | @override 21 | Block rebuild(void Function(BlockBuilder) updates) => 22 | (toBuilder()..update(updates)).build(); 23 | 24 | @override 25 | _$BlockBuilder toBuilder() => new _$BlockBuilder()..replace(this); 26 | 27 | @override 28 | bool operator ==(Object other) { 29 | if (identical(other, this)) return true; 30 | return other is Block && statements == other.statements; 31 | } 32 | 33 | @override 34 | int get hashCode { 35 | var _$hash = 0; 36 | _$hash = $jc(_$hash, statements.hashCode); 37 | _$hash = $jf(_$hash); 38 | return _$hash; 39 | } 40 | 41 | @override 42 | String toString() { 43 | return (newBuiltValueToStringHelper(r'Block') 44 | ..add('statements', statements)) 45 | .toString(); 46 | } 47 | } 48 | 49 | class _$BlockBuilder extends BlockBuilder { 50 | _$Block? _$v; 51 | 52 | @override 53 | ListBuilder get statements { 54 | _$this; 55 | return super.statements; 56 | } 57 | 58 | @override 59 | set statements(ListBuilder statements) { 60 | _$this; 61 | super.statements = statements; 62 | } 63 | 64 | _$BlockBuilder() : super._(); 65 | 66 | BlockBuilder get _$this { 67 | final $v = _$v; 68 | if ($v != null) { 69 | super.statements = $v.statements.toBuilder(); 70 | _$v = null; 71 | } 72 | return this; 73 | } 74 | 75 | @override 76 | void replace(Block other) { 77 | ArgumentError.checkNotNull(other, 'other'); 78 | _$v = other as _$Block; 79 | } 80 | 81 | @override 82 | void update(void Function(BlockBuilder)? updates) { 83 | if (updates != null) updates(this); 84 | } 85 | 86 | @override 87 | Block build() => _build(); 88 | 89 | _$Block _build() { 90 | _$Block _$result; 91 | try { 92 | _$result = _$v ?? new _$Block._(statements: statements.build()); 93 | } catch (_) { 94 | late String _$failedField; 95 | try { 96 | _$failedField = 'statements'; 97 | statements.build(); 98 | } catch (e) { 99 | throw new BuiltValueNestedFieldError( 100 | r'Block', _$failedField, e.toString()); 101 | } 102 | rethrow; 103 | } 104 | replace(_$result); 105 | return _$result; 106 | } 107 | } 108 | 109 | // ignore_for_file: deprecated_member_use_from_same_package,type=lint 110 | -------------------------------------------------------------------------------- /lib/src/specs/constructor.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../mixins/annotations.dart'; 10 | import '../mixins/dartdoc.dart'; 11 | import 'code.dart'; 12 | import 'expression.dart'; 13 | import 'method.dart'; 14 | import 'reference.dart'; 15 | 16 | part 'constructor.g.dart'; 17 | 18 | @immutable 19 | abstract class Constructor extends Object 20 | with HasAnnotations, HasDartDocs 21 | implements Built { 22 | factory Constructor([void Function(ConstructorBuilder) updates]) = 23 | _$Constructor; 24 | 25 | Constructor._(); 26 | 27 | @override 28 | BuiltList get annotations; 29 | 30 | @override 31 | BuiltList get docs; 32 | 33 | /// Optional parameters. 34 | BuiltList get optionalParameters; 35 | 36 | /// Required parameters. 37 | BuiltList get requiredParameters; 38 | 39 | /// Constructor initializer statements. 40 | BuiltList get initializers; 41 | 42 | /// Body of the method. 43 | Code? get body; 44 | 45 | /// Whether the constructor should be prefixed with `external`. 46 | bool get external; 47 | 48 | /// Whether the constructor should be prefixed with `const`. 49 | bool get constant; 50 | 51 | /// Whether this constructor should be prefixed with `factory`. 52 | bool get factory; 53 | 54 | /// Whether this constructor is a simple lambda expression. 55 | bool? get lambda; 56 | 57 | /// Name of the constructor - optional. 58 | String? get name; 59 | 60 | /// If non-null, redirect to this constructor. 61 | Reference? get redirect; 62 | } 63 | 64 | abstract class ConstructorBuilder extends Object 65 | with HasAnnotationsBuilder, HasDartDocsBuilder 66 | implements Builder { 67 | factory ConstructorBuilder() = _$ConstructorBuilder; 68 | 69 | ConstructorBuilder._(); 70 | 71 | @override 72 | ListBuilder annotations = ListBuilder(); 73 | 74 | @override 75 | ListBuilder docs = ListBuilder(); 76 | 77 | /// Optional parameters. 78 | ListBuilder optionalParameters = ListBuilder(); 79 | 80 | /// Required parameters. 81 | ListBuilder requiredParameters = ListBuilder(); 82 | 83 | /// Constructor initializer statements. 84 | ListBuilder initializers = ListBuilder(); 85 | 86 | /// Body of the constructor. 87 | Code? body; 88 | 89 | /// Whether the constructor should be prefixed with `const`. 90 | bool constant = false; 91 | 92 | /// Whether the constructor should be prefixed with `external`. 93 | bool external = false; 94 | 95 | /// Whether this constructor should be prefixed with `factory`. 96 | bool factory = false; 97 | 98 | /// Whether this constructor is a simple lambda expression. 99 | bool? lambda; 100 | 101 | /// Name of the constructor - optional. 102 | String? name; 103 | 104 | /// If non-null, redirect to this constructor. 105 | Reference? redirect; 106 | } 107 | -------------------------------------------------------------------------------- /lib/src/specs/constructor.g.dart: -------------------------------------------------------------------------------- 1 | // GENERATED CODE - DO NOT MODIFY BY HAND 2 | 3 | part of 'constructor.dart'; 4 | 5 | // ************************************************************************** 6 | // BuiltValueGenerator 7 | // ************************************************************************** 8 | 9 | class _$Constructor extends Constructor { 10 | @override 11 | final BuiltList annotations; 12 | @override 13 | final BuiltList docs; 14 | @override 15 | final BuiltList optionalParameters; 16 | @override 17 | final BuiltList requiredParameters; 18 | @override 19 | final BuiltList initializers; 20 | @override 21 | final Code? body; 22 | @override 23 | final bool external; 24 | @override 25 | final bool constant; 26 | @override 27 | final bool factory; 28 | @override 29 | final bool? lambda; 30 | @override 31 | final String? name; 32 | @override 33 | final Reference? redirect; 34 | 35 | factory _$Constructor([void Function(ConstructorBuilder)? updates]) => 36 | (new ConstructorBuilder()..update(updates)).build() as _$Constructor; 37 | 38 | _$Constructor._( 39 | {required this.annotations, 40 | required this.docs, 41 | required this.optionalParameters, 42 | required this.requiredParameters, 43 | required this.initializers, 44 | this.body, 45 | required this.external, 46 | required this.constant, 47 | required this.factory, 48 | this.lambda, 49 | this.name, 50 | this.redirect}) 51 | : super._() { 52 | BuiltValueNullFieldError.checkNotNull( 53 | annotations, r'Constructor', 'annotations'); 54 | BuiltValueNullFieldError.checkNotNull(docs, r'Constructor', 'docs'); 55 | BuiltValueNullFieldError.checkNotNull( 56 | optionalParameters, r'Constructor', 'optionalParameters'); 57 | BuiltValueNullFieldError.checkNotNull( 58 | requiredParameters, r'Constructor', 'requiredParameters'); 59 | BuiltValueNullFieldError.checkNotNull( 60 | initializers, r'Constructor', 'initializers'); 61 | BuiltValueNullFieldError.checkNotNull(external, r'Constructor', 'external'); 62 | BuiltValueNullFieldError.checkNotNull(constant, r'Constructor', 'constant'); 63 | BuiltValueNullFieldError.checkNotNull(factory, r'Constructor', 'factory'); 64 | } 65 | 66 | @override 67 | Constructor rebuild(void Function(ConstructorBuilder) updates) => 68 | (toBuilder()..update(updates)).build(); 69 | 70 | @override 71 | _$ConstructorBuilder toBuilder() => new _$ConstructorBuilder()..replace(this); 72 | 73 | @override 74 | bool operator ==(Object other) { 75 | if (identical(other, this)) return true; 76 | return other is Constructor && 77 | annotations == other.annotations && 78 | docs == other.docs && 79 | optionalParameters == other.optionalParameters && 80 | requiredParameters == other.requiredParameters && 81 | initializers == other.initializers && 82 | body == other.body && 83 | external == other.external && 84 | constant == other.constant && 85 | factory == other.factory && 86 | lambda == other.lambda && 87 | name == other.name && 88 | redirect == other.redirect; 89 | } 90 | 91 | @override 92 | int get hashCode { 93 | var _$hash = 0; 94 | _$hash = $jc(_$hash, annotations.hashCode); 95 | _$hash = $jc(_$hash, docs.hashCode); 96 | _$hash = $jc(_$hash, optionalParameters.hashCode); 97 | _$hash = $jc(_$hash, requiredParameters.hashCode); 98 | _$hash = $jc(_$hash, initializers.hashCode); 99 | _$hash = $jc(_$hash, body.hashCode); 100 | _$hash = $jc(_$hash, external.hashCode); 101 | _$hash = $jc(_$hash, constant.hashCode); 102 | _$hash = $jc(_$hash, factory.hashCode); 103 | _$hash = $jc(_$hash, lambda.hashCode); 104 | _$hash = $jc(_$hash, name.hashCode); 105 | _$hash = $jc(_$hash, redirect.hashCode); 106 | _$hash = $jf(_$hash); 107 | return _$hash; 108 | } 109 | 110 | @override 111 | String toString() { 112 | return (newBuiltValueToStringHelper(r'Constructor') 113 | ..add('annotations', annotations) 114 | ..add('docs', docs) 115 | ..add('optionalParameters', optionalParameters) 116 | ..add('requiredParameters', requiredParameters) 117 | ..add('initializers', initializers) 118 | ..add('body', body) 119 | ..add('external', external) 120 | ..add('constant', constant) 121 | ..add('factory', factory) 122 | ..add('lambda', lambda) 123 | ..add('name', name) 124 | ..add('redirect', redirect)) 125 | .toString(); 126 | } 127 | } 128 | 129 | class _$ConstructorBuilder extends ConstructorBuilder { 130 | _$Constructor? _$v; 131 | 132 | @override 133 | ListBuilder get annotations { 134 | _$this; 135 | return super.annotations; 136 | } 137 | 138 | @override 139 | set annotations(ListBuilder annotations) { 140 | _$this; 141 | super.annotations = annotations; 142 | } 143 | 144 | @override 145 | ListBuilder get docs { 146 | _$this; 147 | return super.docs; 148 | } 149 | 150 | @override 151 | set docs(ListBuilder docs) { 152 | _$this; 153 | super.docs = docs; 154 | } 155 | 156 | @override 157 | ListBuilder get optionalParameters { 158 | _$this; 159 | return super.optionalParameters; 160 | } 161 | 162 | @override 163 | set optionalParameters(ListBuilder optionalParameters) { 164 | _$this; 165 | super.optionalParameters = optionalParameters; 166 | } 167 | 168 | @override 169 | ListBuilder get requiredParameters { 170 | _$this; 171 | return super.requiredParameters; 172 | } 173 | 174 | @override 175 | set requiredParameters(ListBuilder requiredParameters) { 176 | _$this; 177 | super.requiredParameters = requiredParameters; 178 | } 179 | 180 | @override 181 | ListBuilder get initializers { 182 | _$this; 183 | return super.initializers; 184 | } 185 | 186 | @override 187 | set initializers(ListBuilder initializers) { 188 | _$this; 189 | super.initializers = initializers; 190 | } 191 | 192 | @override 193 | Code? get body { 194 | _$this; 195 | return super.body; 196 | } 197 | 198 | @override 199 | set body(Code? body) { 200 | _$this; 201 | super.body = body; 202 | } 203 | 204 | @override 205 | bool get external { 206 | _$this; 207 | return super.external; 208 | } 209 | 210 | @override 211 | set external(bool external) { 212 | _$this; 213 | super.external = external; 214 | } 215 | 216 | @override 217 | bool get constant { 218 | _$this; 219 | return super.constant; 220 | } 221 | 222 | @override 223 | set constant(bool constant) { 224 | _$this; 225 | super.constant = constant; 226 | } 227 | 228 | @override 229 | bool get factory { 230 | _$this; 231 | return super.factory; 232 | } 233 | 234 | @override 235 | set factory(bool factory) { 236 | _$this; 237 | super.factory = factory; 238 | } 239 | 240 | @override 241 | bool? get lambda { 242 | _$this; 243 | return super.lambda; 244 | } 245 | 246 | @override 247 | set lambda(bool? lambda) { 248 | _$this; 249 | super.lambda = lambda; 250 | } 251 | 252 | @override 253 | String? get name { 254 | _$this; 255 | return super.name; 256 | } 257 | 258 | @override 259 | set name(String? name) { 260 | _$this; 261 | super.name = name; 262 | } 263 | 264 | @override 265 | Reference? get redirect { 266 | _$this; 267 | return super.redirect; 268 | } 269 | 270 | @override 271 | set redirect(Reference? redirect) { 272 | _$this; 273 | super.redirect = redirect; 274 | } 275 | 276 | _$ConstructorBuilder() : super._(); 277 | 278 | ConstructorBuilder get _$this { 279 | final $v = _$v; 280 | if ($v != null) { 281 | super.annotations = $v.annotations.toBuilder(); 282 | super.docs = $v.docs.toBuilder(); 283 | super.optionalParameters = $v.optionalParameters.toBuilder(); 284 | super.requiredParameters = $v.requiredParameters.toBuilder(); 285 | super.initializers = $v.initializers.toBuilder(); 286 | super.body = $v.body; 287 | super.external = $v.external; 288 | super.constant = $v.constant; 289 | super.factory = $v.factory; 290 | super.lambda = $v.lambda; 291 | super.name = $v.name; 292 | super.redirect = $v.redirect; 293 | _$v = null; 294 | } 295 | return this; 296 | } 297 | 298 | @override 299 | void replace(Constructor other) { 300 | ArgumentError.checkNotNull(other, 'other'); 301 | _$v = other as _$Constructor; 302 | } 303 | 304 | @override 305 | void update(void Function(ConstructorBuilder)? updates) { 306 | if (updates != null) updates(this); 307 | } 308 | 309 | @override 310 | Constructor build() => _build(); 311 | 312 | _$Constructor _build() { 313 | _$Constructor _$result; 314 | try { 315 | _$result = _$v ?? 316 | new _$Constructor._( 317 | annotations: annotations.build(), 318 | docs: docs.build(), 319 | optionalParameters: optionalParameters.build(), 320 | requiredParameters: requiredParameters.build(), 321 | initializers: initializers.build(), 322 | body: body, 323 | external: BuiltValueNullFieldError.checkNotNull( 324 | external, r'Constructor', 'external'), 325 | constant: BuiltValueNullFieldError.checkNotNull( 326 | constant, r'Constructor', 'constant'), 327 | factory: BuiltValueNullFieldError.checkNotNull( 328 | factory, r'Constructor', 'factory'), 329 | lambda: lambda, 330 | name: name, 331 | redirect: redirect); 332 | } catch (_) { 333 | late String _$failedField; 334 | try { 335 | _$failedField = 'annotations'; 336 | annotations.build(); 337 | _$failedField = 'docs'; 338 | docs.build(); 339 | _$failedField = 'optionalParameters'; 340 | optionalParameters.build(); 341 | _$failedField = 'requiredParameters'; 342 | requiredParameters.build(); 343 | _$failedField = 'initializers'; 344 | initializers.build(); 345 | } catch (e) { 346 | throw new BuiltValueNestedFieldError( 347 | r'Constructor', _$failedField, e.toString()); 348 | } 349 | rethrow; 350 | } 351 | replace(_$result); 352 | return _$result; 353 | } 354 | } 355 | 356 | // ignore_for_file: deprecated_member_use_from_same_package,type=lint 357 | -------------------------------------------------------------------------------- /lib/src/specs/directive.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_value/built_value.dart'; 6 | import 'package:collection/collection.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../visitors.dart'; 11 | 12 | part 'directive.g.dart'; 13 | 14 | @immutable 15 | abstract class Directive 16 | implements Built, Spec, Comparable { 17 | factory Directive([void Function(DirectiveBuilder) updates]) = _$Directive; 18 | 19 | factory Directive.import( 20 | String url, { 21 | String? as, 22 | List show = const [], 23 | List hide = const [], 24 | }) => 25 | Directive((builder) => builder 26 | ..as = as 27 | ..type = DirectiveType.import 28 | ..url = url 29 | ..show.addAll(show) 30 | ..hide.addAll(hide)); 31 | 32 | factory Directive.importDeferredAs( 33 | String url, 34 | String as, { 35 | List show = const [], 36 | List hide = const [], 37 | }) => 38 | Directive((builder) => builder 39 | ..as = as 40 | ..type = DirectiveType.import 41 | ..url = url 42 | ..deferred = true 43 | ..show.addAll(show) 44 | ..hide.addAll(hide)); 45 | 46 | factory Directive.export( 47 | String url, { 48 | List show = const [], 49 | List hide = const [], 50 | }) => 51 | Directive((builder) => builder 52 | ..type = DirectiveType.export 53 | ..url = url 54 | ..show.addAll(show) 55 | ..hide.addAll(hide)); 56 | 57 | factory Directive.part(String url) => Directive((builder) => builder 58 | ..type = DirectiveType.part 59 | ..url = url); 60 | 61 | factory Directive.partOf(String url) => Directive((builder) => builder 62 | ..type = DirectiveType.partOf 63 | ..url = url); 64 | 65 | Directive._(); 66 | 67 | String? get as; 68 | 69 | String get url; 70 | 71 | DirectiveType get type; 72 | 73 | List get show; 74 | 75 | List get hide; 76 | 77 | bool get deferred; 78 | 79 | @override 80 | R accept( 81 | SpecVisitor visitor, [ 82 | R? context, 83 | ]) => 84 | visitor.visitDirective(this, context); 85 | 86 | @override 87 | int compareTo(Directive other) => _compareDirectives(this, other); 88 | } 89 | 90 | abstract class DirectiveBuilder 91 | implements Builder { 92 | factory DirectiveBuilder() = _$DirectiveBuilder; 93 | 94 | DirectiveBuilder._(); 95 | 96 | bool deferred = false; 97 | 98 | String? as; 99 | 100 | String? url; 101 | 102 | List show = []; 103 | 104 | List hide = []; 105 | 106 | DirectiveType? type; 107 | } 108 | 109 | enum DirectiveType { 110 | import, 111 | export, 112 | part, 113 | partOf, 114 | } 115 | 116 | /// Sort import URIs represented by [a] and [b] to honor the 117 | /// "Effective Dart" ordering rules which are enforced by the 118 | /// `directives_ordering` lint. 119 | /// 120 | /// 1. `import`s before `export`s 121 | /// 2. `dart:` 122 | /// 3. `package:` 123 | /// 4. relative 124 | /// 5. `part`s 125 | int _compareDirectives(Directive a, Directive b) { 126 | // NOTE: using the fact that `import` is before `export` in the 127 | // `DirectiveType` enum – which allows us to compare using `indexOf`. 128 | var value = DirectiveType.values 129 | .indexOf(a.type) 130 | .compareTo(DirectiveType.values.indexOf(b.type)); 131 | 132 | if (value == 0) { 133 | final uriA = Uri.parse(a.url); 134 | final uriB = Uri.parse(b.url); 135 | 136 | if (uriA.hasScheme) { 137 | if (uriB.hasScheme) { 138 | // If both import URIs have schemes, compare them based on scheme 139 | // `dart` will sort before `package` which is what we want 140 | // schemes are case-insensitive, so compare accordingly 141 | value = compareAsciiLowerCase(uriA.scheme, uriB.scheme); 142 | } else { 143 | value = -1; 144 | } 145 | } else if (uriB.hasScheme) { 146 | value = 1; 147 | } 148 | 149 | // If both schemes are the same, compare based on path 150 | if (value == 0) { 151 | value = compareAsciiLowerCase(uriA.path, uriB.path); 152 | } 153 | 154 | assert((value == 0) == (a.url == b.url)); 155 | } 156 | 157 | return value; 158 | } 159 | -------------------------------------------------------------------------------- /lib/src/specs/directive.g.dart: -------------------------------------------------------------------------------- 1 | // GENERATED CODE - DO NOT MODIFY BY HAND 2 | 3 | part of 'directive.dart'; 4 | 5 | // ************************************************************************** 6 | // BuiltValueGenerator 7 | // ************************************************************************** 8 | 9 | class _$Directive extends Directive { 10 | @override 11 | final String? as; 12 | @override 13 | final String url; 14 | @override 15 | final DirectiveType type; 16 | @override 17 | final List show; 18 | @override 19 | final List hide; 20 | @override 21 | final bool deferred; 22 | 23 | factory _$Directive([void Function(DirectiveBuilder)? updates]) => 24 | (new DirectiveBuilder()..update(updates)).build() as _$Directive; 25 | 26 | _$Directive._( 27 | {this.as, 28 | required this.url, 29 | required this.type, 30 | required this.show, 31 | required this.hide, 32 | required this.deferred}) 33 | : super._() { 34 | BuiltValueNullFieldError.checkNotNull(url, r'Directive', 'url'); 35 | BuiltValueNullFieldError.checkNotNull(type, r'Directive', 'type'); 36 | BuiltValueNullFieldError.checkNotNull(show, r'Directive', 'show'); 37 | BuiltValueNullFieldError.checkNotNull(hide, r'Directive', 'hide'); 38 | BuiltValueNullFieldError.checkNotNull(deferred, r'Directive', 'deferred'); 39 | } 40 | 41 | @override 42 | Directive rebuild(void Function(DirectiveBuilder) updates) => 43 | (toBuilder()..update(updates)).build(); 44 | 45 | @override 46 | _$DirectiveBuilder toBuilder() => new _$DirectiveBuilder()..replace(this); 47 | 48 | @override 49 | bool operator ==(Object other) { 50 | if (identical(other, this)) return true; 51 | return other is Directive && 52 | as == other.as && 53 | url == other.url && 54 | type == other.type && 55 | show == other.show && 56 | hide == other.hide && 57 | deferred == other.deferred; 58 | } 59 | 60 | @override 61 | int get hashCode { 62 | var _$hash = 0; 63 | _$hash = $jc(_$hash, as.hashCode); 64 | _$hash = $jc(_$hash, url.hashCode); 65 | _$hash = $jc(_$hash, type.hashCode); 66 | _$hash = $jc(_$hash, show.hashCode); 67 | _$hash = $jc(_$hash, hide.hashCode); 68 | _$hash = $jc(_$hash, deferred.hashCode); 69 | _$hash = $jf(_$hash); 70 | return _$hash; 71 | } 72 | 73 | @override 74 | String toString() { 75 | return (newBuiltValueToStringHelper(r'Directive') 76 | ..add('as', as) 77 | ..add('url', url) 78 | ..add('type', type) 79 | ..add('show', show) 80 | ..add('hide', hide) 81 | ..add('deferred', deferred)) 82 | .toString(); 83 | } 84 | } 85 | 86 | class _$DirectiveBuilder extends DirectiveBuilder { 87 | _$Directive? _$v; 88 | 89 | @override 90 | String? get as { 91 | _$this; 92 | return super.as; 93 | } 94 | 95 | @override 96 | set as(String? as) { 97 | _$this; 98 | super.as = as; 99 | } 100 | 101 | @override 102 | String? get url { 103 | _$this; 104 | return super.url; 105 | } 106 | 107 | @override 108 | set url(String? url) { 109 | _$this; 110 | super.url = url; 111 | } 112 | 113 | @override 114 | DirectiveType? get type { 115 | _$this; 116 | return super.type; 117 | } 118 | 119 | @override 120 | set type(DirectiveType? type) { 121 | _$this; 122 | super.type = type; 123 | } 124 | 125 | @override 126 | List get show { 127 | _$this; 128 | return super.show; 129 | } 130 | 131 | @override 132 | set show(List show) { 133 | _$this; 134 | super.show = show; 135 | } 136 | 137 | @override 138 | List get hide { 139 | _$this; 140 | return super.hide; 141 | } 142 | 143 | @override 144 | set hide(List hide) { 145 | _$this; 146 | super.hide = hide; 147 | } 148 | 149 | @override 150 | bool get deferred { 151 | _$this; 152 | return super.deferred; 153 | } 154 | 155 | @override 156 | set deferred(bool deferred) { 157 | _$this; 158 | super.deferred = deferred; 159 | } 160 | 161 | _$DirectiveBuilder() : super._(); 162 | 163 | DirectiveBuilder get _$this { 164 | final $v = _$v; 165 | if ($v != null) { 166 | super.as = $v.as; 167 | super.url = $v.url; 168 | super.type = $v.type; 169 | super.show = $v.show; 170 | super.hide = $v.hide; 171 | super.deferred = $v.deferred; 172 | _$v = null; 173 | } 174 | return this; 175 | } 176 | 177 | @override 178 | void replace(Directive other) { 179 | ArgumentError.checkNotNull(other, 'other'); 180 | _$v = other as _$Directive; 181 | } 182 | 183 | @override 184 | void update(void Function(DirectiveBuilder)? updates) { 185 | if (updates != null) updates(this); 186 | } 187 | 188 | @override 189 | Directive build() => _build(); 190 | 191 | _$Directive _build() { 192 | final _$result = _$v ?? 193 | new _$Directive._( 194 | as: as, 195 | url: 196 | BuiltValueNullFieldError.checkNotNull(url, r'Directive', 'url'), 197 | type: BuiltValueNullFieldError.checkNotNull( 198 | type, r'Directive', 'type'), 199 | show: BuiltValueNullFieldError.checkNotNull( 200 | show, r'Directive', 'show'), 201 | hide: BuiltValueNullFieldError.checkNotNull( 202 | hide, r'Directive', 'hide'), 203 | deferred: BuiltValueNullFieldError.checkNotNull( 204 | deferred, r'Directive', 'deferred')); 205 | replace(_$result); 206 | return _$result; 207 | } 208 | } 209 | 210 | // ignore_for_file: deprecated_member_use_from_same_package,type=lint 211 | -------------------------------------------------------------------------------- /lib/src/specs/enum.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../../code_builder.dart'; 10 | import '../mixins/annotations.dart'; 11 | import '../mixins/dartdoc.dart'; 12 | import '../mixins/generics.dart'; 13 | import '../visitors.dart'; 14 | 15 | part 'enum.g.dart'; 16 | 17 | @immutable 18 | abstract class Enum extends Object 19 | with HasAnnotations, HasDartDocs, HasGenerics 20 | implements Built, Spec { 21 | factory Enum([void Function(EnumBuilder) updates]) = _$Enum; 22 | 23 | Enum._(); 24 | 25 | String get name; 26 | 27 | BuiltList get values; 28 | 29 | @override 30 | BuiltList get annotations; 31 | 32 | @override 33 | BuiltList get docs; 34 | 35 | BuiltList get implements; 36 | 37 | BuiltList get mixins; 38 | 39 | @override 40 | BuiltList get types; 41 | 42 | BuiltList get constructors; 43 | BuiltList get methods; 44 | BuiltList get fields; 45 | 46 | @override 47 | R accept( 48 | SpecVisitor visitor, [ 49 | R? context, 50 | ]) => 51 | visitor.visitEnum(this, context); 52 | } 53 | 54 | abstract class EnumBuilder extends Object 55 | with HasAnnotationsBuilder, HasDartDocsBuilder, HasGenericsBuilder 56 | implements Builder { 57 | factory EnumBuilder() = _$EnumBuilder; 58 | 59 | EnumBuilder._(); 60 | 61 | String? name; 62 | 63 | ListBuilder values = ListBuilder(); 64 | 65 | @override 66 | ListBuilder annotations = ListBuilder(); 67 | 68 | @override 69 | ListBuilder docs = ListBuilder(); 70 | 71 | ListBuilder implements = ListBuilder(); 72 | ListBuilder mixins = ListBuilder(); 73 | 74 | @override 75 | ListBuilder types = ListBuilder(); 76 | 77 | ListBuilder constructors = ListBuilder(); 78 | ListBuilder methods = ListBuilder(); 79 | ListBuilder fields = ListBuilder(); 80 | } 81 | 82 | @immutable 83 | abstract class EnumValue extends Object 84 | with HasAnnotations, HasDartDocs, HasGenerics 85 | implements Built { 86 | factory EnumValue([void Function(EnumValueBuilder) updates]) = _$EnumValue; 87 | 88 | EnumValue._(); 89 | 90 | String get name; 91 | 92 | @override 93 | BuiltList get annotations; 94 | 95 | @override 96 | BuiltList get docs; 97 | 98 | /// The name of the constructor to target. 99 | /// 100 | /// If `null` uses the unnamed constructor. 101 | String? get constructorName; 102 | 103 | @override 104 | BuiltList get types; 105 | 106 | /// Arguments to the constructor. 107 | BuiltList get arguments; 108 | 109 | /// Named arguments to the constructor. 110 | BuiltMap get namedArguments; 111 | } 112 | 113 | abstract class EnumValueBuilder extends Object 114 | with HasAnnotationsBuilder, HasDartDocsBuilder, HasGenericsBuilder 115 | implements Builder { 116 | factory EnumValueBuilder() = _$EnumValueBuilder; 117 | 118 | EnumValueBuilder._(); 119 | 120 | String? name; 121 | 122 | @override 123 | ListBuilder annotations = ListBuilder(); 124 | 125 | @override 126 | ListBuilder docs = ListBuilder(); 127 | 128 | /// The name of the constructor to target. 129 | String? constructorName; 130 | 131 | @override 132 | ListBuilder types = ListBuilder(); 133 | 134 | /// Arguments to the constructor. 135 | ListBuilder arguments = ListBuilder(); 136 | 137 | /// Named arguments to the constructor. 138 | MapBuilder namedArguments = MapBuilder(); 139 | } 140 | -------------------------------------------------------------------------------- /lib/src/specs/expression/binary.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | part of '../expression.dart'; 6 | 7 | /// Represents two expressions ([left] and [right]) and an [operator]. 8 | class BinaryExpression extends Expression { 9 | final Expression left; 10 | final Expression right; 11 | final String operator; 12 | final bool addSpace; 13 | @override 14 | final bool isConst; 15 | 16 | const BinaryExpression._( 17 | this.left, 18 | this.right, 19 | this.operator, { 20 | this.addSpace = true, 21 | this.isConst = false, 22 | }); 23 | 24 | @override 25 | R accept(ExpressionVisitor visitor, [R? context]) => 26 | visitor.visitBinaryExpression(this, context); 27 | } 28 | -------------------------------------------------------------------------------- /lib/src/specs/expression/closure.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | part of '../expression.dart'; 6 | 7 | /// Returns [method] as closure, removing its return type and type parameters. 8 | Expression toClosure(Method method) { 9 | final withoutTypes = method.rebuild((b) { 10 | b.returns = null; 11 | b.types.clear(); 12 | }); 13 | return ClosureExpression._(withoutTypes); 14 | } 15 | 16 | /// Returns [method] as a (possibly) generic closure, removing its return type. 17 | Expression toGenericClosure(Method method) { 18 | final withoutReturnType = method.rebuild((b) { 19 | b.returns = null; 20 | }); 21 | return ClosureExpression._(withoutReturnType); 22 | } 23 | 24 | class ClosureExpression extends Expression { 25 | final Method method; 26 | 27 | const ClosureExpression._(this.method); 28 | 29 | @override 30 | R accept(ExpressionVisitor visitor, [R? context]) => 31 | visitor.visitClosureExpression(this, context); 32 | } 33 | -------------------------------------------------------------------------------- /lib/src/specs/expression/code.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | part of '../expression.dart'; 6 | 7 | /// Represents a [Code] block as an [Expression]. 8 | class CodeExpression extends Expression { 9 | @override 10 | final Code code; 11 | 12 | /// **INTERNAL ONLY**: Used to wrap [Code] as an [Expression]. 13 | const CodeExpression(this.code); 14 | 15 | @override 16 | R accept(ExpressionVisitor visitor, [R? context]) => 17 | visitor.visitCodeExpression(this, context); 18 | } 19 | -------------------------------------------------------------------------------- /lib/src/specs/expression/invoke.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | // ignore_for_file: deprecated_member_use_from_same_package 6 | 7 | part of '../expression.dart'; 8 | 9 | /// Represents invoking [target] as a method with arguments. 10 | class InvokeExpression extends Expression { 11 | /// Target of the method invocation. 12 | final Expression target; 13 | 14 | /// Optional; type of invocation. 15 | @Deprecated('Use isConst instead') 16 | final InvokeExpressionType? type; 17 | 18 | @override 19 | final bool isConst; 20 | 21 | final List positionalArguments; 22 | final Map namedArguments; 23 | final List typeArguments; 24 | final String? name; 25 | 26 | const InvokeExpression._( 27 | this.target, 28 | this.positionalArguments, 29 | this.namedArguments, 30 | this.typeArguments, 31 | ) : name = null, 32 | type = null, 33 | isConst = false; 34 | 35 | const InvokeExpression.newOf( 36 | this.target, 37 | this.positionalArguments, [ 38 | this.namedArguments = const {}, 39 | this.typeArguments = const [], 40 | this.name, 41 | ]) : type = InvokeExpressionType.newInstance, 42 | isConst = false; 43 | 44 | const InvokeExpression.constOf( 45 | this.target, 46 | this.positionalArguments, [ 47 | this.namedArguments = const {}, 48 | this.typeArguments = const [], 49 | this.name, 50 | ]) : type = InvokeExpressionType.constInstance, 51 | isConst = true; 52 | 53 | @override 54 | R accept(ExpressionVisitor visitor, [R? context]) => 55 | visitor.visitInvokeExpression(this, context); 56 | 57 | @override 58 | String toString() => 59 | '${type ?? ''} $target($positionalArguments, $namedArguments)'; 60 | } 61 | 62 | enum InvokeExpressionType { 63 | newInstance, 64 | constInstance, 65 | } 66 | -------------------------------------------------------------------------------- /lib/src/specs/expression/literal.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | part of '../expression.dart'; 6 | 7 | /// Converts a runtime Dart [literal] value into an [Expression]. 8 | /// 9 | /// Supported Dart types are translated into literal expressions. 10 | /// If the [literal] is already an [Expression] it is returned without change to 11 | /// allow operating on a collection of mixed simple literals and more complex 12 | /// expressions. 13 | /// Unsupported inputs invoke the [onError] callback. 14 | Expression literal(Object? literal, {Expression Function(Object)? onError}) { 15 | if (literal is Expression) return literal; 16 | if (literal is bool) return literalBool(literal); 17 | if (literal is num) return literalNum(literal); 18 | if (literal is String) return literalString(literal); 19 | if (literal is List) return literalList(literal); 20 | if (literal is Set) return literalSet(literal); 21 | if (literal is Map) return literalMap(literal); 22 | if (literal == null) return literalNull; 23 | if (onError != null) return onError(literal); 24 | throw UnsupportedError('Not a supported literal type: $literal.'); 25 | } 26 | 27 | /// Represents the literal value `true`. 28 | const Expression literalTrue = LiteralExpression._('true'); 29 | 30 | /// Represents the literal value `false`. 31 | const Expression literalFalse = LiteralExpression._('false'); 32 | 33 | /// Create a literal expression from a boolean [value]. 34 | Expression literalBool(bool value) => value ? literalTrue : literalFalse; 35 | 36 | /// Represents the literal value `null`. 37 | const Expression literalNull = LiteralExpression._('null'); 38 | 39 | /// Create a literal expression from a number [value]. 40 | Expression literalNum(num value) => LiteralExpression._('$value'); 41 | 42 | /// Create a literal expression from a string [value]. 43 | /// 44 | /// **NOTE**: The string is always formatted `''`. 45 | /// 46 | /// If [raw] is `true`, creates a raw String formatted `r''` and the 47 | /// value may not contain a single quote. 48 | /// Escapes single quotes and newlines in the value. 49 | Expression literalString(String value, {bool raw = false}) { 50 | if (raw && value.contains('\'')) { 51 | throw ArgumentError('Cannot include a single quote in a raw string'); 52 | } 53 | final escaped = value.replaceAll('\'', '\\\'').replaceAll('\n', '\\n'); 54 | return LiteralExpression._("${raw ? 'r' : ''}'$escaped'"); 55 | } 56 | 57 | /// Create a literal `...` operator for use when creating a Map literal. 58 | /// 59 | /// *NOTE* This is used as a sentinel when constructing a `literalMap` or a 60 | /// or `literalConstMap` to signify that the value should be spread. Do NOT 61 | /// reuse the value when creating a Map with multiple spreads. 62 | Expression literalSpread() => LiteralSpreadExpression._(false); 63 | 64 | /// Create a literal `...?` operator for use when creating a Map literal. 65 | /// 66 | /// *NOTE* This is used as a sentinel when constructing a `literalMap` or a 67 | /// or `literalConstMap` to signify that the value should be spread. Do NOT 68 | /// reuse the value when creating a Map with multiple spreads. 69 | Expression literalNullSafeSpread() => LiteralSpreadExpression._(true); 70 | 71 | /// Creates a literal list expression from [values]. 72 | LiteralListExpression literalList(Iterable values, 73 | [Reference? type]) => 74 | LiteralListExpression._(false, values.toList(), type); 75 | 76 | /// Creates a literal `const` list expression from [values]. 77 | LiteralListExpression literalConstList(List values, 78 | [Reference? type]) => 79 | LiteralListExpression._(true, values, type); 80 | 81 | /// Creates a literal set expression from [values]. 82 | LiteralSetExpression literalSet(Iterable values, [Reference? type]) => 83 | LiteralSetExpression._(false, values.toSet(), type); 84 | 85 | /// Creates a literal `const` set expression from [values]. 86 | LiteralSetExpression literalConstSet(Set values, [Reference? type]) => 87 | LiteralSetExpression._(true, values, type); 88 | 89 | /// Create a literal map expression from [values]. 90 | LiteralMapExpression literalMap( 91 | Map values, [ 92 | Reference? keyType, 93 | Reference? valueType, 94 | ]) => 95 | LiteralMapExpression._(false, values, keyType, valueType); 96 | 97 | /// Create a literal `const` map expression from [values]. 98 | LiteralMapExpression literalConstMap( 99 | Map values, [ 100 | Reference? keyType, 101 | Reference? valueType, 102 | ]) => 103 | LiteralMapExpression._(true, values, keyType, valueType); 104 | 105 | /// Create a literal record expression from [positionalFieldValues] and 106 | /// [namedFieldValues]. 107 | LiteralRecordExpression literalRecord(List positionalFieldValues, 108 | Map namedFieldValues) => 109 | LiteralRecordExpression._(false, positionalFieldValues, namedFieldValues); 110 | 111 | /// Create a literal `const` record expression from [positionalFieldValues] and 112 | /// [namedFieldValues]. 113 | LiteralRecordExpression literalConstRecord(List positionalFieldValues, 114 | Map namedFieldValues) => 115 | LiteralRecordExpression._(true, positionalFieldValues, namedFieldValues); 116 | 117 | /// Represents a literal value in Dart source code. 118 | /// 119 | /// For example, `LiteralExpression('null')` should emit `null`. 120 | /// 121 | /// Some common literals and helpers are available as methods/fields: 122 | /// * [literal] 123 | /// * [literalBool] and [literalTrue], [literalFalse] 124 | /// * [literalNull] 125 | /// * [literalList] and [literalConstList] 126 | /// * [literalSet] and [literalConstSet] 127 | class LiteralExpression extends Expression { 128 | final String literal; 129 | 130 | const LiteralExpression._(this.literal); 131 | 132 | @override 133 | R accept(ExpressionVisitor visitor, [R? context]) => 134 | visitor.visitLiteralExpression(this, context); 135 | 136 | @override 137 | String toString() => literal; 138 | } 139 | 140 | class LiteralSpreadExpression extends LiteralExpression { 141 | LiteralSpreadExpression._(bool nullAware) 142 | : super._('...${nullAware ? '?' : ''}'); 143 | } 144 | 145 | class LiteralListExpression extends Expression { 146 | @override 147 | final bool isConst; 148 | final List values; 149 | final Reference? type; 150 | 151 | const LiteralListExpression._(this.isConst, this.values, this.type); 152 | 153 | @override 154 | R accept(ExpressionVisitor visitor, [R? context]) => 155 | visitor.visitLiteralListExpression(this, context); 156 | 157 | @override 158 | String toString() => '[${values.map(literal).join(', ')}]'; 159 | } 160 | 161 | class LiteralSetExpression extends Expression { 162 | @override 163 | final bool isConst; 164 | final Set values; 165 | final Reference? type; 166 | 167 | const LiteralSetExpression._(this.isConst, this.values, this.type); 168 | 169 | @override 170 | R accept(ExpressionVisitor visitor, [R? context]) => 171 | visitor.visitLiteralSetExpression(this, context); 172 | 173 | @override 174 | String toString() => '{${values.map(literal).join(', ')}}'; 175 | } 176 | 177 | class LiteralMapExpression extends Expression { 178 | @override 179 | final bool isConst; 180 | final Map values; 181 | final Reference? keyType; 182 | final Reference? valueType; 183 | 184 | const LiteralMapExpression._( 185 | this.isConst, 186 | this.values, 187 | this.keyType, 188 | this.valueType, 189 | ); 190 | 191 | @override 192 | R accept(ExpressionVisitor visitor, [R? context]) => 193 | visitor.visitLiteralMapExpression(this, context); 194 | 195 | @override 196 | String toString() => '{$values}'; 197 | } 198 | 199 | class LiteralRecordExpression extends Expression { 200 | @override 201 | final bool isConst; 202 | final List positionalFieldValues; 203 | final Map namedFieldValues; 204 | 205 | const LiteralRecordExpression._( 206 | this.isConst, this.positionalFieldValues, this.namedFieldValues); 207 | 208 | @override 209 | R accept(ExpressionVisitor visitor, [R? context]) => 210 | visitor.visitLiteralRecordExpression(this, context); 211 | 212 | @override 213 | String toString() { 214 | final allFields = positionalFieldValues.map((v) => v.toString()).followedBy( 215 | namedFieldValues.entries.map((e) => '${e.key}: ${e.value}')); 216 | return '(${allFields.join(', ')})'; 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /lib/src/specs/expression/parenthesized.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | part of '../expression.dart'; 6 | 7 | /// An [Expression] wrapped with parenthesis. 8 | class ParenthesizedExpression extends Expression { 9 | final Expression inner; 10 | 11 | const ParenthesizedExpression._(this.inner); 12 | 13 | @override 14 | R accept(ExpressionVisitor visitor, [R? context]) => 15 | visitor.visitParenthesizedExpression(this, context); 16 | } 17 | -------------------------------------------------------------------------------- /lib/src/specs/extension.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../mixins/annotations.dart'; 11 | import '../mixins/dartdoc.dart'; 12 | import '../mixins/generics.dart'; 13 | import '../visitors.dart'; 14 | import 'expression.dart'; 15 | import 'field.dart'; 16 | import 'method.dart'; 17 | import 'reference.dart'; 18 | 19 | part 'extension.g.dart'; 20 | 21 | @immutable 22 | abstract class Extension extends Object 23 | with HasAnnotations, HasDartDocs, HasGenerics 24 | implements Built, Spec { 25 | factory Extension([void Function(ExtensionBuilder b) updates]) = _$Extension; 26 | 27 | Extension._(); 28 | 29 | @override 30 | BuiltList get annotations; 31 | 32 | @override 33 | BuiltList get docs; 34 | 35 | Reference? get on; 36 | 37 | @override 38 | BuiltList get types; 39 | 40 | BuiltList get methods; 41 | 42 | BuiltList get fields; 43 | 44 | /// Name of the extension - optional. 45 | String? get name; 46 | 47 | @override 48 | R accept( 49 | SpecVisitor visitor, [ 50 | R? context, 51 | ]) => 52 | visitor.visitExtension(this, context); 53 | } 54 | 55 | abstract class ExtensionBuilder extends Object 56 | with HasAnnotationsBuilder, HasDartDocsBuilder, HasGenericsBuilder 57 | implements Builder { 58 | factory ExtensionBuilder() = _$ExtensionBuilder; 59 | 60 | ExtensionBuilder._(); 61 | 62 | @override 63 | ListBuilder annotations = ListBuilder(); 64 | 65 | @override 66 | ListBuilder docs = ListBuilder(); 67 | 68 | Reference? on; 69 | 70 | @override 71 | ListBuilder types = ListBuilder(); 72 | 73 | ListBuilder methods = ListBuilder(); 74 | ListBuilder fields = ListBuilder(); 75 | 76 | /// Name of the extension - optional. 77 | String? name; 78 | } 79 | -------------------------------------------------------------------------------- /lib/src/specs/extension.g.dart: -------------------------------------------------------------------------------- 1 | // GENERATED CODE - DO NOT MODIFY BY HAND 2 | 3 | part of 'extension.dart'; 4 | 5 | // ************************************************************************** 6 | // BuiltValueGenerator 7 | // ************************************************************************** 8 | 9 | class _$Extension extends Extension { 10 | @override 11 | final BuiltList annotations; 12 | @override 13 | final BuiltList docs; 14 | @override 15 | final Reference? on; 16 | @override 17 | final BuiltList types; 18 | @override 19 | final BuiltList methods; 20 | @override 21 | final BuiltList fields; 22 | @override 23 | final String? name; 24 | 25 | factory _$Extension([void Function(ExtensionBuilder)? updates]) => 26 | (new ExtensionBuilder()..update(updates)).build() as _$Extension; 27 | 28 | _$Extension._( 29 | {required this.annotations, 30 | required this.docs, 31 | this.on, 32 | required this.types, 33 | required this.methods, 34 | required this.fields, 35 | this.name}) 36 | : super._() { 37 | BuiltValueNullFieldError.checkNotNull( 38 | annotations, r'Extension', 'annotations'); 39 | BuiltValueNullFieldError.checkNotNull(docs, r'Extension', 'docs'); 40 | BuiltValueNullFieldError.checkNotNull(types, r'Extension', 'types'); 41 | BuiltValueNullFieldError.checkNotNull(methods, r'Extension', 'methods'); 42 | BuiltValueNullFieldError.checkNotNull(fields, r'Extension', 'fields'); 43 | } 44 | 45 | @override 46 | Extension rebuild(void Function(ExtensionBuilder) updates) => 47 | (toBuilder()..update(updates)).build(); 48 | 49 | @override 50 | _$ExtensionBuilder toBuilder() => new _$ExtensionBuilder()..replace(this); 51 | 52 | @override 53 | bool operator ==(Object other) { 54 | if (identical(other, this)) return true; 55 | return other is Extension && 56 | annotations == other.annotations && 57 | docs == other.docs && 58 | on == other.on && 59 | types == other.types && 60 | methods == other.methods && 61 | fields == other.fields && 62 | name == other.name; 63 | } 64 | 65 | @override 66 | int get hashCode { 67 | var _$hash = 0; 68 | _$hash = $jc(_$hash, annotations.hashCode); 69 | _$hash = $jc(_$hash, docs.hashCode); 70 | _$hash = $jc(_$hash, on.hashCode); 71 | _$hash = $jc(_$hash, types.hashCode); 72 | _$hash = $jc(_$hash, methods.hashCode); 73 | _$hash = $jc(_$hash, fields.hashCode); 74 | _$hash = $jc(_$hash, name.hashCode); 75 | _$hash = $jf(_$hash); 76 | return _$hash; 77 | } 78 | 79 | @override 80 | String toString() { 81 | return (newBuiltValueToStringHelper(r'Extension') 82 | ..add('annotations', annotations) 83 | ..add('docs', docs) 84 | ..add('on', on) 85 | ..add('types', types) 86 | ..add('methods', methods) 87 | ..add('fields', fields) 88 | ..add('name', name)) 89 | .toString(); 90 | } 91 | } 92 | 93 | class _$ExtensionBuilder extends ExtensionBuilder { 94 | _$Extension? _$v; 95 | 96 | @override 97 | ListBuilder get annotations { 98 | _$this; 99 | return super.annotations; 100 | } 101 | 102 | @override 103 | set annotations(ListBuilder annotations) { 104 | _$this; 105 | super.annotations = annotations; 106 | } 107 | 108 | @override 109 | ListBuilder get docs { 110 | _$this; 111 | return super.docs; 112 | } 113 | 114 | @override 115 | set docs(ListBuilder docs) { 116 | _$this; 117 | super.docs = docs; 118 | } 119 | 120 | @override 121 | Reference? get on { 122 | _$this; 123 | return super.on; 124 | } 125 | 126 | @override 127 | set on(Reference? on) { 128 | _$this; 129 | super.on = on; 130 | } 131 | 132 | @override 133 | ListBuilder get types { 134 | _$this; 135 | return super.types; 136 | } 137 | 138 | @override 139 | set types(ListBuilder types) { 140 | _$this; 141 | super.types = types; 142 | } 143 | 144 | @override 145 | ListBuilder get methods { 146 | _$this; 147 | return super.methods; 148 | } 149 | 150 | @override 151 | set methods(ListBuilder methods) { 152 | _$this; 153 | super.methods = methods; 154 | } 155 | 156 | @override 157 | ListBuilder get fields { 158 | _$this; 159 | return super.fields; 160 | } 161 | 162 | @override 163 | set fields(ListBuilder fields) { 164 | _$this; 165 | super.fields = fields; 166 | } 167 | 168 | @override 169 | String? get name { 170 | _$this; 171 | return super.name; 172 | } 173 | 174 | @override 175 | set name(String? name) { 176 | _$this; 177 | super.name = name; 178 | } 179 | 180 | _$ExtensionBuilder() : super._(); 181 | 182 | ExtensionBuilder get _$this { 183 | final $v = _$v; 184 | if ($v != null) { 185 | super.annotations = $v.annotations.toBuilder(); 186 | super.docs = $v.docs.toBuilder(); 187 | super.on = $v.on; 188 | super.types = $v.types.toBuilder(); 189 | super.methods = $v.methods.toBuilder(); 190 | super.fields = $v.fields.toBuilder(); 191 | super.name = $v.name; 192 | _$v = null; 193 | } 194 | return this; 195 | } 196 | 197 | @override 198 | void replace(Extension other) { 199 | ArgumentError.checkNotNull(other, 'other'); 200 | _$v = other as _$Extension; 201 | } 202 | 203 | @override 204 | void update(void Function(ExtensionBuilder)? updates) { 205 | if (updates != null) updates(this); 206 | } 207 | 208 | @override 209 | Extension build() => _build(); 210 | 211 | _$Extension _build() { 212 | _$Extension _$result; 213 | try { 214 | _$result = _$v ?? 215 | new _$Extension._( 216 | annotations: annotations.build(), 217 | docs: docs.build(), 218 | on: on, 219 | types: types.build(), 220 | methods: methods.build(), 221 | fields: fields.build(), 222 | name: name); 223 | } catch (_) { 224 | late String _$failedField; 225 | try { 226 | _$failedField = 'annotations'; 227 | annotations.build(); 228 | _$failedField = 'docs'; 229 | docs.build(); 230 | 231 | _$failedField = 'types'; 232 | types.build(); 233 | _$failedField = 'methods'; 234 | methods.build(); 235 | _$failedField = 'fields'; 236 | fields.build(); 237 | } catch (e) { 238 | throw new BuiltValueNestedFieldError( 239 | r'Extension', _$failedField, e.toString()); 240 | } 241 | rethrow; 242 | } 243 | replace(_$result); 244 | return _$result; 245 | } 246 | } 247 | 248 | // ignore_for_file: deprecated_member_use_from_same_package,type=lint 249 | -------------------------------------------------------------------------------- /lib/src/specs/extension_type.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../mixins/annotations.dart'; 11 | import '../mixins/dartdoc.dart'; 12 | import '../mixins/generics.dart'; 13 | import '../visitors.dart'; 14 | import 'constructor.dart'; 15 | import 'expression.dart'; 16 | import 'field.dart'; 17 | import 'method.dart'; 18 | import 'reference.dart'; 19 | 20 | part 'extension_type.g.dart'; 21 | 22 | @immutable 23 | abstract class ExtensionType extends Object 24 | with HasAnnotations, HasDartDocs, HasGenerics 25 | implements Built, Spec { 26 | factory ExtensionType([void Function(ExtensionTypeBuilder)? updates]) = 27 | _$ExtensionType; 28 | 29 | ExtensionType._(); 30 | 31 | @override 32 | BuiltList get annotations; 33 | 34 | @override 35 | BuiltList get docs; 36 | 37 | /// Whether this extension type is declared as `const`. 38 | bool get constant; 39 | 40 | String get name; 41 | 42 | @override 43 | BuiltList get types; 44 | 45 | /// Name of the extension type's primary constructor. An empty string 46 | /// will make it unnamed. 47 | String get primaryConstructorName; 48 | 49 | RepresentationDeclaration get representationDeclaration; 50 | 51 | BuiltList get implements; 52 | 53 | BuiltList get constructors; 54 | 55 | BuiltList get fields; 56 | 57 | BuiltList get methods; 58 | 59 | @override 60 | R accept( 61 | SpecVisitor visitor, [ 62 | R? context, 63 | ]) => 64 | visitor.visitExtensionType(this, context); 65 | } 66 | 67 | abstract class ExtensionTypeBuilder extends Object 68 | with HasAnnotationsBuilder, HasDartDocsBuilder, HasGenericsBuilder 69 | implements Builder { 70 | factory ExtensionTypeBuilder() = _$ExtensionTypeBuilder; 71 | 72 | ExtensionTypeBuilder._(); 73 | 74 | @override 75 | void update(void Function(ExtensionTypeBuilder)? updates) { 76 | updates?.call(this); 77 | } 78 | 79 | @override 80 | ListBuilder annotations = ListBuilder(); 81 | 82 | @override 83 | ListBuilder docs = ListBuilder(); 84 | 85 | /// Whether this extension type is declared as `const`. 86 | bool constant = false; 87 | 88 | String? name; 89 | 90 | @override 91 | ListBuilder types = ListBuilder(); 92 | 93 | /// Name of the extension type's primary constructor. An empty string 94 | /// will make it unnamed. 95 | String primaryConstructorName = ''; 96 | 97 | RepresentationDeclaration? representationDeclaration; 98 | 99 | ListBuilder implements = ListBuilder(); 100 | 101 | ListBuilder constructors = ListBuilder(); 102 | 103 | ListBuilder fields = ListBuilder(); 104 | 105 | ListBuilder methods = ListBuilder(); 106 | } 107 | 108 | abstract class RepresentationDeclaration extends Object 109 | with HasAnnotations, HasDartDocs 110 | implements 111 | Built { 112 | factory RepresentationDeclaration( 113 | [void Function(RepresentationDeclarationBuilder)? updates]) = 114 | _$RepresentationDeclaration; 115 | 116 | RepresentationDeclaration._(); 117 | 118 | @override 119 | BuiltList get annotations; 120 | 121 | @override 122 | BuiltList get docs; 123 | 124 | Reference get declaredRepresentationType; 125 | 126 | String get name; 127 | } 128 | 129 | abstract class RepresentationDeclarationBuilder extends Object 130 | with HasAnnotationsBuilder, HasDartDocsBuilder 131 | implements 132 | Builder { 133 | factory RepresentationDeclarationBuilder() = 134 | _$RepresentationDeclarationBuilder; 135 | 136 | RepresentationDeclarationBuilder._(); 137 | 138 | @override 139 | void update(void Function(RepresentationDeclarationBuilder)? updates) { 140 | updates?.call(this); 141 | } 142 | 143 | @override 144 | ListBuilder annotations = ListBuilder(); 145 | 146 | @override 147 | ListBuilder docs = ListBuilder(); 148 | 149 | Reference? declaredRepresentationType; 150 | 151 | String? name; 152 | } 153 | -------------------------------------------------------------------------------- /lib/src/specs/field.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../mixins/annotations.dart'; 11 | import '../mixins/dartdoc.dart'; 12 | import '../visitors.dart'; 13 | import 'code.dart'; 14 | import 'expression.dart'; 15 | import 'reference.dart'; 16 | 17 | part 'field.g.dart'; 18 | 19 | @immutable 20 | abstract class Field extends Object 21 | with HasAnnotations, HasDartDocs 22 | implements Built, Spec { 23 | factory Field([void Function(FieldBuilder) updates]) = _$Field; 24 | 25 | Field._(); 26 | 27 | @override 28 | BuiltList get annotations; 29 | 30 | @override 31 | BuiltList get docs; 32 | 33 | /// Field assignment, if any. 34 | Code? get assignment; 35 | 36 | /// Whether this field should be prefixed with `static`. 37 | /// 38 | /// This is only valid within classes. 39 | bool get static; 40 | 41 | /// Whether this field should be prefixed with `late`. 42 | bool get late; 43 | 44 | /// Whether the field should be prefixed with `external`. 45 | bool get external; 46 | 47 | /// Name of the field. 48 | String get name; 49 | 50 | Reference? get type; 51 | 52 | FieldModifier get modifier; 53 | 54 | @override 55 | R accept( 56 | SpecVisitor visitor, [ 57 | R? context, 58 | ]) => 59 | visitor.visitField(this, context); 60 | } 61 | 62 | enum FieldModifier { 63 | var$, 64 | final$, 65 | constant, 66 | } 67 | 68 | abstract class FieldBuilder extends Object 69 | with HasAnnotationsBuilder, HasDartDocsBuilder 70 | implements Builder { 71 | factory FieldBuilder() = _$FieldBuilder; 72 | 73 | FieldBuilder._(); 74 | 75 | @override 76 | ListBuilder annotations = ListBuilder(); 77 | 78 | @override 79 | ListBuilder docs = ListBuilder(); 80 | 81 | /// Field assignment, if any. 82 | Code? assignment; 83 | 84 | /// Whether this field should be prefixed with `static`. 85 | /// 86 | /// This is only valid within classes. 87 | bool static = false; 88 | 89 | /// Whether this field should be prefixed with `late`. 90 | bool late = false; 91 | 92 | /// Whether the field should be prefixed with `external`. 93 | bool external = false; 94 | 95 | /// Name of the field. 96 | String? name; 97 | 98 | Reference? type; 99 | 100 | FieldModifier modifier = FieldModifier.var$; 101 | } 102 | -------------------------------------------------------------------------------- /lib/src/specs/field.g.dart: -------------------------------------------------------------------------------- 1 | // GENERATED CODE - DO NOT MODIFY BY HAND 2 | 3 | part of 'field.dart'; 4 | 5 | // ************************************************************************** 6 | // BuiltValueGenerator 7 | // ************************************************************************** 8 | 9 | class _$Field extends Field { 10 | @override 11 | final BuiltList annotations; 12 | @override 13 | final BuiltList docs; 14 | @override 15 | final Code? assignment; 16 | @override 17 | final bool static; 18 | @override 19 | final bool late; 20 | @override 21 | final bool external; 22 | @override 23 | final String name; 24 | @override 25 | final Reference? type; 26 | @override 27 | final FieldModifier modifier; 28 | 29 | factory _$Field([void Function(FieldBuilder)? updates]) => 30 | (new FieldBuilder()..update(updates)).build() as _$Field; 31 | 32 | _$Field._( 33 | {required this.annotations, 34 | required this.docs, 35 | this.assignment, 36 | required this.static, 37 | required this.late, 38 | required this.external, 39 | required this.name, 40 | this.type, 41 | required this.modifier}) 42 | : super._() { 43 | BuiltValueNullFieldError.checkNotNull(annotations, r'Field', 'annotations'); 44 | BuiltValueNullFieldError.checkNotNull(docs, r'Field', 'docs'); 45 | BuiltValueNullFieldError.checkNotNull(static, r'Field', 'static'); 46 | BuiltValueNullFieldError.checkNotNull(late, r'Field', 'late'); 47 | BuiltValueNullFieldError.checkNotNull(external, r'Field', 'external'); 48 | BuiltValueNullFieldError.checkNotNull(name, r'Field', 'name'); 49 | BuiltValueNullFieldError.checkNotNull(modifier, r'Field', 'modifier'); 50 | } 51 | 52 | @override 53 | Field rebuild(void Function(FieldBuilder) updates) => 54 | (toBuilder()..update(updates)).build(); 55 | 56 | @override 57 | _$FieldBuilder toBuilder() => new _$FieldBuilder()..replace(this); 58 | 59 | @override 60 | bool operator ==(Object other) { 61 | if (identical(other, this)) return true; 62 | return other is Field && 63 | annotations == other.annotations && 64 | docs == other.docs && 65 | assignment == other.assignment && 66 | static == other.static && 67 | late == other.late && 68 | external == other.external && 69 | name == other.name && 70 | type == other.type && 71 | modifier == other.modifier; 72 | } 73 | 74 | @override 75 | int get hashCode { 76 | var _$hash = 0; 77 | _$hash = $jc(_$hash, annotations.hashCode); 78 | _$hash = $jc(_$hash, docs.hashCode); 79 | _$hash = $jc(_$hash, assignment.hashCode); 80 | _$hash = $jc(_$hash, static.hashCode); 81 | _$hash = $jc(_$hash, late.hashCode); 82 | _$hash = $jc(_$hash, external.hashCode); 83 | _$hash = $jc(_$hash, name.hashCode); 84 | _$hash = $jc(_$hash, type.hashCode); 85 | _$hash = $jc(_$hash, modifier.hashCode); 86 | _$hash = $jf(_$hash); 87 | return _$hash; 88 | } 89 | 90 | @override 91 | String toString() { 92 | return (newBuiltValueToStringHelper(r'Field') 93 | ..add('annotations', annotations) 94 | ..add('docs', docs) 95 | ..add('assignment', assignment) 96 | ..add('static', static) 97 | ..add('late', late) 98 | ..add('external', external) 99 | ..add('name', name) 100 | ..add('type', type) 101 | ..add('modifier', modifier)) 102 | .toString(); 103 | } 104 | } 105 | 106 | class _$FieldBuilder extends FieldBuilder { 107 | _$Field? _$v; 108 | 109 | @override 110 | ListBuilder get annotations { 111 | _$this; 112 | return super.annotations; 113 | } 114 | 115 | @override 116 | set annotations(ListBuilder annotations) { 117 | _$this; 118 | super.annotations = annotations; 119 | } 120 | 121 | @override 122 | ListBuilder get docs { 123 | _$this; 124 | return super.docs; 125 | } 126 | 127 | @override 128 | set docs(ListBuilder docs) { 129 | _$this; 130 | super.docs = docs; 131 | } 132 | 133 | @override 134 | Code? get assignment { 135 | _$this; 136 | return super.assignment; 137 | } 138 | 139 | @override 140 | set assignment(Code? assignment) { 141 | _$this; 142 | super.assignment = assignment; 143 | } 144 | 145 | @override 146 | bool get static { 147 | _$this; 148 | return super.static; 149 | } 150 | 151 | @override 152 | set static(bool static) { 153 | _$this; 154 | super.static = static; 155 | } 156 | 157 | @override 158 | bool get late { 159 | _$this; 160 | return super.late; 161 | } 162 | 163 | @override 164 | set late(bool late) { 165 | _$this; 166 | super.late = late; 167 | } 168 | 169 | @override 170 | bool get external { 171 | _$this; 172 | return super.external; 173 | } 174 | 175 | @override 176 | set external(bool external) { 177 | _$this; 178 | super.external = external; 179 | } 180 | 181 | @override 182 | String? get name { 183 | _$this; 184 | return super.name; 185 | } 186 | 187 | @override 188 | set name(String? name) { 189 | _$this; 190 | super.name = name; 191 | } 192 | 193 | @override 194 | Reference? get type { 195 | _$this; 196 | return super.type; 197 | } 198 | 199 | @override 200 | set type(Reference? type) { 201 | _$this; 202 | super.type = type; 203 | } 204 | 205 | @override 206 | FieldModifier get modifier { 207 | _$this; 208 | return super.modifier; 209 | } 210 | 211 | @override 212 | set modifier(FieldModifier modifier) { 213 | _$this; 214 | super.modifier = modifier; 215 | } 216 | 217 | _$FieldBuilder() : super._(); 218 | 219 | FieldBuilder get _$this { 220 | final $v = _$v; 221 | if ($v != null) { 222 | super.annotations = $v.annotations.toBuilder(); 223 | super.docs = $v.docs.toBuilder(); 224 | super.assignment = $v.assignment; 225 | super.static = $v.static; 226 | super.late = $v.late; 227 | super.external = $v.external; 228 | super.name = $v.name; 229 | super.type = $v.type; 230 | super.modifier = $v.modifier; 231 | _$v = null; 232 | } 233 | return this; 234 | } 235 | 236 | @override 237 | void replace(Field other) { 238 | ArgumentError.checkNotNull(other, 'other'); 239 | _$v = other as _$Field; 240 | } 241 | 242 | @override 243 | void update(void Function(FieldBuilder)? updates) { 244 | if (updates != null) updates(this); 245 | } 246 | 247 | @override 248 | Field build() => _build(); 249 | 250 | _$Field _build() { 251 | _$Field _$result; 252 | try { 253 | _$result = _$v ?? 254 | new _$Field._( 255 | annotations: annotations.build(), 256 | docs: docs.build(), 257 | assignment: assignment, 258 | static: BuiltValueNullFieldError.checkNotNull( 259 | static, r'Field', 'static'), 260 | late: 261 | BuiltValueNullFieldError.checkNotNull(late, r'Field', 'late'), 262 | external: BuiltValueNullFieldError.checkNotNull( 263 | external, r'Field', 'external'), 264 | name: 265 | BuiltValueNullFieldError.checkNotNull(name, r'Field', 'name'), 266 | type: type, 267 | modifier: BuiltValueNullFieldError.checkNotNull( 268 | modifier, r'Field', 'modifier')); 269 | } catch (_) { 270 | late String _$failedField; 271 | try { 272 | _$failedField = 'annotations'; 273 | annotations.build(); 274 | _$failedField = 'docs'; 275 | docs.build(); 276 | } catch (e) { 277 | throw new BuiltValueNestedFieldError( 278 | r'Field', _$failedField, e.toString()); 279 | } 280 | rethrow; 281 | } 282 | replace(_$result); 283 | return _$result; 284 | } 285 | } 286 | 287 | // ignore_for_file: deprecated_member_use_from_same_package,type=lint 288 | -------------------------------------------------------------------------------- /lib/src/specs/library.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../mixins/annotations.dart'; 11 | import '../mixins/dartdoc.dart'; 12 | import '../visitors.dart'; 13 | import 'directive.dart'; 14 | import 'expression.dart'; 15 | 16 | part 'library.g.dart'; 17 | 18 | @immutable 19 | abstract class Library 20 | with HasAnnotations, HasDartDocs 21 | implements Built, Spec { 22 | factory Library([void Function(LibraryBuilder) updates]) = _$Library; 23 | Library._(); 24 | 25 | @override 26 | BuiltList get annotations; 27 | 28 | @override 29 | BuiltList get docs; 30 | 31 | BuiltList get directives; 32 | BuiltList get body; 33 | 34 | /// Line comments to place at the start of the library. 35 | BuiltList get comments; 36 | 37 | /// A comment indicating the tool this library was generated by. 38 | /// 39 | /// This is typically of the form `Generated by xxx.`; it should exclude 40 | /// leading line comment characters. 41 | String? get generatedByComment; 42 | 43 | /// A list of analysis issues to ignore (`ignore_for_file: ...`). 44 | BuiltList get ignoreForFile; 45 | 46 | /// Name of the library. 47 | /// 48 | /// May be `null` when no [annotations] are specified. 49 | String? get name; 50 | 51 | @override 52 | R accept( 53 | SpecVisitor visitor, [ 54 | R? context, 55 | ]) => 56 | visitor.visitLibrary(this, context); 57 | } 58 | 59 | abstract class LibraryBuilder 60 | with HasAnnotationsBuilder, HasDartDocsBuilder 61 | implements Builder { 62 | factory LibraryBuilder() = _$LibraryBuilder; 63 | LibraryBuilder._(); 64 | 65 | @override 66 | ListBuilder annotations = ListBuilder(); 67 | 68 | @override 69 | ListBuilder docs = ListBuilder(); 70 | 71 | ListBuilder body = ListBuilder(); 72 | ListBuilder directives = ListBuilder(); 73 | 74 | ListBuilder comments = ListBuilder(); 75 | String? generatedByComment; 76 | ListBuilder ignoreForFile = ListBuilder(); 77 | 78 | String? name; 79 | } 80 | -------------------------------------------------------------------------------- /lib/src/specs/library.g.dart: -------------------------------------------------------------------------------- 1 | // GENERATED CODE - DO NOT MODIFY BY HAND 2 | 3 | part of 'library.dart'; 4 | 5 | // ************************************************************************** 6 | // BuiltValueGenerator 7 | // ************************************************************************** 8 | 9 | class _$Library extends Library { 10 | @override 11 | final BuiltList annotations; 12 | @override 13 | final BuiltList docs; 14 | @override 15 | final BuiltList directives; 16 | @override 17 | final BuiltList body; 18 | @override 19 | final BuiltList comments; 20 | @override 21 | final String? generatedByComment; 22 | @override 23 | final BuiltList ignoreForFile; 24 | @override 25 | final String? name; 26 | 27 | factory _$Library([void Function(LibraryBuilder)? updates]) => 28 | (new LibraryBuilder()..update(updates)).build() as _$Library; 29 | 30 | _$Library._( 31 | {required this.annotations, 32 | required this.docs, 33 | required this.directives, 34 | required this.body, 35 | required this.comments, 36 | this.generatedByComment, 37 | required this.ignoreForFile, 38 | this.name}) 39 | : super._() { 40 | BuiltValueNullFieldError.checkNotNull( 41 | annotations, r'Library', 'annotations'); 42 | BuiltValueNullFieldError.checkNotNull(docs, r'Library', 'docs'); 43 | BuiltValueNullFieldError.checkNotNull(directives, r'Library', 'directives'); 44 | BuiltValueNullFieldError.checkNotNull(body, r'Library', 'body'); 45 | BuiltValueNullFieldError.checkNotNull(comments, r'Library', 'comments'); 46 | BuiltValueNullFieldError.checkNotNull( 47 | ignoreForFile, r'Library', 'ignoreForFile'); 48 | } 49 | 50 | @override 51 | Library rebuild(void Function(LibraryBuilder) updates) => 52 | (toBuilder()..update(updates)).build(); 53 | 54 | @override 55 | _$LibraryBuilder toBuilder() => new _$LibraryBuilder()..replace(this); 56 | 57 | @override 58 | bool operator ==(Object other) { 59 | if (identical(other, this)) return true; 60 | return other is Library && 61 | annotations == other.annotations && 62 | docs == other.docs && 63 | directives == other.directives && 64 | body == other.body && 65 | comments == other.comments && 66 | generatedByComment == other.generatedByComment && 67 | ignoreForFile == other.ignoreForFile && 68 | name == other.name; 69 | } 70 | 71 | @override 72 | int get hashCode { 73 | var _$hash = 0; 74 | _$hash = $jc(_$hash, annotations.hashCode); 75 | _$hash = $jc(_$hash, docs.hashCode); 76 | _$hash = $jc(_$hash, directives.hashCode); 77 | _$hash = $jc(_$hash, body.hashCode); 78 | _$hash = $jc(_$hash, comments.hashCode); 79 | _$hash = $jc(_$hash, generatedByComment.hashCode); 80 | _$hash = $jc(_$hash, ignoreForFile.hashCode); 81 | _$hash = $jc(_$hash, name.hashCode); 82 | _$hash = $jf(_$hash); 83 | return _$hash; 84 | } 85 | 86 | @override 87 | String toString() { 88 | return (newBuiltValueToStringHelper(r'Library') 89 | ..add('annotations', annotations) 90 | ..add('docs', docs) 91 | ..add('directives', directives) 92 | ..add('body', body) 93 | ..add('comments', comments) 94 | ..add('generatedByComment', generatedByComment) 95 | ..add('ignoreForFile', ignoreForFile) 96 | ..add('name', name)) 97 | .toString(); 98 | } 99 | } 100 | 101 | class _$LibraryBuilder extends LibraryBuilder { 102 | _$Library? _$v; 103 | 104 | @override 105 | ListBuilder get annotations { 106 | _$this; 107 | return super.annotations; 108 | } 109 | 110 | @override 111 | set annotations(ListBuilder annotations) { 112 | _$this; 113 | super.annotations = annotations; 114 | } 115 | 116 | @override 117 | ListBuilder get docs { 118 | _$this; 119 | return super.docs; 120 | } 121 | 122 | @override 123 | set docs(ListBuilder docs) { 124 | _$this; 125 | super.docs = docs; 126 | } 127 | 128 | @override 129 | ListBuilder get directives { 130 | _$this; 131 | return super.directives; 132 | } 133 | 134 | @override 135 | set directives(ListBuilder directives) { 136 | _$this; 137 | super.directives = directives; 138 | } 139 | 140 | @override 141 | ListBuilder get body { 142 | _$this; 143 | return super.body; 144 | } 145 | 146 | @override 147 | set body(ListBuilder body) { 148 | _$this; 149 | super.body = body; 150 | } 151 | 152 | @override 153 | ListBuilder get comments { 154 | _$this; 155 | return super.comments; 156 | } 157 | 158 | @override 159 | set comments(ListBuilder comments) { 160 | _$this; 161 | super.comments = comments; 162 | } 163 | 164 | @override 165 | String? get generatedByComment { 166 | _$this; 167 | return super.generatedByComment; 168 | } 169 | 170 | @override 171 | set generatedByComment(String? generatedByComment) { 172 | _$this; 173 | super.generatedByComment = generatedByComment; 174 | } 175 | 176 | @override 177 | ListBuilder get ignoreForFile { 178 | _$this; 179 | return super.ignoreForFile; 180 | } 181 | 182 | @override 183 | set ignoreForFile(ListBuilder ignoreForFile) { 184 | _$this; 185 | super.ignoreForFile = ignoreForFile; 186 | } 187 | 188 | @override 189 | String? get name { 190 | _$this; 191 | return super.name; 192 | } 193 | 194 | @override 195 | set name(String? name) { 196 | _$this; 197 | super.name = name; 198 | } 199 | 200 | _$LibraryBuilder() : super._(); 201 | 202 | LibraryBuilder get _$this { 203 | final $v = _$v; 204 | if ($v != null) { 205 | super.annotations = $v.annotations.toBuilder(); 206 | super.docs = $v.docs.toBuilder(); 207 | super.directives = $v.directives.toBuilder(); 208 | super.body = $v.body.toBuilder(); 209 | super.comments = $v.comments.toBuilder(); 210 | super.generatedByComment = $v.generatedByComment; 211 | super.ignoreForFile = $v.ignoreForFile.toBuilder(); 212 | super.name = $v.name; 213 | _$v = null; 214 | } 215 | return this; 216 | } 217 | 218 | @override 219 | void replace(Library other) { 220 | ArgumentError.checkNotNull(other, 'other'); 221 | _$v = other as _$Library; 222 | } 223 | 224 | @override 225 | void update(void Function(LibraryBuilder)? updates) { 226 | if (updates != null) updates(this); 227 | } 228 | 229 | @override 230 | Library build() => _build(); 231 | 232 | _$Library _build() { 233 | _$Library _$result; 234 | try { 235 | _$result = _$v ?? 236 | new _$Library._( 237 | annotations: annotations.build(), 238 | docs: docs.build(), 239 | directives: directives.build(), 240 | body: body.build(), 241 | comments: comments.build(), 242 | generatedByComment: generatedByComment, 243 | ignoreForFile: ignoreForFile.build(), 244 | name: name); 245 | } catch (_) { 246 | late String _$failedField; 247 | try { 248 | _$failedField = 'annotations'; 249 | annotations.build(); 250 | _$failedField = 'docs'; 251 | docs.build(); 252 | _$failedField = 'directives'; 253 | directives.build(); 254 | _$failedField = 'body'; 255 | body.build(); 256 | _$failedField = 'comments'; 257 | comments.build(); 258 | 259 | _$failedField = 'ignoreForFile'; 260 | ignoreForFile.build(); 261 | } catch (e) { 262 | throw new BuiltValueNestedFieldError( 263 | r'Library', _$failedField, e.toString()); 264 | } 265 | rethrow; 266 | } 267 | replace(_$result); 268 | return _$result; 269 | } 270 | } 271 | 272 | // ignore_for_file: deprecated_member_use_from_same_package,type=lint 273 | -------------------------------------------------------------------------------- /lib/src/specs/method.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../mixins/annotations.dart'; 11 | import '../mixins/dartdoc.dart'; 12 | import '../mixins/generics.dart'; 13 | import '../visitors.dart'; 14 | import 'code.dart'; 15 | import 'expression.dart'; 16 | import 'reference.dart'; 17 | 18 | part 'method.g.dart'; 19 | 20 | const _$void = Reference('void'); 21 | 22 | @immutable 23 | abstract class Method extends Object 24 | with HasAnnotations, HasGenerics, HasDartDocs 25 | implements Built, Spec { 26 | factory Method([void Function(MethodBuilder) updates]) = _$Method; 27 | 28 | factory Method.returnsVoid([void Function(MethodBuilder)? updates]) => 29 | Method((b) { 30 | if (updates != null) { 31 | updates(b); 32 | } 33 | b.returns = _$void; 34 | }); 35 | 36 | Method._(); 37 | 38 | @override 39 | BuiltList get annotations; 40 | 41 | @override 42 | BuiltList get docs; 43 | 44 | @override 45 | BuiltList get types; 46 | 47 | /// Optional parameters. 48 | BuiltList get optionalParameters; 49 | 50 | /// Required parameters. 51 | BuiltList get requiredParameters; 52 | 53 | /// Body of the method. 54 | Code? get body; 55 | 56 | /// Whether the method should be prefixed with `external`. 57 | bool get external; 58 | 59 | /// Whether this method is a simple lambda expression. 60 | /// 61 | /// May be `null` to be inferred based on the value of [body]. 62 | bool? get lambda; 63 | 64 | /// Whether this method should be prefixed with `static`. 65 | /// 66 | /// This is only valid within classes. 67 | bool get static; 68 | 69 | /// Name of the method or function. 70 | /// 71 | /// May be `null` when being used as a [closure]. 72 | String? get name; 73 | 74 | /// Whether this is a getter or setter. 75 | MethodType? get type; 76 | 77 | /// Whether this method is `async`, `async*`, or `sync*`. 78 | MethodModifier? get modifier; 79 | 80 | Reference? get returns; 81 | 82 | @override 83 | R accept( 84 | SpecVisitor visitor, [ 85 | R? context, 86 | ]) => 87 | visitor.visitMethod(this, context); 88 | 89 | /// This method as a closure. 90 | Expression get closure => toClosure(this); 91 | 92 | /// This method as a (possibly) generic closure. 93 | Expression get genericClosure => toGenericClosure(this); 94 | } 95 | 96 | abstract class MethodBuilder extends Object 97 | with HasAnnotationsBuilder, HasGenericsBuilder, HasDartDocsBuilder 98 | implements Builder { 99 | factory MethodBuilder() = _$MethodBuilder; 100 | 101 | MethodBuilder._(); 102 | 103 | @override 104 | void update(void Function(MethodBuilder)? updates) { 105 | updates?.call(this); 106 | } 107 | 108 | @override 109 | ListBuilder annotations = ListBuilder(); 110 | 111 | @override 112 | ListBuilder docs = ListBuilder(); 113 | 114 | @override 115 | ListBuilder types = ListBuilder(); 116 | 117 | /// Optional parameters. 118 | ListBuilder optionalParameters = ListBuilder(); 119 | 120 | /// Required parameters. 121 | ListBuilder requiredParameters = ListBuilder(); 122 | 123 | /// Body of the method. 124 | Code? body; 125 | 126 | /// Whether the method should be prefixed with `external`. 127 | bool external = false; 128 | 129 | /// Whether this method is a simple lambda expression. 130 | /// 131 | /// If not specified this is inferred from the [body]. 132 | bool? lambda; 133 | 134 | /// Whether this method should be prefixed with `static`. 135 | /// 136 | /// This is only valid within classes. 137 | bool static = false; 138 | 139 | /// Name of the method or function. 140 | String? name; 141 | 142 | /// Whether this is a getter or setter. 143 | MethodType? type; 144 | 145 | /// Whether this method is `async`, `async*`, or `sync*`. 146 | MethodModifier? modifier; 147 | 148 | Reference? returns; 149 | } 150 | 151 | enum MethodType { 152 | getter, 153 | setter, 154 | } 155 | 156 | enum MethodModifier { 157 | async, 158 | asyncStar, 159 | syncStar, 160 | } 161 | 162 | abstract class Parameter extends Object 163 | with HasAnnotations, HasGenerics, HasDartDocs 164 | implements Built { 165 | factory Parameter([void Function(ParameterBuilder) updates]) = _$Parameter; 166 | 167 | Parameter._(); 168 | 169 | /// If not `null`, a default assignment if the parameter is optional. 170 | Code? get defaultTo; 171 | 172 | /// Name of the parameter. 173 | String get name; 174 | 175 | /// Whether this parameter should be named, if optional. 176 | bool get named; 177 | 178 | /// Whether this parameter should be field formal (i.e. `this.`). 179 | /// 180 | /// This is only valid on constructors; 181 | bool get toThis; 182 | 183 | /// Whether this parameter should be passed to super\ 184 | /// constructor (i.e. `super.`). 185 | /// 186 | /// This is only valid on constructors; 187 | bool get toSuper; 188 | 189 | @override 190 | BuiltList get annotations; 191 | 192 | @override 193 | BuiltList get docs; 194 | 195 | @override 196 | BuiltList get types; 197 | 198 | /// Type of the parameter; 199 | Reference? get type; 200 | 201 | /// Whether this parameter should be annotated with the `required` keyword. 202 | /// 203 | /// This is only valid on named parameters. 204 | /// 205 | /// This is only valid when the output is targeting a Dart language version 206 | /// that supports null safety. 207 | bool get required; 208 | 209 | /// Whether this parameter should be annotated with the `covariant` keyword. 210 | /// 211 | /// This is only valid on instance methods. 212 | bool get covariant; 213 | } 214 | 215 | abstract class ParameterBuilder extends Object 216 | with HasAnnotationsBuilder, HasGenericsBuilder, HasDartDocsBuilder 217 | implements Builder { 218 | factory ParameterBuilder() = _$ParameterBuilder; 219 | 220 | ParameterBuilder._(); 221 | 222 | @override 223 | void update(void Function(ParameterBuilder)? updates) { 224 | updates?.call(this); 225 | } 226 | 227 | /// If not `null`, a default assignment if the parameter is optional. 228 | Code? defaultTo; 229 | 230 | /// Name of the parameter. 231 | late final String name; 232 | 233 | /// Whether this parameter should be named, if optional. 234 | bool named = false; 235 | 236 | /// Whether this parameter should be field formal (i.e. `this.`). 237 | /// 238 | /// This is only valid on constructors; 239 | bool toThis = false; 240 | 241 | /// Whether this parameter should be passed to super\ 242 | /// constructor (i.e. `super.`). 243 | /// 244 | /// This is only valid on constructors; 245 | bool toSuper = false; 246 | 247 | @override 248 | ListBuilder annotations = ListBuilder(); 249 | 250 | @override 251 | ListBuilder docs = ListBuilder(); 252 | 253 | @override 254 | ListBuilder types = ListBuilder(); 255 | 256 | /// Type of the parameter; 257 | Reference? type; 258 | 259 | /// Whether this parameter should be annotated with the `required` keyword. 260 | /// 261 | /// This is only valid on named parameters. 262 | /// 263 | /// This is only valid when the output is targeting a Dart language version 264 | /// that supports null safety. 265 | bool required = false; 266 | 267 | /// Whether this parameter should be annotated with the `covariant` keyword. 268 | /// 269 | /// This is only valid on instance methods. 270 | bool covariant = false; 271 | } 272 | -------------------------------------------------------------------------------- /lib/src/specs/mixin.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../mixins/annotations.dart'; 11 | import '../mixins/dartdoc.dart'; 12 | import '../mixins/generics.dart'; 13 | import '../visitors.dart'; 14 | import 'expression.dart'; 15 | import 'field.dart'; 16 | import 'method.dart'; 17 | import 'reference.dart'; 18 | 19 | part 'mixin.g.dart'; 20 | 21 | @immutable 22 | abstract class Mixin extends Object 23 | with HasAnnotations, HasDartDocs, HasGenerics 24 | implements Built, Spec { 25 | factory Mixin([void Function(MixinBuilder b) updates]) = _$Mixin; 26 | 27 | Mixin._(); 28 | 29 | /// Whether the mixin is a `base mixin`. 30 | bool get base; 31 | 32 | @override 33 | BuiltList get annotations; 34 | 35 | @override 36 | BuiltList get docs; 37 | 38 | Reference? get on; 39 | 40 | BuiltList get implements; 41 | 42 | @override 43 | BuiltList get types; 44 | 45 | BuiltList get methods; 46 | BuiltList get fields; 47 | 48 | /// Name of the mixin. 49 | String get name; 50 | 51 | @override 52 | R accept( 53 | SpecVisitor visitor, [ 54 | R? context, 55 | ]) => 56 | visitor.visitMixin(this, context); 57 | } 58 | 59 | abstract class MixinBuilder extends Object 60 | with HasAnnotationsBuilder, HasDartDocsBuilder, HasGenericsBuilder 61 | implements Builder { 62 | factory MixinBuilder() = _$MixinBuilder; 63 | 64 | MixinBuilder._(); 65 | 66 | /// Whether the mixin is a `base mixin`. 67 | bool base = false; 68 | 69 | @override 70 | ListBuilder annotations = ListBuilder(); 71 | 72 | @override 73 | ListBuilder docs = ListBuilder(); 74 | 75 | Reference? on; 76 | 77 | ListBuilder implements = ListBuilder(); 78 | 79 | @override 80 | ListBuilder types = ListBuilder(); 81 | 82 | ListBuilder methods = ListBuilder(); 83 | 84 | ListBuilder fields = ListBuilder(); 85 | 86 | /// Name of the mixin. 87 | String? name; 88 | } 89 | -------------------------------------------------------------------------------- /lib/src/specs/mixin.g.dart: -------------------------------------------------------------------------------- 1 | // GENERATED CODE - DO NOT MODIFY BY HAND 2 | 3 | part of 'mixin.dart'; 4 | 5 | // ************************************************************************** 6 | // BuiltValueGenerator 7 | // ************************************************************************** 8 | 9 | class _$Mixin extends Mixin { 10 | @override 11 | final bool base; 12 | @override 13 | final BuiltList annotations; 14 | @override 15 | final BuiltList docs; 16 | @override 17 | final Reference? on; 18 | @override 19 | final BuiltList implements; 20 | @override 21 | final BuiltList types; 22 | @override 23 | final BuiltList methods; 24 | @override 25 | final BuiltList fields; 26 | @override 27 | final String name; 28 | 29 | factory _$Mixin([void Function(MixinBuilder)? updates]) => 30 | (new MixinBuilder()..update(updates)).build() as _$Mixin; 31 | 32 | _$Mixin._( 33 | {required this.base, 34 | required this.annotations, 35 | required this.docs, 36 | this.on, 37 | required this.implements, 38 | required this.types, 39 | required this.methods, 40 | required this.fields, 41 | required this.name}) 42 | : super._() { 43 | BuiltValueNullFieldError.checkNotNull(base, r'Mixin', 'base'); 44 | BuiltValueNullFieldError.checkNotNull(annotations, r'Mixin', 'annotations'); 45 | BuiltValueNullFieldError.checkNotNull(docs, r'Mixin', 'docs'); 46 | BuiltValueNullFieldError.checkNotNull(implements, r'Mixin', 'implements'); 47 | BuiltValueNullFieldError.checkNotNull(types, r'Mixin', 'types'); 48 | BuiltValueNullFieldError.checkNotNull(methods, r'Mixin', 'methods'); 49 | BuiltValueNullFieldError.checkNotNull(fields, r'Mixin', 'fields'); 50 | BuiltValueNullFieldError.checkNotNull(name, r'Mixin', 'name'); 51 | } 52 | 53 | @override 54 | Mixin rebuild(void Function(MixinBuilder) updates) => 55 | (toBuilder()..update(updates)).build(); 56 | 57 | @override 58 | _$MixinBuilder toBuilder() => new _$MixinBuilder()..replace(this); 59 | 60 | @override 61 | bool operator ==(Object other) { 62 | if (identical(other, this)) return true; 63 | return other is Mixin && 64 | base == other.base && 65 | annotations == other.annotations && 66 | docs == other.docs && 67 | on == other.on && 68 | implements == other.implements && 69 | types == other.types && 70 | methods == other.methods && 71 | fields == other.fields && 72 | name == other.name; 73 | } 74 | 75 | @override 76 | int get hashCode { 77 | var _$hash = 0; 78 | _$hash = $jc(_$hash, base.hashCode); 79 | _$hash = $jc(_$hash, annotations.hashCode); 80 | _$hash = $jc(_$hash, docs.hashCode); 81 | _$hash = $jc(_$hash, on.hashCode); 82 | _$hash = $jc(_$hash, implements.hashCode); 83 | _$hash = $jc(_$hash, types.hashCode); 84 | _$hash = $jc(_$hash, methods.hashCode); 85 | _$hash = $jc(_$hash, fields.hashCode); 86 | _$hash = $jc(_$hash, name.hashCode); 87 | _$hash = $jf(_$hash); 88 | return _$hash; 89 | } 90 | 91 | @override 92 | String toString() { 93 | return (newBuiltValueToStringHelper(r'Mixin') 94 | ..add('base', base) 95 | ..add('annotations', annotations) 96 | ..add('docs', docs) 97 | ..add('on', on) 98 | ..add('implements', implements) 99 | ..add('types', types) 100 | ..add('methods', methods) 101 | ..add('fields', fields) 102 | ..add('name', name)) 103 | .toString(); 104 | } 105 | } 106 | 107 | class _$MixinBuilder extends MixinBuilder { 108 | _$Mixin? _$v; 109 | 110 | @override 111 | bool get base { 112 | _$this; 113 | return super.base; 114 | } 115 | 116 | @override 117 | set base(bool base) { 118 | _$this; 119 | super.base = base; 120 | } 121 | 122 | @override 123 | ListBuilder get annotations { 124 | _$this; 125 | return super.annotations; 126 | } 127 | 128 | @override 129 | set annotations(ListBuilder annotations) { 130 | _$this; 131 | super.annotations = annotations; 132 | } 133 | 134 | @override 135 | ListBuilder get docs { 136 | _$this; 137 | return super.docs; 138 | } 139 | 140 | @override 141 | set docs(ListBuilder docs) { 142 | _$this; 143 | super.docs = docs; 144 | } 145 | 146 | @override 147 | Reference? get on { 148 | _$this; 149 | return super.on; 150 | } 151 | 152 | @override 153 | set on(Reference? on) { 154 | _$this; 155 | super.on = on; 156 | } 157 | 158 | @override 159 | ListBuilder get implements { 160 | _$this; 161 | return super.implements; 162 | } 163 | 164 | @override 165 | set implements(ListBuilder implements) { 166 | _$this; 167 | super.implements = implements; 168 | } 169 | 170 | @override 171 | ListBuilder get types { 172 | _$this; 173 | return super.types; 174 | } 175 | 176 | @override 177 | set types(ListBuilder types) { 178 | _$this; 179 | super.types = types; 180 | } 181 | 182 | @override 183 | ListBuilder get methods { 184 | _$this; 185 | return super.methods; 186 | } 187 | 188 | @override 189 | set methods(ListBuilder methods) { 190 | _$this; 191 | super.methods = methods; 192 | } 193 | 194 | @override 195 | ListBuilder get fields { 196 | _$this; 197 | return super.fields; 198 | } 199 | 200 | @override 201 | set fields(ListBuilder fields) { 202 | _$this; 203 | super.fields = fields; 204 | } 205 | 206 | @override 207 | String? get name { 208 | _$this; 209 | return super.name; 210 | } 211 | 212 | @override 213 | set name(String? name) { 214 | _$this; 215 | super.name = name; 216 | } 217 | 218 | _$MixinBuilder() : super._(); 219 | 220 | MixinBuilder get _$this { 221 | final $v = _$v; 222 | if ($v != null) { 223 | super.base = $v.base; 224 | super.annotations = $v.annotations.toBuilder(); 225 | super.docs = $v.docs.toBuilder(); 226 | super.on = $v.on; 227 | super.implements = $v.implements.toBuilder(); 228 | super.types = $v.types.toBuilder(); 229 | super.methods = $v.methods.toBuilder(); 230 | super.fields = $v.fields.toBuilder(); 231 | super.name = $v.name; 232 | _$v = null; 233 | } 234 | return this; 235 | } 236 | 237 | @override 238 | void replace(Mixin other) { 239 | ArgumentError.checkNotNull(other, 'other'); 240 | _$v = other as _$Mixin; 241 | } 242 | 243 | @override 244 | void update(void Function(MixinBuilder)? updates) { 245 | if (updates != null) updates(this); 246 | } 247 | 248 | @override 249 | Mixin build() => _build(); 250 | 251 | _$Mixin _build() { 252 | _$Mixin _$result; 253 | try { 254 | _$result = _$v ?? 255 | new _$Mixin._( 256 | base: 257 | BuiltValueNullFieldError.checkNotNull(base, r'Mixin', 'base'), 258 | annotations: annotations.build(), 259 | docs: docs.build(), 260 | on: on, 261 | implements: implements.build(), 262 | types: types.build(), 263 | methods: methods.build(), 264 | fields: fields.build(), 265 | name: BuiltValueNullFieldError.checkNotNull( 266 | name, r'Mixin', 'name')); 267 | } catch (_) { 268 | late String _$failedField; 269 | try { 270 | _$failedField = 'annotations'; 271 | annotations.build(); 272 | _$failedField = 'docs'; 273 | docs.build(); 274 | 275 | _$failedField = 'implements'; 276 | implements.build(); 277 | _$failedField = 'types'; 278 | types.build(); 279 | _$failedField = 'methods'; 280 | methods.build(); 281 | _$failedField = 'fields'; 282 | fields.build(); 283 | } catch (e) { 284 | throw new BuiltValueNestedFieldError( 285 | r'Mixin', _$failedField, e.toString()); 286 | } 287 | rethrow; 288 | } 289 | replace(_$result); 290 | return _$result; 291 | } 292 | } 293 | 294 | // ignore_for_file: deprecated_member_use_from_same_package,type=lint 295 | -------------------------------------------------------------------------------- /lib/src/specs/reference.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_value/built_value.dart'; 6 | import 'package:meta/meta.dart'; 7 | 8 | import '../base.dart'; 9 | import '../visitors.dart'; 10 | import 'code.dart'; 11 | import 'expression.dart'; 12 | import 'type_reference.dart'; 13 | 14 | /// Short-hand for `Reference(symbol, url)`. 15 | Reference refer(String symbol, [String? url]) => Reference(symbol, url); 16 | 17 | /// A reference to [symbol], such as a class, or top-level method or field. 18 | /// 19 | /// References can be collected and collated in order to automatically generate 20 | /// `import` statements for all used symbols. 21 | @immutable 22 | class Reference extends Expression implements Spec { 23 | /// Relative, `package:` or `dart:` URL of the library. 24 | /// 25 | /// May be omitted (`null`) in order to express "same library". 26 | final String? url; 27 | 28 | /// Name of the class, method, or field. 29 | /// 30 | /// May be `null` for references without symbols, for instance a function type 31 | /// has no symbol. 32 | final String? symbol; 33 | 34 | /// Create a reference to [symbol] in [url]. 35 | const Reference(this.symbol, [this.url]); 36 | 37 | @override 38 | R accept( 39 | SpecVisitor visitor, [ 40 | R? context, 41 | ]) => 42 | visitor.visitReference(this, context); 43 | 44 | @override 45 | int get hashCode => '$url#$symbol'.hashCode; 46 | 47 | @override 48 | bool operator ==(Object other) => 49 | other is Reference && other.url == url && other.symbol == symbol; 50 | 51 | /// Returns a new instance of this expression. 52 | Expression newInstance( 53 | Iterable positionalArguments, [ 54 | Map namedArguments = const {}, 55 | List typeArguments = const [], 56 | ]) => 57 | InvokeExpression.newOf( 58 | this, 59 | positionalArguments.toList(), 60 | namedArguments, 61 | typeArguments, 62 | ); 63 | 64 | /// Returns a new instance of this expression with a named constructor. 65 | Expression newInstanceNamed( 66 | String name, 67 | Iterable positionalArguments, [ 68 | Map namedArguments = const {}, 69 | List typeArguments = const [], 70 | ]) => 71 | InvokeExpression.newOf( 72 | this, 73 | positionalArguments.toList(), 74 | namedArguments, 75 | typeArguments, 76 | name, 77 | ); 78 | 79 | /// Returns a const instance of this expression. 80 | Expression constInstance( 81 | Iterable positionalArguments, [ 82 | Map namedArguments = const {}, 83 | List typeArguments = const [], 84 | ]) => 85 | InvokeExpression.constOf( 86 | this, 87 | positionalArguments.toList(), 88 | namedArguments, 89 | typeArguments, 90 | ); 91 | 92 | /// Returns a const instance of this expression with a named constructor. 93 | Expression constInstanceNamed( 94 | String name, 95 | Iterable positionalArguments, [ 96 | Map namedArguments = const {}, 97 | List typeArguments = const [], 98 | ]) => 99 | InvokeExpression.constOf( 100 | this, 101 | positionalArguments.toList(), 102 | namedArguments, 103 | typeArguments, 104 | name, 105 | ); 106 | 107 | @override 108 | Expression get expression => CodeExpression(Code.scope((a) => a(this))); 109 | 110 | @override 111 | String toString() => (newBuiltValueToStringHelper('Reference') 112 | ..add('url', url) 113 | ..add('symbol', symbol)) 114 | .toString(); 115 | 116 | /// Returns as a [TypeReference], which allows adding generic type parameters. 117 | Reference get type => TypeReference((b) => b 118 | ..url = url 119 | ..symbol = symbol); 120 | } 121 | -------------------------------------------------------------------------------- /lib/src/specs/type_function.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../mixins/generics.dart'; 11 | import '../visitors.dart'; 12 | import 'code.dart'; 13 | import 'expression.dart'; 14 | import 'reference.dart'; 15 | 16 | part 'type_function.g.dart'; 17 | 18 | @immutable 19 | abstract class FunctionType extends Expression 20 | with HasGenerics 21 | implements Built, Reference, Spec { 22 | factory FunctionType([ 23 | void Function(FunctionTypeBuilder) updates, 24 | ]) = _$FunctionType; 25 | 26 | FunctionType._(); 27 | 28 | @override 29 | R accept( 30 | SpecVisitor visitor, [ 31 | R? context, 32 | ]) => 33 | visitor.visitFunctionType(this, context); 34 | 35 | /// Return type. 36 | Reference? get returnType; 37 | 38 | @override 39 | BuiltList get types; 40 | 41 | /// Required positional parameters of this function type. 42 | BuiltList get requiredParameters; 43 | 44 | /// Optional positional parameters of this function type. 45 | BuiltList get optionalParameters; 46 | 47 | /// Named optional parameters of this function type. 48 | BuiltMap get namedParameters; 49 | 50 | /// Named required parameters of this function type. 51 | BuiltMap get namedRequiredParameters; 52 | 53 | @override 54 | String? get url => null; 55 | 56 | @override 57 | String? get symbol => null; 58 | 59 | @override 60 | Reference get type => this; 61 | 62 | /// Optional nullability. 63 | /// 64 | /// An emitter may ignore this if the output is not targeting a Dart language 65 | /// version that supports null safety. 66 | bool? get isNullable; 67 | 68 | @override 69 | Expression newInstance( 70 | Iterable positionalArguments, [ 71 | Map namedArguments = const {}, 72 | List typeArguments = const [], 73 | ]) => 74 | throw UnsupportedError('Cannot instantiate a function type.'); 75 | 76 | @override 77 | Expression newInstanceNamed( 78 | String name, 79 | Iterable positionalArguments, [ 80 | Map namedArguments = const {}, 81 | List typeArguments = const [], 82 | ]) => 83 | throw UnsupportedError('Cannot instantiate a function type.'); 84 | 85 | @override 86 | Expression constInstance( 87 | Iterable positionalArguments, [ 88 | Map namedArguments = const {}, 89 | List typeArguments = const [], 90 | ]) => 91 | throw UnsupportedError('Cannot "const" a function type.'); 92 | 93 | @override 94 | Expression constInstanceNamed( 95 | String name, 96 | Iterable positionalArguments, [ 97 | Map namedArguments = const {}, 98 | List typeArguments = const [], 99 | ]) => 100 | throw UnsupportedError('Cannot "const" a function type.'); 101 | 102 | /// A typedef assignment to this type. 103 | Code toTypeDef(String name) => createTypeDef(name, this); 104 | } 105 | 106 | abstract class FunctionTypeBuilder extends Object 107 | with HasGenericsBuilder 108 | implements Builder { 109 | factory FunctionTypeBuilder() = _$FunctionTypeBuilder; 110 | 111 | FunctionTypeBuilder._(); 112 | 113 | Reference? returnType; 114 | 115 | @override 116 | ListBuilder types = ListBuilder(); 117 | 118 | ListBuilder requiredParameters = ListBuilder(); 119 | 120 | ListBuilder optionalParameters = ListBuilder(); 121 | 122 | MapBuilder namedParameters = 123 | MapBuilder(); 124 | 125 | MapBuilder namedRequiredParameters = 126 | MapBuilder(); 127 | 128 | bool? isNullable; 129 | 130 | String? url; 131 | 132 | String? symbol; 133 | } 134 | -------------------------------------------------------------------------------- /lib/src/specs/type_function.g.dart: -------------------------------------------------------------------------------- 1 | // GENERATED CODE - DO NOT MODIFY BY HAND 2 | 3 | part of 'type_function.dart'; 4 | 5 | // ************************************************************************** 6 | // BuiltValueGenerator 7 | // ************************************************************************** 8 | 9 | class _$FunctionType extends FunctionType { 10 | @override 11 | final Reference? returnType; 12 | @override 13 | final BuiltList types; 14 | @override 15 | final BuiltList requiredParameters; 16 | @override 17 | final BuiltList optionalParameters; 18 | @override 19 | final BuiltMap namedParameters; 20 | @override 21 | final BuiltMap namedRequiredParameters; 22 | @override 23 | final bool? isNullable; 24 | 25 | factory _$FunctionType([void Function(FunctionTypeBuilder)? updates]) => 26 | (new FunctionTypeBuilder()..update(updates)).build() as _$FunctionType; 27 | 28 | _$FunctionType._( 29 | {this.returnType, 30 | required this.types, 31 | required this.requiredParameters, 32 | required this.optionalParameters, 33 | required this.namedParameters, 34 | required this.namedRequiredParameters, 35 | this.isNullable}) 36 | : super._() { 37 | BuiltValueNullFieldError.checkNotNull(types, r'FunctionType', 'types'); 38 | BuiltValueNullFieldError.checkNotNull( 39 | requiredParameters, r'FunctionType', 'requiredParameters'); 40 | BuiltValueNullFieldError.checkNotNull( 41 | optionalParameters, r'FunctionType', 'optionalParameters'); 42 | BuiltValueNullFieldError.checkNotNull( 43 | namedParameters, r'FunctionType', 'namedParameters'); 44 | BuiltValueNullFieldError.checkNotNull( 45 | namedRequiredParameters, r'FunctionType', 'namedRequiredParameters'); 46 | } 47 | 48 | @override 49 | FunctionType rebuild(void Function(FunctionTypeBuilder) updates) => 50 | (toBuilder()..update(updates)).build(); 51 | 52 | @override 53 | _$FunctionTypeBuilder toBuilder() => 54 | new _$FunctionTypeBuilder()..replace(this); 55 | 56 | @override 57 | bool operator ==(Object other) { 58 | if (identical(other, this)) return true; 59 | return other is FunctionType && 60 | returnType == other.returnType && 61 | types == other.types && 62 | requiredParameters == other.requiredParameters && 63 | optionalParameters == other.optionalParameters && 64 | namedParameters == other.namedParameters && 65 | namedRequiredParameters == other.namedRequiredParameters && 66 | isNullable == other.isNullable; 67 | } 68 | 69 | @override 70 | int get hashCode { 71 | var _$hash = 0; 72 | _$hash = $jc(_$hash, returnType.hashCode); 73 | _$hash = $jc(_$hash, types.hashCode); 74 | _$hash = $jc(_$hash, requiredParameters.hashCode); 75 | _$hash = $jc(_$hash, optionalParameters.hashCode); 76 | _$hash = $jc(_$hash, namedParameters.hashCode); 77 | _$hash = $jc(_$hash, namedRequiredParameters.hashCode); 78 | _$hash = $jc(_$hash, isNullable.hashCode); 79 | _$hash = $jf(_$hash); 80 | return _$hash; 81 | } 82 | 83 | @override 84 | String toString() { 85 | return (newBuiltValueToStringHelper(r'FunctionType') 86 | ..add('returnType', returnType) 87 | ..add('types', types) 88 | ..add('requiredParameters', requiredParameters) 89 | ..add('optionalParameters', optionalParameters) 90 | ..add('namedParameters', namedParameters) 91 | ..add('namedRequiredParameters', namedRequiredParameters) 92 | ..add('isNullable', isNullable)) 93 | .toString(); 94 | } 95 | } 96 | 97 | class _$FunctionTypeBuilder extends FunctionTypeBuilder { 98 | _$FunctionType? _$v; 99 | 100 | @override 101 | Reference? get returnType { 102 | _$this; 103 | return super.returnType; 104 | } 105 | 106 | @override 107 | set returnType(Reference? returnType) { 108 | _$this; 109 | super.returnType = returnType; 110 | } 111 | 112 | @override 113 | ListBuilder get types { 114 | _$this; 115 | return super.types; 116 | } 117 | 118 | @override 119 | set types(ListBuilder types) { 120 | _$this; 121 | super.types = types; 122 | } 123 | 124 | @override 125 | ListBuilder get requiredParameters { 126 | _$this; 127 | return super.requiredParameters; 128 | } 129 | 130 | @override 131 | set requiredParameters(ListBuilder requiredParameters) { 132 | _$this; 133 | super.requiredParameters = requiredParameters; 134 | } 135 | 136 | @override 137 | ListBuilder get optionalParameters { 138 | _$this; 139 | return super.optionalParameters; 140 | } 141 | 142 | @override 143 | set optionalParameters(ListBuilder optionalParameters) { 144 | _$this; 145 | super.optionalParameters = optionalParameters; 146 | } 147 | 148 | @override 149 | MapBuilder get namedParameters { 150 | _$this; 151 | return super.namedParameters; 152 | } 153 | 154 | @override 155 | set namedParameters(MapBuilder namedParameters) { 156 | _$this; 157 | super.namedParameters = namedParameters; 158 | } 159 | 160 | @override 161 | MapBuilder get namedRequiredParameters { 162 | _$this; 163 | return super.namedRequiredParameters; 164 | } 165 | 166 | @override 167 | set namedRequiredParameters( 168 | MapBuilder namedRequiredParameters) { 169 | _$this; 170 | super.namedRequiredParameters = namedRequiredParameters; 171 | } 172 | 173 | @override 174 | bool? get isNullable { 175 | _$this; 176 | return super.isNullable; 177 | } 178 | 179 | @override 180 | set isNullable(bool? isNullable) { 181 | _$this; 182 | super.isNullable = isNullable; 183 | } 184 | 185 | _$FunctionTypeBuilder() : super._(); 186 | 187 | FunctionTypeBuilder get _$this { 188 | final $v = _$v; 189 | if ($v != null) { 190 | super.returnType = $v.returnType; 191 | super.types = $v.types.toBuilder(); 192 | super.requiredParameters = $v.requiredParameters.toBuilder(); 193 | super.optionalParameters = $v.optionalParameters.toBuilder(); 194 | super.namedParameters = $v.namedParameters.toBuilder(); 195 | super.namedRequiredParameters = $v.namedRequiredParameters.toBuilder(); 196 | super.isNullable = $v.isNullable; 197 | _$v = null; 198 | } 199 | return this; 200 | } 201 | 202 | @override 203 | void replace(FunctionType other) { 204 | ArgumentError.checkNotNull(other, 'other'); 205 | _$v = other as _$FunctionType; 206 | } 207 | 208 | @override 209 | void update(void Function(FunctionTypeBuilder)? updates) { 210 | if (updates != null) updates(this); 211 | } 212 | 213 | @override 214 | FunctionType build() => _build(); 215 | 216 | _$FunctionType _build() { 217 | _$FunctionType _$result; 218 | try { 219 | _$result = _$v ?? 220 | new _$FunctionType._( 221 | returnType: returnType, 222 | types: types.build(), 223 | requiredParameters: requiredParameters.build(), 224 | optionalParameters: optionalParameters.build(), 225 | namedParameters: namedParameters.build(), 226 | namedRequiredParameters: namedRequiredParameters.build(), 227 | isNullable: isNullable); 228 | } catch (_) { 229 | late String _$failedField; 230 | try { 231 | _$failedField = 'types'; 232 | types.build(); 233 | _$failedField = 'requiredParameters'; 234 | requiredParameters.build(); 235 | _$failedField = 'optionalParameters'; 236 | optionalParameters.build(); 237 | _$failedField = 'namedParameters'; 238 | namedParameters.build(); 239 | _$failedField = 'namedRequiredParameters'; 240 | namedRequiredParameters.build(); 241 | } catch (e) { 242 | throw new BuiltValueNestedFieldError( 243 | r'FunctionType', _$failedField, e.toString()); 244 | } 245 | rethrow; 246 | } 247 | replace(_$result); 248 | return _$result; 249 | } 250 | } 251 | 252 | // ignore_for_file: deprecated_member_use_from_same_package,type=lint 253 | -------------------------------------------------------------------------------- /lib/src/specs/type_record.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../visitors.dart'; 11 | import 'expression.dart'; 12 | import 'reference.dart'; 13 | 14 | part 'type_record.g.dart'; 15 | 16 | @immutable 17 | abstract class RecordType extends Expression 18 | implements Built, Reference, Spec { 19 | factory RecordType([ 20 | void Function(RecordTypeBuilder) updates, 21 | ]) = _$RecordType; 22 | 23 | RecordType._(); 24 | 25 | @override 26 | R accept( 27 | SpecVisitor visitor, [ 28 | R? context, 29 | ]) => 30 | visitor.visitRecordType(this, context); 31 | 32 | BuiltList get positionalFieldTypes; 33 | 34 | BuiltMap get namedFieldTypes; 35 | 36 | @override 37 | String? get url => null; 38 | 39 | @override 40 | String? get symbol => null; 41 | 42 | @override 43 | Reference get type => this; 44 | 45 | /// Optional nullability. 46 | bool? get isNullable; 47 | 48 | @override 49 | Expression newInstance( 50 | Iterable positionalArguments, [ 51 | Map namedArguments = const {}, 52 | List typeArguments = const [], 53 | ]) => 54 | throw UnsupportedError('Cannot instantiate a record type.'); 55 | 56 | @override 57 | Expression newInstanceNamed( 58 | String name, 59 | Iterable positionalArguments, [ 60 | Map namedArguments = const {}, 61 | List typeArguments = const [], 62 | ]) => 63 | throw UnsupportedError('Cannot instantiate a record type.'); 64 | 65 | @override 66 | Expression constInstance( 67 | Iterable positionalArguments, [ 68 | Map namedArguments = const {}, 69 | List typeArguments = const [], 70 | ]) => 71 | throw UnsupportedError('Cannot "const" a record type.'); 72 | 73 | @override 74 | Expression constInstanceNamed( 75 | String name, 76 | Iterable positionalArguments, [ 77 | Map namedArguments = const {}, 78 | List typeArguments = const [], 79 | ]) => 80 | throw UnsupportedError('Cannot "const" a record type.'); 81 | } 82 | 83 | abstract class RecordTypeBuilder extends Object 84 | implements Builder { 85 | factory RecordTypeBuilder() = _$RecordTypeBuilder; 86 | 87 | RecordTypeBuilder._(); 88 | 89 | ListBuilder positionalFieldTypes = ListBuilder(); 90 | 91 | MapBuilder namedFieldTypes = 92 | MapBuilder(); 93 | 94 | bool? isNullable; 95 | 96 | String? url; 97 | 98 | String? symbol; 99 | } 100 | -------------------------------------------------------------------------------- /lib/src/specs/type_record.g.dart: -------------------------------------------------------------------------------- 1 | // GENERATED CODE - DO NOT MODIFY BY HAND 2 | 3 | part of 'type_record.dart'; 4 | 5 | // ************************************************************************** 6 | // BuiltValueGenerator 7 | // ************************************************************************** 8 | 9 | class _$RecordType extends RecordType { 10 | @override 11 | final BuiltList positionalFieldTypes; 12 | @override 13 | final BuiltMap namedFieldTypes; 14 | @override 15 | final bool? isNullable; 16 | 17 | factory _$RecordType([void Function(RecordTypeBuilder)? updates]) => 18 | (new RecordTypeBuilder()..update(updates)).build() as _$RecordType; 19 | 20 | _$RecordType._( 21 | {required this.positionalFieldTypes, 22 | required this.namedFieldTypes, 23 | this.isNullable}) 24 | : super._() { 25 | BuiltValueNullFieldError.checkNotNull( 26 | positionalFieldTypes, r'RecordType', 'positionalFieldTypes'); 27 | BuiltValueNullFieldError.checkNotNull( 28 | namedFieldTypes, r'RecordType', 'namedFieldTypes'); 29 | } 30 | 31 | @override 32 | RecordType rebuild(void Function(RecordTypeBuilder) updates) => 33 | (toBuilder()..update(updates)).build(); 34 | 35 | @override 36 | _$RecordTypeBuilder toBuilder() => new _$RecordTypeBuilder()..replace(this); 37 | 38 | @override 39 | bool operator ==(Object other) { 40 | if (identical(other, this)) return true; 41 | return other is RecordType && 42 | positionalFieldTypes == other.positionalFieldTypes && 43 | namedFieldTypes == other.namedFieldTypes && 44 | isNullable == other.isNullable; 45 | } 46 | 47 | @override 48 | int get hashCode { 49 | var _$hash = 0; 50 | _$hash = $jc(_$hash, positionalFieldTypes.hashCode); 51 | _$hash = $jc(_$hash, namedFieldTypes.hashCode); 52 | _$hash = $jc(_$hash, isNullable.hashCode); 53 | _$hash = $jf(_$hash); 54 | return _$hash; 55 | } 56 | 57 | @override 58 | String toString() { 59 | return (newBuiltValueToStringHelper(r'RecordType') 60 | ..add('positionalFieldTypes', positionalFieldTypes) 61 | ..add('namedFieldTypes', namedFieldTypes) 62 | ..add('isNullable', isNullable)) 63 | .toString(); 64 | } 65 | } 66 | 67 | class _$RecordTypeBuilder extends RecordTypeBuilder { 68 | _$RecordType? _$v; 69 | 70 | @override 71 | ListBuilder get positionalFieldTypes { 72 | _$this; 73 | return super.positionalFieldTypes; 74 | } 75 | 76 | @override 77 | set positionalFieldTypes(ListBuilder positionalFieldTypes) { 78 | _$this; 79 | super.positionalFieldTypes = positionalFieldTypes; 80 | } 81 | 82 | @override 83 | MapBuilder get namedFieldTypes { 84 | _$this; 85 | return super.namedFieldTypes; 86 | } 87 | 88 | @override 89 | set namedFieldTypes(MapBuilder namedFieldTypes) { 90 | _$this; 91 | super.namedFieldTypes = namedFieldTypes; 92 | } 93 | 94 | @override 95 | bool? get isNullable { 96 | _$this; 97 | return super.isNullable; 98 | } 99 | 100 | @override 101 | set isNullable(bool? isNullable) { 102 | _$this; 103 | super.isNullable = isNullable; 104 | } 105 | 106 | _$RecordTypeBuilder() : super._(); 107 | 108 | RecordTypeBuilder get _$this { 109 | final $v = _$v; 110 | if ($v != null) { 111 | super.positionalFieldTypes = $v.positionalFieldTypes.toBuilder(); 112 | super.namedFieldTypes = $v.namedFieldTypes.toBuilder(); 113 | super.isNullable = $v.isNullable; 114 | _$v = null; 115 | } 116 | return this; 117 | } 118 | 119 | @override 120 | void replace(RecordType other) { 121 | ArgumentError.checkNotNull(other, 'other'); 122 | _$v = other as _$RecordType; 123 | } 124 | 125 | @override 126 | void update(void Function(RecordTypeBuilder)? updates) { 127 | if (updates != null) updates(this); 128 | } 129 | 130 | @override 131 | RecordType build() => _build(); 132 | 133 | _$RecordType _build() { 134 | _$RecordType _$result; 135 | try { 136 | _$result = _$v ?? 137 | new _$RecordType._( 138 | positionalFieldTypes: positionalFieldTypes.build(), 139 | namedFieldTypes: namedFieldTypes.build(), 140 | isNullable: isNullable); 141 | } catch (_) { 142 | late String _$failedField; 143 | try { 144 | _$failedField = 'positionalFieldTypes'; 145 | positionalFieldTypes.build(); 146 | _$failedField = 'namedFieldTypes'; 147 | namedFieldTypes.build(); 148 | } catch (e) { 149 | throw new BuiltValueNestedFieldError( 150 | r'RecordType', _$failedField, e.toString()); 151 | } 152 | rethrow; 153 | } 154 | replace(_$result); 155 | return _$result; 156 | } 157 | } 158 | 159 | // ignore_for_file: deprecated_member_use_from_same_package,type=lint 160 | -------------------------------------------------------------------------------- /lib/src/specs/type_reference.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../mixins/generics.dart'; 11 | import '../visitors.dart'; 12 | import 'code.dart'; 13 | import 'expression.dart'; 14 | import 'reference.dart'; 15 | 16 | part 'type_reference.g.dart'; 17 | 18 | @immutable 19 | abstract class TypeReference extends Expression 20 | with HasGenerics 21 | implements Built, Reference, Spec { 22 | factory TypeReference([ 23 | void Function(TypeReferenceBuilder) updates, 24 | ]) = _$TypeReference; 25 | 26 | TypeReference._(); 27 | 28 | @override 29 | String get symbol; 30 | 31 | @override 32 | String? get url; 33 | 34 | /// Optional bound generic. 35 | Reference? get bound; 36 | 37 | @override 38 | BuiltList get types; 39 | 40 | /// Optional nullability. 41 | /// 42 | /// An emitter may ignore this if the output is not targeting a Dart language 43 | /// version that supports null safety. 44 | bool? get isNullable; 45 | 46 | @override 47 | R accept( 48 | SpecVisitor visitor, [ 49 | R? context, 50 | ]) => 51 | visitor.visitType(this, context); 52 | 53 | @override 54 | Expression get expression => CodeExpression(Code.scope((a) => a(this))); 55 | 56 | @override 57 | TypeReference get type => this; 58 | 59 | @override 60 | Expression newInstance( 61 | Iterable positionalArguments, [ 62 | Map namedArguments = const {}, 63 | List typeArguments = const [], 64 | ]) => 65 | InvokeExpression.newOf( 66 | this, 67 | positionalArguments.toList(), 68 | namedArguments, 69 | typeArguments, 70 | ); 71 | 72 | @override 73 | Expression newInstanceNamed( 74 | String name, 75 | Iterable positionalArguments, [ 76 | Map namedArguments = const {}, 77 | List typeArguments = const [], 78 | ]) => 79 | InvokeExpression.newOf( 80 | this, 81 | positionalArguments.toList(), 82 | namedArguments, 83 | typeArguments, 84 | name, 85 | ); 86 | 87 | @override 88 | Expression constInstance( 89 | Iterable positionalArguments, [ 90 | Map namedArguments = const {}, 91 | List typeArguments = const [], 92 | ]) => 93 | InvokeExpression.constOf( 94 | this, 95 | positionalArguments.toList(), 96 | namedArguments, 97 | typeArguments, 98 | ); 99 | 100 | @override 101 | Expression constInstanceNamed( 102 | String name, 103 | Iterable positionalArguments, [ 104 | Map namedArguments = const {}, 105 | List typeArguments = const [], 106 | ]) => 107 | InvokeExpression.constOf( 108 | this, 109 | positionalArguments.toList(), 110 | namedArguments, 111 | typeArguments, 112 | name, 113 | ); 114 | } 115 | 116 | abstract class TypeReferenceBuilder extends Object 117 | with HasGenericsBuilder 118 | implements Builder { 119 | factory TypeReferenceBuilder() = _$TypeReferenceBuilder; 120 | 121 | TypeReferenceBuilder._(); 122 | 123 | String? symbol; 124 | 125 | String? url; 126 | 127 | /// Optional bound generic. 128 | Reference? bound; 129 | 130 | @override 131 | ListBuilder types = ListBuilder(); 132 | 133 | /// Optional nullability. 134 | /// 135 | /// An emitter may ignore this if the output is not targeting a Dart language 136 | /// version that supports null safety. 137 | bool? isNullable; 138 | } 139 | -------------------------------------------------------------------------------- /lib/src/specs/type_reference.g.dart: -------------------------------------------------------------------------------- 1 | // GENERATED CODE - DO NOT MODIFY BY HAND 2 | 3 | part of 'type_reference.dart'; 4 | 5 | // ************************************************************************** 6 | // BuiltValueGenerator 7 | // ************************************************************************** 8 | 9 | class _$TypeReference extends TypeReference { 10 | @override 11 | final String symbol; 12 | @override 13 | final String? url; 14 | @override 15 | final Reference? bound; 16 | @override 17 | final BuiltList types; 18 | @override 19 | final bool? isNullable; 20 | 21 | factory _$TypeReference([void Function(TypeReferenceBuilder)? updates]) => 22 | (new TypeReferenceBuilder()..update(updates)).build() as _$TypeReference; 23 | 24 | _$TypeReference._( 25 | {required this.symbol, 26 | this.url, 27 | this.bound, 28 | required this.types, 29 | this.isNullable}) 30 | : super._() { 31 | BuiltValueNullFieldError.checkNotNull(symbol, r'TypeReference', 'symbol'); 32 | BuiltValueNullFieldError.checkNotNull(types, r'TypeReference', 'types'); 33 | } 34 | 35 | @override 36 | TypeReference rebuild(void Function(TypeReferenceBuilder) updates) => 37 | (toBuilder()..update(updates)).build(); 38 | 39 | @override 40 | _$TypeReferenceBuilder toBuilder() => 41 | new _$TypeReferenceBuilder()..replace(this); 42 | 43 | @override 44 | bool operator ==(Object other) { 45 | if (identical(other, this)) return true; 46 | return other is TypeReference && 47 | symbol == other.symbol && 48 | url == other.url && 49 | bound == other.bound && 50 | types == other.types && 51 | isNullable == other.isNullable; 52 | } 53 | 54 | @override 55 | int get hashCode { 56 | var _$hash = 0; 57 | _$hash = $jc(_$hash, symbol.hashCode); 58 | _$hash = $jc(_$hash, url.hashCode); 59 | _$hash = $jc(_$hash, bound.hashCode); 60 | _$hash = $jc(_$hash, types.hashCode); 61 | _$hash = $jc(_$hash, isNullable.hashCode); 62 | _$hash = $jf(_$hash); 63 | return _$hash; 64 | } 65 | 66 | @override 67 | String toString() { 68 | return (newBuiltValueToStringHelper(r'TypeReference') 69 | ..add('symbol', symbol) 70 | ..add('url', url) 71 | ..add('bound', bound) 72 | ..add('types', types) 73 | ..add('isNullable', isNullable)) 74 | .toString(); 75 | } 76 | } 77 | 78 | class _$TypeReferenceBuilder extends TypeReferenceBuilder { 79 | _$TypeReference? _$v; 80 | 81 | @override 82 | String? get symbol { 83 | _$this; 84 | return super.symbol; 85 | } 86 | 87 | @override 88 | set symbol(String? symbol) { 89 | _$this; 90 | super.symbol = symbol; 91 | } 92 | 93 | @override 94 | String? get url { 95 | _$this; 96 | return super.url; 97 | } 98 | 99 | @override 100 | set url(String? url) { 101 | _$this; 102 | super.url = url; 103 | } 104 | 105 | @override 106 | Reference? get bound { 107 | _$this; 108 | return super.bound; 109 | } 110 | 111 | @override 112 | set bound(Reference? bound) { 113 | _$this; 114 | super.bound = bound; 115 | } 116 | 117 | @override 118 | ListBuilder get types { 119 | _$this; 120 | return super.types; 121 | } 122 | 123 | @override 124 | set types(ListBuilder types) { 125 | _$this; 126 | super.types = types; 127 | } 128 | 129 | @override 130 | bool? get isNullable { 131 | _$this; 132 | return super.isNullable; 133 | } 134 | 135 | @override 136 | set isNullable(bool? isNullable) { 137 | _$this; 138 | super.isNullable = isNullable; 139 | } 140 | 141 | _$TypeReferenceBuilder() : super._(); 142 | 143 | TypeReferenceBuilder get _$this { 144 | final $v = _$v; 145 | if ($v != null) { 146 | super.symbol = $v.symbol; 147 | super.url = $v.url; 148 | super.bound = $v.bound; 149 | super.types = $v.types.toBuilder(); 150 | super.isNullable = $v.isNullable; 151 | _$v = null; 152 | } 153 | return this; 154 | } 155 | 156 | @override 157 | void replace(TypeReference other) { 158 | ArgumentError.checkNotNull(other, 'other'); 159 | _$v = other as _$TypeReference; 160 | } 161 | 162 | @override 163 | void update(void Function(TypeReferenceBuilder)? updates) { 164 | if (updates != null) updates(this); 165 | } 166 | 167 | @override 168 | TypeReference build() => _build(); 169 | 170 | _$TypeReference _build() { 171 | _$TypeReference _$result; 172 | try { 173 | _$result = _$v ?? 174 | new _$TypeReference._( 175 | symbol: BuiltValueNullFieldError.checkNotNull( 176 | symbol, r'TypeReference', 'symbol'), 177 | url: url, 178 | bound: bound, 179 | types: types.build(), 180 | isNullable: isNullable); 181 | } catch (_) { 182 | late String _$failedField; 183 | try { 184 | _$failedField = 'types'; 185 | types.build(); 186 | } catch (e) { 187 | throw new BuiltValueNestedFieldError( 188 | r'TypeReference', _$failedField, e.toString()); 189 | } 190 | rethrow; 191 | } 192 | replace(_$result); 193 | return _$result; 194 | } 195 | } 196 | 197 | // ignore_for_file: deprecated_member_use_from_same_package,type=lint 198 | -------------------------------------------------------------------------------- /lib/src/specs/typedef.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:built_collection/built_collection.dart'; 6 | import 'package:built_value/built_value.dart'; 7 | import 'package:meta/meta.dart'; 8 | 9 | import '../base.dart'; 10 | import '../mixins/annotations.dart'; 11 | import '../mixins/dartdoc.dart'; 12 | import '../mixins/generics.dart'; 13 | import '../visitors.dart'; 14 | import 'expression.dart'; 15 | import 'reference.dart'; 16 | 17 | part 'typedef.g.dart'; 18 | 19 | @immutable 20 | abstract class TypeDef extends Object 21 | with HasAnnotations, HasDartDocs, HasGenerics 22 | implements Built, Spec { 23 | factory TypeDef([void Function(TypeDefBuilder)? updates]) = _$TypeDef; 24 | 25 | TypeDef._(); 26 | 27 | /// Name of the typedef. 28 | String get name; 29 | 30 | /// The right hand side of the typedef. 31 | /// 32 | /// Typically a reference to a type, or a Function type. 33 | Expression get definition; 34 | 35 | @override 36 | R accept( 37 | SpecVisitor visitor, [ 38 | R? context, 39 | ]) => 40 | visitor.visitTypeDef(this, context); 41 | } 42 | 43 | abstract class TypeDefBuilder extends Object 44 | with HasAnnotationsBuilder, HasDartDocsBuilder, HasGenericsBuilder 45 | implements Builder { 46 | factory TypeDefBuilder() = _$TypeDefBuilder; 47 | 48 | TypeDefBuilder._(); 49 | 50 | @override 51 | ListBuilder annotations = ListBuilder(); 52 | 53 | @override 54 | ListBuilder docs = ListBuilder(); 55 | 56 | @override 57 | ListBuilder types = ListBuilder(); 58 | 59 | String? name; 60 | 61 | Expression? definition; 62 | } 63 | -------------------------------------------------------------------------------- /lib/src/specs/typedef.g.dart: -------------------------------------------------------------------------------- 1 | // GENERATED CODE - DO NOT MODIFY BY HAND 2 | 3 | part of 'typedef.dart'; 4 | 5 | // ************************************************************************** 6 | // BuiltValueGenerator 7 | // ************************************************************************** 8 | 9 | class _$TypeDef extends TypeDef { 10 | @override 11 | final String name; 12 | @override 13 | final Expression definition; 14 | @override 15 | final BuiltList annotations; 16 | @override 17 | final BuiltList docs; 18 | @override 19 | final BuiltList types; 20 | 21 | factory _$TypeDef([void Function(TypeDefBuilder)? updates]) => 22 | (new TypeDefBuilder()..update(updates)).build() as _$TypeDef; 23 | 24 | _$TypeDef._( 25 | {required this.name, 26 | required this.definition, 27 | required this.annotations, 28 | required this.docs, 29 | required this.types}) 30 | : super._() { 31 | BuiltValueNullFieldError.checkNotNull(name, r'TypeDef', 'name'); 32 | BuiltValueNullFieldError.checkNotNull(definition, r'TypeDef', 'definition'); 33 | BuiltValueNullFieldError.checkNotNull( 34 | annotations, r'TypeDef', 'annotations'); 35 | BuiltValueNullFieldError.checkNotNull(docs, r'TypeDef', 'docs'); 36 | BuiltValueNullFieldError.checkNotNull(types, r'TypeDef', 'types'); 37 | } 38 | 39 | @override 40 | TypeDef rebuild(void Function(TypeDefBuilder) updates) => 41 | (toBuilder()..update(updates)).build(); 42 | 43 | @override 44 | _$TypeDefBuilder toBuilder() => new _$TypeDefBuilder()..replace(this); 45 | 46 | @override 47 | bool operator ==(Object other) { 48 | if (identical(other, this)) return true; 49 | return other is TypeDef && 50 | name == other.name && 51 | definition == other.definition && 52 | annotations == other.annotations && 53 | docs == other.docs && 54 | types == other.types; 55 | } 56 | 57 | @override 58 | int get hashCode { 59 | var _$hash = 0; 60 | _$hash = $jc(_$hash, name.hashCode); 61 | _$hash = $jc(_$hash, definition.hashCode); 62 | _$hash = $jc(_$hash, annotations.hashCode); 63 | _$hash = $jc(_$hash, docs.hashCode); 64 | _$hash = $jc(_$hash, types.hashCode); 65 | _$hash = $jf(_$hash); 66 | return _$hash; 67 | } 68 | 69 | @override 70 | String toString() { 71 | return (newBuiltValueToStringHelper(r'TypeDef') 72 | ..add('name', name) 73 | ..add('definition', definition) 74 | ..add('annotations', annotations) 75 | ..add('docs', docs) 76 | ..add('types', types)) 77 | .toString(); 78 | } 79 | } 80 | 81 | class _$TypeDefBuilder extends TypeDefBuilder { 82 | _$TypeDef? _$v; 83 | 84 | @override 85 | String? get name { 86 | _$this; 87 | return super.name; 88 | } 89 | 90 | @override 91 | set name(String? name) { 92 | _$this; 93 | super.name = name; 94 | } 95 | 96 | @override 97 | Expression? get definition { 98 | _$this; 99 | return super.definition; 100 | } 101 | 102 | @override 103 | set definition(Expression? definition) { 104 | _$this; 105 | super.definition = definition; 106 | } 107 | 108 | @override 109 | ListBuilder get annotations { 110 | _$this; 111 | return super.annotations; 112 | } 113 | 114 | @override 115 | set annotations(ListBuilder annotations) { 116 | _$this; 117 | super.annotations = annotations; 118 | } 119 | 120 | @override 121 | ListBuilder get docs { 122 | _$this; 123 | return super.docs; 124 | } 125 | 126 | @override 127 | set docs(ListBuilder docs) { 128 | _$this; 129 | super.docs = docs; 130 | } 131 | 132 | @override 133 | ListBuilder get types { 134 | _$this; 135 | return super.types; 136 | } 137 | 138 | @override 139 | set types(ListBuilder types) { 140 | _$this; 141 | super.types = types; 142 | } 143 | 144 | _$TypeDefBuilder() : super._(); 145 | 146 | TypeDefBuilder get _$this { 147 | final $v = _$v; 148 | if ($v != null) { 149 | super.name = $v.name; 150 | super.definition = $v.definition; 151 | super.annotations = $v.annotations.toBuilder(); 152 | super.docs = $v.docs.toBuilder(); 153 | super.types = $v.types.toBuilder(); 154 | _$v = null; 155 | } 156 | return this; 157 | } 158 | 159 | @override 160 | void replace(TypeDef other) { 161 | ArgumentError.checkNotNull(other, 'other'); 162 | _$v = other as _$TypeDef; 163 | } 164 | 165 | @override 166 | void update(void Function(TypeDefBuilder)? updates) { 167 | if (updates != null) updates(this); 168 | } 169 | 170 | @override 171 | TypeDef build() => _build(); 172 | 173 | _$TypeDef _build() { 174 | _$TypeDef _$result; 175 | try { 176 | _$result = _$v ?? 177 | new _$TypeDef._( 178 | name: BuiltValueNullFieldError.checkNotNull( 179 | name, r'TypeDef', 'name'), 180 | definition: BuiltValueNullFieldError.checkNotNull( 181 | definition, r'TypeDef', 'definition'), 182 | annotations: annotations.build(), 183 | docs: docs.build(), 184 | types: types.build()); 185 | } catch (_) { 186 | late String _$failedField; 187 | try { 188 | _$failedField = 'annotations'; 189 | annotations.build(); 190 | _$failedField = 'docs'; 191 | docs.build(); 192 | _$failedField = 'types'; 193 | types.build(); 194 | } catch (e) { 195 | throw new BuiltValueNestedFieldError( 196 | r'TypeDef', _$failedField, e.toString()); 197 | } 198 | rethrow; 199 | } 200 | replace(_$result); 201 | return _$result; 202 | } 203 | } 204 | 205 | // ignore_for_file: deprecated_member_use_from_same_package,type=lint 206 | -------------------------------------------------------------------------------- /lib/src/visitors.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:meta/meta.dart'; 6 | 7 | import 'base.dart'; 8 | import 'specs/class.dart'; 9 | import 'specs/constructor.dart'; 10 | import 'specs/directive.dart'; 11 | import 'specs/enum.dart'; 12 | import 'specs/expression.dart'; 13 | import 'specs/extension.dart'; 14 | import 'specs/extension_type.dart'; 15 | import 'specs/field.dart'; 16 | import 'specs/library.dart'; 17 | import 'specs/method.dart'; 18 | import 'specs/mixin.dart'; 19 | import 'specs/reference.dart'; 20 | import 'specs/type_function.dart'; 21 | import 'specs/type_record.dart'; 22 | import 'specs/type_reference.dart'; 23 | import 'specs/typedef.dart'; 24 | 25 | @optionalTypeArgs 26 | abstract class SpecVisitor { 27 | const SpecVisitor._(); 28 | 29 | T visitAnnotation(Expression spec, [T? context]); 30 | 31 | T visitClass(Class spec, [T? context]); 32 | 33 | T visitMixin(Mixin spec, [T? context]); 34 | 35 | T visitExtension(Extension spec, [T? context]); 36 | 37 | T visitExtensionType(ExtensionType spec, [T? context]); 38 | 39 | T visitEnum(Enum spec, [T? context]); 40 | 41 | T visitConstructor(Constructor spec, String clazz, [T? context]); 42 | 43 | T visitDirective(Directive spec, [T? context]); 44 | 45 | T visitField(Field spec, [T? context]); 46 | 47 | T visitLibrary(Library spec, [T? context]); 48 | 49 | T visitFunctionType(FunctionType spec, [T? context]); 50 | 51 | T visitTypeDef(TypeDef spec, [T? context]); 52 | 53 | T visitMethod(Method spec, [T? context]); 54 | 55 | T visitRecordType(RecordType spec, [T? context]); 56 | 57 | T visitReference(Reference spec, [T? context]); 58 | 59 | T visitSpec(Spec spec, [T? context]); 60 | 61 | T visitType(TypeReference spec, [T? context]); 62 | 63 | T visitTypeParameters(Iterable specs, [T? context]); 64 | } 65 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: code_builder 2 | version: 4.10.1-wip 3 | description: A fluent, builder-based library for generating valid Dart code. 4 | repository: https://github.com/dart-lang/code_builder 5 | 6 | environment: 7 | sdk: ^3.5.0 8 | 9 | dependencies: 10 | built_collection: ^5.0.0 11 | built_value: ^8.0.0 12 | collection: ^1.15.0 13 | matcher: ^0.12.10 14 | meta: ^1.3.0 15 | 16 | dev_dependencies: 17 | build: ^2.0.0 18 | build_runner: ^2.0.3 19 | built_value_generator: ^8.0.0 20 | dart_flutter_team_lints: ^3.0.0 21 | dart_style: ^2.3.7 22 | source_gen: ^1.0.0 23 | test: ^1.16.0 24 | -------------------------------------------------------------------------------- /test/allocator_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import 'common.dart'; 9 | 10 | void main() { 11 | useDartfmt(); 12 | 13 | group('Allocator', () { 14 | Allocator allocator; 15 | 16 | test('should return the exact (non-prefixed) symbol', () { 17 | allocator = Allocator(); 18 | expect(allocator.allocate(refer('Foo', 'package:foo')), 'Foo'); 19 | }); 20 | 21 | test('should collect import URLs', () { 22 | allocator = Allocator() 23 | ..allocate(refer('List', 'dart:core')) 24 | ..allocate(refer('LinkedHashMap', 'dart:collection')) 25 | ..allocate(refer('someSymbol')); 26 | expect(allocator.imports.map((d) => d.url), [ 27 | 'dart:core', 28 | 'dart:collection', 29 | ]); 30 | }); 31 | 32 | test('.none should do nothing', () { 33 | allocator = Allocator.none; 34 | expect(allocator.allocate(refer('Foo', 'package:foo')), 'Foo'); 35 | expect(allocator.imports, isEmpty); 36 | }); 37 | 38 | test('.simplePrefixing should add import prefixes', () { 39 | allocator = Allocator.simplePrefixing(); 40 | expect( 41 | allocator.allocate(refer('List', 'dart:core')), 42 | 'List', 43 | ); 44 | expect( 45 | allocator.allocate(refer('LinkedHashMap', 'dart:collection')), 46 | '_i1.LinkedHashMap', 47 | ); 48 | expect(allocator.imports.map((d) => '${d.url} as ${d.as}'), [ 49 | 'dart:collection as _i1', 50 | ]); 51 | }); 52 | }); 53 | } 54 | -------------------------------------------------------------------------------- /test/common.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:dart_style/dart_style.dart'; 7 | 8 | String _format(String source) { 9 | final formatter = DartFormatter( 10 | languageVersion: DartFormatter.latestLanguageVersion, 11 | ); 12 | 13 | try { 14 | return formatter.format(source); 15 | } on FormatterException catch (_) { 16 | return formatter.formatStatement(source); 17 | } 18 | } 19 | 20 | /// Should be invoked in `main()` of every test in `test/**_test.dart`. 21 | void useDartfmt() => EqualsDart.format = _format; 22 | -------------------------------------------------------------------------------- /test/const_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import 'common.dart'; 9 | 10 | void main() { 11 | useDartfmt(); 12 | 13 | final constMap = literalConstMap({ 14 | 'list': literalConstList([]), 15 | 'duration': refer('Duration').constInstance([]), 16 | }); 17 | 18 | test('expression', () { 19 | expect(constMap, equalsDart(r''' 20 | const {'list': [], 'duration': Duration(), }''')); 21 | }); 22 | 23 | test('assignConst', () { 24 | expect( 25 | // ignore: deprecated_member_use_from_same_package 26 | constMap.assignConst('constField'), 27 | equalsDart(r''' 28 | const constField = {'list': [], 'duration': Duration(), }''', 29 | DartEmitter.scoped()), 30 | ); 31 | }); 32 | 33 | test('assign to declared constant', () { 34 | expect( 35 | declareConst('constField').assign(constMap), 36 | equalsDart(r''' 37 | const constField = {'list': [], 'duration': Duration(), }''', 38 | DartEmitter.scoped()), 39 | ); 40 | }); 41 | 42 | test('assign to declared non-constant', () { 43 | expect( 44 | declareVar('varField').assign(constMap), 45 | equalsDart(r''' 46 | var varField = const {'list': [], 'duration': Duration(), }''', 47 | DartEmitter.scoped())); 48 | }); 49 | 50 | final library = Library((b) => b 51 | ..body.add(Field((b) => b 52 | ..name = 'val1' 53 | ..modifier = FieldModifier.constant 54 | ..assignment = refer('ConstClass').constInstance([]).code)) 55 | ..body.add(Field((b) => b 56 | ..name = 'val2' 57 | ..modifier = FieldModifier.constant 58 | ..assignment = 59 | refer('ConstClass').constInstanceNamed('other', []).code))); 60 | 61 | test('should emit a source file with imports in defined order', () { 62 | expect( 63 | library, 64 | equalsDart(r''' 65 | const val1 = ConstClass(); 66 | const val2 = ConstClass.other();'''), 67 | ); 68 | }); 69 | } 70 | -------------------------------------------------------------------------------- /test/directive_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import 'common.dart'; 9 | 10 | void main() { 11 | useDartfmt(); 12 | 13 | final $LinkedHashMap = refer('LinkedHashMap', 'dart:collection'); 14 | 15 | final library = Library((b) => b 16 | ..directives.add(Directive.export('../relative.dart')) 17 | ..directives.add(Directive.export('package:foo/foo.dart')) 18 | ..directives.add(Directive.part('lib.g.dart')) 19 | ..body.add(Field((b) => b 20 | ..name = 'relativeRef' 21 | ..modifier = FieldModifier.final$ 22 | ..assignment = 23 | refer('Relative', '../relative.dart').newInstance([]).code)) 24 | ..body.add(Field((b) => b 25 | ..name = 'pkgRefFoo' 26 | ..modifier = FieldModifier.final$ 27 | ..assignment = refer('Foo', 'package:foo/foo.dart').newInstance([]).code)) 28 | ..body.add(Field((b) => b 29 | ..name = 'pkgRefBar' 30 | ..modifier = FieldModifier.final$ 31 | ..assignment = refer('Bar', 'package:foo/bar.dart').newInstance([]).code)) 32 | ..body.add(Field((b) => b 33 | ..name = 'collectionRef' 34 | ..modifier = FieldModifier.final$ 35 | ..assignment = $LinkedHashMap.newInstance([]).code))); 36 | 37 | test('should emit a source file with imports in defined order', () { 38 | expect( 39 | library, 40 | equalsDart(r''' 41 | // ignore_for_file: no_leading_underscores_for_library_prefixes 42 | import '../relative.dart' as _i1; 43 | import 'package:foo/foo.dart' as _i2; 44 | import 'package:foo/bar.dart' as _i3; 45 | import 'dart:collection' as _i4; 46 | export '../relative.dart'; 47 | export 'package:foo/foo.dart'; 48 | part 'lib.g.dart'; 49 | 50 | final relativeRef = _i1.Relative(); 51 | final pkgRefFoo = _i2.Foo(); 52 | final pkgRefBar = _i3.Bar(); 53 | final collectionRef = _i4.LinkedHashMap();''', DartEmitter.scoped()), 54 | ); 55 | }); 56 | 57 | test('should emit a source file with ordered', () { 58 | expect( 59 | library, 60 | equalsDart(r''' 61 | // ignore_for_file: no_leading_underscores_for_library_prefixes 62 | import 'dart:collection' as _i4; 63 | 64 | import 'package:foo/bar.dart' as _i3; 65 | import 'package:foo/foo.dart' as _i2; 66 | 67 | import '../relative.dart' as _i1; 68 | 69 | export 'package:foo/foo.dart'; 70 | export '../relative.dart'; 71 | 72 | part 'lib.g.dart'; 73 | 74 | final relativeRef = _i1.Relative(); 75 | final pkgRefFoo = _i2.Foo(); 76 | final pkgRefBar = _i3.Bar(); 77 | final collectionRef = _i4.LinkedHashMap();''', 78 | DartEmitter.scoped(orderDirectives: true)), 79 | ); 80 | }); 81 | } 82 | -------------------------------------------------------------------------------- /test/e2e/injection_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import '../common.dart'; 9 | 10 | void main() { 11 | useDartfmt(); 12 | 13 | test('should generate a complex generated file', () { 14 | // Imports from an existing Dart library. 15 | final $App = refer('App', 'package:app/app.dart'); 16 | final $Module = refer('Module', 'package:app/module.dart'); 17 | final $Thing = refer('Thing', 'package:app/thing.dart'); 18 | 19 | final clazz = ClassBuilder() 20 | ..name = 'Injector' 21 | ..implements.add($App) 22 | ..fields.add(Field((b) => b 23 | ..modifier = FieldModifier.final$ 24 | ..name = '_module' 25 | ..type = $Module.type)) 26 | ..constructors.add(Constructor((b) => b 27 | ..requiredParameters.add(Parameter((b) => b 28 | ..name = '_module' 29 | ..toThis = true)))) 30 | ..methods.add(Method((b) => b 31 | ..name = 'getThing' 32 | ..body = $Thing.newInstance([ 33 | refer('_module').property('get1').call([]), 34 | refer('_module').property('get2').call([]), 35 | ]).code 36 | ..returns = $Thing 37 | ..annotations.add(refer('override')))); 38 | 39 | expect( 40 | clazz.build(), 41 | equalsDart(r''' 42 | class Injector implements App { 43 | Injector(this._module); 44 | 45 | final Module _module; 46 | 47 | @override 48 | Thing getThing() => Thing(_module.get1(), _module.get2(), ); 49 | } 50 | '''), 51 | ); 52 | 53 | expect( 54 | clazz.build(), 55 | equalsDart(r''' 56 | class Injector implements _i1.App { 57 | Injector(this._module); 58 | 59 | final _i2.Module _module; 60 | 61 | @override 62 | _i3.Thing getThing() => _i3.Thing(_module.get1(), _module.get2(), ); 63 | } 64 | ''', DartEmitter(allocator: Allocator.simplePrefixing())), 65 | ); 66 | }); 67 | } 68 | -------------------------------------------------------------------------------- /test/matcher_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | void main() { 9 | test('describes mismatches', () { 10 | const actual = Code('final x=1;'); 11 | equalsDart('final y=2;').expectMismatch(actual, ''' 12 | Expected: final y=2; 13 | Actual: StaticCode: 14 | Which: is different. 15 | Expected: final y=2; 16 | Actual: final x=1; 17 | ^ 18 | Differ at offset 6 19 | '''); 20 | }); 21 | } 22 | 23 | extension on Matcher { 24 | void expectMismatch(dynamic actual, String mismatch) { 25 | expect( 26 | () => expect(actual, this), 27 | throwsA(isA().having( 28 | (e) => e.message, 'message', equalsIgnoringWhitespace(mismatch)))); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /test/specs/code/statement_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import '../../common.dart'; 9 | 10 | void main() { 11 | useDartfmt(); 12 | 13 | test('should emit a block of code', () { 14 | expect( 15 | Block.of([ 16 | const Code('if (foo) {'), 17 | const Code(' print(true);'), 18 | const Code('}'), 19 | ]), 20 | equalsDart(r''' 21 | if (foo) { 22 | print(true); 23 | } 24 | '''), 25 | ); 26 | }); 27 | 28 | test('should emit a block of code including expressions', () { 29 | expect( 30 | Block.of([ 31 | const Code('if (foo) {'), 32 | refer('print')([literalTrue]).statement, 33 | const Code('}'), 34 | ]), 35 | equalsDart(r''' 36 | if (foo) { 37 | print(true); 38 | } 39 | '''), 40 | ); 41 | }); 42 | 43 | test('should emit a block of code with lazily invoked generators', () { 44 | expect( 45 | Method((b) => b 46 | ..name = 'main' 47 | ..body = Block.of([ 48 | const Code('if ('), 49 | lazyCode(() => refer('foo').code), 50 | const Code(') {'), 51 | refer('print')([literalTrue]).statement, 52 | const Code('}'), 53 | ])), 54 | equalsDart(r''' 55 | main() { 56 | if (foo) { 57 | print(true); 58 | } 59 | } 60 | '''), 61 | ); 62 | }); 63 | } 64 | -------------------------------------------------------------------------------- /test/specs/extension_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import '../common.dart'; 9 | 10 | void main() { 11 | useDartfmt(); 12 | 13 | test('should create an extension', () { 14 | expect( 15 | Extension((b) => b 16 | ..name = 'Foo' 17 | ..on = TypeReference((b) => b.symbol = 'Bar')), 18 | equalsDart(r''' 19 | extension Foo on Bar {} 20 | '''), 21 | ); 22 | }); 23 | 24 | test('should create an extension without an identifier', () { 25 | expect( 26 | Extension((b) => b..on = TypeReference((b) => b.symbol = 'Bar')), 27 | equalsDart(r''' 28 | extension on Bar {} 29 | '''), 30 | ); 31 | }); 32 | 33 | test('should create an extension with documentation', () { 34 | expect( 35 | Extension( 36 | (b) => b 37 | ..name = 'Foo' 38 | ..on = TypeReference((b) => b.symbol = 'Bar') 39 | ..docs.addAll( 40 | const [ 41 | '/// My favorite extension.', 42 | ], 43 | ), 44 | ), 45 | equalsDart(r''' 46 | /// My favorite extension. 47 | extension Foo on Bar {} 48 | '''), 49 | ); 50 | }); 51 | 52 | test('should create an extension with annotations', () { 53 | expect( 54 | Extension( 55 | (b) => b 56 | ..name = 'Foo' 57 | ..on = TypeReference((b) => b.symbol = 'Bar') 58 | ..annotations.addAll([ 59 | refer('deprecated'), 60 | refer('Deprecated') 61 | .call([literalString('This is an old extension')]) 62 | ]), 63 | ), 64 | equalsDart(r''' 65 | @deprecated 66 | @Deprecated('This is an old extension') 67 | extension Foo on Bar {} 68 | '''), 69 | ); 70 | }); 71 | 72 | test('should create an extension with a generic type', () { 73 | expect( 74 | Extension((b) => b 75 | ..name = 'Foo' 76 | ..on = TypeReference((b) => b.symbol = 'Bar') 77 | ..types.add(refer('T'))), 78 | equalsDart(r''' 79 | extension Foo on Bar {} 80 | '''), 81 | ); 82 | }); 83 | 84 | test('should create an extension with multiple generic types', () { 85 | expect( 86 | Extension( 87 | (b) => b 88 | ..name = 'Map' 89 | ..on = TypeReference((b) => b.symbol = 'Bar') 90 | ..types.addAll([ 91 | refer('K'), 92 | refer('V'), 93 | ]), 94 | ), 95 | equalsDart(r''' 96 | extension Map on Bar {} 97 | '''), 98 | ); 99 | }); 100 | 101 | test('should create an extension with a bound generic type', () { 102 | expect( 103 | Extension((b) => b 104 | ..name = 'Foo' 105 | ..on = TypeReference((b) => b.symbol = 'Bar') 106 | ..types.add(TypeReference((b) => b 107 | ..symbol = 'T' 108 | ..bound = TypeReference((b) => b 109 | ..symbol = 'Comparable' 110 | ..types.add(refer('T').type))))), 111 | equalsDart(r''' 112 | extension Foo> on Bar {} 113 | '''), 114 | ); 115 | }); 116 | 117 | test('should create an extension with a method', () { 118 | expect( 119 | Extension((b) => b 120 | ..name = 'Foo' 121 | ..on = TypeReference((b) => b.symbol = 'Bar') 122 | ..methods.add(Method((b) => b 123 | ..name = 'parseInt' 124 | ..returns = refer('int') 125 | ..body = Code.scope( 126 | (a) => 'return int.parse(this);', 127 | )))), 128 | equalsDart(r''' 129 | extension Foo on Bar { 130 | int parseInt() { 131 | return int.parse(this); 132 | } 133 | } 134 | '''), 135 | ); 136 | }); 137 | } 138 | -------------------------------------------------------------------------------- /test/specs/extension_type_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import '../common.dart'; 9 | 10 | void main() { 11 | useDartfmt(); 12 | 13 | test('minimum extension type', () { 14 | expect( 15 | ExtensionType((b) => b 16 | ..name = 'Foo' 17 | ..representationDeclaration = RepresentationDeclaration((b) => b 18 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'int') 19 | ..name = 'bar')), 20 | equalsDart(r''' 21 | extension type Foo(int bar) { } 22 | '''), 23 | ); 24 | }); 25 | 26 | test('const extension type', () { 27 | expect( 28 | ExtensionType((b) => b 29 | ..name = 'Foo' 30 | ..constant = true 31 | ..representationDeclaration = RepresentationDeclaration((b) => b 32 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'int') 33 | ..name = 'bar')), 34 | equalsDart(r''' 35 | extension type const Foo(int bar) { } 36 | '''), 37 | ); 38 | }); 39 | 40 | test('extension type with metadata', () { 41 | expect( 42 | ExtensionType((b) => b 43 | ..name = 'Foo' 44 | ..representationDeclaration = RepresentationDeclaration((b) => b 45 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'int') 46 | ..name = 'bar') 47 | ..docs.add( 48 | '/// My favorite extension type.', 49 | ) 50 | ..annotations.addAll([ 51 | refer('deprecated'), 52 | refer('Deprecated') 53 | .call([literalString('This is an old extension type')]) 54 | ])), 55 | equalsDart(r''' 56 | /// My favorite extension type. 57 | @deprecated 58 | @Deprecated('This is an old extension type') 59 | extension type Foo(int bar) { } 60 | '''), 61 | ); 62 | }); 63 | 64 | test('extension type with generics', () { 65 | expect( 66 | ExtensionType((b) => b 67 | ..name = 'Foo' 68 | ..types.addAll([ 69 | TypeReference((b) => b..symbol = 'T'), 70 | TypeReference((b) => b..symbol = 'U') 71 | ]) 72 | ..representationDeclaration = RepresentationDeclaration((b) => b 73 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'T') 74 | ..name = 'bar')), 75 | equalsDart(r''' 76 | extension type Foo(T bar) { } 77 | '''), 78 | ); 79 | }); 80 | 81 | test('extension type with generics bound', () { 82 | expect( 83 | ExtensionType((b) => b 84 | ..name = 'Foo' 85 | ..types.add(TypeReference((b) => b 86 | ..symbol = 'T' 87 | ..bound = TypeReference((b) => b..symbol = 'num'))) 88 | ..representationDeclaration = RepresentationDeclaration((b) => b 89 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'T') 90 | ..name = 'bar')), 91 | equalsDart(r''' 92 | extension type Foo(T bar) { } 93 | '''), 94 | ); 95 | }); 96 | 97 | test('extension type with named primary constructor', () { 98 | expect( 99 | ExtensionType((b) => b 100 | ..name = 'Foo' 101 | ..primaryConstructorName = 'named' 102 | ..representationDeclaration = RepresentationDeclaration((b) => b 103 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'int') 104 | ..name = 'bar')), 105 | equalsDart(r''' 106 | extension type Foo.named(int bar) { } 107 | '''), 108 | ); 109 | }); 110 | 111 | test('extension type with metadata on field', () { 112 | expect( 113 | ExtensionType((b) => b 114 | ..name = 'Foo' 115 | ..representationDeclaration = RepresentationDeclaration((b) => b 116 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'int') 117 | ..name = 'bar' 118 | ..docs.add( 119 | '/// My favorite representation declaration.', 120 | ) 121 | ..annotations.addAll([ 122 | refer('deprecated'), 123 | refer('Deprecated').call( 124 | [literalString('This is an old representation declaration')]) 125 | ]))), 126 | equalsDart(r''' 127 | extension type Foo(/// My favorite representation declaration. 128 | @deprecated 129 | @Deprecated('This is an old representation declaration') 130 | int bar) { } 131 | '''), 132 | ); 133 | }); 134 | 135 | test('extension type with implements', () { 136 | expect( 137 | ExtensionType((b) => b 138 | ..name = 'Foo' 139 | ..implements.add(TypeReference((b) => b.symbol = 'num')) 140 | ..representationDeclaration = RepresentationDeclaration((b) => b 141 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'int') 142 | ..name = 'bar')), 143 | equalsDart(r''' 144 | extension type Foo(int bar) implements num { } 145 | '''), 146 | ); 147 | }); 148 | 149 | test('extension type with multiple implements', () { 150 | expect( 151 | ExtensionType((b) => b 152 | ..name = 'Foo' 153 | ..implements.addAll([ 154 | TypeReference((b) => b.symbol = 'num'), 155 | TypeReference((b) => b.symbol = 'Object') 156 | ]) 157 | ..representationDeclaration = RepresentationDeclaration((b) => b 158 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'int') 159 | ..name = 'bar')), 160 | equalsDart(r''' 161 | extension type Foo(int bar) implements num,Object { } 162 | '''), 163 | ); 164 | }); 165 | 166 | test('extension type with constructors', () { 167 | expect( 168 | ExtensionType((b) => b 169 | ..name = 'Foo' 170 | ..primaryConstructorName = '_' 171 | ..representationDeclaration = RepresentationDeclaration((b) => b 172 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'int') 173 | ..name = 'bar') 174 | ..constructors.addAll([ 175 | Constructor((b) => b.requiredParameters.add(Parameter((b) => b 176 | ..toThis = true 177 | ..name = 'bar'))), 178 | Constructor((b) => b 179 | ..name = 'named' 180 | ..factory = true 181 | ..requiredParameters.add(Parameter((b) => b 182 | ..type = TypeReference((b) => b.symbol = 'int') 183 | ..name = 'baz')) 184 | ..body = const Code('return Foo(baz);')) 185 | ])), 186 | equalsDart(r''' 187 | extension type Foo._(int bar) { 188 | Foo(this.bar); 189 | 190 | factory Foo.named(int baz) { 191 | return Foo(baz); 192 | } 193 | } 194 | '''), 195 | ); 196 | }); 197 | 198 | test('extension type with external field', () { 199 | expect( 200 | ExtensionType((b) => b 201 | ..name = 'Foo' 202 | ..representationDeclaration = RepresentationDeclaration((b) => b 203 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'int') 204 | ..name = 'bar') 205 | ..fields.add(Field((b) => b 206 | ..external = true 207 | ..type = TypeReference((b) => b.symbol = 'int') 208 | ..name = 'property'))), 209 | equalsDart(r''' 210 | extension type Foo(int bar) { 211 | external int property; 212 | } 213 | '''), 214 | ); 215 | }); 216 | 217 | test('extension type with methods', () { 218 | expect( 219 | ExtensionType((b) => b 220 | ..name = 'Foo' 221 | ..representationDeclaration = RepresentationDeclaration((b) => b 222 | ..declaredRepresentationType = TypeReference((b) => b.symbol = 'int') 223 | ..name = 'bar') 224 | ..methods.addAll([ 225 | Method((b) => b 226 | ..type = MethodType.getter 227 | ..returns = TypeReference((b) => b.symbol = 'int') 228 | ..name = 'value' 229 | ..body = const Code('return this.bar;')), 230 | Method((b) => b 231 | ..returns = TypeReference((b) => b.symbol = 'int') 232 | ..name = 'getValue' 233 | ..lambda = true 234 | ..body = const Code('this.bar')) 235 | ])), 236 | equalsDart(r''' 237 | extension type Foo(int bar) { 238 | int get value { return this.bar; } 239 | int getValue() => this.bar; 240 | } 241 | '''), 242 | ); 243 | }); 244 | } 245 | -------------------------------------------------------------------------------- /test/specs/field_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import '../common.dart'; 9 | 10 | void main() { 11 | useDartfmt(); 12 | 13 | test('should create a field', () { 14 | expect( 15 | Field((b) => b..name = 'foo'), 16 | equalsDart(r''' 17 | var foo; 18 | '''), 19 | ); 20 | }); 21 | 22 | test('should create a typed field', () { 23 | expect( 24 | Field((b) => b 25 | ..name = 'foo' 26 | ..type = refer('String')), 27 | equalsDart(r''' 28 | String foo; 29 | '''), 30 | ); 31 | }); 32 | 33 | test('should create a final field', () { 34 | expect( 35 | Field((b) => b 36 | ..name = 'foo' 37 | ..modifier = FieldModifier.final$), 38 | equalsDart(r''' 39 | final foo; 40 | '''), 41 | ); 42 | }); 43 | 44 | test('should create a constant field', () { 45 | expect( 46 | Field((b) => b 47 | ..name = 'foo' 48 | ..modifier = FieldModifier.constant), 49 | equalsDart(r''' 50 | const foo; 51 | '''), 52 | ); 53 | }); 54 | 55 | test('should create a late field if using null-safety', () { 56 | expect( 57 | Field((b) => b 58 | ..late = true 59 | ..name = 'foo'), 60 | equalsDart(r''' 61 | late var foo; 62 | ''', DartEmitter(useNullSafetySyntax: true)), 63 | ); 64 | }); 65 | 66 | test('should not create a late field if not using null-safety', () { 67 | expect( 68 | Field((b) => b 69 | ..late = true 70 | ..name = 'foo'), 71 | equalsDart(r''' 72 | var foo; 73 | '''), 74 | ); 75 | }); 76 | 77 | test('should create a static late field', () { 78 | expect( 79 | Field((b) => b 80 | ..static = true 81 | ..late = true 82 | ..name = 'foo'), 83 | equalsDart(r''' 84 | static late var foo; 85 | ''', DartEmitter(useNullSafetySyntax: true)), 86 | ); 87 | }); 88 | 89 | test('should create a field with an assignment', () { 90 | expect( 91 | Field((b) => b 92 | ..name = 'foo' 93 | ..assignment = const Code('1')), 94 | equalsDart(r''' 95 | var foo = 1; 96 | '''), 97 | ); 98 | }); 99 | 100 | test('should create a external field', () { 101 | expect( 102 | Field((b) => b 103 | ..name = 'value' 104 | ..external = true 105 | ..type = refer('double') 106 | ..annotations.addAll([refer('Float').call([])])), 107 | equalsDart(r''' 108 | @Float() 109 | external double value; 110 | '''), 111 | ); 112 | }); 113 | } 114 | -------------------------------------------------------------------------------- /test/specs/library_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import '../common.dart'; 9 | 10 | void main() { 11 | useDartfmt(); 12 | 13 | group('File', () { 14 | final $LinkedHashMap = refer('LinkedHashMap', 'dart:collection'); 15 | 16 | test('should emit a source file with leading line comments', () { 17 | expect( 18 | Library( 19 | (b) => b 20 | ..comments.add('Generated by foo.') 21 | ..body.add( 22 | Class((b) => b..name = 'Foo'), 23 | ), 24 | ), 25 | equalsDart(r''' 26 | // Generated by foo. 27 | 28 | class Foo { } 29 | ''', DartEmitter(allocator: Allocator())), 30 | ); 31 | }); 32 | 33 | test('should emit a source file with multiple leading comments', () { 34 | expect( 35 | Library( 36 | (b) => b 37 | ..comments.addAll([ 38 | 'Generated by foo!', 39 | '', 40 | 'Avoid editing by hand.', 41 | ]) 42 | ..body.add( 43 | Class((b) => b..name = 'Foo'), 44 | ), 45 | ), 46 | equalsDart(r''' 47 | // Generated by foo! 48 | // 49 | // Avoid editing by hand. 50 | 51 | class Foo { } 52 | ''', DartEmitter(allocator: Allocator())), 53 | ); 54 | }); 55 | 56 | test('should emit a source file with a generated-by comment', () { 57 | expect( 58 | Library( 59 | (b) => b 60 | ..generatedByComment = 'Generated by fooBar.' 61 | ..body.add( 62 | Class((b) => b..name = 'Foo'), 63 | ), 64 | ), 65 | equalsDart(r''' 66 | // Generated by fooBar. 67 | 68 | class Foo { } 69 | ''', DartEmitter(allocator: Allocator())), 70 | ); 71 | }); 72 | 73 | test('should emit a source file with ignore comments', () { 74 | expect( 75 | Library( 76 | (b) => b 77 | ..ignoreForFile.add('sort_constructors_first') 78 | ..body.add( 79 | Class((b) => b..name = 'Foo'), 80 | ), 81 | ), 82 | equalsDart(r''' 83 | // ignore_for_file: sort_constructors_first 84 | 85 | class Foo { } 86 | ''', DartEmitter(allocator: Allocator())), 87 | ); 88 | }); 89 | 90 | test('should emit a source file with multiple, sorted ignore comments', () { 91 | expect( 92 | Library( 93 | (b) => b 94 | ..ignoreForFile.addAll([ 95 | 'type=lint', 96 | 'sort_constructors_first', 97 | 'implementation_imports', 98 | 'file_names', 99 | ]) 100 | ..body.add( 101 | Class((b) => b..name = 'Foo'), 102 | ), 103 | ), 104 | equalsDart(r''' 105 | // ignore_for_file: file_names, implementation_imports, sort_constructors_first 106 | // ignore_for_file: type=lint 107 | 108 | class Foo { } 109 | ''', DartEmitter(allocator: Allocator())), 110 | ); 111 | }); 112 | 113 | test('should emit with line comments, generated-by, and ignore-for-file', 114 | () { 115 | expect( 116 | Library( 117 | (b) => b 118 | ..comments.add('Generic copyright statement.') 119 | ..generatedByComment = 'Generated by fooBar.' 120 | ..ignoreForFile.add('sort_constructors_first') 121 | ..body.add( 122 | Class((b) => b..name = 'Foo'), 123 | ), 124 | ), 125 | equalsDart(r''' 126 | // Generic copyright statement. 127 | 128 | // Generated by fooBar. 129 | 130 | // ignore_for_file: sort_constructors_first 131 | 132 | class Foo { } 133 | ''', DartEmitter(allocator: Allocator())), 134 | ); 135 | }); 136 | 137 | test('should emit a source file with manual imports', () { 138 | expect( 139 | Library((b) => b 140 | ..directives.add(Directive.import('dart:collection')) 141 | ..body.add(Field((b) => b 142 | ..name = 'test' 143 | ..modifier = FieldModifier.final$ 144 | ..assignment = $LinkedHashMap.newInstance([]).code))), 145 | equalsDart(r''' 146 | import 'dart:collection'; 147 | 148 | final test = LinkedHashMap(); 149 | ''', DartEmitter()), 150 | ); 151 | }); 152 | 153 | test('should emit a source file with a deferred import', () { 154 | expect( 155 | Library( 156 | (b) => b 157 | ..directives.add( 158 | Directive.importDeferredAs( 159 | 'package:foo/foo.dart', 160 | 'foo', 161 | ), 162 | ), 163 | ), 164 | equalsDart(r''' 165 | import 'package:foo/foo.dart' deferred as foo; 166 | '''), 167 | ); 168 | }); 169 | 170 | test('should emit a source file with a "show" combinator', () { 171 | expect( 172 | Library( 173 | (b) => b 174 | ..directives.add( 175 | Directive.import( 176 | 'package:foo/foo.dart', 177 | show: ['Foo', 'Bar'], 178 | ), 179 | ), 180 | ), 181 | equalsDart(r''' 182 | import 'package:foo/foo.dart' show Foo, Bar; 183 | '''), 184 | ); 185 | }); 186 | 187 | test('should emit a source file with a "hide" combinator', () { 188 | expect( 189 | Library( 190 | (b) => b 191 | ..directives.add( 192 | Directive.import( 193 | 'package:foo/foo.dart', 194 | hide: ['Foo', 'Bar'], 195 | ), 196 | ), 197 | ), 198 | equalsDart(r''' 199 | import 'package:foo/foo.dart' hide Foo, Bar; 200 | '''), 201 | ); 202 | }); 203 | 204 | test('should emit a source file with allocation', () { 205 | expect( 206 | Library((b) => b 207 | ..body.add(Field((b) => b 208 | ..name = 'test' 209 | ..modifier = FieldModifier.final$ 210 | ..assignment = Code.scope((a) => '${a($LinkedHashMap)}()')))), 211 | equalsDart(r''' 212 | import 'dart:collection'; 213 | 214 | final test = LinkedHashMap(); 215 | ''', DartEmitter(allocator: Allocator())), 216 | ); 217 | }); 218 | 219 | test('should emit a source file with allocation + prefixing', () { 220 | expect( 221 | Library((b) => b 222 | ..body.add(Field((b) => b 223 | ..name = 'test' 224 | ..modifier = FieldModifier.final$ 225 | ..assignment = Code.scope((a) => '${a($LinkedHashMap)}()')))), 226 | equalsDart(r''' 227 | // ignore_for_file: no_leading_underscores_for_library_prefixes 228 | import 'dart:collection' as _i1; 229 | 230 | final test = _i1.LinkedHashMap(); 231 | ''', DartEmitter(allocator: Allocator.simplePrefixing())), 232 | ); 233 | }); 234 | 235 | test('should emit a source file with part directives', () { 236 | expect( 237 | Library( 238 | (b) => b 239 | ..directives.add( 240 | Directive.part('test.g.dart'), 241 | ), 242 | ), 243 | equalsDart(r''' 244 | part 'test.g.dart'; 245 | ''', DartEmitter()), 246 | ); 247 | }); 248 | 249 | test('should emit a source file with part of directives', () { 250 | expect( 251 | Library( 252 | (b) => b 253 | ..directives.add( 254 | Directive.partOf('test.dart'), 255 | ), 256 | ), 257 | equalsDart(r''' 258 | part of 'test.dart'; 259 | ''', DartEmitter()), 260 | ); 261 | }); 262 | 263 | test('should emit a source file with annotations', () { 264 | expect( 265 | Library( 266 | (b) => b 267 | ..name = 'js_interop' 268 | ..annotations.add( 269 | refer('JS', 'package:js/js.dart').call([]), 270 | ), 271 | ), 272 | equalsDart(r''' 273 | @JS() 274 | library js_interop; 275 | import 'package:js/js.dart'; 276 | ''', DartEmitter(allocator: Allocator())), 277 | ); 278 | }); 279 | 280 | test('should emit an unnamed library source file with annotations', () { 281 | expect( 282 | Library( 283 | (b) => b 284 | ..annotations.add( 285 | refer('JS', 'package:js/js.dart').call([]), 286 | ), 287 | ), 288 | equalsDart(r''' 289 | @JS() 290 | library; 291 | import 'package:js/js.dart'; 292 | ''', DartEmitter(allocator: Allocator())), 293 | ); 294 | }); 295 | 296 | test('should emit an unnamed library source file with documentation', () { 297 | expect( 298 | Library( 299 | (b) => b 300 | ..docs.addAll( 301 | const [ 302 | '/// My favorite library.', 303 | ], 304 | ), 305 | ), 306 | equalsDart(r''' 307 | /// My favorite library. 308 | library; 309 | '''), 310 | ); 311 | }); 312 | }); 313 | } 314 | -------------------------------------------------------------------------------- /test/specs/mixin_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import '../common.dart'; 9 | 10 | void main() { 11 | useDartfmt(); 12 | 13 | test('should create a mixin', () { 14 | expect( 15 | Mixin((b) => b..name = 'Foo'), 16 | equalsDart(r''' 17 | mixin Foo {} 18 | '''), 19 | ); 20 | }); 21 | 22 | test('should create a base mixin', () { 23 | expect( 24 | Mixin((b) => b 25 | ..name = 'Foo' 26 | ..base = true), 27 | equalsDart(r''' 28 | base mixin Foo {} 29 | '''), 30 | ); 31 | }); 32 | 33 | test('should create a mixin with documentations', () { 34 | expect( 35 | Mixin( 36 | (b) => b 37 | ..name = 'Foo' 38 | ..docs.addAll( 39 | const [ 40 | '/// My favorite mixin.', 41 | ], 42 | ), 43 | ), 44 | equalsDart(r''' 45 | /// My favorite mixin. 46 | mixin Foo {} 47 | '''), 48 | ); 49 | }); 50 | 51 | test('should create a mixin with annotations', () { 52 | expect( 53 | Mixin( 54 | (b) => b 55 | ..name = 'Foo' 56 | ..annotations.addAll([ 57 | refer('deprecated'), 58 | refer('Deprecated').call([literalString('This is an old mixin')]) 59 | ]), 60 | ), 61 | equalsDart(r''' 62 | @deprecated 63 | @Deprecated('This is an old mixin') 64 | mixin Foo {} 65 | '''), 66 | ); 67 | }); 68 | 69 | test('should create a mixin with a generic type', () { 70 | expect( 71 | Mixin((b) => b 72 | ..name = 'List' 73 | ..types.add(refer('T'))), 74 | equalsDart(r''' 75 | mixin List {} 76 | '''), 77 | ); 78 | }); 79 | 80 | test('should create a mixin with multiple generic types', () { 81 | expect( 82 | Mixin( 83 | (b) => b 84 | ..name = 'Map' 85 | ..types.addAll([ 86 | refer('K'), 87 | refer('V'), 88 | ]), 89 | ), 90 | equalsDart(r''' 91 | mixin Map {} 92 | '''), 93 | ); 94 | }); 95 | 96 | test('should create a mixin with a bound generic type', () { 97 | expect( 98 | Mixin((b) => b 99 | ..name = 'Comparable' 100 | ..types.add(TypeReference((b) => b 101 | ..symbol = 'T' 102 | ..bound = TypeReference((b) => b 103 | ..symbol = 'Comparable' 104 | ..types.add(refer('T').type))))), 105 | equalsDart(r''' 106 | mixin Comparable> {} 107 | '''), 108 | ); 109 | }); 110 | 111 | test('should create a mixin on another mixin', () { 112 | expect( 113 | Mixin((b) => b 114 | ..name = 'Foo' 115 | ..on = TypeReference((b) => b.symbol = 'Bar')), 116 | equalsDart(r''' 117 | mixin Foo on Bar {} 118 | '''), 119 | ); 120 | }); 121 | 122 | test('should create a mixin implementing another mixin', () { 123 | expect( 124 | Mixin((b) => b 125 | ..name = 'Foo' 126 | ..on = TypeReference((b) => b.symbol = 'Bar') 127 | ..implements.add(TypeReference((b) => b.symbol = 'Foo'))), 128 | equalsDart(r''' 129 | mixin Foo on Bar implements Foo {} 130 | '''), 131 | ); 132 | }); 133 | 134 | test('should create a mixin with a method', () { 135 | expect( 136 | Mixin((b) => b 137 | ..name = 'Foo' 138 | ..methods.add(Method((b) => b 139 | ..name = 'foo' 140 | ..body = const Code('return 1+ 2;')))), 141 | equalsDart(r''' 142 | mixin Foo { 143 | foo() { 144 | return 1 + 2; 145 | } 146 | } 147 | '''), 148 | ); 149 | }); 150 | } 151 | -------------------------------------------------------------------------------- /test/specs/record_type_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import '../common.dart'; 9 | 10 | void main() { 11 | late DartEmitter emitter; 12 | 13 | useDartfmt(); 14 | 15 | setUp(() => emitter = DartEmitter.scoped(useNullSafetySyntax: true)); 16 | 17 | final intRef = TypeReference((b) => b.symbol = 'int'); 18 | TypeReference listRef(TypeReference argType) => TypeReference((b) => b 19 | ..symbol = 'List' 20 | ..types.add(argType)); 21 | 22 | test('should create an empty record type', () { 23 | expect(RecordType(), equalsDart('()', emitter)); 24 | }); 25 | 26 | test('should create a record type with positional fields', () { 27 | expect( 28 | RecordType((b) => b 29 | ..positionalFieldTypes.addAll( 30 | [intRef, listRef(intRef).rebuild((b) => b..isNullable = true)]) 31 | ..isNullable = true), 32 | equalsDart('(int, List?)?', emitter), 33 | ); 34 | }); 35 | 36 | test('should create a record type with one positional field', () { 37 | expect(RecordType((b) => b..positionalFieldTypes.add(intRef)), 38 | equalsDart('(int,)', emitter)); 39 | }); 40 | 41 | test('should create a record type with named fields', () { 42 | expect( 43 | RecordType((b) => b 44 | ..namedFieldTypes.addAll({ 45 | 'named': intRef, 46 | 'other': listRef(intRef), 47 | })), 48 | equalsDart('({int named, List other})', emitter)); 49 | }); 50 | 51 | test('should create a record type with both positional and named fields', () { 52 | expect( 53 | RecordType((b) => b 54 | ..positionalFieldTypes.add(listRef(intRef)) 55 | ..namedFieldTypes.addAll({'named': intRef}) 56 | ..isNullable = true), 57 | equalsDart('(List, {int named})?', emitter)); 58 | }); 59 | 60 | test('should create a nested record type', () { 61 | expect( 62 | RecordType((b) => b 63 | ..positionalFieldTypes.add(RecordType((b) => b 64 | ..namedFieldTypes.addAll({'named': intRef, 'other': intRef}) 65 | ..isNullable = true))), 66 | equalsDart('(({int named, int other})?,)', emitter)); 67 | }); 68 | } 69 | -------------------------------------------------------------------------------- /test/specs/type_reference_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file 2 | // for details. All rights reserved. Use of this source code is governed by a 3 | // BSD-style license that can be found in the LICENSE file. 4 | 5 | import 'package:code_builder/code_builder.dart'; 6 | import 'package:test/test.dart'; 7 | 8 | import '../common.dart'; 9 | 10 | void main() { 11 | useDartfmt(); 12 | 13 | test('should create a nullable type in a pre-Null Safety library', () { 14 | expect( 15 | TypeReference((b) => b 16 | ..symbol = 'Foo' 17 | ..isNullable = true), 18 | equalsDart(r''' 19 | Foo 20 | '''), 21 | ); 22 | }); 23 | 24 | group('in a Null Safety library', () { 25 | late DartEmitter emitter; 26 | 27 | setUp(() => emitter = DartEmitter.scoped(useNullSafetySyntax: true)); 28 | 29 | test('should create a nullable type', () { 30 | expect( 31 | TypeReference((b) => b 32 | ..symbol = 'Foo' 33 | ..isNullable = true), 34 | equalsDart(r'Foo?', emitter), 35 | ); 36 | }); 37 | 38 | test('should create a non-nullable type', () { 39 | expect( 40 | TypeReference((b) => b.symbol = 'Foo'), 41 | equalsDart(r'Foo', emitter), 42 | ); 43 | }); 44 | 45 | test('should create a type with nullable type arguments', () { 46 | expect( 47 | TypeReference((b) => b 48 | ..symbol = 'List' 49 | ..types.add(TypeReference((b) => b 50 | ..symbol = 'int' 51 | ..isNullable = true))), 52 | equalsDart(r'List', emitter), 53 | ); 54 | }); 55 | }); 56 | } 57 | -------------------------------------------------------------------------------- /tool/regenerate.sh: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file 2 | # for details. All rights reserved. Use of this source code is governed by a 3 | # BSD-style license that can be found in the LICENSE file. 4 | 5 | dart run build_runner generate-build-script 6 | dart compile kernel .dart_tool/build/entrypoint/build.dart 7 | dart .dart_tool/build/entrypoint/build.dill build --delete-conflicting-outputs 8 | --------------------------------------------------------------------------------