Skip to content

Commit 70388e5

Browse files
ivasilovcharislam
andauthored
fix: Use amaro for stripping types in code samples (supabase#41229)
* Fix the error for babel/preset-typescript in docs. * Unfix the version. * Try using amaro for type stripping. * Run prettier after stripping types. * Fix tests. --------- Co-authored-by: Charis Lam <[email protected]>
1 parent 7ba95d2 commit 70388e5

File tree

4 files changed

+41
-76
lines changed

4 files changed

+41
-76
lines changed

apps/docs/features/directives/CodeSample.test.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterAll, beforeAll, describe, it, expect, vi } from 'vitest'
1+
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
22

33
import { stripIndent } from 'common-tags'
44
import { fromMarkdown } from 'mdast-util-from-markdown'
@@ -593,35 +593,35 @@ Some more text.
593593
const users = [
594594
{ id: 1, name: 'John', email: '[email protected]' },
595595
{ id: 2, name: 'Jane' },
596-
];
596+
]
597597
598598
function getUserById(id) {
599-
return users.find((user) => user.id === id);
599+
return users.find((user) => user.id === id)
600600
}
601601
602602
function createUser(name, email) {
603-
const newId = Math.max(...users.map((u) => u.id)) + 1;
604-
const newUser = { id: newId, name };
603+
const newId = Math.max(...users.map((u) => u.id)) + 1
604+
const newUser = { id: newId, name }
605605
if (email) {
606-
newUser.email = email;
606+
newUser.email = email
607607
}
608-
users.push(newUser);
609-
return newUser;
608+
users.push(newUser)
609+
return newUser
610610
}
611611
612612
class UserManager {
613-
users = [];
613+
users = []
614614
615615
constructor(initialUsers = []) {
616-
this.users = initialUsers;
616+
this.users = initialUsers
617617
}
618618
619619
addUser(user) {
620-
this.users.push(user);
620+
this.users.push(user)
621621
}
622622
623623
getUsers() {
624-
return [...this.users];
624+
return [...this.users]
625625
}
626626
}
627627
\`\`\`
@@ -698,33 +698,33 @@ Some more text.
698698
const users = [
699699
{ id: 1, name: 'John', email: '[email protected]' },
700700
{ id: 2, name: 'Jane' },
701-
];
701+
]
702702
703703
// ...
704704
705705
function createUser(name, email) {
706-
const newId = Math.max(...users.map((u) => u.id)) + 1;
707-
const newUser = { id: newId, name };
706+
const newId = Math.max(...users.map((u) => u.id)) + 1
707+
const newUser = { id: newId, name }
708708
if (email) {
709-
newUser.email = email;
709+
newUser.email = email
710710
}
711-
users.push(newUser);
712-
return newUser;
711+
users.push(newUser)
712+
return newUser
713713
}
714714
715715
class UserManager {
716-
users = [];
716+
users = []
717717
718718
constructor(initialUsers = []) {
719-
this.users = initialUsers;
719+
this.users = initialUsers
720720
}
721721
722722
addUser(user) {
723-
this.users.push(user);
723+
this.users.push(user)
724724
}
725725
726726
getUsers() {
727-
return [...this.users];
727+
return [...this.users]
728728
}
729729
}
730730
\`\`\`

apps/docs/features/directives/CodeSample.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@
3333

3434
import * as acorn from 'acorn'
3535
import tsPlugin from 'acorn-typescript'
36-
import { type DefinitionContent, type BlockContent, type Code, type Root } from 'mdast'
36+
import amaro from 'amaro'
37+
import { type BlockContent, type Code, type DefinitionContent, type Root } from 'mdast'
3738
import type { MdxJsxAttributeValueExpression, MdxJsxFlowElement } from 'mdast-util-mdx-jsx'
3839
import assert from 'node:assert'
3940
import { readFile } from 'node:fs/promises'
4041
import { join } from 'node:path'
41-
import { removeTypes } from 'remove-types'
42+
import prettier from 'prettier'
4243
import { type Parent } from 'unist'
4344
import { visitParents } from 'unist-util-visit-parents'
4445
import { z, type SafeParseError } from 'zod'
@@ -260,7 +261,11 @@ async function rewriteNodes(contentMap: Map<MdxJsxFlowElement, [CodeSampleMeta,
260261

261262
let processedContent = content
262263
if (meta.convertToJs) {
263-
processedContent = await removeTypes(content)
264+
const { code } = amaro.transformSync(content, { mode: 'strip-only' })
265+
266+
const prettierConfig = await prettier.resolveConfig('./.prettierrc.js')
267+
processedContent = await prettier.format(code, { ...prettierConfig, parser: 'typescript' })
268+
264269
// Convert TypeScript/TSX language to JavaScript/JSX when converting types
265270
assert(
266271
lang === 'typescript' || lang === 'tsx',

apps/docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@
109109
"remark-emoji": "^3.1.2",
110110
"remark-gfm": "^3.0.1",
111111
"remark-math": "^6.0.0",
112-
"remove-types": "1.0.0",
113112
"server-only": "^0.0.1",
114113
"shared-data": "workspace:*",
115114
"ui": "workspace:*",
@@ -142,6 +141,7 @@
142141
"@types/react-dom": "catalog:",
143142
"@types/unist": "^2.0.6",
144143
"@types/uuid": "^10.0.0",
144+
"amaro": "^1.1.5",
145145
"api-types": "workspace:*",
146146
"cheerio": "^1.0.0-rc.12",
147147
"chokidar": "^4.0.3",

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)