Skip to content

Commit

Permalink
feat: More directives support (#15)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bdlukaa authored Dec 24, 2023
1 parent 4df7bd3 commit c6f5b00
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 4 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down
15 changes: 12 additions & 3 deletions lib/src/compilation_unit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> serializeCompilationUnit(CompilationUnit unit) {
Expand Down Expand Up @@ -41,11 +43,18 @@ Map<String, dynamic> serializeCompilationUnit(CompilationUnit unit) {
late Map<String, dynamic> 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}');
}
Expand Down
10 changes: 10 additions & 0 deletions lib/src/import_directive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ Map<String, dynamic> serializeImportDirective(ImportDirective import_) {
'hide': hides.isEmpty ? null : hides,
});
}

Map<String, dynamic> serializeAugmentationImportDirective(
AugmentationImportDirective import_,
) {
return filterMap(<String, dynamic>{
'kind': 'import',
'uri': import_.uri.stringValue,
'augmentation': true,
});
}
19 changes: 19 additions & 0 deletions lib/src/library_directive.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:dartdoc_json/src/utils.dart';

Map<String, dynamic> serializeLibraryDirective(LibraryDirective library) {
return filterMap(<String, dynamic>{
'kind': 'library',
'name': library.name2?.name,
});
}

Map<String, dynamic> serializeLibraryAugmentationDirective(
LibraryAugmentationDirective libraryAgumentation,
) {
return filterMap(<String, dynamic>{
'kind': 'library',
'augmentation': true,
'uri': libraryAgumentation.uri,
});
}
17 changes: 17 additions & 0 deletions lib/src/part_directive.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:dartdoc_json/src/utils.dart';

Map<String, dynamic> serializePartDirective(PartDirective part) {
return filterMap(<String, dynamic>{
'kind': 'part',
'uri': part.uri.stringValue,
});
}

Map<String, dynamic> serializePartOfDirective(PartOfDirective partOf) {
return filterMap(<String, dynamic>{
'kind': 'part-of',
'uri': partOf.uri?.stringValue,
'name': partOf.libraryName?.name,
});
}
14 changes: 14 additions & 0 deletions test/library_directive_test.dart
Original file line number Diff line number Diff line change
@@ -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'},
);
});
});
}
45 changes: 45 additions & 0 deletions test/part_directive_test.dart
Original file line number Diff line number Diff line change
@@ -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'},
);
});
});
}

0 comments on commit c6f5b00

Please sign in to comment.