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:
@@ -1,5 +1,15 @@
|
|||||||
|
import { parseAccessPathTokens } from "../../shared/access-paths";
|
||||||
|
|
||||||
|
const methodTokenRegex = /^Method\[(.+)]$/;
|
||||||
|
|
||||||
export function parseRubyMethodFromPath(path: string): string {
|
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) {
|
if (match) {
|
||||||
return match[1];
|
return match[1];
|
||||||
} else {
|
} else {
|
||||||
@@ -11,9 +21,22 @@ export function parseRubyAccessPath(path: string): {
|
|||||||
methodName: string;
|
methodName: string;
|
||||||
path: 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) {
|
if (match) {
|
||||||
return { methodName: match[1], path: match[2] };
|
return {
|
||||||
|
methodName: match[1],
|
||||||
|
path: tokens
|
||||||
|
.slice(1)
|
||||||
|
.map((token) => token.text)
|
||||||
|
.join("."),
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
return { methodName: "", path: "" };
|
return { methodName: "", path: "" };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user