Skip to content

Infer class properties #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/infer/kind.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ module.exports = function () {
} else if (t.isExpressionStatement(path)) {
// module.exports = function() {}
findKind(path.node.expression.right);
} else if (t.isClassProperty(path)) {
comment.kind = 'member';
} else if (t.isProperty(path)) {
// { foo: function() {} }
findKind(path.node.value);
Expand Down
27 changes: 11 additions & 16 deletions lib/infer/membership.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ module.exports = function () {
/*
* Same as above but for `b: c` vs `b: function () {}`.
*/
if (n.isProperty(path.node) &&
if (n.isObjectProperty(path.node) &&
n.isIdentifier(path.node.key)) {
path = path.get('key');
}
Expand Down Expand Up @@ -221,23 +221,18 @@ module.exports = function () {
}

// class Foo { bar() { } }
if (n.isClassMethod(path) &&
n.isClassBody(path.parentPath) &&
n.isClassDeclaration(path.parentPath.parentPath)) {
identifiers = [path.parentPath.parentPath.node.id.name];
var scope = 'instance';
if (path.node.static == true) {
scope = 'static';
}
inferMembershipFromIdentifiers(comment, identifiers, scope);
}

// var Foo = class { bar() { } }
if (n.isClassMethod(path) &&
// class Foo { prop: T }
// var Foo = class { prop: T }
if ((n.isClassMethod(path) || n.isClassProperty(path)) &&
n.isClassBody(path.parentPath) &&
n.isClassExpression(path.parentPath.parentPath)) {
identifiers = extractIdentifiers(path.parentPath.parentPath.parentPath.get('left'));
scope = 'instance';
n.isClass(path.parentPath.parentPath)) {
if (n.isExpression(path.parentPath.parentPath)) {
identifiers = extractIdentifiers(path.parentPath.parentPath.parentPath.get('left'));
} else {
identifiers = [path.parentPath.parentPath.node.id.name];
}
var scope = 'instance';
if (path.node.static == true) {
scope = 'static';
}
Expand Down
189 changes: 99 additions & 90 deletions test/fixture/es6-import.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,105 @@
"kind": "class",
"members": {
"instance": [
{
"description": {
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "This is a property of the sink.",
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 32,
"offset": 31
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 32,
"offset": 31
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 32,
"offset": 31
}
}
},
"tags": [],
"loc": {
"start": {
"line": 24,
"column": 2
},
"end": {
"line": 26,
"column": 5
}
},
"context": {
"loc": {
"start": {
"line": 27,
"column": 2
},
"end": {
"line": 27,
"column": 18
}
},
"code": "/**\n * This function destructures with defaults.\n */\nfunction destructure({phoneNumbers = [], emailAddresses = [], ...params} = {}) {\n}\n\n/**\n * Similar, but with an array\n */\nfunction destructure([a, b, c]) {\n}\n\n/**\n * This function returns the number one.\n * @param {Array<Number>} a an array of numbers\n * @returns {Number} numberone\n */\nvar multiply = (a, b) => a * b;\n\n/**\n * This is a sink\n */\nclass Sink {\n /**\n * This is a property of the sink.\n */\n staticProp = 42;\n\n /**\n * Is it empty\n */\n empty() {\n return 1;\n }\n\n /**\n * This method says hello\n */\n static hello() {\n return 'hello';\n }\n\n /**\n * This is a getter method: it should be documented\n * as a property.\n */\n get aGetter() {\n return 42;\n }\n\n /**\n * @param {number} height the height of the thing\n * @param {number} width the width of the thing\n */\n constructor(height, width) {\n this.height = height;\n this.width = width;\n }\n}\n\n/**\n * This function takes rest params\n */\nfunction functionWithRest(...someParams) {\n}\n\n/**\n * So does this one, with types\n */\nfunction functionWithRestAndType(...someParams: number) {\n}\n\n// FUNCTION TYPES\n\n/**\n * This is an async method\n */\nasync function foo() { }\n\nexport default multiply;\n\n/**\n * @public\n */\nexport default (thisIsTheArgument) => {};\n\n/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nmodule.exports = () => (<p>hello</p>);\n\n/**\n * This tests our support of optional parameters in ES6\n */\nfunction veryImportantTransform(foo = 'bar') {\n return \"42\";\n}\n\n// ACCESS LEVELS\n\n/**\n * A private function\n * @private\n */\nfunction iAmPrivate() { }\n\n/**\n * A protected function\n * @protected\n */\nfunction iAmProtected() { }\n\n/**\n * A public function\n * @public\n */\nfunction iAmPublic() { }\n\n/**\n * A private function using the access tag\n * @access private\n */\nfunction iAmAccessPrivate() { }\n\n/**\n * This is re-exported\n */\nexport { execute } from 'external-module';\n"
},
"errors": [],
"name": "staticProp",
"kind": "member",
"memberof": "Sink",
"scope": "instance",
"members": {
"instance": [],
"static": []
},
"path": [
{
"name": "Sink",
"kind": "class"
},
{
"name": "staticProp",
"kind": "member",
"scope": "instance"
}
],
"namespace": "Sink#staticProp"
},
{
"description": {
"type": "root",
Expand Down Expand Up @@ -1344,96 +1443,6 @@
],
"namespace": "Sink"
},
{
"description": {
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "This is a property of the sink.",
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 32,
"offset": 31
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 32,
"offset": 31
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 32,
"offset": 31
}
}
},
"tags": [],
"loc": {
"start": {
"line": 24,
"column": 2
},
"end": {
"line": 26,
"column": 5
}
},
"context": {
"loc": {
"start": {
"line": 27,
"column": 2
},
"end": {
"line": 27,
"column": 18
}
},
"code": "/**\n * This function destructures with defaults.\n */\nfunction destructure({phoneNumbers = [], emailAddresses = [], ...params} = {}) {\n}\n\n/**\n * Similar, but with an array\n */\nfunction destructure([a, b, c]) {\n}\n\n/**\n * This function returns the number one.\n * @param {Array<Number>} a an array of numbers\n * @returns {Number} numberone\n */\nvar multiply = (a, b) => a * b;\n\n/**\n * This is a sink\n */\nclass Sink {\n /**\n * This is a property of the sink.\n */\n staticProp = 42;\n\n /**\n * Is it empty\n */\n empty() {\n return 1;\n }\n\n /**\n * This method says hello\n */\n static hello() {\n return 'hello';\n }\n\n /**\n * This is a getter method: it should be documented\n * as a property.\n */\n get aGetter() {\n return 42;\n }\n\n /**\n * @param {number} height the height of the thing\n * @param {number} width the width of the thing\n */\n constructor(height, width) {\n this.height = height;\n this.width = width;\n }\n}\n\n/**\n * This function takes rest params\n */\nfunction functionWithRest(...someParams) {\n}\n\n/**\n * So does this one, with types\n */\nfunction functionWithRestAndType(...someParams: number) {\n}\n\n// FUNCTION TYPES\n\n/**\n * This is an async method\n */\nasync function foo() { }\n\nexport default multiply;\n\n/**\n * @public\n */\nexport default (thisIsTheArgument) => {};\n\n/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nmodule.exports = () => (<p>hello</p>);\n\n/**\n * This tests our support of optional parameters in ES6\n */\nfunction veryImportantTransform(foo = 'bar') {\n return \"42\";\n}\n\n// ACCESS LEVELS\n\n/**\n * A private function\n * @private\n */\nfunction iAmPrivate() { }\n\n/**\n * A protected function\n * @protected\n */\nfunction iAmProtected() { }\n\n/**\n * A public function\n * @public\n */\nfunction iAmPublic() { }\n\n/**\n * A private function using the access tag\n * @access private\n */\nfunction iAmAccessPrivate() { }\n\n/**\n * This is re-exported\n */\nexport { execute } from 'external-module';\n"
},
"errors": [],
"name": "staticProp",
"members": {
"instance": [],
"static": []
},
"path": [
{
"name": "staticProp"
}
],
"namespace": "staticProp"
},
{
"description": {
"type": "root",
Expand Down
8 changes: 4 additions & 4 deletions test/fixture/es6-import.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Returns **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer

This is a sink

## staticProp

This is a property of the sink.

## empty

Is it empty
Expand All @@ -69,10 +73,6 @@ as a property.

This method says hello

# staticProp

This is a property of the sink.

# functionWithRest

This function takes rest params
Expand Down
90 changes: 45 additions & 45 deletions test/fixture/es6-import.output.md.json
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,51 @@
"indent": []
}
},
{
"depth": 2,
"type": "heading",
"children": [
{
"type": "text",
"value": "staticProp"
}
]
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "This is a property of the sink.",
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 32,
"offset": 31
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 32,
"offset": 31
},
"indent": []
}
},
{
"depth": 2,
"type": "heading",
Expand Down Expand Up @@ -1185,51 +1230,6 @@
"indent": []
}
},
{
"depth": 1,
"type": "heading",
"children": [
{
"type": "text",
"value": "staticProp"
}
]
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "This is a property of the sink.",
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 32,
"offset": 31
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 32,
"offset": 31
},
"indent": []
}
},
{
"depth": 1,
"type": "heading",
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy