-
Notifications
You must be signed in to change notification settings - Fork 9
feat: legacy json generator #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3a4b27b
feat(json): add legacy JSON generator
flakey5 35e6f58
code review (1)
avivkeller 96c0c26
code review (2)
avivkeller 8becb05
code review (3)
avivkeller 5419773
code review (4)
avivkeller 06ef8ea
Merge branch 'main' into legacy-json
ovflowd 03797f7
Merge branch 'main' into legacy-json
ovflowd fb9da69
resolve review
avivkeller 158869e
lint jsdoc
avivkeller 420d7c5
fixup! lint jsdoc
avivkeller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,68 @@ | ||
'use strict'; | ||
|
||
import { writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
|
||
/** | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* This generator consolidates data from the `legacy-json` generator into a single | ||
* JSON file (`all.json`). | ||
* | ||
* @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', | ||
|
||
/** | ||
* Generates the legacy JSON `all.json` file. | ||
* | ||
* @param {Input} input | ||
* @param {Partial<GeneratorOptions>} options | ||
*/ | ||
async generate(input, { output }) { | ||
/** | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* The consolidated output object that will contain | ||
* combined data from all sections in the input. | ||
* | ||
* @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]); | ||
} | ||
}); | ||
}); | ||
|
||
if (output) { | ||
await writeFile(join(output, 'all.json'), JSON.stringify(generatedValue)); | ||
} | ||
|
||
return generatedValue; | ||
}, | ||
}; |
This file contains hidden or 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 hidden or 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,36 @@ | ||
// 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 = /\((.+)\);?$/; | ||
|
||
// The plurals associated with each section type. | ||
export const SECTION_TYPE_PLURALS = { | ||
module: 'modules', | ||
misc: 'miscs', | ||
class: 'classes', | ||
method: 'methods', | ||
property: 'properties', | ||
global: 'globals', | ||
example: 'examples', | ||
ctor: 'signatures', | ||
classMethod: 'classMethods', | ||
event: 'events', | ||
var: 'vars', | ||
}; | ||
|
||
// The keys to not promote when promoting children. | ||
export const UNPROMOTED_KEYS = ['textRaw', 'name', 'type', 'desc', 'miscs']; |
This file contains hidden or 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,77 @@ | ||
'use strict'; | ||
|
||
import { writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
import { groupNodesByModule } from '../../utils/generators.mjs'; | ||
import { createSectionBuilder } 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', | ||
|
||
/** | ||
* Generates a legacy JSON file. | ||
* | ||
* @param {Input} input | ||
* @param {Partial<GeneratorOptions>} options | ||
*/ | ||
async generate(input, { output }) { | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const buildSection = createSectionBuilder(); | ||
|
||
// 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 => { | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
if (output) { | ||
await writeFile( | ||
join(output, `${node.api}.json`), | ||
JSON.stringify(section) | ||
); | ||
} | ||
}) | ||
); | ||
|
||
return generatedValues; | ||
}, | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.