Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.6.0

- Add support for extension types
- Add support for class type aliases (`class C = B with M;`)
- Add support for the `base`, `interface`, `final`, `sealed` and `mixin class` modifiers

## 0.5.0

- Remove Analyzer deprecations
Expand Down
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ declaration = {
// What kind of declaration this is. Note that some declarations may only
// appear at the top level of the file, while others are nested within
// top-level declarations.
"kind": "class" | "mixin" | "extension" | "variable" | "typedef" | "enum" |
"function" | "constructor" | "field" | "getter" | "setter" |
"method",
"kind": "class" | "mixin" | "extension" | "extension-type" | "variable" |
"typedef" | "enum" | "function" | "constructor" | "field" |
"getter" | "setter" | "method",

// The name of the declared symbol. For constructors this will be either
// the name of the enclosing class, or the dotted name such as
Expand All @@ -79,10 +79,14 @@ declaration = {
// declared object.
"annotations": array[annotation],

// For "class"/"mixin"/"extension" declarations, this is the list of
// entities defined within: constructors, methods, fields, etc.
// For "class"/"mixin"/"extension"/"extension-type" declarations, this is
// the list of entities defined within: constructors, methods, fields, etc.
"members": array[declaration],

// For an "extension-type" declaration, the representation object that
// describes the underlying representation type, such as `(int value)`.
"representation": representation,

// The name of the entity that this class extends. This name may contain
// template parameters.
"extends": string,
Expand Down Expand Up @@ -110,6 +114,18 @@ declaration = {
// Whether the class was declared as `abstract`.
"abstract": bool,

// Whether the class/mixin was declared with the `base` modifier.
"base": bool,

// Whether the class was declared with the `interface` modifier.
"interface": bool,

// Whether the class was declared with the `sealed` modifier.
"sealed": bool,

// Whether the class was declared as a `mixin class`.
"mixinClass": bool,

// Whether the constructor/field/variable were declared as `const`.
"const": bool,

Expand All @@ -119,7 +135,7 @@ declaration = {
// Whether the method was declared as `static`.
"static": bool,

// Whether the variable/field were declared as `final`.
// Whether the variable/field/class were declared as `final`.
"final": bool,

// Whether the variable/field were declared as `late`.
Expand All @@ -139,6 +155,19 @@ typeParameter = {
"extends": string,
}

representation = {
// The name of the representation field, such as "value" in
// `extension type Meters(int value)`.
"name": string,

// The representation type, such as "int".
"type": string,

// The name of the primary constructor, if a named one was given, such as
// "raw" in `extension type Id.raw(String value)`.
"constructorName": string,
}

annotation = {
// The name of the annotation, including the leading "@".
"name": string,
Expand Down
10 changes: 9 additions & 1 deletion lib/src/class_declaration.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:dartdoc_json/src/annotations.dart';
import 'package:dartdoc_json/src/class_modifiers.dart';
import 'package:dartdoc_json/src/comment.dart';
import 'package:dartdoc_json/src/extends_clause.dart';
import 'package:dartdoc_json/src/implements_clause.dart';
Expand All @@ -19,7 +20,14 @@ Map<String, dynamic>? serializeClassDeclaration(ClassDeclaration class_) {
'kind': 'class',
'name': class_.name.lexeme,
'typeParameters': serializeTypeParameterList(class_.typeParameters),
'abstract': class_.abstractKeyword == null ? null : true,
...serializeClassModifiers(
abstractKeyword: class_.abstractKeyword,
baseKeyword: class_.baseKeyword,
interfaceKeyword: class_.interfaceKeyword,
finalKeyword: class_.finalKeyword,
sealedKeyword: class_.sealedKeyword,
mixinKeyword: class_.mixinKeyword,
),
'extends': serializeExtendsClause(class_.extendsClause),
'with': serializeWithClause(class_.withClause),
'implements': serializeImplementsClause(class_.implementsClause),
Expand Down
23 changes: 23 additions & 0 deletions lib/src/class_modifiers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:analyzer/dart/ast/token.dart';

/// Serializes the class modifier keywords into a json-compatible object.
///
/// Each modifier that is present is represented as a `true` value, and absent
/// modifiers are represented as `null` so that `filterMap` can remove them.
Map<String, dynamic> serializeClassModifiers({
Token? abstractKeyword,
Token? baseKeyword,
Token? interfaceKeyword,
Token? finalKeyword,
Token? sealedKeyword,
Token? mixinKeyword,
}) {
return <String, dynamic>{
'abstract': abstractKeyword == null ? null : true,
'base': baseKeyword == null ? null : true,
'interface': interfaceKeyword == null ? null : true,
'final': finalKeyword == null ? null : true,
'sealed': sealedKeyword == null ? null : true,
'mixinClass': mixinKeyword == null ? null : true,
};
}
35 changes: 35 additions & 0 deletions lib/src/class_type_alias.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:dartdoc_json/src/annotations.dart';
import 'package:dartdoc_json/src/class_modifiers.dart';
import 'package:dartdoc_json/src/comment.dart';
import 'package:dartdoc_json/src/implements_clause.dart';
import 'package:dartdoc_json/src/type_parameter_list.dart';
import 'package:dartdoc_json/src/utils.dart';
import 'package:dartdoc_json/src/with_clause.dart';

/// Serializes a ClassTypeAlias (`class C = B with M;`) into a json-compatible
/// object.
Map<String, dynamic>? serializeClassTypeAlias(ClassTypeAlias alias) {
final annotations = serializeAnnotations(alias.metadata);
if (alias.name.lexeme.startsWith('_') || hasPrivateAnnotation(annotations)) {
return null;
}
return filterMap(<String, dynamic>{
'kind': 'class',
'name': alias.name.lexeme,
'typeParameters': serializeTypeParameterList(alias.typeParameters),
...serializeClassModifiers(
abstractKeyword: alias.abstractKeyword,
baseKeyword: alias.baseKeyword,
interfaceKeyword: alias.interfaceKeyword,
finalKeyword: alias.finalKeyword,
sealedKeyword: alias.sealedKeyword,
mixinKeyword: alias.mixinKeyword,
),
'extends': alias.superclass.toString(),
'with': serializeWithClause(alias.withClause),
'implements': serializeImplementsClause(alias.implementsClause),
'annotations': annotations,
'description': serializeComment(alias.documentationComment),
});
}
7 changes: 7 additions & 0 deletions lib/src/compilation_unit.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:dartdoc_json/src/class_declaration.dart';
import 'package:dartdoc_json/src/class_type_alias.dart';
import 'package:dartdoc_json/src/enum_declaration.dart';
import 'package:dartdoc_json/src/export_directive.dart';
import 'package:dartdoc_json/src/extension_declaration.dart';
import 'package:dartdoc_json/src/extension_type_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';
Expand All @@ -17,10 +19,15 @@ Map<String, dynamic> serializeCompilationUnit(CompilationUnit unit) {
Map<String, dynamic>? serialized;
if (declaration is ClassDeclaration) {
serialized = serializeClassDeclaration(declaration);
} else if (declaration is ClassTypeAlias) {
serialized = serializeClassTypeAlias(declaration);
} else if (declaration is MixinDeclaration) {
serialized = serializeMixinDeclaration(declaration);
} else if (declaration is ExtensionDeclaration) {
serialized = serializeExtensionDeclaration(declaration);
// ignore: experimental_member_use
} else if (declaration is ExtensionTypeDeclaration) {
serialized = serializeExtensionTypeDeclaration(declaration);
} else if (declaration is GenericTypeAlias) {
serialized = serializeGenericTypeAlias(declaration);
} else if (declaration is EnumDeclaration) {
Expand Down
8 changes: 6 additions & 2 deletions lib/src/constructor_declaration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ Map<String, dynamic>? serializeConstructorDeclaration(
className = (constructor.parent! as ClassDeclaration).name.lexeme;
} else if (constructor.parent is EnumDeclaration) {
className = (constructor.parent! as EnumDeclaration).name.lexeme;
// ignore: experimental_member_use
} else if (constructor.parent is ExtensionTypeDeclaration) {
// ignore: experimental_member_use
className = (constructor.parent! as ExtensionTypeDeclaration).name.lexeme;
} else {
throw UnsupportedError(
'Constructor parent is neither ClassDeclaration nor EnumDeclaration. '
'It is ${constructor.parent.runtimeType}',
'Constructor parent is not a ClassDeclaration, EnumDeclaration or '
'ExtensionTypeDeclaration. It is ${constructor.parent.runtimeType}',
);
}

Expand Down
46 changes: 46 additions & 0 deletions lib/src/extension_type_declaration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// The ExtensionTypeDeclaration and RepresentationDeclaration AST nodes are
// still marked experimental in the analyzer, even though extension types are
// a stable language feature since Dart 3.3.
// ignore_for_file: experimental_member_use

import 'package:analyzer/dart/ast/ast.dart';
import 'package:dartdoc_json/src/annotations.dart';
import 'package:dartdoc_json/src/comment.dart';
import 'package:dartdoc_json/src/implements_clause.dart';
import 'package:dartdoc_json/src/member_list.dart';
import 'package:dartdoc_json/src/type_parameter_list.dart';
import 'package:dartdoc_json/src/utils.dart';

/// Serializes an ExtensionTypeDeclaration into a json-compatible object.
Map<String, dynamic>? serializeExtensionTypeDeclaration(
ExtensionTypeDeclaration extensionType,
) {
final annotations = serializeAnnotations(extensionType.metadata);
if (extensionType.name.lexeme.startsWith('_') ||
hasPrivateAnnotation(annotations)) {
return null;
}
return filterMap(<String, dynamic>{
'kind': 'extension-type',
'name': extensionType.name.lexeme,
'typeParameters': serializeTypeParameterList(extensionType.typeParameters),
'const': extensionType.constKeyword == null ? null : true,
'representation': serializeRepresentation(extensionType.representation),
'implements': serializeImplementsClause(extensionType.implementsClause),
'annotations': annotations,
'description': serializeComment(extensionType.documentationComment),
'members': serializeMemberList(extensionType.members),
});
}

/// Serializes the representation declaration (the `(int value)` part) of an
/// extension type into a json-compatible object.
Map<String, dynamic> serializeRepresentation(
RepresentationDeclaration representation,
) {
return filterMap(<String, dynamic>{
'name': representation.fieldName.lexeme,
'type': representation.fieldType.toString(),
'constructorName': representation.constructorName?.name.lexeme,
});
}
1 change: 1 addition & 0 deletions lib/src/mixin_declaration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Map<String, dynamic>? serializeMixinDeclaration(MixinDeclaration mixin) {
'kind': 'mixin',
'name': mixin.name.lexeme,
'typeParameters': serializeTypeParameterList(mixin.typeParameters),
'base': mixin.baseKeyword == null ? null : true,
'implements': serializeImplementsClause(mixin.implementsClause),
'on': serializeOnClause(mixin.onClause),
'annotations': annotations,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dartdoc_json
description: Package for rendering dartdocs as JSON files.
version: 0.5.0
version: 0.6.0
repository: https://github.com/bluefireteam/dartdoc_json
funding:
- https://patreon.com/bluefireoss
Expand Down
34 changes: 34 additions & 0 deletions test/class_declaration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,39 @@ void main() {
},
);
});

test('class modifiers', () {
expect(parseAsJson('sealed class X {}'), {
'kind': 'class',
'name': 'X',
'sealed': true,
});
expect(parseAsJson('base class X {}'), {
'kind': 'class',
'name': 'X',
'base': true,
});
expect(parseAsJson('interface class X {}'), {
'kind': 'class',
'name': 'X',
'interface': true,
});
expect(parseAsJson('final class X {}'), {
'kind': 'class',
'name': 'X',
'final': true,
});
expect(parseAsJson('mixin class X {}'), {
'kind': 'class',
'name': 'X',
'mixinClass': true,
});
expect(parseAsJson('abstract base class X {}'), {
'kind': 'class',
'name': 'X',
'abstract': true,
'base': true,
});
});
});
}
55 changes: 55 additions & 0 deletions test/class_type_alias_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import 'package:test/test.dart';

import 'utils.dart';

void main() {
group('ClassTypeAlias', () {
test('simple class type alias', () {
expect(
parseAsJson('class C = B with M;'),
{
'kind': 'class',
'name': 'C',
'extends': 'B',
'with': ['M'],
},
);
});

test('private class type alias', () {
expect(
parseAsJson('class _C = B with M;'),
isEmpty,
);
});

test('class type alias with implements and modifiers', () {
expect(
parseAsJson('abstract class C = B with M, N implements I;'),
{
'kind': 'class',
'name': 'C',
'abstract': true,
'extends': 'B',
'with': ['M', 'N'],
'implements': ['I'],
},
);
});

test('class type alias with type parameters', () {
expect(
parseAsJson('class C<T> = B<T> with M<T>;'),
{
'kind': 'class',
'name': 'C',
'typeParameters': [
{'name': 'T'},
],
'extends': 'B<T>',
'with': ['M<T>'],
},
);
});
});
}
Loading
Loading