Skip to content

Commit

Permalink
Inject macro implementation into analyzer and CFE, test it.
Browse files Browse the repository at this point in the history
The "macros" are just hardcoded for now.
  • Loading branch information
davidmorgan committed Jul 18, 2024
1 parent f476bb1 commit 55da478
Show file tree
Hide file tree
Showing 21 changed files with 901 additions and 16 deletions.
353 changes: 337 additions & 16 deletions .github/workflows/dart.yml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkgs/_analyzer_macros/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:dart_flutter_team_lints/analysis_options.yaml
15 changes: 15 additions & 0 deletions pkgs/_analyzer_macros/mono_pkg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sdk:
- pubspec
- dev

stages:
- analyze_and_format:
- analyze: --fatal-infos .
- format:
sdk:
- dev
- unit_test:
- test: --test-randomize-ordering-seed=random
os:
- linux
- windows
51 changes: 51 additions & 0 deletions pkgs/_analyzer_macros/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: _analyzer_macros
publish-to: none
description: Macro support for the analyzer.
resolution: workspace

environment:
sdk: ^3.5.0-259.0.dev

dev_dependencies:
_macros: any
analyzer: any
dart_flutter_team_lints: ^3.0.0
macros: any
test: ^1.25.0

dependency_overrides:
_fe_analyzer_shared:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/_fe_analyzer_shared
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
_macros:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/_macros
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
analyzer:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/analyzer
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
analyzer_utilities:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/analyzer_utilities
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
heap_snapshot:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/heap_snapshot
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
linter:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/linter
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
vm_service:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/vm_service
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
146 changes: 146 additions & 0 deletions pkgs/_analyzer_macros/test/analyzer_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:_macros/src/executor.dart';
import 'package:_macros/src/executor/serialization.dart';
import 'package:analyzer/dart/analysis/analysis_context.dart';
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/src/summary2/macro_injected_impl.dart';
import 'package:macros/macros.dart';
import 'package:macros/src/executor.dart';
import 'package:test/test.dart';

void main() {
late AnalysisContext analysisContext;
late TestMacroPackageConfigs packageConfigs;
late TestMacroRunner runner;

group('analyzer with injected macro implementation', () {
setUp(() {
// Set up analyzer.
final directory = Directory('test/package_under_test');
final contextCollection =
AnalysisContextCollection(includedPaths: [directory.absolute.path]);
analysisContext = contextCollection.contexts.first;

// Inject test macro implementation.
packageConfigs = TestMacroPackageConfigs();
runner = TestMacroRunner();
macroImplementation =
MacroImplementation(packageConfigs: packageConfigs, runner: runner);
});

test('discovers macros, runs them, applies augmentations', () async {
final path = File('test/package_under_test/lib/apply_declare_x.dart')
.absolute
.path;

// No analysis errors.
final errors =
await analysisContext.currentSession.getErrors(path) as ErrorsResult;
expect(errors.errors, isEmpty);

// Macro was found by the analyzer and run.
expect(packageConfigs.macroWasFound, true);
expect(runner.macroWasRun, true);

// The expected new declaration augmentation was applied.
final resolvedLibrary = (await analysisContext.currentSession
.getResolvedLibrary(path)) as ResolvedLibraryResult;
final clazz = resolvedLibrary.element.getClass('ClassWithMacroApplied')!;
expect(clazz.fields, isEmpty);
expect(clazz.augmented.fields, isNotEmpty);
expect(clazz.augmented.fields.single.name, 'x');
});
});
}

class TestMacroPackageConfigs implements MacroPackageConfigs {
bool macroWasFound = false;

@override
bool isMacro(Uri uri, String name) {
final result =
uri.toString() == 'package:_test_macros/declare_x_macro.dart' &&
name == 'DeclareX';
if (result) {
macroWasFound = true;
}
return result;
}
}

class TestMacroRunner implements MacroRunner {
bool macroWasRun = false;

@override
RunningMacro run(Uri uri, String name) {
macroWasRun = true;
return TestRunningMacro();
}
}

class TestRunningMacro implements RunningMacro {
@override
Future<MacroExecutionResult> executeDeclarationsPhase(MacroTarget target,
DeclarationPhaseIntrospector declarationsPhaseIntrospector) async {
return TestMacroExecutionResult(typeAugmentations: {
(target as Declaration).identifier: [
DeclarationCode.fromParts(['int get x => 3;'])
],
});
}

@override
Future<MacroExecutionResult> executeDefinitionsPhase(MacroTarget target,
DefinitionPhaseIntrospector definitionPhaseIntrospector) async {
return TestMacroExecutionResult();
}

@override
Future<MacroExecutionResult> executeTypesPhase(
MacroTarget target, TypePhaseIntrospector typePhaseIntrospector) async {
return TestMacroExecutionResult();
}
}

class TestMacroExecutionResult implements MacroExecutionResult {
@override
List<Diagnostic> get diagnostics => [];

@override
Map<Identifier, Iterable<DeclarationCode>> get enumValueAugmentations => {};

@override
MacroException? get exception => null;

@override
Map<Identifier, NamedTypeAnnotationCode> get extendsTypeAugmentations => {};

@override
Map<Identifier, Iterable<TypeAnnotationCode>> get interfaceAugmentations =>
{};

@override
Iterable<DeclarationCode> get libraryAugmentations => {};

@override
Map<Identifier, Iterable<TypeAnnotationCode>> get mixinAugmentations => {};

@override
Iterable<String> get newTypeNames => [];

@override
void serialize(Serializer serializer) {}

@override
Map<Identifier, Iterable<DeclarationCode>> typeAugmentations;

TestMacroExecutionResult(
{Map<Identifier, Iterable<DeclarationCode>>? typeAugmentations})
: typeAugmentations = typeAugmentations ?? {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
analyzer:
enable-experiment:
- macros
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:_test_macros/declare_x_macro.dart';

@DeclareX()
class ClassWithMacroApplied {}
9 changes: 9 additions & 0 deletions pkgs/_analyzer_macros/test/package_under_test/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: package_under_test
publish-to: none
resolution: workspace

environment:
sdk: ^3.5.0-259.0.dev

dependencies:
_test_macros: any
1 change: 1 addition & 0 deletions pkgs/_cfe_macros/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:dart_flutter_team_lints/analysis_options.yaml
15 changes: 15 additions & 0 deletions pkgs/_cfe_macros/mono_pkg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sdk:
- pubspec
- dev

stages:
- analyze_and_format:
- analyze: --fatal-infos .
- format:
sdk:
- dev
- unit_test:
- test: --test-randomize-ordering-seed=random
os:
- linux
- windows
91 changes: 91 additions & 0 deletions pkgs/_cfe_macros/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: _cfe_macros
publish-to: none
description: Macro support for the CFE.
resolution: workspace

environment:
sdk: ^3.5.0-259.0.dev

dev_dependencies:
dart_flutter_team_lints: ^3.0.0
front_end: any
frontend_server: any
macros: ^0.1.2-main.4
test: ^1.25.0

dependency_overrides:
_js_interop_checks:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/_js_interop_checks
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
build_integration:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/build_integration
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
compiler:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/compiler
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
dart2js_info:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/dart2js_info
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
dart2wasm:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/dart2wasm
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
dev_compiler:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/dev_compiler
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
front_end:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/front_end
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
frontend_server:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/frontend_server
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
js_ast:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/js_ast
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
js_runtime:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/js_runtime
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
js_shared:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/js_shared
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
kernel:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/kernel
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
mmap:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/mmap
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
vm:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/vm
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
wasm_builder:
git:
url: https://github.com/dart-lang/sdk.git
path: pkg/wasm_builder
ref: 9664f0da3547ccd92cf56d2bf362e333edfefd92
Loading

0 comments on commit 55da478

Please sign in to comment.