Skip to content

Commit

Permalink
Add tests for anonymous functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Aug 31, 2021
1 parent d792dd7 commit eee4c61
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/canonicalizeTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import update from "immutability-helper";
import { transformPrimitiveTargets } from "./util/targetUtils";

const scopeTypeAliasToCanonicalName: Record<string, ScopeType> = {
anonymousFunction: "arrowFunction",
arrowFunction: "anonymousFunction",
dictionary: "map",
regex: "regularExpression",
};
Expand Down
43 changes: 31 additions & 12 deletions src/languages/cpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const STATEMENT_TYPES = [
"switch_statement",
"throw_statement",
"try_statement",
"while_statement"
"while_statement",
];

// > curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-cpp/master/src/node-types.json | jq '[.[] | select(.type == "_type_specifier") | .subtypes[].type]'
Expand All @@ -57,34 +57,53 @@ const TYPE_TYPES = [
"struct_specifier",
"template_type",
"type_identifier",
"union_specifier"
"union_specifier",
];

const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
statement: STATEMENT_TYPES,
class: ["class_specifier", "struct_specifier", "enum_specifier", "union_specifier"],
class: [
"class_specifier",
"struct_specifier",
"enum_specifier",
"union_specifier",
],
className: [
"class_specifier[name]", "struct_specifier[name]", "enum_specifier[name]", "union_specifier[name]",
"function_definition[declarator][declarator][namespace]" // void ClassName::method() {}
"class_specifier[name]",
"struct_specifier[name]",
"enum_specifier[name]",
"union_specifier[name]",
"function_definition[declarator][declarator][namespace]", // void ClassName::method() {}
],
ifStatement: "if_statement",
string: "string_literal",
comment: "comment",
arrowFunction: "lambda_expression",
anonymousFunction: "lambda_expression",
list: "initializer_list",
functionCall: "call_expression",
name: ["*[declarator][declarator][name]", "*[declarator][name]", "*[declarator][declarator]", "*[declarator]", "*[name]"],
name: [
"*[declarator][declarator][name]",
"*[declarator][name]",
"*[declarator][declarator]",
"*[declarator]",
"*[name]",
],
namedFunction: ["function_definition", "declaration.function_declarator"],
type: TYPE_TYPES.concat([ "*[type]" ]),
type: TYPE_TYPES.concat(["*[type]"]),
functionName: [
"function_definition[declarator][declarator][name]", // void C::funcName() {}
"function_definition[declarator][declarator]", // void funcName() {}
"declaration.function_declarator![declarator]", // void funcName();
"function_definition[declarator][declarator]", // void funcName() {}
"declaration.function_declarator![declarator]", // void funcName();
],
value: valueMatcher("*[declarator][value]", "*[value]", "assignment_expression[right]", "optional_parameter_declaration[default_value]"),
value: valueMatcher(
"*[declarator][value]",
"*[value]",
"assignment_expression[right]",
"optional_parameter_declaration[default_value]"
),
collectionItem: argumentMatcher("initializer_list"),
argumentOrParameter: argumentMatcher("parameter_list", "argument_list"),
attribute: "attribute"
attribute: "attribute",
};

export default createPatternMatchers(nodeMatchers);
2 changes: 1 addition & 1 deletion src/languages/csharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcher>> = {
getNameNode,
]),
statement: typeMatcher(...STATEMENT_TYPES),
arrowFunction: typeMatcher("lambda_expression"),
anonymousFunction: typeMatcher("lambda_expression"),
functionCall: typeMatcher("invocation_expression"),
argumentOrParameter: matcher(
nodeFinder(
Expand Down
2 changes: 1 addition & 1 deletion src/languages/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
ifStatement: "if_statement",
string: "string_literal",
comment: "comment",
arrowFunction: "lambda_expression",
anonymousFunction: "lambda_expression",
list: "array_initializer",
functionCall: "method_invocation",
map: "block",
Expand Down
2 changes: 1 addition & 1 deletion src/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
string: "string",
collectionKey: "pair[key]",
ifStatement: "if_statement",
arrowFunction: "lambda",
anonymousFunction: "lambda",
functionCall: "call",
comment: "comment",
class: "decorated_definition?.class_definition",
Expand Down
3 changes: 1 addition & 2 deletions src/languages/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
collectionItem: argumentMatcher(...mapTypes, ...listTypes),
value: valueMatcher(),
ifStatement: "if_statement",
arrowFunction: ["arrow_function", "function"],
anonymousFunction: ["arrow_function", "function"],
name: [
"*[name]",
"optional_parameter.identifier!",
Expand Down Expand Up @@ -129,7 +129,6 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
// export default function
// NB: We require export statement because otherwise it is an anonymous
// function
// TODO: Review tests for these cases
"export_statement.function",
// export default arrow
"export_statement.arrow_function",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
spokenForm: clear funk
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: namedFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |
function bar() {
foo(function () {
});
}
selections:
- anchor: {line: 2, character: 6}
active: {line: 2, character: 6}
finalState:
documentContents: |+
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: namedFunction, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
spokenForm: clear funk
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: namedFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |
function bar() {
foo(() => {
});
}
selections:
- anchor: {line: 2, character: 6}
active: {line: 2, character: 6}
finalState:
documentContents: |+
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: namedFunction, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spokenForm: clear lambda
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
foo(() => {
})
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 0}
finalState:
documentContents: foo()
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
thatMark:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spokenForm: clear lambda
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
foo(function () {
})
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 0}
finalState:
documentContents: foo()
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
thatMark:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spokenForm: clear lambda
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
foo(async function () {
})
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 0}
finalState:
documentContents: foo()
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
thatMark:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spokenForm: clear lambda
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
foo(async () => {
})
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 0}
finalState:
documentContents: foo()
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
thatMark:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}}]
2 changes: 1 addition & 1 deletion src/typings/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type Delimiter =

export type ScopeType =
| "argumentOrParameter"
| "arrowFunction"
| "anonymousFunction"
| "attribute"
| "class"
| "className"
Expand Down

0 comments on commit eee4c61

Please sign in to comment.