diff --git a/lib/utils.js b/lib/utils.js index 421e8ac..c78ac8e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -484,7 +484,13 @@ const inferTypeFromVariableDeclaration = (variable) => { const typeOfValue = expressionsMap[variable.declarator.init.type] || typeof value; if (typeOfValue !== 'undefined') { - return { kind: 'type', text: typeOfValue, type: typeOfValue }; + return { + kind: typeOfValue === 'function' + ? 'function' + : 'type', + text: typeOfValue, + type: typeOfValue, + }; } return anyType; diff --git a/lib/v3/script.js b/lib/v3/script.js index 2ab0c21..06582fe 100644 --- a/lib/v3/script.js +++ b/lib/v3/script.js @@ -102,8 +102,8 @@ class ScriptParser extends EventEmitter { if (inferredType.type === 'function') { parseAndMergeKeywords(comment.keywords, variable); - item.params = variable.params; - item.return = variable.return; + inferredType.params = variable.params; + inferredType.return = variable.return; } this.attachLocationsIfRequired(item, variable, parseContext); diff --git a/lib/v3/v3-utils.js b/lib/v3/v3-utils.js index a79d7a3..2fd1103 100644 --- a/lib/v3/v3-utils.js +++ b/lib/v3/v3-utils.js @@ -57,8 +57,10 @@ function getInnermostBody(node) { /** * * @param {AstNode} nodeParams Array of node parameters + * @return {import('../../typings').SvelteMethodParamItem[]} */ function parseParams(nodeParams) { + /** @type {import('../../typings').SvelteMethodParamItem[]} */ const params = []; if (nodeParams && nodeParams.length) { @@ -82,7 +84,7 @@ function parseParams(nodeParams) { type: inferredType, description: null, optional: true, - defaultValue: param.right.raw + defaultValue: param.right.raw, }); } }); diff --git a/test/svelte3/integration/data/data.spec.js b/test/svelte3/integration/data/data.spec.js index 3780fc4..2736508 100644 --- a/test/svelte3/integration/data/data.spec.js +++ b/test/svelte3/integration/data/data.spec.js @@ -228,10 +228,11 @@ describe('SvelteDoc v3 - Props', () => { expect(data0.name).to.equal('add'); expect(data0.type.type).to.equal('function'); + expect(data0.type.kind).to.equal('function'); + expect(data0.type.params, 'Function expression arguments should be parsed').to.exist; expect(data0.description).to.equal('Adds two numbers `a` and `b`'); - expect(data0.params, 'Function expression arguments should be parsed').to.exist; - const data0params0 = data0.params[0]; + const data0params0 = data0.type.params[0]; expect(data0params0.name).to.equal('a'); expect(data0params0.description).to.be.null; @@ -239,7 +240,7 @@ describe('SvelteDoc v3 - Props', () => { expect(data0params0.defaultValue).to.equal('0'); expect(data0params0.optional).to.be.true; - const data0params1 = data0.params[1]; + const data0params1 = data0.type.params[1]; expect(data0params1.name).to.equal('b'); expect(data0params1.description).to.be.null; @@ -249,10 +250,11 @@ describe('SvelteDoc v3 - Props', () => { expect(data1.name).to.equal('subtract'); expect(data1.type.type).to.equal('function'); + expect(data1.type.kind).to.equal('function'); + expect(data1.type.params, 'Function expression arguments should be parsed').to.exist; expect(data1.description).to.equal('Subtracts two numbers `b` from `a`'); - expect(data1.params, 'Function expression arguments should be parsed').to.exist; - const data1params0 = data1.params[0]; + const data1params0 = data1.type.params[0]; expect(data1params0.name).to.equal('a'); expect(data1params0.description).to.equal('first number'); @@ -260,7 +262,7 @@ describe('SvelteDoc v3 - Props', () => { expect(data1params0.defaultValue).to.be.undefined; expect(data1params0.optional).to.be.false; - const data1params1 = data1.params[1]; + const data1params1 = data1.type.params[1]; expect(data1params1.name).to.equal('b'); expect(data1params1.description).to.equal('second number'); @@ -268,17 +270,17 @@ describe('SvelteDoc v3 - Props', () => { expect(data1params1.defaultValue).to.equal('0'); expect(data1params1.optional).to.be.true; - expect(data1.return, 'function expression return keyword should be parsed').to.exist; - expect(data1.return.type).to.exist; - expect(data1.return.type.type).to.equal('number'); - expect(data1.return.description).to.equal('the difference'); + expect(data1.type.return, 'function expression return keyword should be parsed').to.exist; + expect(data1.type.return.type).to.exist; + expect(data1.type.return.type.type).to.equal('number'); + expect(data1.type.return.description).to.equal('the difference'); expect(data2.name).to.equal('multiply'); expect(data2.type.type).to.equal('function'); + expect(data2.type.params, 'Function expression arguments should be parsed').to.exist; expect(data2.description).to.equal('Multiplies two numbers `a` and `b`'); - expect(data2.params, 'Function expression arguments should be parsed').to.exist; - const data2params0 = data2.params[0]; + const data2params0 = data2.type.params[0]; expect(data2params0.name).to.equal('a'); expect(data2params0.description).to.equal('first number'); @@ -286,7 +288,7 @@ describe('SvelteDoc v3 - Props', () => { expect(data2params0.defaultValue).to.equal('0'); expect(data2params0.optional).to.be.true; - const data2params1 = data2.params[1]; + const data2params1 = data2.type.params[1]; expect(data2params1.name).to.equal('b'); expect(data2params1.description).to.equal('second number'); @@ -294,15 +296,16 @@ describe('SvelteDoc v3 - Props', () => { expect(data2params1.defaultValue).to.equal('1'); expect(data2params1.optional).to.be.true; - expect(data2.return, 'function expression return keyword should be parsed').to.exist; - expect(data2.return.type).to.exist; - expect(data2.return.type.type).to.equal('number'); - expect(data2.return.description).to.equal('the total'); + expect(data2.type.return, 'function expression return keyword should be parsed').to.exist; + expect(data2.type.return.type).to.exist; + expect(data2.type.return.type.type).to.equal('number'); + expect(data2.type.return.description).to.equal('the total'); expect(data3.name).to.equal('done'); expect(data3.type.type).to.equal('function'); + expect(data3.type.kind).to.equal('function'); + expect(data3.type.params).to.not.exist; expect(data3.description).to.be.null; - expect(data3.params).to.not.exist; done(); }).catch(e => { diff --git a/typings.d.ts b/typings.d.ts index 87e54c0..131f8f5 100644 --- a/typings.d.ts +++ b/typings.d.ts @@ -14,7 +14,19 @@ export interface JSDocKeyword { description: string; } +export type JSDocTypeKind = 'type' | 'const' | 'union' | 'function'; + export interface JSDocTypeBase { + /** + * The kind of JSDocType object. + * That property can identify which additional properties included in that object. + * + * @see JSDocTypeElement + * @see JSDocTypeConst + * @see JSDocTypeUnion + * @see JSDocTypeFunction + */ + kind: JSDocTypeKind; /** * The text representation of this type. */ @@ -63,7 +75,26 @@ export interface JSDocTypeUnion extends JSDocTypeBase { type: JSDocType[]; } -export type JSDocType = JSDocTypeElement | JSDocTypeConst | JSDocTypeUnion; +export interface IMethodDefinition { + /** + * The list of parameter items of the function expression. + */ + params?: SvelteMethodParamItem[]; + /** + * The return item of the function expression. This exists if an item with 'name' equal + * to 'returns' or 'return' exists in 'keywords'. + */ + return?: SvelteMethodReturnItem; +} + +/** + * @since {4.2.0} + */ +export interface JSDocTypeFunction extends JSDocTypeBase, IMethodDefinition { + kind: 'function'; +} + +export type JSDocType = JSDocTypeElement | JSDocTypeConst | JSDocTypeUnion | JSDocTypeFunction; /** * Represents a source location of symbol. @@ -187,17 +218,6 @@ export interface SvelteDataItem extends ISvelteItem { * @since {2.2.0} */ importPath?: string; - - /** - * The list of parameter items of the function expression. - */ - params?: SvelteMethodParamItem[]; - - /** - * The return item of the function expression. This exists if an item with 'name' equal - * to 'returns' or 'return' exists in 'keywords'. - */ - return?: SvelteMethodReturnItem; } export interface SvelteComputedItem extends ISvelteItem { @@ -255,19 +275,7 @@ export interface SvelteMethodReturnItem { description?: string; } -export interface SvelteMethodItem extends ISvelteItem { - /** - * The list of parameter items of the method. - * @since {4.0.0} - */ - params?: SvelteMethodParamItem[]; - - /** - * The return item of the method. This exists if an item with 'name' equal - * to 'returns' or 'return' exists in 'keywords'. - * @since {4.0.0} - */ - return?: SvelteMethodReturnItem; +export interface SvelteMethodItem extends ISvelteItem, IMethodDefinition { } export interface SvelteComponentItem extends ISvelteItem {