Skip to content

Commit

Permalink
fix: use not for won't in behavior strings
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Jun 4, 2024
1 parent 87aa28c commit 87a33fd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion fixtures/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Do something else:
assertEquals("bar", "bar");
```

Won't do something more:
Not do something more:

```typescript
assertNotEquals("bar", "baz");
Expand Down
24 changes: 12 additions & 12 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ const describePattern = {
callee: { type: "Identifier", value: "describe" },
arguments: [
{ expression: { value: _("target") } },
{ expression: { body: { stmts: _("its") } } },
{ expression: { body: { stmts: _("stmts") } } },
],
},
};

type DescribePrecursor = {
target: string;
its: Statement[];
stmts: Statement[];
};

const itPattern = {
Expand All @@ -29,40 +29,40 @@ const itPattern = {
callee: { type: "Identifier", value: "it" },
arguments: [
{ expression: { value: _("behavior") } },
{ expression: { body: { stmts: _("code") } } },
{ expression: { body: { stmts: _("stmts") } } },
],
},
};

type ItPrecursor = {
behavior: string;
code: Statement[];
stmts: Statement[];
};

/**
* Parse a test code string to an array of describe objects.
* @param src The test code string.
*/
export default async function parse(src: string): Promise<Describe[]> {
const tree = await swc.parse(src, {
const { body, span } = await swc.parse(src, {
comments: true,
syntax: "typescript",
});
const getSpan = (stmt: Statement) => ({
start: stmt.span.start - tree.span.start,
end: stmt.span.end - tree.span.start,
start: stmt.span.start - span.start,
end: stmt.span.end - span.start,
});
return mapNotNullish(
tree.body,
body,
(item) => match(describePattern, item) as DescribePrecursor | undefined,
).map(({ target, its }) => ({
).map(({ target, stmts }) => ({
target,
its: mapNotNullish(
its,
stmts,
(it) => match(itPattern, it) as ItPrecursor | undefined,
).map(({ behavior, code }) => ({
).map(({ behavior, stmts }) => ({
behavior,
code: code.map((stmt) => {
code: stmts.map((stmt) => {
const { start, end } = getSpan(stmt);
return src.slice(start, end);
}).join("\n"),
Expand Down
2 changes: 1 addition & 1 deletion src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const defaultFormatter = (behavior: string) => {
} else if (behavior.startsWith("n't be ") || behavior.startsWith("not be ")) {
behavior = "not " + behavior.slice(7);
} else if (behavior.startsWith("n't ") || behavior.startsWith("not ")) {
behavior = "won't " + behavior.slice(4);
behavior = "not " + behavior.slice(4);
}
return behavior[0].toLocaleUpperCase() + behavior.slice(1);
};
Expand Down
12 changes: 6 additions & 6 deletions src/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ describe("defaultFormatter", () => {
);
});

it("should replace `should not do` with `Won't`", () => {
it("should replace `should not do` with `Not`", () => {
assertEquals(
defaultFormatter("should not do something"),
"Won't do something",
"Not do something",
);
});

it("should replace `shouldn't do` with `Won't do`", () => {
it("should replace `shouldn't do` with `Not do`", () => {
assertEquals(
defaultFormatter("shouldn't do something"),
"Won't do something",
"Not do something",
);
});

Expand Down Expand Up @@ -119,7 +119,7 @@ describe("stringifyDescribe", () => {
assert("foo" !== "bar");
\`\`\`
Won't do something else:
Not do something else:
\`\`\`typescript
console.log("bar");
Expand Down Expand Up @@ -171,7 +171,7 @@ describe("stringify", () => {
### \`bar\`
Won't do something else:
Not do something else:
\`\`\`typescript
console.log("bar");
Expand Down

0 comments on commit 87a33fd

Please sign in to comment.