Skip to content

Commit 7a6fd4f

Browse files
committed
fix(parser): override data.context.kind if member kind is unknown
- can occur when an identifier is a keyword Signed-off-by: Lexus Drumgold <[email protected]>
1 parent fceb4f9 commit 7a6fd4f

File tree

5 files changed

+88
-13
lines changed

5 files changed

+88
-13
lines changed

__fixtures__/coordinate-pair.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@ import type Coordinate from './coordinate'
99
* Pair of numbers representing a single point on a two-dimensional grid.
1010
*/
1111
interface CoordinatePair {
12+
/**
13+
* Error handler.
14+
*
15+
* @param {Error} error - Error to handle
16+
* @return {void} Nothing when complete
17+
*/
18+
catch?(error: Error): void
19+
1220
/** x-coordinate. */
1321
x: Coordinate
1422

1523
/** y-coordinate. */
1624
y: Coordinate
1725

26+
/** Coordinate type. */
27+
type?: string
28+
1829
/**
1930
* Returns a string representation of the coordinate.
2031
*

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"@flex-development/tutils": "5.0.1",
6969
"detab": "3.0.1",
7070
"unist-builder": "3.0.0",
71+
"unist-util-source": "4.0.1",
7172
"vfile": "5.3.5",
7273
"vfile-location": "4.0.1"
7374
},

src/__snapshots__/attacher.functional.snap

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ root[2]
437437
`;
438438

439439
exports[`functional:attacher > should create ast for interface declaration 1`] = `
440-
root[5]
440+
root[7]
441441
│ data: {}
442442
├─0 comment[2] (1:1-4:4, 0-77)
443443
│ │ data: {"context":null,"value":"/**\\n * @file Fixtures - CoordinatePair\\n * @module fixtures/CoordinatePair\\n */"}
@@ -446,22 +446,34 @@ root[5]
446446
│ └─1 block-tag[0] (3:4-3:35, 42-73)
447447
│ data: {"tag":"@module","text":"fixtures/CoordinatePair","value":"@module fixtures/CoordinatePair"}
448448
├─1 comment[1] (8:1-10:4, 123-204)
449-
│ │ data: {"context":{"identifier":"CoordinatePair","kind":"interface","members":["x","y","toString"],"modifiers":[],"parent":null,"position":{"end":{"line":24,"column":2,"offset":465},"start":{"line":11,"column":1,"offset":205}}},"value":"/**\\n * Pair of numbers representing a single point on a two-dimensional grid.\\n */"}
449+
│ │ data: {"context":{"identifier":"CoordinatePair","kind":"interface","members":["catch","x","y","type","toString"],"modifiers":[],"parent":null,"position":{"end":{"line":35,"column":2,"offset":661},"start":{"line":11,"column":1,"offset":205}}},"value":"/**\\n * Pair of numbers representing a single point on a two-dimensional grid.\\n */"}
450450
│ └─0 implicit-description[0] (9:4-9:74, 130-200)
451451
│ data: {"value":"Pair of numbers representing a single point on a two-dimensional grid."}
452-
├─2 comment[1] (12:3-12:23, 234-254)
453-
│ │ data: {"context":{"identifier":"x","kind":"property-signature","members":[],"modifiers":[],"parent":"CoordinatePair","position":{"end":{"line":13,"column":16,"offset":270},"start":{"line":13,"column":3,"offset":257}}},"value":"/** x-coordinate. */"}
454-
│ └─0 implicit-description[0] (12:7-12:20, 238-251)
452+
├─2 comment[3] (12:3-17:6, 234-354)
453+
│ │ data: {"context":{"identifier":"catch","kind":"method-signature","members":[],"modifiers":[],"parent":"CoordinatePair","position":{"end":{"line":18,"column":29,"offset":383},"start":{"line":18,"column":3,"offset":357}}},"value":"/**\\n * Error handler.\\n *\\n * @param {Error} error - Error to handle\\n * @return {void} Nothing when complete\\n */"}
454+
│ ├─0 implicit-description[0] (13:6-13:20, 243-257)
455+
│ │ data: {"value":"Error handler."}
456+
│ ├─1 block-tag[0] (15:6-15:44, 268-306)
457+
│ │ data: {"tag":"@param","text":"{Error} error - Error to handle","value":"@param {Error} error - Error to handle"}
458+
│ └─2 block-tag[0] (16:6-16:42, 312-348)
459+
│ data: {"tag":"@return","text":"{void} Nothing when complete","value":"@return {void} Nothing when complete"}
460+
├─3 comment[1] (20:3-20:23, 387-407)
461+
│ │ data: {"context":{"identifier":"x","kind":"property-signature","members":[],"modifiers":[],"parent":"CoordinatePair","position":{"end":{"line":21,"column":16,"offset":423},"start":{"line":21,"column":3,"offset":410}}},"value":"/** x-coordinate. */"}
462+
│ └─0 implicit-description[0] (20:7-20:20, 391-404)
455463
│ data: {"value":"x-coordinate."}
456-
├─3 comment[1] (15:3-15:23, 274-294)
457-
│ │ data: {"context":{"identifier":"y","kind":"property-signature","members":[],"modifiers":[],"parent":"CoordinatePair","position":{"end":{"line":16,"column":16,"offset":310},"start":{"line":16,"column":3,"offset":297}}},"value":"/** y-coordinate. */"}
458-
│ └─0 implicit-description[0] (15:7-15:20, 278-291)
464+
├─4 comment[1] (23:3-23:23, 427-447)
465+
│ │ data: {"context":{"identifier":"y","kind":"property-signature","members":[],"modifiers":[],"parent":"CoordinatePair","position":{"end":{"line":24,"column":16,"offset":463},"start":{"line":24,"column":3,"offset":450}}},"value":"/** y-coordinate. */"}
466+
│ └─0 implicit-description[0] (23:7-23:20, 431-444)
459467
│ data: {"value":"y-coordinate."}
460-
└─4 comment[2] (18:3-22:6, 314-442)
461-
│ data: {"context":{"identifier":"toString","kind":"method-signature","members":[],"modifiers":[],"parent":"CoordinatePair","position":{"end":{"line":23,"column":21,"offset":463},"start":{"line":23,"column":3,"offset":445}}},"value":"/**\\n * Returns a string representation of the coordinate.\\n *\\n * @return {string} String representation of coordinate\\n */"}
462-
├─0 implicit-description[0] (19:6-19:56, 323-373)
468+
├─5 comment[1] (26:3-26:26, 467-490)
469+
│ │ data: {"context":{"identifier":"type","kind":"property-signature","members":[],"modifiers":[],"parent":"CoordinatePair","position":{"end":{"line":27,"column":16,"offset":506},"start":{"line":27,"column":3,"offset":493}}},"value":"/** Coordinate type. */"}
470+
│ └─0 implicit-description[0] (26:7-26:23, 471-487)
471+
│ data: {"value":"Coordinate type."}
472+
└─6 comment[2] (29:3-33:6, 510-638)
473+
│ data: {"context":{"identifier":"toString","kind":"method-signature","members":[],"modifiers":[],"parent":"CoordinatePair","position":{"end":{"line":34,"column":21,"offset":659},"start":{"line":34,"column":3,"offset":641}}},"value":"/**\\n * Returns a string representation of the coordinate.\\n *\\n * @return {string} String representation of coordinate\\n */"}
474+
├─0 implicit-description[0] (30:6-30:56, 519-569)
463475
│ data: {"value":"Returns a string representation of the coordinate."}
464-
└─1 block-tag[0] (21:6-21:58, 384-436)
476+
└─1 block-tag[0] (32:6-32:58, 580-632)
465477
data: {"tag":"@return","text":"{string} String representation of coordinate","value":"@return {string} String representation of coordinate"}
466478
`;
467479

src/parser.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import type { Nullable } from '@flex-development/tutils'
77
import { detab } from 'detab'
8+
import type { Node, Position } from 'unist'
89
import { u } from 'unist-builder'
10+
import { source } from 'unist-util-source'
911
import type { VFile } from 'vfile'
1012
import { location } from 'vfile-location'
1113
import type { Context } from './data'
@@ -395,6 +397,29 @@ class Parser extends AbstractParser<Root> {
395397

396398
// exactly one ident over => member
397399
if (n.position.start.column === start.column + indent_size) {
400+
// override unknown member kind
401+
// this can happen when a member's identifier is a keyword
402+
if (n.data.context?.kind === Kind.UNKNOWN) {
403+
const { position } = n.data.context
404+
405+
/**
406+
* Declaration source code.
407+
*
408+
* @const {string} source
409+
*/
410+
const source: string = this.source(position)!
411+
412+
// reset unknown member kind
413+
switch (true) {
414+
case !!METHOD_REGEX.exec(source):
415+
n.data.context.kind = 'method' as Kind
416+
break
417+
case !!MEMBER_OR_PROPERTY_REGEX.exec(source):
418+
n.data.context.kind = 'property' as Kind
419+
break
420+
}
421+
}
422+
398423
// ensure member kind is Kind
399424
switch (n.data.context?.kind) {
400425
case 'method' as Kind:
@@ -548,6 +573,20 @@ class Parser extends AbstractParser<Root> {
548573
}
549574
}
550575

576+
/**
577+
* Retrieves the source code of a node or position.
578+
*
579+
* @see https://github.com/syntax-tree/unist-util-source
580+
*
581+
* @protected
582+
*
583+
* @param {Node | Position} value - Node or position
584+
* @return {Nullable<string>} Source code or `null`
585+
*/
586+
protected source(value: Node | Position): Nullable<string> {
587+
return source(value, this.file)
588+
}
589+
551590
/**
552591
* Removes comment delimiters (`/**`, `*\/`, ` * `) from `value`.
553592
*

yarn.lock

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ __metadata:
861861
unified: "npm:10.1.2"
862862
unist-builder: "npm:3.0.0"
863863
unist-util-inspect: "npm:7.0.1"
864+
unist-util-source: "npm:4.0.1"
864865
version-bump-prompt: "npm:6.1.0"
865866
vfile: "npm:5.3.5"
866867
vfile-location: "npm:4.0.1"
@@ -7900,6 +7901,17 @@ __metadata:
79007901
languageName: node
79017902
linkType: hard
79027903

7904+
"unist-util-source@npm:4.0.1":
7905+
version: 4.0.1
7906+
resolution: "unist-util-source@npm:4.0.1"
7907+
dependencies:
7908+
"@types/unist": "npm:^2.0.0"
7909+
vfile: "npm:^5.0.0"
7910+
vfile-location: "npm:^4.0.0"
7911+
checksum: 926c41a1c472eadeadbe0712ca3e97492db2d0c8f5cc079eb62e4fcf9d85eab2c90d52d57d180e1275ac911a9afcf29eb947bb5b6e0981df26eacf3ff2d8fd13
7912+
languageName: node
7913+
linkType: hard
7914+
79037915
"unist-util-stringify-position@npm:^2.0.0":
79047916
version: 2.0.3
79057917
resolution: "unist-util-stringify-position@npm:2.0.3"
@@ -8030,7 +8042,7 @@ __metadata:
80308042
languageName: node
80318043
linkType: hard
80328044

8033-
"vfile-location@npm:4.0.1":
8045+
"vfile-location@npm:4.0.1, vfile-location@npm:^4.0.0":
80348046
version: 4.0.1
80358047
resolution: "vfile-location@npm:4.0.1"
80368048
dependencies:

0 commit comments

Comments
 (0)