Skip to content
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
19 changes: 16 additions & 3 deletions packages/isomorphic/locatorGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ export class PythonLocatorFactory implements LocatorFactory {
}

private regexToString(body: RegExp) {
const suffix = body.flags.includes('i') ? ', re.IGNORECASE' : '';
const suffix = regexFlagsSuffix(body, 're', regexFlagNames.python);
return `re.compile(r"${normalizeEscapedRegexQuotes(body.source).replace(/\\\//, '/').replace(/"/g, '\\"')}"${suffix})`;
}

Expand Down Expand Up @@ -605,7 +605,7 @@ export class JavaLocatorFactory implements LocatorFactory {
}

private regexToString(body: RegExp) {
const suffix = body.flags.includes('i') ? ', Pattern.CASE_INSENSITIVE' : '';
const suffix = regexFlagsSuffix(body, 'Pattern', regexFlagNames.java);
return `Pattern.compile(${this.quote(normalizeEscapedRegexQuotes(body.source))}${suffix})`;
}

Expand Down Expand Up @@ -711,7 +711,7 @@ export class CSharpLocatorFactory implements LocatorFactory {
}

private regexToString(body: RegExp): string {
const suffix = body.flags.includes('i') ? ', RegexOptions.IgnoreCase' : '';
const suffix = regexFlagsSuffix(body, 'RegexOptions', regexFlagNames.csharp);
return `new Regex(${this.quote(normalizeEscapedRegexQuotes(body.source))}${suffix})`;
}

Expand Down Expand Up @@ -776,6 +776,19 @@ const generators: Record<Language, new (preferredQuote?: Quote) => LocatorFactor
jsonl: JsonlLocatorFactory,
};

// Regular expression flags supported by the Python, Java and C# locators.
export const regexFlagNames = {
python: { i: 'IGNORECASE', m: 'MULTILINE', s: 'DOTALL' },
java: { i: 'CASE_INSENSITIVE', m: 'MULTILINE', s: 'DOTALL' },
csharp: { i: 'IgnoreCase', m: 'Multiline', s: 'Singleline' },
};

// E.g. `, re.IGNORECASE | re.MULTILINE`.
function regexFlagsSuffix(body: RegExp, flagsEnum: string, names: Record<string, string>): string {
const flags = [...body.flags].filter(flag => flag in names).map(flag => `${flagsEnum}.${names[flag]}`);
return flags.length ? `, ${flags.join(' | ')}` : '';
}

function isRegExp(obj: any): obj is RegExp {
return obj instanceof RegExp;
}
Loading
Loading