-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(json): add legacy JSON generator
Co-Authored-By: flakey5 <73616808+flakey5@users.noreply.github.com>
- Loading branch information
1 parent
fb68333
commit 3a4b27b
Showing
20 changed files
with
920 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
import { writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
|
||
/** | ||
* @typedef {Array<import('../legacy-json/types.d.ts').Section>} Input | ||
* | ||
* @type {import('../types.d.ts').GeneratorMetadata<Input, import('./types.d.ts').Output>} | ||
*/ | ||
export default { | ||
name: 'legacy-json-all', | ||
|
||
version: '1.0.0', | ||
|
||
description: | ||
'Generates the `all.json` file from the `legacy-json` generator, which includes all the modules in one single file.', | ||
|
||
dependsOn: 'legacy-json', | ||
|
||
async generate(input, { output }) { | ||
/** | ||
* @type {import('./types.d.ts').Output} | ||
*/ | ||
const generatedValue = { | ||
miscs: [], | ||
modules: [], | ||
classes: [], | ||
globals: [], | ||
methods: [], | ||
}; | ||
|
||
const propertiesToCopy = [ | ||
'miscs', | ||
'modules', | ||
'classes', | ||
'globals', | ||
'methods', | ||
]; | ||
|
||
input.forEach(section => { | ||
// Copy the relevant properties from each section into our output | ||
propertiesToCopy.forEach(property => { | ||
if (section[property]) { | ||
generatedValue[property].push(...section[property]); | ||
} | ||
}); | ||
}); | ||
|
||
await writeFile(join(output, 'all.json'), JSON.stringify(generatedValue)); | ||
|
||
return generatedValue; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { | ||
MiscSection, | ||
Section, | ||
SignatureSection, | ||
ModuleSection, | ||
} from '../legacy-json/types'; | ||
|
||
export interface Output { | ||
miscs: Array<MiscSection>; | ||
modules: Array<Section>; | ||
classes: Array<SignatureSection>; | ||
globals: Array<ModuleSection | { type: 'global' }>; | ||
methods: Array<SignatureSection>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Grabs a method's return value | ||
export const RETURN_EXPRESSION = /^returns?\s*:?\s*/i; | ||
|
||
// Grabs a method's name | ||
export const NAME_EXPRESSION = /^['`"]?([^'`": {]+)['`"]?\s*:?\s*/; | ||
|
||
// Denotes a method's type | ||
export const TYPE_EXPRESSION = /^\{([^}]+)\}\s*/; | ||
|
||
// Checks if there's a leading hyphen | ||
export const LEADING_HYPHEN = /^-\s*/; | ||
|
||
// Grabs the default value if present | ||
export const DEFAULT_EXPRESSION = /\s*\*\*Default:\*\*\s*([^]+)$/i; | ||
|
||
// Grabs the parameters from a method's signature | ||
// ex/ 'new buffer.Blob([sources[, options]])'.match(PARAM_EXPRESSION) === ['([sources[, options]])', '[sources[, options]]'] | ||
export const PARAM_EXPRESSION = /\((.+)\);?$/; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
import { writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
import { groupNodesByModule } from '../../utils/generators.mjs'; | ||
import buildSection from './utils/buildSection.mjs'; | ||
|
||
/** | ||
* This generator is responsible for generating the legacy JSON files for the | ||
* legacy API docs for retro-compatibility. It is to be replaced while we work | ||
* on the new schema for this file. | ||
* | ||
* This is a top-level generator, intaking the raw AST tree of the api docs. | ||
* It generates JSON files to the specified output directory given by the | ||
* config. | ||
* | ||
* @typedef {Array<ApiDocMetadataEntry>} Input | ||
* | ||
* @type {import('../types.d.ts').GeneratorMetadata<Input, import('./types.d.ts').Section[]>} | ||
*/ | ||
export default { | ||
name: 'legacy-json', | ||
|
||
version: '1.0.0', | ||
|
||
description: 'Generates the legacy version of the JSON API docs.', | ||
|
||
dependsOn: 'ast', | ||
|
||
async generate(input, { output }) { | ||
// This array holds all the generated values for each module | ||
const generatedValues = []; | ||
|
||
const groupedModules = groupNodesByModule(input); | ||
|
||
// Gets the first nodes of each module, which is considered the "head" | ||
const headNodes = input.filter(node => node.heading.depth === 1); | ||
|
||
/** | ||
* @param {ApiDocMetadataEntry} head | ||
* @returns {import('./types.d.ts').ModuleSection} | ||
*/ | ||
const processModuleNodes = head => { | ||
const nodes = groupedModules.get(head.api); | ||
|
||
const section = buildSection(head, nodes); | ||
generatedValues.push(section); | ||
|
||
return section; | ||
}; | ||
|
||
await Promise.all( | ||
headNodes.map(async node => { | ||
// Get the json for the node's section | ||
const section = processModuleNodes(node); | ||
|
||
// Write it to the output file | ||
await writeFile( | ||
join(output, `${node.api}.json`), | ||
JSON.stringify(section) | ||
); | ||
}) | ||
); | ||
|
||
return generatedValues; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { ListItem } from 'mdast'; | ||
|
||
export interface HierarchizedEntry extends ApiDocMetadataEntry { | ||
hierarchyChildren: Array<ApiDocMetadataEntry>; | ||
} | ||
|
||
export interface Meta { | ||
changes: Array<ApiDocMetadataChange>; | ||
added?: Array<string>; | ||
napiVersion?: Array<string>; | ||
deprecated?: Array<string>; | ||
removed?: Array<string>; | ||
} | ||
|
||
export interface SectionBase { | ||
type: string; | ||
name: string; | ||
textRaw: string; | ||
displayName?: string; | ||
desc: string; | ||
shortDesc?: string; | ||
stability?: number; | ||
stabilityText?: string; | ||
meta?: Meta; | ||
} | ||
|
||
export interface ModuleSection extends SectionBase { | ||
type: 'module'; | ||
source: string; | ||
miscs?: Array<MiscSection>; | ||
modules?: Array<ModuleSection>; | ||
classes?: Array<SignatureSection>; | ||
methods?: Array<MethodSignature>; | ||
properties?: Array<PropertySection>; | ||
globals?: ModuleSection | { type: 'global' }; | ||
signatures?: Array<SignatureSection>; | ||
} | ||
|
||
export interface SignatureSection extends SectionBase { | ||
type: 'class' | 'ctor' | 'classMethod' | 'method'; | ||
signatures: Array<MethodSignature>; | ||
} | ||
|
||
export type Section = | ||
| SignatureSection | ||
| PropertySection | ||
| EventSection | ||
| MiscSection; | ||
|
||
export interface Parameter { | ||
name: string; | ||
optional?: boolean; | ||
default?: string; | ||
} | ||
|
||
export interface MethodSignature { | ||
params: Array<Parameter>; | ||
return?: string; | ||
} | ||
|
||
export interface PropertySection extends SectionBase { | ||
type: 'property'; | ||
[key: string]: string | undefined; | ||
} | ||
|
||
export interface EventSection extends SectionBase { | ||
type: 'event'; | ||
params: Array<ListItem>; | ||
} | ||
|
||
export interface MiscSection extends SectionBase { | ||
type: 'misc'; | ||
[key: string]: string | undefined; | ||
} | ||
|
||
export interface List { | ||
textRaw: string; | ||
desc?: string; | ||
name: string; | ||
type?: string; | ||
default?: string; | ||
options?: List; | ||
} |
Oops, something went wrong.