Skip to content

Commit 51fa35c

Browse files
anandthakkertmcw
authored andcommitted
Infer name for string-literal property keys (#487)
1 parent 8229ff5 commit 51fa35c

File tree

5 files changed

+236
-0
lines changed

5 files changed

+236
-0
lines changed

lib/infer/name.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ module.exports = function () {
3131
comment.name = node.name;
3232
return true;
3333
}
34+
if (node && node.type === 'StringLiteral' && node.value) {
35+
comment.name = node.value;
36+
return true;
37+
}
3438
}
3539

3640
if (comment.context.ast) {
@@ -57,6 +61,21 @@ module.exports = function () {
5761
path.stop();
5862
}
5963
},
64+
/**
65+
* Attempt to extract the name from a string literal that is the `key`
66+
* part of an ObjectProperty node. If the name can be resolved, it
67+
* will stop traversing.
68+
* @param {Object} path ast path
69+
* @returns {undefined} has side-effects
70+
* @private
71+
*/
72+
StringLiteral: function (path) {
73+
if (path.parent.type === 'ObjectProperty' && path.node === path.parent.key) {
74+
if (inferName(path, path.node)) {
75+
path.stop();
76+
}
77+
}
78+
},
6079
/**
6180
* Attempt to extract the name from an Identifier node.
6281
* If the name can be resolved, it will stop traversing.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @alias MyContainerObject
3+
*/
4+
const obj = {
5+
/**
6+
* The foo property
7+
*/
8+
'foo': {
9+
bar: 0
10+
}
11+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
[
2+
{
3+
"description": "",
4+
"tags": [
5+
{
6+
"title": "alias",
7+
"description": null,
8+
"lineNumber": 1,
9+
"name": "MyContainerObject"
10+
}
11+
],
12+
"loc": {
13+
"start": {
14+
"line": 1,
15+
"column": 0
16+
},
17+
"end": {
18+
"line": 3,
19+
"column": 3
20+
}
21+
},
22+
"context": {
23+
"loc": {
24+
"start": {
25+
"line": 4,
26+
"column": 0
27+
},
28+
"end": {
29+
"line": 11,
30+
"column": 1
31+
}
32+
},
33+
"code": "/**\n * @alias MyContainerObject\n */\nconst obj = {\n /**\n * The foo property\n */\n 'foo': {\n bar: 0\n }\n}\n"
34+
},
35+
"errors": [],
36+
"alias": "MyContainerObject",
37+
"name": "MyContainerObject",
38+
"kind": "constant",
39+
"members": {
40+
"instance": [],
41+
"static": []
42+
},
43+
"path": [
44+
{
45+
"name": "MyContainerObject",
46+
"kind": "constant"
47+
}
48+
],
49+
"namespace": "MyContainerObject"
50+
},
51+
{
52+
"description": {
53+
"type": "root",
54+
"children": [
55+
{
56+
"type": "paragraph",
57+
"children": [
58+
{
59+
"type": "text",
60+
"value": "The foo property",
61+
"position": {
62+
"start": {
63+
"line": 1,
64+
"column": 1,
65+
"offset": 0
66+
},
67+
"end": {
68+
"line": 1,
69+
"column": 17,
70+
"offset": 16
71+
},
72+
"indent": []
73+
}
74+
}
75+
],
76+
"position": {
77+
"start": {
78+
"line": 1,
79+
"column": 1,
80+
"offset": 0
81+
},
82+
"end": {
83+
"line": 1,
84+
"column": 17,
85+
"offset": 16
86+
},
87+
"indent": []
88+
}
89+
}
90+
],
91+
"position": {
92+
"start": {
93+
"line": 1,
94+
"column": 1,
95+
"offset": 0
96+
},
97+
"end": {
98+
"line": 1,
99+
"column": 17,
100+
"offset": 16
101+
}
102+
}
103+
},
104+
"tags": [],
105+
"loc": {
106+
"start": {
107+
"line": 5,
108+
"column": 2
109+
},
110+
"end": {
111+
"line": 7,
112+
"column": 5
113+
}
114+
},
115+
"context": {
116+
"loc": {
117+
"start": {
118+
"line": 8,
119+
"column": 2
120+
},
121+
"end": {
122+
"line": 10,
123+
"column": 3
124+
}
125+
},
126+
"code": "/**\n * @alias MyContainerObject\n */\nconst obj = {\n /**\n * The foo property\n */\n 'foo': {\n bar: 0\n }\n}\n"
127+
},
128+
"errors": [],
129+
"name": "foo",
130+
"members": {
131+
"instance": [],
132+
"static": []
133+
},
134+
"path": [
135+
{
136+
"name": "foo"
137+
}
138+
],
139+
"namespace": "foo"
140+
}
141+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# MyContainerObject
2+
3+
# foo
4+
5+
The foo property
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"type": "root",
3+
"children": [
4+
{
5+
"depth": 1,
6+
"type": "heading",
7+
"children": [
8+
{
9+
"type": "text",
10+
"value": "MyContainerObject"
11+
}
12+
]
13+
},
14+
{
15+
"depth": 1,
16+
"type": "heading",
17+
"children": [
18+
{
19+
"type": "text",
20+
"value": "foo"
21+
}
22+
]
23+
},
24+
{
25+
"type": "paragraph",
26+
"children": [
27+
{
28+
"type": "text",
29+
"value": "The foo property",
30+
"position": {
31+
"start": {
32+
"line": 1,
33+
"column": 1,
34+
"offset": 0
35+
},
36+
"end": {
37+
"line": 1,
38+
"column": 17,
39+
"offset": 16
40+
},
41+
"indent": []
42+
}
43+
}
44+
],
45+
"position": {
46+
"start": {
47+
"line": 1,
48+
"column": 1,
49+
"offset": 0
50+
},
51+
"end": {
52+
"line": 1,
53+
"column": 17,
54+
"offset": 16
55+
},
56+
"indent": []
57+
}
58+
}
59+
]
60+
}

0 commit comments

Comments
 (0)