Dart API 提取器
这是一个命令行工具,可以解析您的 Dart 文件以提取 API 信息,然后将该信息以 JSON 格式保存到文件中。然后,下游工具可以利用此输出来实现各种目的,例如生成文档。
用法
dart run dartdoc_json.dart FILENAME(s)
输出
生成的 JSON 文件将具有以下结构
root: array[compilationUnit]
compilationUnit = {
// The name of the input file.
"source": string,
// import/export directives within the file.
"directives": array[directive],
// the list of objects defined within the file.
"declarations": array[declaration],
}
directive = {
// What kind of directive this is
"kind": "import" | "export",
// The name of the file being imported or exported
"uri": string,
// List of symbols listed in the `show`/`hide` part of the directive
"show": array[string],
"hide": array[string],
// For "import" statements, the ID after the `as` keyword
"as": string,
}
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",
// The name of the declared symbol. For constructors this will be either
// the name of the enclosing class, or the dotted name such as
// "Color.fromARGB".
"name": string,
// The doc-comment associated with the declaration. This will be a single
// string, joined with newlines, but with comment symbols removed.
"description": string,
// The list of type (template) parameters for the declaration.
"typeParameters": array[typeParameter],
// The list of annotations (such as "@Deprecated") attached to the
// declared object.
"annotations": array[annotation],
// For "class"/"mixin"/"extension" declarations, this is the list of
// entities defined within: constructors, methods, fields, etc.
"members": array[declaration],
// The name of the entity that this class extends. This name may contain
// template parameters.
"extends": string,
// The list of entities defined after the `with` keyword for a class or
// mixin.
"with": array[string],
// The list of entities defined after the `implements` keyword for a
// class or mixin.
"implements": array[string],
// The list of entities defined after the `on` keyword for a mixin.
"on": array[string],
// The list of arguments in a method, function, or constructor.
"parameters": parameterList,
// For enums, the list of values defined within that enum.
"values": array[enumConstant],
// For a function/method declaration, the type of the return value.
"returns": string,
// Whether the class was declared as `abstract`.
"abstract": bool,
// Whether the constructor/field/variable were declared as `const`.
"const": bool,
// Whether the constructor was declared as `factory`.
"factory": bool,
// Whether the method was declared as `static`.
"static": bool,
// Whether the variable/field were declared as `final`.
"final": bool,
// Whether the variable/field were declared as `late`.
"late": bool,
// For variable/field declaration that declare several variables at once
// (such as `int x, y, z;`), this will contain the list of names beyond
// the first name.
"extraNames": array[string],
}
typeParameter = {
// The name of the type parameter, such as "T".
"name": string,
// The name of the type after the `extends` keyword.
"extends": string,
}
annotation = {
// The name of the annotation, including the leading "@".
"name": string,
// For parametrized annotations, this will contain the list of stringified
// arguments that were given to the annotation. For example, if the
// annotation was `@Deprecated("1.6.0")`, then this list will contain
// a single string `'"1.6.0"'`.
"arguments": array[string],
}
parameterList = {
// The list of all parameters given to the function/method, in order of
// their appearance within the function's signature.
"all": array[parameter],
// The number of (optional) positional arguments in function's signature.
// These are the arguments that appear in square brackets. This property
// will not be provided if there are no positional arguments, otherwise
// its value will be less than or equal to the number of arguments in the
// "all" array.
"positional": int,
// The number of named arguments in function's signature. These are the
// arguments that appear in curly braces.
"named": int,
}
parameter = {
// The name of the parameter. This could also be a dotted name, such as
// "this.items", or "super.key".
"name": string,
// The type of the parameter, which can also be a template. The type can
// also be missing.
"type": string,
// The default value of the parameter, if exists.
"default": string,
// If true, indicates the presence of the `covariant` keyword.
"covariant": bool,
}
enumConstant = {
// The name of the enum constant.
"name": string,
// The doc-comment attached to this constant.
"description": string,
// The list of annotations associated with this enum constant.
"annotations": array[annotation],
// The list of arguments used in this enum constant, if the constant
// uses explicit constructor syntax.
"arguments": array[string],
}
生成的 JSON 中的大多数字段都是可选的,并且仅在提供的元素存在于输入 Dart 文件中时包含。
其他命令行选项
命令行工具识别以下选项
--output FILE:要写入的文件名。默认为“out.json”;--root DIR:包的根文件夹路径。如果提供,所有输入文件将相对于此目录查找。但是,生成的 JSON 中的 `source` 字段仍将包含输入文件的名称,而不包含根目录。因此,如果您希望 JSON 文件中的文件名与 Dart 包的导入/导出中使用的文件名匹配,此选项可能会很有用。--pretty:此标志会导致输出包含大量空格,这使得人类更容易阅读,但会增加文件大小。