Skip to content

Commit 8c72fe0

Browse files
committed
Fix Ruby method parsing for methods containing brackets
This fixes parsing of access paths for Ruby methods that contain square brackets, such as `Liquid::Context#[]`. The previous implementation would incorrectly stop capturing at the `]` character, resulting in a method name of `[` for an access path of `Method[[]].ReturnValue`. This fixes it by switching to the shared access path parsing code, which correctly handles square brackets.
1 parent 9cd6daf commit 8c72fe0

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

extensions/ql-vscode/src/model-editor/languages/ruby/access-paths.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
import { parseAccessPathTokens } from "../../shared/access-paths";
2+
3+
const methodTokenRegex = /^Method\[(.+)]$/;
4+
15
export function parseRubyMethodFromPath(path: string): string {
2-
const match = path.match(/Method\[([^\]]+)].*/);
6+
const tokens = parseAccessPathTokens(path);
7+
8+
if (tokens.length === 0) {
9+
return "";
10+
}
11+
12+
const match = tokens[0].text.match(methodTokenRegex);
313
if (match) {
414
return match[1];
515
} else {
@@ -11,9 +21,22 @@ export function parseRubyAccessPath(path: string): {
1121
methodName: string;
1222
path: string;
1323
} {
14-
const match = path.match(/Method\[([^\]]+)]\.(.*)/);
24+
const tokens = parseAccessPathTokens(path);
25+
26+
if (tokens.length === 0) {
27+
return { methodName: "", path: "" };
28+
}
29+
30+
const match = tokens[0].text.match(methodTokenRegex);
31+
1532
if (match) {
16-
return { methodName: match[1], path: match[2] };
33+
return {
34+
methodName: match[1],
35+
path: tokens
36+
.slice(1)
37+
.map((token) => token.text)
38+
.join("."),
39+
};
1740
} else {
1841
return { methodName: "", path: "" };
1942
}

0 commit comments

Comments
 (0)