Skip to content

Commit

Permalink
Auto api better query (#1010)
Browse files Browse the repository at this point in the history
* fix(auto api): adds missing functionality
* only matches types with Public prefix
* query now accepts extended (think "&" operator) and simple types

* fix(api-table/auto): removes Props suffix
  • Loading branch information
TheMcnafaha authored Nov 27, 2024
1 parent 35d75f6 commit 4b4d50e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
31 changes: 17 additions & 14 deletions apps/website/auto-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,30 @@ const parser = new Parser();

/**
* Tree-Sitter query docs: https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax
* Pay particular attention to the following sections: capturing nodes, wildcard nodes, and anchors.
* Pay particular attention to the following sections: capturing nodes, wildcard nodes,predicates,alternations, and anchors.
*
* To have a way of being able to see the Tree-Sitter AST in real-time: the ideal setup comes included in Neovim. In ex mode, simply run
* the command below and you'll have the file's AST viewer open in real-time: `:InspectTree`
**/

const commentPropType = `
(object_type
(comment) @comment
.
(property_signature
name: (_) @prop
type: (type_annotation (_) @type)
)
)`;
const nestedTypeDef = `(_ ${commentPropType})`;
const query = new Query(
TS.tsx,
`(type_alias_declaration
name: (type_identifier) @subComponentName
(intersection_type
(object_type
(comment) @comment
.
(property_signature
name: (_) @prop
type: (type_annotation (_) @type)
)
)
)
)
name: (type_identifier) @subComponentName (#match? @subComponentName "^Public")
[
(${nestedTypeDef})
(${commentPropType})
]
)
`,
);

Expand Down
3 changes: 2 additions & 1 deletion apps/website/src/components/api-table/auto-api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const currentHeader = $(() => {
});

const currentSubHeader = $((text: string) => {
let subHeader = text.replace(/(p|P)rops/, '');
const removePublicPrefix = text.replace(/^Public/, '');
let subHeader = removePublicPrefix.replace(/(p|P)rops/, '');
const hasCapital = /[a-z][A-Z]/.exec(subHeader)?.index;
if (hasCapital != undefined) {
subHeader =
Expand Down

0 comments on commit 4b4d50e

Please sign in to comment.