Skip to content

Commit

Permalink
Handle consts + usage
Browse files Browse the repository at this point in the history
  • Loading branch information
davepagurek committed Feb 11, 2024
1 parent bb80892 commit 4be13d2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/core/shape/vertex.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ p5.prototype.beginContour = function() {
* <a href="#/p5/ellipse">ellipse()</a> or <a href="#/p5/rect">rect()</a> within <a href="#/p5/beginShape">beginShape()</a>.
*
* @method beginShape
* @param {Constant} [kind] either POINTS, LINES, TRIANGLES, TRIANGLE_FAN
* @param {POINTS|LINES|TRIANGLES|TRIANGLE_FAN|TRIANGLE_STRIP|QUADS|QUAD_STRIP|TESS} [kind] either POINTS, LINES, TRIANGLES, TRIANGLE_FAN
* TRIANGLE_STRIP, QUADS, QUAD_STRIP or TESS
* @chainable
* @example
Expand Down Expand Up @@ -603,7 +603,7 @@ p5.prototype.endContour = function() {
* page.
*
* @method endShape
* @param {Constant} [mode] use CLOSE to close the shape
* @param {CLOSE} [mode] use CLOSE to close the shape
* @param {Integer} [count] number of times you want to draw/instance the shape (for WebGL mode).
* @chainable
* @example
Expand Down
72 changes: 65 additions & 7 deletions utils/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ const allData = data.flatMap(entry => {
});

const converted = {
project: {}, // TODO
files: {}, // TODO
project: {}, // Unimplemented, probably not needed
files: {}, // Unimplemented, probably not needed
modules: {},
classes: {},
classitems: [],
warnings: [], // Intentionally unimplemented
consts: {} // TODO
consts: {}
};

function descriptionString(node) {
Expand All @@ -46,12 +46,47 @@ function typeObject(node) {

if (node.type === 'OptionalType') {
return { optional: 1, ...typeObject(node.expression) };
// TODO handle type UndefinedLiteral here
} else if (node.type === 'UnionType') {
const names = node.elements.map(n => typeObject(n).type);
return {
type: names.join('|')
};
} else if (node.type === 'TypeApplication') {
const { type: typeName } = typeObject(node.expression);
const args = node.applications.map(n => typeObject(n).type);
return {
type: `${typeName}<${args.join(', ')}>`
};
} else {
// TODO
// - handle type UndefinedLiteral
// - handle record types
return { type: node.name };
}
}

const constUsage = {};
function registerConstantUsage(name, memberof, node) {
if (!node) return;
if (node.type === 'OptionalType') {
registerConstantUsage(name, memberof, node.expression);
} else if (node.type === 'UnionType') {
for (const element of node.elements) {
registerConstantUsage(name, memberof, element);
}
} else if (node.type === 'TypeApplication') {
registerConstantUsage(name, memberof, node.expression);
for (const element of node.applications) {
registerConstantUsage(name, memberof, element);
}
} else if (node.type === 'NameExpression') {
const constant = constUsage[node.name];
if (constant) {
constant.add(`${memberof}.${name}`);
}
}
}

function locationInfo(node) {
return {
file: node.context.file.slice(node.context.file.indexOf('src/')),
Expand Down Expand Up @@ -100,8 +135,8 @@ for (const entry of allData) {
fileModuleInfo[file].module = module;
fileModuleInfo[file].submodule =
fileModuleInfo[file].submodule || submodule;
fileModuleInfo[file].module =
fileModuleInfo[file].module || forEntry;
fileModuleInfo[file].for =
fileModuleInfo[file].for || forEntry;

modules[module] = modules[module] || {
name: module,
Expand Down Expand Up @@ -132,6 +167,15 @@ function getModuleInfo(entry) {
return { module, submodule, forEntry };
}

// ============================================================================
// Constants
// ============================================================================
for (const entry of allData) {
if (entry.kind === 'constant') {
constUsage[entry.name] = new Set();
}
}

// ============================================================================
// Classes
// ============================================================================
Expand Down Expand Up @@ -198,6 +242,9 @@ for (const entry of allData) {

// Grab property metadata out of other loose nodes.
for (const entry of allData) {
// These are in a different section
if (entry.kind === 'constant') continue;

const { module, submodule, forEntry } = getModuleInfo(entry);
const propTag = entry.tags.find(tag => tag.title === 'property');
const forTag = entry.tags.find(tag => tag.title === 'for');
Expand Down Expand Up @@ -246,11 +293,17 @@ for (const entry of allData) {
// an overload on that method
const prevItem = (classMethods[entry.memberof] || {})[entry.name] || {};

for (const param of entry.params) {
registerConstantUsage(entry.name, prevItem.class || forEntry, param.type);
}

const item = {
name: entry.name,
...locationInfo(entry),
itemtype: 'method',
chainable: prevItem.chainable || entry.tags.some(tag => tag.title === 'chainable'),
chainable: (prevItem.chainable || entry.tags.some(tag => tag.title === 'chainable'))
? 1
: undefined,
description: prevItem.description || descriptionString(entry.description),
example: [
...(prevItem.example || []),
Expand Down Expand Up @@ -292,4 +345,9 @@ for (const className in classMethods) {
}
}

// Done registering const usage, make a finished version
for (const key in constUsage) {
converted.consts[key] = [...constUsage[key]];
}

fs.writeFileSync(path.join(__dirname, '../docs/converted.json'), JSON.stringify(converted, null, 2));

0 comments on commit 4be13d2

Please sign in to comment.