Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
stepankuzmin committed Aug 21, 2024
1 parent acb1fcd commit 89a3c96
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 40 deletions.
41 changes: 24 additions & 17 deletions compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export function compileRaw(proto, options = {}) {

let output = options.dev ? '' : `// code generated by pbf v${version}\n`;
if (options.jsDoc) {
output += typeDef(`import("${options.dev ? '../../index.js' : 'pbf'}").default`, 'Pbf');
output += options.dev ?
typeDef('import("../../index.js").default', 'Pbf') :
typeDef('import("pbf")', 'Pbf');
}
output += writeContext(context, options);
return output;
Expand Down Expand Up @@ -42,7 +44,13 @@ function writeMessage(ctx, options) {
if (!options.noRead) {
const readName = `read${name}`;
if (options.jsDoc) {
code += ['\n/**', ' * @param {Pbf} pbf', ' * @param {number} [end]', ` * @returns {${name}}`, ' */'].join('\n').concat('\n');
code += `
/**
* @param {Pbf} pbf
* @param {number} [end]
* @returns {${name}}
*/
`;
}
code +=
`${writeFunctionExport(options, readName)}function ${readName}(pbf, end) {
Expand All @@ -55,7 +63,13 @@ function writeMessage(ctx, options) {
const {key, value} = getMapTsType(fields);
param = `{key: ${key}; value: ${value}}`;
}
code += ['\n/**', ' * @param {number} tag', ` * @param {${param}} obj`, ' * @param {Pbf} pbf', ' */'].join('\n').concat('\n');
code += `
/**
* @param {number} tag
* @param {${param}} obj
* @param {Pbf} pbf
*/
`;
}
code +=
`function ${readName}Field(tag, obj, pbf) {
Expand Down Expand Up @@ -93,7 +107,12 @@ function writeMessage(ctx, options) {
param = `{key: ${key}; value: ${value}}`;
}

code += ['\n/**', ` * @param {${param}} obj`, ' * @param {Pbf} pbf', ' */'].join('\n').concat('\n');
code += `
/**
* @param {${param}} obj
* @param {Pbf} pbf
*/
`;
}

code += `${writeFunctionExport(options, writeName)}function ${writeName}(obj, pbf) {\n`;
Expand Down Expand Up @@ -480,12 +499,6 @@ function getMapTsType(fields) {
return {key, value};
}

/**
* @param {string} type
* @param {string} name
* @param {{name: string; type: string; required: boolean}} [fields]
* @returns {string}
*/
function typeDef(type, name, fields = []) {
const unionProperties = {};
const properties = fields.map((field) => {
Expand All @@ -512,16 +525,10 @@ function typeDef(type, name, fields = []) {
.concat('\n');
}

/**
* @param {object} ctx
* @param {string} name
* @param {{name: string; type: string; required: boolean}} [fields]
* @returns {string}
*/
function compileType(ctx, name, fields = []) {
if (ctx._proto.map) {
const {key, value} = getMapTsType(fields);
return typeDef(`Object<${key}, ${value}>`, name, []);
return typeDef(`Record<${key}, ${value}>`, name, []);
}

const typedFields = fields.map((field) => {
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/defaults_implicit.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export function writeCustomType(obj, pbf) {
* @property {boolean} [flag]
* @property {number} [weight]
* @property {number} [id]
* @property {Array<string>} tags
* @property {Array<number>} numbers
* @property {string[]} tags
* @property {number[]} numbers
* @property {Uint8Array} [bytes]
* @property {CustomType} [custom]
* @property {Array<MessageType>} types
* @property {MessageType[]} types
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/embedded_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function writeEmbeddedType(obj, pbf) {

/**
* @typedef {object} EmbeddedTypeContainer
* @property {Array<EmbeddedTypeContainerInner>} values
* @property {EmbeddedTypeContainerInner[]} values
*/

/**
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function writeEnvelope(obj, pbf) {
}

/**
* @typedef {Object<string, string>} Envelope_FieldEntry1
* @typedef {Record<string, string>} Envelope_FieldEntry1
*/

/**
Expand Down Expand Up @@ -69,7 +69,7 @@ export function writeEnvelope_FieldEntry1(obj, pbf) {
}

/**
* @typedef {Object<string, number>} Envelope_FieldEntry2
* @typedef {Record<string, number>} Envelope_FieldEntry2
*/

/**
Expand Down
12 changes: 6 additions & 6 deletions test/fixtures/packed.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

/**
* @typedef {object} NotPacked
* @property {Array<number>} value
* @property {Array<number>} types
* @property {number[]} value
* @property {number[]} types
*/

/**
Expand Down Expand Up @@ -38,8 +38,8 @@ export function writeNotPacked(obj, pbf) {

/**
* @typedef {object} FalsePacked
* @property {Array<number>} value
* @property {Array<number>} types
* @property {number[]} value
* @property {number[]} types
*/

/**
Expand Down Expand Up @@ -72,8 +72,8 @@ export function writeFalsePacked(obj, pbf) {

/**
* @typedef {object} Packed
* @property {Array<number>} value
* @property {Array<number>} types
* @property {number[]} value
* @property {number[]} types
*/

/**
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/packed_proto3.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export const MessageType = {

/**
* @typedef {object} NotPacked
* @property {Array<number>} value
* @property {Array<MessageType>} types
* @property {number[]} value
* @property {MessageType[]} types
*/

/**
Expand Down Expand Up @@ -44,8 +44,8 @@ export function writeNotPacked(obj, pbf) {

/**
* @typedef {object} FalsePacked
* @property {Array<number>} value
* @property {Array<MessageType>} types
* @property {number[]} value
* @property {MessageType[]} types
*/

/**
Expand Down Expand Up @@ -78,7 +78,7 @@ export function writeFalsePacked(obj, pbf) {

/**
* @typedef {object} Packed
* @property {Array<number>} value
* @property {number[]} value
*/

/**
Expand Down
12 changes: 6 additions & 6 deletions test/fixtures/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* @typedef {object} Tile
* @property {Array<TileLayer>} layers
* @property {TileLayer[]} layers
*/

/**
Expand Down Expand Up @@ -93,9 +93,9 @@ export function writeTileValue(obj, pbf) {
/**
* @typedef {object} TileFeature
* @property {number} [id]
* @property {Array<number>} tags
* @property {number[]} tags
* @property {TileGeomType} [type]
* @property {Array<number>} geometry
* @property {number[]} geometry
*/

/**
Expand Down Expand Up @@ -134,9 +134,9 @@ export function writeTileFeature(obj, pbf) {
* @typedef {object} TileLayer
* @property {number} version
* @property {string} name
* @property {Array<TileFeature>} features
* @property {Array<string>} keys
* @property {Array<TileValue>} values
* @property {TileFeature[]} features
* @property {string[]} keys
* @property {TileValue[]} values
* @property {number} [extent]
*/

Expand Down

0 comments on commit 89a3c96

Please sign in to comment.