Skip to content

Commit a91067d

Browse files
Tobias HøeghTobias Høegh
authored andcommitted
feat: add support for TypeScript
1 parent f76adf3 commit a91067d

4 files changed

Lines changed: 61 additions & 3 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
3+
const ComponentBox = ({ children }) => children
4+
5+
export const MockTypeAnnotations = () => {
6+
const value = 'hello' as string
7+
8+
return (
9+
<ComponentBox data-test="id">
10+
<span>{value as string}</span>
11+
</ComponentBox>
12+
)
13+
}

__tests__/babelPluginReactLive.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,54 @@ describe('transformFileAsync', () => {
134134
expect(formattedCode.match(/\{`/g)).toHaveLength(8)
135135
expect(formattedCode.match(/`\}/g)).toHaveLength(8)
136136
})
137+
138+
it('should format TypeScript syntax with the babel-ts parser', async () => {
139+
const targetFileWithTypes = nodePath.resolve(
140+
__dirname,
141+
'../__mocks__/ExamplesTypeAnnotations.tsx'
142+
)
143+
const pluginOptionsWithTypes = {
144+
componentName: 'ComponentBox',
145+
filesToMatch: ['ExamplesTypeAnnotations.tsx'],
146+
prettierPath,
147+
}
148+
149+
const babelFileResult = await transformFileAsync(targetFileWithTypes, {
150+
code: true,
151+
presets: [
152+
[
153+
'@babel/preset-env',
154+
{
155+
modules: false,
156+
targets: { firefox: '100' },
157+
},
158+
],
159+
],
160+
plugins: [[babelPluginReactLive, pluginOptionsWithTypes]],
161+
})
162+
163+
const code = removeConsoleNinja(String(babelFileResult?.code))
164+
165+
const formattedCode = prettier.format(code, {
166+
filepath: 'file.tsx',
167+
semi: false,
168+
})
169+
170+
expect(formattedCode).toMatchInlineSnapshot(`
171+
"const ComponentBox = ({ children }) => children
172+
export const MockTypeAnnotations = () => {
173+
const value = "hello"
174+
return (
175+
<ComponentBox data-test="id">{\`<span>{value as string}</span>
176+
\`}</ComponentBox>
177+
)
178+
}
179+
"
180+
`)
181+
})
137182
})
138183

139-
function removeConsoleNinja(code) {
184+
function removeConsoleNinja(code: string): string {
140185
if (code.includes('oo_cm')) {
141186
const index = code.indexOf('function oo_cm()')
142187
code = code.slice(0, index)

babelPluginReactLive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function babelPluginReactLive(babel, options) {
2020

2121
code = prettier.format(code, {
2222
...prettierrc,
23-
parser: 'babel',
23+
parser: 'babel-ts',
2424
})
2525

2626
// Prettier adds a leading ;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"type": "git",
1414
"url": "https://github.com/dnbexperience/eufemia.git"
1515
},
16-
"version": "1.4.4",
16+
"version": "1.4.5",
1717
"main": "babelPluginReactLive.js",
1818
"scripts": {
1919
"test": "jest",

0 commit comments

Comments
 (0)