Skip to content

Commit

Permalink
relax the charset of the globs significantly, and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Jun 3, 2023
1 parent 474afff commit 0f62632
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 29 deletions.
91 changes: 67 additions & 24 deletions packages/turbo-codemod/__tests__/clean-globs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ describe("clean-globs", () => {
`);
});

const { log } = getTransformerHelpers({
transformer: "test",
rootPath: ".",
options: { force: false, dry: false, print: false },
});

test("collapses back-to-back doublestars", () => {
let badGlobPatterns = [
["../../app-store/**/**", "../../app-store/**"],
Expand All @@ -43,25 +49,13 @@ describe("clean-globs", () => {
["**/foo/**/**/**/**/bar/**/**/**", "**/foo/**/bar/**"],
];

const { log } = getTransformerHelpers({
transformer: "test",
rootPath: ".",
options: { force: false, dry: false, print: false },
});

// Now let's test the function
badGlobPatterns.forEach(([input, output]) => {
expect(fixGlobPattern(input, log)).toBe(output);
});
});

test("doesn't update valid globs and prints a message", () => {
const { log } = getTransformerHelpers({
transformer: "test",
rootPath: ".",
options: { force: false, dry: false, print: false },
});

// Now let's test the function
expect(fixGlobPattern("a/b/c/*", log)).toBe("a/b/c/*");
});
Expand All @@ -77,12 +71,6 @@ describe("clean-globs", () => {
["**html", "**/*html"],
];

const { log } = getTransformerHelpers({
transformer: "test",
rootPath: ".",
options: { force: false, dry: false, print: false },
});

// Now let's test the function
badGlobPatterns.forEach(([input, output]) => {
expect(fixGlobPattern(input, log)).toBe(output);
Expand All @@ -98,15 +86,70 @@ describe("clean-globs", () => {
["pre**/foo/bar/baz/qux", "pre*/**/foo/bar/baz/qux"],
];

const { log } = getTransformerHelpers({
transformer: "test",
rootPath: ".",
options: { force: false, dry: false, print: false },
});

// Now let's test the function
badGlobPatterns.forEach(([input, output]) => {
expect(fixGlobPattern(input, log)).toBe(output);
});
});

it("should collapse back-to-back doublestars to a single doublestar", () => {
expect(fixGlobPattern("../../app-store/**/**", log)).toBe(
"../../app-store/**"
);
expect(fixGlobPattern("**/**/result.json", log)).toBe("**/result.json");
});

it("should change **.ext to **/*.ext", () => {
expect(fixGlobPattern("**.js", log)).toBe("**/*.js");
expect(fixGlobPattern("**.json", log)).toBe("**/*.json");
expect(fixGlobPattern("**.ext", log)).toBe("**/*.ext");
});

it("should change prefix** to prefix*/**", () => {
expect(fixGlobPattern("app**", log)).toBe("app*/**");
expect(fixGlobPattern("src**", log)).toBe("src*/**");
expect(fixGlobPattern("prefix**", log)).toBe("prefix*/**");
});

it("should collapse back-to-back doublestars and change **.ext to **/*.ext", () => {
expect(fixGlobPattern("../../app-store/**/**/*.js", log)).toBe(
"../../app-store/**/*.js"
);
expect(fixGlobPattern("**/**/result.json", log)).toBe("**/result.json");
});

it("should collapse back-to-back doublestars and change prefix** to prefix*/**", () => {
expect(fixGlobPattern("../../app-store/**/**prefix**", log)).toBe(
"../../app-store/**/*prefix*/**"
);
expect(fixGlobPattern("**/**/prefix**", log)).toBe("**/prefix*/**");
});

it("should not modify valid glob patterns", () => {
expect(fixGlobPattern("src/**/*.js", log)).toBe("src/**/*.js");
expect(fixGlobPattern("src/**/test/*.js", log)).toBe("src/**/test/*.js");
expect(fixGlobPattern("src/**/test/**/*.js", log)).toBe(
"src/**/test/**/*.js"
);
expect(fixGlobPattern("src/**/test/**/result.json", log)).toBe(
"src/**/test/**/result.json"
);
});

it("should handle glob patterns with non-ASCII characters", () => {
expect(fixGlobPattern("src/日本語/**/*.js", log)).toBe(
"src/日本語/**/*.js"
);
expect(fixGlobPattern("src/中文/**/*.json", log)).toBe(
"src/中文/**/*.json"
);
expect(fixGlobPattern("src/русский/**/*.ts", log)).toBe(
"src/русский/**/*.ts"
);
});
it("should handle glob patterns with emojis", () => {
expect(fixGlobPattern("src/👋**/*.js", log)).toBe("src/👋*/**/*.js");
expect(fixGlobPattern("src/🌎**/*.json", log)).toBe("src/🌎*/**/*.json");
expect(fixGlobPattern("src/🚀**/*.ts", log)).toBe("src/🚀*/**/*.ts");
});
});
11 changes: 6 additions & 5 deletions packages/turbo-codemod/src/transforms/clean-globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,17 @@ export function fixGlobPattern(pattern: string, log: Logger): string {
newPattern = pattern.replace(/\*\*\/\*\*/g, "**");
}

// For 'cypress/integration/**.test.ts', 'scripts/**.mjs', 'scripts/**.js', 'src/types/generated/**.ts'
// Change '**.ext' to '**/*.ext' where 'ext' is 'test.ts', 'mjs', 'js', 'ts'
newPattern = pattern.replace(/(\*\*)([a-z\.]+)/g, "$1/*$2");
// For '**.ext' change to '**/*.ext'
// 'ext' is a filename or extension and can contain almost any character except '*' and '/'
newPattern = pattern.replace(/(\*\*)([^*/]+)/g, "$1/*$2");
if (newPattern !== pattern) {
log.modified(`${pattern} to ${newPattern}`);
pattern = newPattern;
}

// For 'test/prefix**' change to 'test/prefix*/**'
newPattern = pattern.replace(/([a-z_]+)(\*\*)/g, "$1*/$2");
// For 'prefix**' change to 'prefix*/**'
// 'prefix' is a folder name and can contain almost any character except '*' and '/'
newPattern = pattern.replace(/([^*/]+)(\*\*)/g, "$1*/$2");
if (newPattern !== pattern) {
log.modified(`${pattern} to ${newPattern}`);
pattern = newPattern;
Expand Down

0 comments on commit 0f62632

Please sign in to comment.