Skip to content

Commit

Permalink
feat(match-test-replace): allow to replace? is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Oct 26, 2017
1 parent 398a89f commit 1731df3
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/match-test-replace/src/match-test-replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface TestMatchReplaceReturnDict {
// if no match, return { ok: false }
pattern: RegExp;
// replace function
replace: (args: PatternMatchDictArgs) => string;
replace?: (args: PatternMatchDictArgs) => string;
// allow to replace?
replaceTest?: (args: PatternMatchDictArgs) => boolean;
// optional message
Expand All @@ -26,7 +26,7 @@ export interface TestMatchReplaceReturnDict {
export interface MatchTestReplaceReturnResult {
index: number;
match: string;
replace: string;
replace: string | undefined;
message?: string;
}

Expand Down Expand Up @@ -62,6 +62,10 @@ const applyFixes = (text: string, messages: MatchTestReplaceReturnResult[]) => {
// pickup fix range
let start = problem.index;
const end = start + problem.match.length;
// no replace
if (!problem.replace) {
return;
}
let insertionText = problem.replace;
if (end <= lastFixPos) {
// modify
Expand Down Expand Up @@ -129,14 +133,18 @@ export const matchTestReplace = (text: string, dict: TestMatchReplaceReturnDict)
isReplaceOK = dict.replaceTest(dictArgs);
}
if (isReplaceOK) {
const replace = dict.replace(dictArgs);
const replace = typeof dict.replace === "function" ? dict.replace(dictArgs) : undefined;
const message = dict.message ? dict.message(dictArgs) : undefined;
results.push({
index,
match: match,
replace,
message
replace: replace,
message: message
});
// no replace
if (replace === undefined) {
return all;
}
return replace;
} else {
// No replace
Expand Down

0 comments on commit 1731df3

Please sign in to comment.