Skip to content

Commit 28392f1

Browse files
committed
Treat interface keys as members instead of properties
1 parent 3e3f336 commit 28392f1

17 files changed

+653
-192
lines changed

__tests__/__snapshots__/test.js.snap

Lines changed: 337 additions & 89 deletions
Large diffs are not rendered by default.

__tests__/fixture/interface.input.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* This is my interface.
33
*/
44
interface Foo extends Bar, Baz {
5+
/** This is prop 1 */
56
prop1: number,
7+
/** This is prop 2 */
68
prop2: string
79
}

__tests__/lib/infer/__snapshots__/params.js.snap

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,51 @@ Array [
206206
]
207207
`;
208208

209+
exports[`inferParams (typescript) 11`] = `
210+
Array [
211+
Object {
212+
"lineNumber": 1,
213+
"name": "v",
214+
"title": "param",
215+
"type": Object {
216+
"name": "string",
217+
"type": "NameExpression",
218+
},
219+
},
220+
]
221+
`;
222+
223+
exports[`inferParams (typescript) 12`] = `
224+
Array [
225+
Object {
226+
"lineNumber": 1,
227+
"name": "v",
228+
"title": "param",
229+
"type": Object {
230+
"name": "string",
231+
"type": "NameExpression",
232+
},
233+
},
234+
]
235+
`;
236+
237+
exports[`inferParams (typescript) 13`] = `
238+
Array [
239+
Object {
240+
"lineNumber": 1,
241+
"name": "v",
242+
"title": "param",
243+
"type": Object {
244+
"expression": Object {
245+
"name": "string",
246+
"type": "NameExpression",
247+
},
248+
"type": "RestType",
249+
},
250+
},
251+
]
252+
`;
253+
209254
exports[`inferParams 1`] = `
210255
Array [
211256
Object {
@@ -438,6 +483,51 @@ Array [
438483
]
439484
`;
440485

486+
exports[`inferParams 9`] = `
487+
Array [
488+
Object {
489+
"lineNumber": 1,
490+
"name": "v",
491+
"title": "param",
492+
"type": Object {
493+
"name": "string",
494+
"type": "NameExpression",
495+
},
496+
},
497+
]
498+
`;
499+
500+
exports[`inferParams 10`] = `
501+
Array [
502+
Object {
503+
"lineNumber": 1,
504+
"name": "v",
505+
"title": "param",
506+
"type": Object {
507+
"name": "string",
508+
"type": "NameExpression",
509+
},
510+
},
511+
]
512+
`;
513+
514+
exports[`inferParams 11`] = `
515+
Array [
516+
Object {
517+
"lineNumber": 1,
518+
"name": "v",
519+
"title": "param",
520+
"type": Object {
521+
"expression": Object {
522+
"name": "string",
523+
"type": "NameExpression",
524+
},
525+
"type": "RestType",
526+
},
527+
},
528+
]
529+
`;
530+
441531
exports[`mergeTrees 1`] = `
442532
Object {
443533
"errors": Array [

__tests__/lib/infer/kind.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ test('inferKind', function() {
137137
expect(abstractMethod.kind).toBe('function');
138138
expect(abstractMethod.abstract).toBe(true);
139139

140+
expect(inferKind(toComment('interface Foo { /** b */ b(v): void; }')).kind).toBe(
141+
'function'
142+
);
143+
144+
expect(inferKind(toComment('interface Foo { /** b */ b: string; }')).kind).toBe(
145+
'member'
146+
);
147+
148+
expect(inferKind(toComment('interface Foo { /** b */ b(v): void; }', 'test.ts')).kind).toBe(
149+
'function'
150+
);
151+
152+
expect(inferKind(toComment('interface Foo { /** b */ b: string; }', 'test.ts')).kind).toBe(
153+
'member'
154+
);
155+
140156
expect(
141157
inferKind(
142158
toComment(function() {

__tests__/lib/infer/membership.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,38 @@ test('inferMembership - explicit', function() {
324324
scope: 'instance'
325325
});
326326

327+
expect(
328+
pick(
329+
evaluate(`
330+
/** @memberof bar */
331+
interface Foo {
332+
/** */
333+
baz(): string;
334+
}
335+
`)[1], // [0] is an description for class Foo
336+
['memberof', 'scope']
337+
)
338+
).toEqual({
339+
memberof: 'bar.Foo',
340+
scope: 'instance'
341+
});
342+
343+
expect(
344+
pick(
345+
evaluate(`
346+
/** @memberof bar */
347+
interface Foo {
348+
/** */
349+
baz(): string;
350+
}
351+
`, 'test.ts')[1], // [0] is an description for class Foo
352+
['memberof', 'scope']
353+
)
354+
).toEqual({
355+
memberof: 'bar.Foo',
356+
scope: 'instance'
357+
});
358+
327359
expect(
328360
pick(
329361
evaluate(function() {

__tests__/lib/infer/params.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ test('inferParams', function() {
202202
`
203203
).params
204204
).toMatchSnapshot();
205+
206+
expect(
207+
evaluate(`interface Foo { /** b */ b(v: string): void; }`).params
208+
).toMatchSnapshot();
209+
210+
expect(
211+
evaluate(`type Foo = { /** b */ b(v: string): void }`).params
212+
).toMatchSnapshot();
213+
214+
expect(
215+
evaluate(`interface Foo { /** b */ b(...v: string): void; }`).params
216+
).toMatchSnapshot();
205217
});
206218

207219
test('inferParams (typescript)', function() {
@@ -261,4 +273,16 @@ test('inferParams (typescript)', function() {
261273
expect(
262274
evaluate(`abstract class Foo { /** */ abstract f(a?: string); }`, 'test.ts').params
263275
).toMatchSnapshot();
276+
277+
expect(
278+
evaluate(`interface Foo { /** b */ b(v: string): void; }`, 'test.ts').params
279+
).toMatchSnapshot();
280+
281+
expect(
282+
evaluate(`type Foo = { /** b */ b(v: string): void }`, 'test.ts').params
283+
).toMatchSnapshot();
284+
285+
expect(
286+
evaluate(`interface Foo { /** b */ b(...v: string): void; }`, 'test.ts').params
287+
).toMatchSnapshot();
264288
});

__tests__/lib/infer/properties.js

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -72,47 +72,6 @@ test('inferProperties (flow)', function() {
7272
}
7373
}
7474
]);
75-
76-
expect(
77-
evaluate('/** */interface a { b: 1, c: { d: 2 } };').properties
78-
).toEqual([
79-
{
80-
lineNumber: 1,
81-
name: 'b',
82-
title: 'property',
83-
type: {
84-
type: 'NumericLiteralType',
85-
value: 1
86-
}
87-
},
88-
{
89-
lineNumber: 1,
90-
name: 'c',
91-
title: 'property',
92-
type: {
93-
fields: [
94-
{
95-
key: 'd',
96-
type: 'FieldType',
97-
value: {
98-
type: 'NumericLiteralType',
99-
value: 2
100-
}
101-
}
102-
],
103-
type: 'RecordType'
104-
}
105-
},
106-
{
107-
lineNumber: 1,
108-
name: 'c.d',
109-
title: 'property',
110-
type: {
111-
type: 'NumericLiteralType',
112-
value: 2
113-
}
114-
}
115-
]);
11675
});
11776

11877
test('inferProperties (typescript)', function() {
@@ -171,45 +130,4 @@ test('inferProperties (typescript)', function() {
171130
}
172131
}
173132
]);
174-
175-
expect(
176-
evaluate('/** */interface a { b: 1, c: { d: 2 } };', 'test.ts').properties
177-
).toEqual([
178-
{
179-
lineNumber: 1,
180-
name: 'b',
181-
title: 'property',
182-
type: {
183-
type: 'NumericLiteralType',
184-
value: 1
185-
}
186-
},
187-
{
188-
lineNumber: 1,
189-
name: 'c',
190-
title: 'property',
191-
type: {
192-
fields: [
193-
{
194-
key: 'd',
195-
type: 'FieldType',
196-
value: {
197-
type: 'NumericLiteralType',
198-
value: 2
199-
}
200-
}
201-
],
202-
type: 'RecordType'
203-
}
204-
},
205-
{
206-
lineNumber: 1,
207-
name: 'c.d',
208-
title: 'property',
209-
type: {
210-
type: 'NumericLiteralType',
211-
value: 2
212-
}
213-
}
214-
]);
215133
});

__tests__/lib/infer/return.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ test('inferReturn (flow)', function() {
6363
}
6464
}
6565
]);
66+
67+
expect(
68+
evaluate('interface Foo { /** */ bar(): string; }').returns[0].type
69+
).toEqual({
70+
name: 'string',
71+
type: 'NameExpression'
72+
});
73+
74+
expect(
75+
evaluate('type Foo = { /** */ bar(): string; }').returns[0].type
76+
).toEqual({
77+
name: 'string',
78+
type: 'NameExpression'
79+
});
6680
});
6781

6882
test('inferReturn (typescript)', function() {
@@ -132,4 +146,18 @@ test('inferReturn (typescript)', function() {
132146
}
133147
}
134148
]);
149+
150+
expect(
151+
evaluate('interface Foo { /** */ bar(): string; }', 'test.ts').returns[0].type
152+
).toEqual({
153+
name: 'string',
154+
type: 'NameExpression'
155+
});
156+
157+
expect(
158+
evaluate('type Foo = { /** */ bar(): string; }', 'test.ts').returns[0].type
159+
).toEqual({
160+
name: 'string',
161+
type: 'NameExpression'
162+
});
135163
});

__tests__/lib/infer/type.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ test('inferType (flow)', function() {
126126
name: 'boolean',
127127
type: 'NameExpression'
128128
});
129+
130+
expect(
131+
evaluate('interface Foo { /** */ bar: string; }').type
132+
).toEqual({
133+
name: 'string',
134+
type: 'NameExpression'
135+
});
136+
137+
expect(
138+
evaluate('type Foo = { /** */ bar: string; }').type
139+
).toEqual({
140+
name: 'string',
141+
type: 'NameExpression'
142+
});
129143
});
130144

131145
test('inferType (typescript)', function() {
@@ -248,4 +262,18 @@ test('inferType (typescript)', function() {
248262
name: 'boolean',
249263
type: 'NameExpression'
250264
});
265+
266+
expect(
267+
evaluate('interface Foo { /** */ bar: string; }', 'test.ts').type
268+
).toEqual({
269+
name: 'string',
270+
type: 'NameExpression'
271+
});
272+
273+
expect(
274+
evaluate('type Foo = { /** */ bar: string; }', 'test.ts').type
275+
).toEqual({
276+
name: 'string',
277+
type: 'NameExpression'
278+
});
251279
});

src/infer/finders.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function findTarget(path) {
2626
} else if (t.isExpressionStatement(path)) {
2727
// foo.x = TARGET
2828
path = path.get('expression').get('right');
29-
} else if (t.isObjectProperty(path)) {
29+
} else if (t.isObjectProperty(path) || t.isObjectTypeProperty(path)) {
3030
// var foo = { x: TARGET }; object property
3131
path = path.get('value');
3232
} else if (t.isClassProperty(path) && path.get('value').node) {

0 commit comments

Comments
 (0)