Skip to content

Add type transformation #32

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 7 commits into from
Jul 5, 2021
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
1 change: 1 addition & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type ScopeType =
| "pairKey"
| "statement"
| "string"
| "type"
| "value";
export type PieceType = "subtoken" | "character";

Expand Down
24 changes: 3 additions & 21 deletions src/languages/getPojoMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
hasType,
simpleSelectionExtractor,
makeRange,
childNodeMatcher,
getNodeWithLeadingDelimiter,
} from "../nodeMatchers";
import { getKeyNode, getValueNode } from "../treeSitterUtils";

Expand All @@ -27,27 +29,7 @@ export function getPojoMatchers(

return simpleSelectionExtractor(getKeyNode(node)!);
},
value(editor: TextEditor, node: SyntaxNode) {
const valueNode = getValueNode(node);

if (valueNode == null) {
return null;
}

const leadingDelimiterToken = valueNode.previousSibling!;

const leadingDelimiterRange = makeRange(
leadingDelimiterToken.startPosition,
valueNode.startPosition
);

return {
...simpleSelectionExtractor(valueNode),
context: {
leadingDelimiterRange,
},
};
},
value: childNodeMatcher(getValueNode, getNodeWithLeadingDelimiter),
list: hasType(...listTypes),
listElement: delimitedMatcher(
(node) =>
Expand Down
1 change: 1 addition & 0 deletions src/languages/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const nodeMatchers: Record<ScopeType, NodeMatcher> = {
argumentOrParameter: notSupported,
namedFunction: notSupported,
comment: notSupported,
type: notSupported,
};

export default nodeMatchers;
8 changes: 6 additions & 2 deletions src/languages/python.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { SyntaxNode } from "web-tree-sitter";
import { getPojoMatchers } from "./getPojoMatchers";
import {
childNodeMatcher,
delimitedMatcher,
getNodeWithLeadingDelimiter,
hasType,
possiblyWrappedNode,
simpleSelectionExtractor,
} from "../nodeMatchers";
import { NodeMatcher, ScopeType } from "../Types";
import { TextEditor } from "vscode";
import { getDefinitionNode } from "../treeSitterUtils";

// TODO figure out how to properly use super types
Expand Down Expand Up @@ -121,6 +121,9 @@ function possiblyDecoratedDefinition(...typeNames: string[]): NodeMatcher {
);
}

export const getTypeNode = (node: SyntaxNode) =>
node.children.find((child) => child.type === "type") ?? null;

const nodeMatchers: Record<ScopeType, NodeMatcher> = {
...getPojoMatchers(
["dictionary", "dictionary_comprehension"],
Expand All @@ -143,6 +146,7 @@ const nodeMatchers: Record<ScopeType, NodeMatcher> = {
),
namedFunction: possiblyDecoratedDefinition("function_definition"),
comment: hasType("comment"),
type: childNodeMatcher(getTypeNode, getNodeWithLeadingDelimiter),
};

export default nodeMatchers;
19 changes: 19 additions & 0 deletions src/languages/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
hasType,
possiblyWrappedNode,
simpleSelectionExtractor,
getNodeWithLeadingDelimiter,
childNodeMatcher,
} from "../nodeMatchers";
import { NodeMatcher, ScopeType } from "../Types";
import { getDeclarationNode, getValueNode } from "../treeSitterUtils";
Expand Down Expand Up @@ -120,6 +122,13 @@ const isNamedArrowFunction = (node: SyntaxNode) => {
);
};

export const getTypeNode = (node: SyntaxNode) => {
const typeAnnotationNode = node.children.find((child) =>
["type_annotation", "opting_type_annotation"].includes(child.type)
);
return typeAnnotationNode?.lastChild ?? null;
};

const nodeMatchers: Record<ScopeType, NodeMatcher> = {
...getPojoMatchers(
["object"],
Expand All @@ -131,6 +140,16 @@ const nodeMatchers: Record<ScopeType, NodeMatcher> = {
statement: possiblyExportedDeclaration(...STATEMENT_TYPES),
arrowFunction: hasType("arrow_function"),
functionCall: hasType("call_expression", "new_expression"),
type: cascadingMatcher(
// Typed parameters, properties, and functions
childNodeMatcher(getTypeNode, getNodeWithLeadingDelimiter),

// Type alias/interface declarations
possiblyExportedDeclaration(
"type_alias_declaration",
"interface_declaration"
)
),
argumentOrParameter: delimitedMatcher(
(node) =>
(node.parent?.type === "arguments" &&
Expand Down
33 changes: 33 additions & 0 deletions src/nodeMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@ export function hasType(...typeNames: string[]): NodeMatcher {
typeNames.includes(node.type) ? simpleSelectionExtractor(node) : null;
}

export function childNodeMatcher(
getMatchingChildNode: (node: SyntaxNode) => SyntaxNode | null,
extractor: (node: SyntaxNode) => SelectionWithContext
): NodeMatcher {
return (editor: TextEditor, node: SyntaxNode) => {
const returnNode = getMatchingChildNode(node);

if (returnNode == null) {
return null;
}

return extractor(returnNode);
};
}

export function getNodeWithLeadingDelimiter(
node: SyntaxNode
): SelectionWithContext {
const leadingDelimiterToken = node.previousSibling!;

const leadingDelimiterRange = makeRange(
leadingDelimiterToken.startPosition,
node.startPosition
);

return {
...simpleSelectionExtractor(node),
context: {
leadingDelimiterRange,
},
};
}

export const notSupported: NodeMatcher = (
editor: TextEditor,
node: SyntaxNode
Expand Down