Skip to content

Commit 698e2a5

Browse files
author
Simon Engledew
committed
Just convert the pattern into a RegExp...
1 parent 4d86261 commit 698e2a5

9 files changed

Lines changed: 65 additions & 110 deletions

lib/actions-util.js

Lines changed: 22 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.test.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions-util.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ test("patternIsSuperset()", (t) => {
281281
t.false(actionsutil.patternIsSuperset("feature-*", "**"));
282282
t.false(actionsutil.patternIsSuperset("a/**/c", "a/**/d"));
283283
t.false(actionsutil.patternIsSuperset("a/**/c", "a/**"));
284+
t.true(actionsutil.patternIsSuperset("a/**", "a/**/c"));
284285
t.true(actionsutil.patternIsSuperset("a/**/c", "a/main-**/c"));
286+
t.false(actionsutil.patternIsSuperset("a/**/b/**/c", "a/**/d/**/c"));
285287
t.false(actionsutil.patternIsSuperset("a/main-**/c", "a/**/c"));
286288
t.true(
287289
actionsutil.patternIsSuperset(

src/actions-util.ts

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -134,69 +134,36 @@ function isObject(o: unknown): o is object {
134134
return o !== null && typeof o === "object";
135135
}
136136

137-
const WORKSPACE_BRANCES_PATTERN = new RegExp("(\\*\\*?|/)");
138-
139-
function tokenize(value: string): string[] {
140-
return value.split(WORKSPACE_BRANCES_PATTERN).reduce(function (arr, cur) {
141-
if (cur) {
142-
arr.push(cur);
143-
}
144-
return arr;
145-
}, [] as ReturnType<typeof tokenize>);
146-
}
147-
148-
function considerToken(
149-
a: string,
150-
b: string
151-
): { advance: boolean; consume: boolean } {
152-
switch (a) {
153-
case "*":
154-
return { advance: b === "/", consume: b !== "/" };
155-
case "**":
156-
return { advance: false, consume: true };
157-
default:
158-
return { advance: a === b, consume: a === b };
159-
}
160-
}
161-
162-
function tokensAreSuperset(tokensA: string[], tokensB: string[]) {
163-
let indexA = 0;
164-
let indexB = 0;
165-
166-
let advance;
167-
let consume = true;
168-
169-
while (advance || consume) {
170-
const currentA = tokensA[indexA];
171-
const currentB = tokensB[indexB];
172-
173-
if (currentB === undefined) {
174-
return true;
175-
}
176-
if (currentA === undefined) {
177-
return false;
178-
}
179-
180-
const next = considerToken(currentA, currentB);
181-
182-
advance = next.advance;
183-
consume = next.consume;
184-
185-
if (consume) {
186-
indexB += 1;
187-
}
188-
if (advance) {
189-
indexA += 1;
190-
}
191-
}
192-
return false;
137+
const GLOB_PATTERN = new RegExp("(\\*\\*?)");
138+
139+
function escapeRegExp(string) {
140+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
141+
}
142+
143+
function patternToRegExp(value) {
144+
return new RegExp(
145+
`^${value
146+
.split(GLOB_PATTERN)
147+
.reduce(function (arr, cur) {
148+
if (cur) {
149+
if (cur === "**") {
150+
arr.push(".*?");
151+
} else if (cur === "*") {
152+
arr.push("[^/]*?");
153+
} else {
154+
arr.push(escapeRegExp(cur));
155+
}
156+
}
157+
return arr;
158+
}, [])
159+
.join("")}$`
160+
);
193161
}
194162

163+
// this function should return true if patternA is a superset of patternB
164+
// e.g: * is a superset of main-* but main-* is not a superset of *.
195165
export function patternIsSuperset(patternA: string, patternB: string): boolean {
196-
const tokensA = tokenize(patternA);
197-
const tokensB = tokenize(patternB);
198-
199-
return tokensAreSuperset(tokensA, tokensB) && tokensAreSuperset(tokensA.reverse(), tokensB.reverse());
166+
return patternToRegExp(patternA).test(patternB);
200167
}
201168

202169
function branchesToArray(branches?: string | null | string[]): string[] | "**" {

src/init-action.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ async function run() {
101101
// we do not want to worry users if linting is failing
102102
// but we do want to send a status report containing this error code
103103
// below
104-
const userWorkflowErrors = workflowErrors.filter(o => o.code !== 'LintFailed');
104+
const userWorkflowErrors = workflowErrors.filter(
105+
(o) => o.code !== "LintFailed"
106+
);
105107

106108
if (userWorkflowErrors.length > 0) {
107109
core.warning(actionsUtil.formatWorkflowErrors(userWorkflowErrors));

0 commit comments

Comments
 (0)