fix: Use amaro for stripping types in code samples (#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 <26616127+charislam@users.noreply.github.com>
This commit is contained in:
Ivan VasilovandCharis Lam authored and GitHub committed 2025-12-10 16:23:46 +01:00
1 parent 7ba95d27f3
commit 70388e50bb
4 files changed
+41 -76

No files matched your search

@@ -1,4 +1,4 @@
import { afterAll, beforeAll, describe, it, expect, vi } from 'vitest'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import { stripIndent } from 'common-tags'
import { fromMarkdown } from 'mdast-util-from-markdown'
@@ -593,35 +593,35 @@ Some more text.
const users = [
{ id: 1, name: 'John', email: 'john@example.com' },
{ id: 2, name: 'Jane' },
];
]
function getUserById(id) {
return users.find((user) => user.id === id);
return users.find((user) => user.id === id)
}
function createUser(name, email) {
const newId = Math.max(...users.map((u) => u.id)) + 1;
const newUser = { id: newId, name };
const newId = Math.max(...users.map((u) => u.id)) + 1
const newUser = { id: newId, name }
if (email) {
newUser.email = email;
newUser.email = email
}
users.push(newUser);
return newUser;
users.push(newUser)
return newUser
}
class UserManager {
users = [];
users = []
constructor(initialUsers = []) {
this.users = initialUsers;
this.users = initialUsers
}
addUser(user) {
this.users.push(user);
this.users.push(user)
}
getUsers() {
return [...this.users];
return [...this.users]
}
}
\`\`\`
@@ -698,33 +698,33 @@ Some more text.
const users = [
{ id: 1, name: 'John', email: 'john@example.com' },
{ id: 2, name: 'Jane' },
];
]
// ...
function createUser(name, email) {
const newId = Math.max(...users.map((u) => u.id)) + 1;
const newUser = { id: newId, name };
const newId = Math.max(...users.map((u) => u.id)) + 1
const newUser = { id: newId, name }
if (email) {
newUser.email = email;
newUser.email = email
}
users.push(newUser);
return newUser;
users.push(newUser)
return newUser
}
class UserManager {
users = [];
users = []
constructor(initialUsers = []) {
this.users = initialUsers;
this.users = initialUsers
}
addUser(user) {
this.users.push(user);
this.users.push(user)
}
getUsers() {
return [...this.users];
return [...this.users]
}
}
\`\`\`
+8 -3
View File
@@ -33,12 +33,13 @@
import * as acorn from 'acorn'
import tsPlugin from 'acorn-typescript'
import { type DefinitionContent, type BlockContent, type Code, type Root } from 'mdast'
import amaro from 'amaro'
import { type BlockContent, type Code, type DefinitionContent, type Root } from 'mdast'
import type { MdxJsxAttributeValueExpression, MdxJsxFlowElement } from 'mdast-util-mdx-jsx'
import assert from 'node:assert'
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { removeTypes } from 'remove-types'
import prettier from 'prettier'
import { type Parent } from 'unist'
import { visitParents } from 'unist-util-visit-parents'
import { z, type SafeParseError } from 'zod'
@@ -260,7 +261,11 @@ async function rewriteNodes(contentMap: Map<MdxJsxFlowElement, [CodeSampleMeta,
let processedContent = content
if (meta.convertToJs) {
processedContent = await removeTypes(content)
const { code } = amaro.transformSync(content, { mode: 'strip-only' })
const prettierConfig = await prettier.resolveConfig('./.prettierrc.js')
processedContent = await prettier.format(code, { ...prettierConfig, parser: 'typescript' })
// Convert TypeScript/TSX language to JavaScript/JSX when converting types
assert(
lang === 'typescript' || lang === 'tsx',
+1 -1
View File
@@ -109,7 +109,6 @@
"remark-emoji": "^3.1.2",
"remark-gfm": "^3.0.1",
"remark-math": "^6.0.0",
"remove-types": "1.0.0",
"server-only": "^0.0.1",
"shared-data": "workspace:*",
"ui": "workspace:*",
@@ -142,6 +141,7 @@
"@types/react-dom": "catalog:",
"@types/unist": "^2.0.6",
"@types/uuid": "^10.0.0",
"amaro": "^1.1.5",
"api-types": "workspace:*",
"cheerio": "^1.0.0-rc.12",
"chokidar": "^4.0.3",
+10 -50
View File
@@ -583,9 +583,6 @@ importers:
remark-math:
specifier: ^6.0.0
version: 6.0.0(supports-color@8.1.1)
remove-types:
specifier: 1.0.0
version: 1.0.0(supports-color@8.1.1)
server-only:
specifier: ^0.0.1
version: 0.0.1
@@ -677,6 +674,9 @@ importers:
'@types/uuid':
specifier: ^10.0.0
version: 10.0.0
amaro:
specifier: ^1.1.5
version: 1.1.5
api-types:
specifier: workspace:*
version: link:../../packages/api-types
@@ -3082,10 +3082,6 @@ packages:
resolution: {integrity: sha512-URMGH08NzYFhubNSGJrpUEphGKQwMQYBySzat5cAByY1/YgIRkULnIy3tAMeszlL/so2HbeilYloUmSpd7GdVw==}
engines: {node: '>=6.9.0'}
'@babel/helper-plugin-utils@7.26.5':
resolution: {integrity: sha512-RS+jZcRdZdRFzMyr+wcsaqOmld1/EqTghfaBGQQd/WnRdzdlvSZ//kF7U8VQTxf1ynZ4cjUcYgjVGx13ewNPMg==}
engines: {node: '>=6.9.0'}
'@babel/helper-plugin-utils@7.27.1':
resolution: {integrity: sha512-1gn1Up5YXka3YYAHGKpbideQ5Yjf1tDa9qYcgysz+cNCXukyLl6DjPXhD3VRwSb8c0J9tA4b2+rHEZtc6R0tlw==}
engines: {node: '>=6.9.0'}
@@ -3126,12 +3122,6 @@ packages:
engines: {node: '>=6.0.0'}
hasBin: true
'@babel/plugin-syntax-decorators@7.25.9':
resolution: {integrity: sha512-ryzI0McXUPJnRCvMo4lumIKZUzhYUO/ScI+Mz4YVaTLt04DHNSjEUjKVvbzQjZFLuod/cYEc07mJWhzl6v4DPg==}
engines: {node: '>=6.9.0'}
peerDependencies:
'@babel/core': ^7.0.0-0
'@babel/plugin-syntax-import-assertions@7.26.0':
resolution: {integrity: sha512-QCWT5Hh830hK5EQa7XzuqIkQU9tT/whqbDz7kuaZMHFl1inRRg7JnuAEOQ0Ur0QUl0NufCk1msK2BeY79Aj/eg==}
engines: {node: '>=6.9.0'}
@@ -3168,12 +3158,6 @@ packages:
peerDependencies:
'@babel/core': ^7.0.0-0
'@babel/plugin-transform-typescript@7.27.0':
resolution: {integrity: sha512-fRGGjO2UEGPjvEcyAZXRXAS8AfdaQoq7HnxAbJoAoW10B9xOKesmmndJv+Sym2a+9FHWZ9KbyyLCe9s0Sn5jtg==}
engines: {node: '>=6.9.0'}
peerDependencies:
'@babel/core': ^7.0.0-0
'@babel/plugin-transform-typescript@7.28.0':
resolution: {integrity: sha512-4AEiDEBPIZvLQaWlc9liCavE0xRM0dNca41WtBeM3jgFptfUOSG9z0uteLhq6+3rq+WB6jIvUwKDTpXEHPJ2Vg==}
engines: {node: '>=6.9.0'}
@@ -9858,6 +9842,10 @@ packages:
alien-signals@2.0.7:
resolution: {integrity: sha512-wE7y3jmYeb0+h6mr5BOovuqhFv22O/MV9j5p0ndJsa7z1zJNPGQ4ph5pQk/kTTCWRC3xsA4SmtwmkzQO+7NCNg==}
amaro@1.1.5:
resolution: {integrity: sha512-oo72OEYOSfSPe+96V+jh41gaFWfl9HddXFAHMFM+emjdZkzbtcg0bFWoYaWf8IMlqmZ0VM8F13frI8Ktt3+ADA==}
engines: {node: '>=22'}
amazon-cognito-identity-js@6.3.15:
resolution: {integrity: sha512-G2mzTlGYHKYh9oZDO0Gk94xVQ4iY9GYWBaYScbDYvz05ps6dqi0IvdNx1Lxi7oA3tjS5X+mUN7/svFJJdOB9YA==}
@@ -16955,9 +16943,6 @@ packages:
remove-trailing-spaces@1.0.9:
resolution: {integrity: sha512-xzG7w5IRijvIkHIjDk65URsJJ7k4J95wmcArY5PRcmjldIOl7oTvG8+X2Ag690R7SfwiOcHrWZKVc1Pp5WIOzA==}
remove-types@1.0.0:
resolution: {integrity: sha512-G7Hk1Q+UJ5DvlNAoJZObxANkBZGiGdp589rVcTW/tYqJWJ5rwfraSnKSQaETN8Epaytw8J40nS/zC7bcHGv36w==}
rename-keys@1.2.0:
resolution: {integrity: sha512-U7XpAktpbSgHTRSNRrjKSrjYkZKuhUukfoBlXWXUExCAqhzh1TU3BDRAfJmarcl5voKS+pbKU9MvyLWKZ4UEEg==}
engines: {node: '>= 0.8.0'}
@@ -20541,8 +20526,6 @@ snapshots:
dependencies:
'@babel/types': 7.28.4
'@babel/helper-plugin-utils@7.26.5': {}
'@babel/helper-plugin-utils@7.27.1': {}
'@babel/helper-replace-supers@7.27.1(@babel/core@7.28.4(supports-color@8.1.1))(supports-color@8.1.1)':
@@ -20580,11 +20563,6 @@ snapshots:
dependencies:
'@babel/types': 7.28.4
'@babel/plugin-syntax-decorators@7.25.9(@babel/core@7.28.4(supports-color@8.1.1))':
dependencies:
'@babel/core': 7.28.4(supports-color@8.1.1)
'@babel/helper-plugin-utils': 7.27.1
'@babel/plugin-syntax-import-assertions@7.26.0(@babel/core@7.28.4(supports-color@8.1.1))':
dependencies:
'@babel/core': 7.28.4(supports-color@8.1.1)
@@ -20618,17 +20596,6 @@ snapshots:
'@babel/core': 7.28.4(supports-color@8.1.1)
'@babel/helper-plugin-utils': 7.27.1
'@babel/plugin-transform-typescript@7.27.0(@babel/core@7.28.4(supports-color@8.1.1))(supports-color@8.1.1)':
dependencies:
'@babel/core': 7.28.4(supports-color@8.1.1)
'@babel/helper-annotate-as-pure': 7.27.3
'@babel/helper-create-class-features-plugin': 7.28.3(@babel/core@7.28.4(supports-color@8.1.1))(supports-color@8.1.1)
'@babel/helper-plugin-utils': 7.27.1
'@babel/helper-skip-transparent-expression-wrappers': 7.27.1(supports-color@8.1.1)
'@babel/plugin-syntax-typescript': 7.27.1(@babel/core@7.28.4(supports-color@8.1.1))
transitivePeerDependencies:
- supports-color
'@babel/plugin-transform-typescript@7.28.0(@babel/core@7.28.4(supports-color@8.1.1))(supports-color@8.1.1)':
dependencies:
'@babel/core': 7.28.4(supports-color@8.1.1)
@@ -21603,7 +21570,7 @@ snapshots:
'@graphql-codegen/client-preset@4.8.0(encoding@0.1.13)(graphql-sock@1.0.1(graphql@16.11.0))(graphql@16.11.0)':
dependencies:
'@babel/helper-plugin-utils': 7.26.5
'@babel/helper-plugin-utils': 7.27.1
'@babel/template': 7.27.2
'@graphql-codegen/add': 5.0.3(graphql@16.11.0)
'@graphql-codegen/gql-tag-operations': 4.0.17(encoding@0.1.13)(graphql@16.11.0)
@@ -29272,6 +29239,8 @@ snapshots:
alien-signals@2.0.7: {}
amaro@1.1.5: {}
amazon-cognito-identity-js@6.3.15(encoding@0.1.13):
dependencies:
'@aws-crypto/sha256-js': 1.2.2
@@ -38036,15 +38005,6 @@ snapshots:
remove-trailing-spaces@1.0.9: {}
remove-types@1.0.0(supports-color@8.1.1):
dependencies:
'@babel/core': 7.28.4(supports-color@8.1.1)
'@babel/plugin-syntax-decorators': 7.25.9(@babel/core@7.28.4(supports-color@8.1.1))
'@babel/plugin-transform-typescript': 7.27.0(@babel/core@7.28.4(supports-color@8.1.1))(supports-color@8.1.1)
prettier: 2.8.8
transitivePeerDependencies:
- supports-color
rename-keys@1.2.0: {}
repeat-element@1.1.4: {}