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.
This commit is contained in:
Koen Vlaswinkel
2024-01-31 14:07:54 +01:00
parent 9cd6dafdf4
commit 8c72fe0f8d

View File

@@ -1,5 +1,15 @@
import { parseAccessPathTokens } from "../../shared/access-paths";
const methodTokenRegex = /^Method\[(.+)]$/;
export function parseRubyMethodFromPath(path: string): string {
const match = path.match(/Method\[([^\]]+)].*/);
const tokens = parseAccessPathTokens(path);
if (tokens.length === 0) {
return "";
}
const match = tokens[0].text.match(methodTokenRegex);
if (match) {
return match[1];
} else {
@@ -11,9 +21,22 @@ export function parseRubyAccessPath(path: string): {
methodName: string;
path: string;
} {
const match = path.match(/Method\[([^\]]+)]\.(.*)/);
const tokens = parseAccessPathTokens(path);
if (tokens.length === 0) {
return { methodName: "", path: "" };
}
const match = tokens[0].text.match(methodTokenRegex);
if (match) {
return { methodName: match[1], path: match[2] };
return {
methodName: match[1],
path: tokens
.slice(1)
.map((token) => token.text)
.join("."),
};
} else {
return { methodName: "", path: "" };
}