From 4ed5edf88dbbedd36ea2aed4ef2fe09af1e3740a Mon Sep 17 00:00:00 2001 From: Tim De Pauw Date: Thu, 8 Mar 2018 01:12:17 +0100 Subject: [PATCH] feat: Support class property @type tag (#1028) --- __tests__/__snapshots__/test.js.snap | 617 +++++++++++++++++- __tests__/fixture/es6-class-property.input.js | 25 + src/parse.js | 4 +- 3 files changed, 644 insertions(+), 2 deletions(-) create mode 100644 __tests__/fixture/es6-class-property.input.js diff --git a/__tests__/__snapshots__/test.js.snap b/__tests__/__snapshots__/test.js.snap index 0360cd9..5e2795e 100644 --- a/__tests__/__snapshots__/test.js.snap +++ b/__tests__/__snapshots__/test.js.snap @@ -12762,6 +12762,10 @@ Array [ ], "throws": Array [], "todos": Array [], + "type": Object { + "name": "string", + "type": "NameExpression", + }, }, ], "static": Array [], @@ -13151,6 +13155,26 @@ Object { }, "type": "paragraph", }, + Object { + "children": Array [ + Object { + "type": "text", + "value": "Type: ", + }, + Object { + "children": Array [ + Object { + "type": "text", + "value": "string", + }, + ], + "identifier": "1", + "referenceType": "full", + "type": "linkReference", + }, + ], + "type": "paragraph", + }, Object { "children": Array [ Object { @@ -13329,6 +13353,567 @@ Object { } `; +exports[`outputs es6-class-property.input.js JSON 1`] = ` +Array [ + Object { + "augments": Array [], + "context": Object { + "loc": Object { + "end": Object { + "column": 1, + "line": 25, + }, + "start": Object { + "column": 0, + "line": 4, + }, + }, + }, + "description": Object { + "children": Array [ + Object { + "children": Array [ + Object { + "position": Object { + "end": Object { + "column": 23, + "line": 1, + "offset": 22, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "text", + "value": "This is for issue 906.", + }, + ], + "position": Object { + "end": Object { + "column": 23, + "line": 1, + "offset": 22, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "paragraph", + }, + ], + "position": Object { + "end": Object { + "column": 23, + "line": 1, + "offset": 22, + }, + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "root", + }, + "errors": Array [], + "examples": Array [], + "kind": "class", + "loc": Object { + "end": Object { + "column": 3, + "line": 3, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "members": Object { + "events": Array [], + "global": Array [], + "inner": Array [], + "instance": Array [ + Object { + "augments": Array [], + "context": Object { + "loc": Object { + "end": Object { + "column": 3, + "line": 11, + }, + "start": Object { + "column": 2, + "line": 9, + }, + }, + }, + "description": Object { + "children": Array [ + Object { + "children": Array [ + Object { + "position": Object { + "end": Object { + "column": 31, + "line": 1, + "offset": 30, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "text", + "value": "This is a read-write property.", + }, + ], + "position": Object { + "end": Object { + "column": 31, + "line": 1, + "offset": 30, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "paragraph", + }, + ], + "position": Object { + "end": Object { + "column": 31, + "line": 1, + "offset": 30, + }, + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "root", + }, + "errors": Array [], + "examples": Array [], + "kind": "member", + "loc": Object { + "end": Object { + "column": 5, + "line": 8, + }, + "start": Object { + "column": 2, + "line": 5, + }, + }, + "memberof": "Issue906", + "members": Object { + "events": Array [], + "global": Array [], + "inner": Array [], + "instance": Array [], + "static": Array [], + }, + "name": "readWriteProp", + "namespace": "Issue906#readWriteProp", + "params": Array [], + "path": Array [ + Object { + "kind": "class", + "name": "Issue906", + }, + Object { + "kind": "member", + "name": "readWriteProp", + "scope": "instance", + }, + ], + "properties": Array [], + "returns": Array [], + "scope": "instance", + "sees": Array [], + "tags": Array [ + Object { + "description": null, + "lineNumber": 2, + "title": "type", + "type": Object { + "name": "boolean", + "type": "NameExpression", + }, + }, + ], + "throws": Array [], + "todos": Array [], + "type": Object { + "name": "boolean", + "type": "NameExpression", + }, + }, + Object { + "augments": Array [], + "context": Object { + "loc": Object { + "end": Object { + "column": 3, + "line": 24, + }, + "start": Object { + "column": 2, + "line": 22, + }, + }, + }, + "description": Object { + "children": Array [ + Object { + "children": Array [ + Object { + "position": Object { + "end": Object { + "column": 30, + "line": 1, + "offset": 29, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "text", + "value": "This is a read-only property.", + }, + ], + "position": Object { + "end": Object { + "column": 30, + "line": 1, + "offset": 29, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "paragraph", + }, + ], + "position": Object { + "end": Object { + "column": 30, + "line": 1, + "offset": 29, + }, + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "root", + }, + "errors": Array [], + "examples": Array [], + "kind": "member", + "loc": Object { + "end": Object { + "column": 5, + "line": 21, + }, + "start": Object { + "column": 2, + "line": 17, + }, + }, + "memberof": "Issue906", + "members": Object { + "events": Array [], + "global": Array [], + "inner": Array [], + "instance": Array [], + "static": Array [], + }, + "name": "readOnlyProp", + "namespace": "Issue906#readOnlyProp", + "params": Array [], + "path": Array [ + Object { + "kind": "class", + "name": "Issue906", + }, + Object { + "kind": "member", + "name": "readOnlyProp", + "scope": "instance", + }, + ], + "properties": Array [], + "readonly": true, + "returns": Array [], + "scope": "instance", + "sees": Array [], + "tags": Array [ + Object { + "description": null, + "lineNumber": 2, + "title": "type", + "type": Object { + "name": "string", + "type": "NameExpression", + }, + }, + Object { + "description": null, + "lineNumber": 3, + "title": "readonly", + }, + ], + "throws": Array [], + "todos": Array [], + "type": Object { + "name": "string", + "type": "NameExpression", + }, + }, + ], + "static": Array [], + }, + "name": "Issue906", + "namespace": "Issue906", + "params": Array [], + "path": Array [ + Object { + "kind": "class", + "name": "Issue906", + }, + ], + "properties": Array [], + "returns": Array [], + "sees": Array [], + "tags": Array [], + "throws": Array [], + "todos": Array [], + }, +] +`; + +exports[`outputs es6-class-property.input.js markdown 1`] = `"[object Object]"`; + +exports[`outputs es6-class-property.input.js markdown AST 1`] = ` +Object { + "children": Array [ + Object { + "type": "html", + "value": "", + }, + Object { + "children": Array [ + Object { + "type": "text", + "value": "Issue906", + }, + ], + "depth": 2, + "type": "heading", + }, + Object { + "children": Array [ + Object { + "position": Position { + "end": Object { + "column": 23, + "line": 1, + "offset": 22, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "text", + "value": "This is for issue 906.", + }, + ], + "position": Position { + "end": Object { + "column": 23, + "line": 1, + "offset": 22, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "paragraph", + }, + Object { + "children": Array [ + Object { + "type": "text", + "value": "readWriteProp", + }, + ], + "depth": 3, + "type": "heading", + }, + Object { + "children": Array [ + Object { + "position": Position { + "end": Object { + "column": 31, + "line": 1, + "offset": 30, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "text", + "value": "This is a read-write property.", + }, + ], + "position": Position { + "end": Object { + "column": 31, + "line": 1, + "offset": 30, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "paragraph", + }, + Object { + "children": Array [ + Object { + "type": "text", + "value": "Type: ", + }, + Object { + "children": Array [ + Object { + "type": "text", + "value": "boolean", + }, + ], + "identifier": "1", + "referenceType": "full", + "type": "linkReference", + }, + ], + "type": "paragraph", + }, + Object { + "children": Array [ + Object { + "type": "text", + "value": "readOnlyProp", + }, + ], + "depth": 3, + "type": "heading", + }, + Object { + "children": Array [ + Object { + "position": Position { + "end": Object { + "column": 30, + "line": 1, + "offset": 29, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "text", + "value": "This is a read-only property.", + }, + ], + "position": Position { + "end": Object { + "column": 30, + "line": 1, + "offset": 29, + }, + "indent": Array [], + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": "paragraph", + }, + Object { + "children": Array [ + Object { + "type": "text", + "value": "Type: ", + }, + Object { + "children": Array [ + Object { + "type": "text", + "value": "string", + }, + ], + "identifier": "2", + "referenceType": "full", + "type": "linkReference", + }, + ], + "type": "paragraph", + }, + Object { + "identifier": "1", + "title": undefined, + "type": "definition", + "url": "https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean", + }, + Object { + "identifier": "2", + "title": undefined, + "type": "definition", + "url": "https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String", + }, + ], + "type": "root", +} +`; + exports[`outputs es6-default2.input.js JSON 1`] = ` Array [ Object { @@ -14495,6 +15080,10 @@ Array [ ], "throws": Array [], "todos": Array [], + "type": Object { + "name": "Object", + "type": "NameExpression", + }, }, ] `; @@ -14553,6 +15142,26 @@ Object { }, "type": "paragraph", }, + Object { + "children": Array [ + Object { + "type": "text", + "value": "Type: ", + }, + Object { + "children": Array [ + Object { + "type": "text", + "value": "Object", + }, + ], + "identifier": "1", + "referenceType": "full", + "type": "linkReference", + }, + ], + "type": "paragraph", + }, Object { "children": Array [ Object { @@ -14651,7 +15260,7 @@ Object { "value": "Event", }, ], - "identifier": "1", + "identifier": "2", "referenceType": "full", "type": "linkReference", }, @@ -14711,6 +15320,12 @@ Object { "identifier": "1", "title": undefined, "type": "definition", + "url": "https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object", + }, + Object { + "identifier": "2", + "title": undefined, + "type": "definition", "url": "https://developer.mozilla.org/docs/Web/API/Event", }, ], diff --git a/__tests__/fixture/es6-class-property.input.js b/__tests__/fixture/es6-class-property.input.js new file mode 100644 index 0000000..ee201c7 --- /dev/null +++ b/__tests__/fixture/es6-class-property.input.js @@ -0,0 +1,25 @@ +/** + * This is for issue 906. + */ +export class Issue906 { + /** + * This is a read-write property. + * @type {boolean} + */ + get readWriteProp() { + return this._rw; + } + + set readWriteProp(value) { + this._rw = value; + } + + /** + * This is a read-only property. + * @type {string} + * @readonly + */ + get readOnlyProp() { + return 'foo'; + } +} diff --git a/src/parse.js b/src/parse.js index 5b5512c..295b661 100644 --- a/src/parse.js +++ b/src/parse.js @@ -386,7 +386,9 @@ const flatteners = { result.todos.push(parseMarkdown(tag.description)); }, tutorial: todo, - type: todo, + type(result, tag) { + result.type = tag.type; + }, typedef: flattenKindShorthand, var: synonym('member'), /**