diff --git a/compile.js b/compile.js index 719ba76..a7bc227 100644 --- a/compile.js +++ b/compile.js @@ -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; @@ -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) { @@ -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) { @@ -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`; @@ -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) => { @@ -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) => { diff --git a/test/fixtures/defaults_implicit.js b/test/fixtures/defaults_implicit.js index 54d0573..6fb1c01 100644 --- a/test/fixtures/defaults_implicit.js +++ b/test/fixtures/defaults_implicit.js @@ -43,11 +43,11 @@ export function writeCustomType(obj, pbf) { * @property {boolean} [flag] * @property {number} [weight] * @property {number} [id] - * @property {Array} tags - * @property {Array} numbers + * @property {string[]} tags + * @property {number[]} numbers * @property {Uint8Array} [bytes] * @property {CustomType} [custom] - * @property {Array} types + * @property {MessageType[]} types */ /** diff --git a/test/fixtures/embedded_type.js b/test/fixtures/embedded_type.js index e355c21..0fc9057 100644 --- a/test/fixtures/embedded_type.js +++ b/test/fixtures/embedded_type.js @@ -41,7 +41,7 @@ export function writeEmbeddedType(obj, pbf) { /** * @typedef {object} EmbeddedTypeContainer - * @property {Array} values + * @property {EmbeddedTypeContainerInner[]} values */ /** diff --git a/test/fixtures/map.js b/test/fixtures/map.js index 22f30e7..a4b9217 100644 --- a/test/fixtures/map.js +++ b/test/fixtures/map.js @@ -37,7 +37,7 @@ export function writeEnvelope(obj, pbf) { } /** - * @typedef {Object} Envelope_FieldEntry1 + * @typedef {Record} Envelope_FieldEntry1 */ /** @@ -69,7 +69,7 @@ export function writeEnvelope_FieldEntry1(obj, pbf) { } /** - * @typedef {Object} Envelope_FieldEntry2 + * @typedef {Record} Envelope_FieldEntry2 */ /** diff --git a/test/fixtures/packed.js b/test/fixtures/packed.js index 9340091..f9c676b 100644 --- a/test/fixtures/packed.js +++ b/test/fixtures/packed.js @@ -4,8 +4,8 @@ /** * @typedef {object} NotPacked - * @property {Array} value - * @property {Array} types + * @property {number[]} value + * @property {number[]} types */ /** @@ -38,8 +38,8 @@ export function writeNotPacked(obj, pbf) { /** * @typedef {object} FalsePacked - * @property {Array} value - * @property {Array} types + * @property {number[]} value + * @property {number[]} types */ /** @@ -72,8 +72,8 @@ export function writeFalsePacked(obj, pbf) { /** * @typedef {object} Packed - * @property {Array} value - * @property {Array} types + * @property {number[]} value + * @property {number[]} types */ /** diff --git a/test/fixtures/packed_proto3.js b/test/fixtures/packed_proto3.js index d0daee2..414e2aa 100644 --- a/test/fixtures/packed_proto3.js +++ b/test/fixtures/packed_proto3.js @@ -10,8 +10,8 @@ export const MessageType = { /** * @typedef {object} NotPacked - * @property {Array} value - * @property {Array} types + * @property {number[]} value + * @property {MessageType[]} types */ /** @@ -44,8 +44,8 @@ export function writeNotPacked(obj, pbf) { /** * @typedef {object} FalsePacked - * @property {Array} value - * @property {Array} types + * @property {number[]} value + * @property {MessageType[]} types */ /** @@ -78,7 +78,7 @@ export function writeFalsePacked(obj, pbf) { /** * @typedef {object} Packed - * @property {Array} value + * @property {number[]} value */ /** diff --git a/test/fixtures/vector_tile.js b/test/fixtures/vector_tile.js index ca9e9c8..ccad203 100644 --- a/test/fixtures/vector_tile.js +++ b/test/fixtures/vector_tile.js @@ -4,7 +4,7 @@ /** * @typedef {object} Tile - * @property {Array} layers + * @property {TileLayer[]} layers */ /** @@ -93,9 +93,9 @@ export function writeTileValue(obj, pbf) { /** * @typedef {object} TileFeature * @property {number} [id] - * @property {Array} tags + * @property {number[]} tags * @property {TileGeomType} [type] - * @property {Array} geometry + * @property {number[]} geometry */ /** @@ -134,9 +134,9 @@ export function writeTileFeature(obj, pbf) { * @typedef {object} TileLayer * @property {number} version * @property {string} name - * @property {Array} features - * @property {Array} keys - * @property {Array} values + * @property {TileFeature[]} features + * @property {string[]} keys + * @property {TileValue[]} values * @property {number} [extent] */