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
166 changes: 166 additions & 0 deletions lib/glueNaming.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import { assertEquals } from "@std/assert";
import { getGlueName } from "./glueNaming.ts";
import { join } from "@std/path";

async function createTempDir(): Promise<{ path: string } & AsyncDisposable> {
const tempDir = await Deno.makeTempDir({
prefix: "glue-naming-test-",
});
return {
path: tempDir,
async [Symbol.asyncDispose]() {
await Deno.remove(tempDir, { recursive: true });
},
};
}

Deno.test("getGlueName - explicit name", async () => {
const name = await getGlueName("some/path/file.ts", "explicit-name");
assertEquals(name, "explicit-name");
});

Deno.test("getGlueName - file comment", async () => {
await using tempDir = await createTempDir();
const filePath = join(tempDir.path, "myGlue.ts");

await Deno.writeTextFile(
filePath,
`
// glue-name comment-name
import something from "somewhere";

console.log("hello");
`.trim(),
);

const name = await getGlueName(filePath);
assertEquals(name, "comment-name");
});

Deno.test("getGlueName - file comment with spaces", async () => {
await using tempDir = await createTempDir();
const filePath = join(tempDir.path, "myGlueSpaces.ts");

await Deno.writeTextFile(
filePath,
`
// glue-name spaced-name
console.log("hello");
`.trim(),
);

const name = await getGlueName(filePath);
assertEquals(name, "spaced-name");
});

Deno.test("getGlueName - fallback to filename", async () => {
await using tempDir = await createTempDir();
const filePath = join(tempDir.path, "my-file-name.ts");
await Deno.writeTextFile(filePath, "");
const name = await getGlueName(filePath);
assertEquals(name, "my-file-name.ts");
});

Deno.test("getGlueName - fallback to filename if comment missing", async () => {
await using tempDir = await createTempDir();
const filePath = join(tempDir.path, "noComment.ts");

await Deno.writeTextFile(
filePath,
`
// regular comment
console.log("hello");
`.trim(),
);

const name = await getGlueName(filePath);
assertEquals(name, "noComment.ts");
});

Deno.test("getGlueName - fallback to filename with multiple extensions", async () => {
await using tempDir = await createTempDir();
const filePath = join(tempDir.path, "my.glue.script.ts");
await Deno.writeTextFile(filePath, "");
const name = await getGlueName(filePath);
assertEquals(name, "my.glue.script.ts");
});

Deno.test("getGlueName - fallback to filename with underscores", async () => {
await using tempDir = await createTempDir();
const filePath = join(tempDir.path, "name_with_underscores.js");
await Deno.writeTextFile(filePath, "");
const name = await getGlueName(filePath);
assertEquals(name, "name_with_underscores.js");
});

Deno.test("getGlueName - fallback to filename with dashes", async () => {
await using tempDir = await createTempDir();
const filePath = join(tempDir.path, "name-with-dashes.tsx");
await Deno.writeTextFile(filePath, "");
const name = await getGlueName(filePath);
assertEquals(name, "name-with-dashes.tsx");
});

Deno.test("getGlueName - file comment with extreme spacing", async () => {
await using tempDir = await createTempDir();
const filePath = join(tempDir.path, "myGlueExtremeSpaces.ts");

await Deno.writeTextFile(
filePath,
`
// glue-name extremely-spaced
console.log("hello");
`.trim(),
);

const name = await getGlueName(filePath);
assertEquals(name, "extremely-spaced");
});

Deno.test("getGlueName - file comment with no spaces", async () => {
await using tempDir = await createTempDir();
const filePath = join(tempDir.path, "myGlueNoSpaces.ts");

await Deno.writeTextFile(
filePath,
`
//glue-name compact
console.log("hello");
`.trim(),
);

const name = await getGlueName(filePath);
assertEquals(name, "compact");
});

Deno.test("getGlueName - file comment indented", async () => {
await using tempDir = await createTempDir();
const filePath = join(tempDir.path, "myGlueIndented.ts");

await Deno.writeTextFile(
filePath,
`
// glue-name indented
console.log("hello");
`.trim(),
);

const name = await getGlueName(filePath);
assertEquals(name, "indented");
});

Deno.test("getGlueName - JSON string", async () => {
await using tempDir = await createTempDir();
const filePath = join(tempDir.path, "myGlueJsonName.ts");

await Deno.writeTextFile(
filePath,
`
// glue-name "foo \\" bar \u2603 \\u2603 "
console.log("hello");
`.trim(),
);

const name = await getGlueName(filePath);
assertEquals(name, 'foo " bar \u2603 \u2603 ');
});
8 changes: 6 additions & 2 deletions lib/glueNaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ export async function getGlueName(filePath: string, explicitName?: string): Prom
.pipeThrough(new TextLineStream()); // split string line by line

for await (const line of readable) {
const match = line.match(/^\s*\/\/\s*glue-name\s+(\S+)\s*$/);
const match = line.match(/^\s*\/\/\s*glue-name\s+([^"]\S*|".*")\s*$/);
if (match) {
return match[1].trim();
if (match[1].startsWith('"')) {
return JSON.parse(match[1]);
} else {
return match[1];
}
}
}

Expand Down
178 changes: 0 additions & 178 deletions tests/glueNaming.test.ts

This file was deleted.