From c6f5b008177893124e1a00366c0685dcdf19024a Mon Sep 17 00:00:00 2001 From: Bruno D'Luka <45696119+bdlukaa@users.noreply.github.com> Date: Sun, 24 Dec 2023 17:24:35 -0300 Subject: [PATCH] feat: More directives support (#15) * feat: part directive * feat: library directive * feat: library augmentation * chore: update README * feat: support augmentation import * feat(test): part and part-of * feat(test): library * chore: format * chore: remove augment test * chore: format --- README.md | 9 ++++++- lib/src/compilation_unit.dart | 15 ++++++++--- lib/src/import_directive.dart | 10 +++++++ lib/src/library_directive.dart | 19 ++++++++++++++ lib/src/part_directive.dart | 17 ++++++++++++ test/library_directive_test.dart | 14 ++++++++++ test/part_directive_test.dart | 45 ++++++++++++++++++++++++++++++++ 7 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 lib/src/library_directive.dart create mode 100644 lib/src/part_directive.dart create mode 100644 test/library_directive_test.dart create mode 100644 test/part_directive_test.dart diff --git a/README.md b/README.md index ce31297..d852616 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ compilationUnit = { directive = { // What kind of directive this is - "kind": "import" | "export", + "kind": "import" | "export" | "part" | "part-of" | "library", // The name of the file being imported or exported "uri": string, @@ -46,6 +46,13 @@ directive = { // For "import" statements, the ID after the `as` keyword "as": string, + + // For "library" statements, the name of the library + // For "part-of" statements, the name of the library that the file is a part of + "name": string, + + // Whether the directive references an augmentation library + "augmentation": bool, } declaration = { diff --git a/lib/src/compilation_unit.dart b/lib/src/compilation_unit.dart index 01d0a00..8d15595 100644 --- a/lib/src/compilation_unit.dart +++ b/lib/src/compilation_unit.dart @@ -6,7 +6,9 @@ import 'package:dartdoc_json/src/extension_declaration.dart'; import 'package:dartdoc_json/src/function_declaration.dart'; import 'package:dartdoc_json/src/generic_type_alias.dart'; import 'package:dartdoc_json/src/import_directive.dart'; +import 'package:dartdoc_json/src/library_directive.dart'; import 'package:dartdoc_json/src/mixin_declaration.dart'; +import 'package:dartdoc_json/src/part_directive.dart'; import 'package:dartdoc_json/src/top_level_variable_declaration.dart'; Map serializeCompilationUnit(CompilationUnit unit) { @@ -41,11 +43,18 @@ Map serializeCompilationUnit(CompilationUnit unit) { late Map serialized; if (directive is ImportDirective) { serialized = serializeImportDirective(directive); + } else if (directive is AugmentationImportDirective) { + serialized = serializeAugmentationImportDirective(directive); } else if (directive is ExportDirective) { serialized = serializeExportDirective(directive); - // } else if (directive is LibraryDirective) { - // } else if (directive is PartDirective) { - // } else if (directive is PartOfDirective) { + } else if (directive is LibraryDirective) { + serialized = serializeLibraryDirective(directive); + } else if (directive is LibraryAugmentationDirective) { + serialized = serializeLibraryAugmentationDirective(directive); + } else if (directive is PartDirective) { + serialized = serializePartDirective(directive); + } else if (directive is PartOfDirective) { + serialized = serializePartOfDirective(directive); } else { throw AssertionError('Unknown directive type: ${directive.runtimeType}'); } diff --git a/lib/src/import_directive.dart b/lib/src/import_directive.dart index e6a7513..2028ae4 100644 --- a/lib/src/import_directive.dart +++ b/lib/src/import_directive.dart @@ -14,3 +14,13 @@ Map serializeImportDirective(ImportDirective import_) { 'hide': hides.isEmpty ? null : hides, }); } + +Map serializeAugmentationImportDirective( + AugmentationImportDirective import_, +) { + return filterMap({ + 'kind': 'import', + 'uri': import_.uri.stringValue, + 'augmentation': true, + }); +} diff --git a/lib/src/library_directive.dart b/lib/src/library_directive.dart new file mode 100644 index 0000000..5d81285 --- /dev/null +++ b/lib/src/library_directive.dart @@ -0,0 +1,19 @@ +import 'package:analyzer/dart/ast/ast.dart'; +import 'package:dartdoc_json/src/utils.dart'; + +Map serializeLibraryDirective(LibraryDirective library) { + return filterMap({ + 'kind': 'library', + 'name': library.name2?.name, + }); +} + +Map serializeLibraryAugmentationDirective( + LibraryAugmentationDirective libraryAgumentation, +) { + return filterMap({ + 'kind': 'library', + 'augmentation': true, + 'uri': libraryAgumentation.uri, + }); +} diff --git a/lib/src/part_directive.dart b/lib/src/part_directive.dart new file mode 100644 index 0000000..4407fe9 --- /dev/null +++ b/lib/src/part_directive.dart @@ -0,0 +1,17 @@ +import 'package:analyzer/dart/ast/ast.dart'; +import 'package:dartdoc_json/src/utils.dart'; + +Map serializePartDirective(PartDirective part) { + return filterMap({ + 'kind': 'part', + 'uri': part.uri.stringValue, + }); +} + +Map serializePartOfDirective(PartOfDirective partOf) { + return filterMap({ + 'kind': 'part-of', + 'uri': partOf.uri?.stringValue, + 'name': partOf.libraryName?.name, + }); +} diff --git a/test/library_directive_test.dart b/test/library_directive_test.dart new file mode 100644 index 0000000..314754b --- /dev/null +++ b/test/library_directive_test.dart @@ -0,0 +1,14 @@ +import 'package:test/test.dart'; + +import 'utils.dart'; + +void main() { + group('LibraryDirective', () { + test('simple library', () { + expect( + parseAsJson('library foo;'), + {'kind': 'library', 'name': 'foo'}, + ); + }); + }); +} diff --git a/test/part_directive_test.dart b/test/part_directive_test.dart new file mode 100644 index 0000000..773cf82 --- /dev/null +++ b/test/part_directive_test.dart @@ -0,0 +1,45 @@ +import 'package:test/test.dart'; + +import 'utils.dart'; + +void main() { + group('PartDirective', () { + test('simple part', () { + expect( + parseAsJson('part "foo.dart";'), + {'kind': 'part', 'uri': 'foo.dart'}, + ); + }); + + test('multiple part', () { + expect( + parseAsJson(''' + part 'foo.dart'; + part 'boo.dart'; + '''), + [ + {'kind': 'part', 'uri': 'foo.dart'}, + {'kind': 'part', 'uri': 'boo.dart'}, + ], + ); + }); + }); + + group('PartOfDirective', () { + test('simple part', () { + expect( + parseAsJson('part of "foo.dart";'), + {'kind': 'part-of', 'uri': 'foo.dart'}, + ); + }); + + test('part of library', () { + expect( + parseAsJson(''' + part of foo; + '''), + {'kind': 'part-of', 'name': 'foo'}, + ); + }); + }); +}