Skip to content

Commit

Permalink
feat: small refactoring in typings
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprey committed Dec 14, 2021
1 parent 28b288e commit cd14754
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 47 deletions.
8 changes: 7 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/v3/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion lib/v3/v3-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -82,7 +84,7 @@ function parseParams(nodeParams) {
type: inferredType,
description: null,
optional: true,
defaultValue: param.right.raw
defaultValue: param.right.raw,
});
}
});
Expand Down
39 changes: 21 additions & 18 deletions test/svelte3/integration/data/data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,19 @@ 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;
expect(data0params0.type.type).to.equal('number');
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;
Expand All @@ -249,60 +250,62 @@ 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');
expect(data1params0.type.type).to.equal('number');
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');
expect(data1params1.type.type).to.equal('number');
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');
expect(data2params0.type.type).to.equal('number');
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');
expect(data2params1.type.type).to.equal('number');
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 => {
Expand Down
58 changes: 33 additions & 25 deletions typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cd14754

Please sign in to comment.