Unverified Commit d8cb8c5f authored by Leroy Witteveen's avatar Leroy Witteveen Committed by GitHub

Fixed "Expected jsx identifier" error on TypeScript generics & angle bracket…

Fixed "Expected jsx identifier" error on TypeScript generics & angle bracket type assertions in .ts files (#30619)

* Fixed "Expected jsx identifier" error on TypeScript generics & angle bracket type assertions

* Ignore error from node-notifier on M1

* Add tests
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent d7d1a052
...@@ -33,7 +33,7 @@ const nextDistPath = ...@@ -33,7 +33,7 @@ const nextDistPath =
/(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/ /(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/
function getSWCOptions({ function getSWCOptions({
isTypeScript, filename,
isServer, isServer,
development, development,
isPageFile, isPageFile,
...@@ -42,11 +42,15 @@ function getSWCOptions({ ...@@ -42,11 +42,15 @@ function getSWCOptions({
hasReactRefresh, hasReactRefresh,
isCommonJS, isCommonJS,
}) { }) {
const isTSFile = filename.endsWith('.ts')
const isTypeScript = isTSFile || filename.endsWith('.tsx')
const jsc = { const jsc = {
parser: { parser: {
syntax: isTypeScript ? 'typescript' : 'ecmascript', syntax: isTypeScript ? 'typescript' : 'ecmascript',
dynamicImport: true, dynamicImport: true,
[isTypeScript ? 'tsx' : 'jsx']: true, // Exclude regular TypeScript files from React transformation to prevent e.g. generic parameters and angle-bracket type assertion from being interpreted as JSX tags.
[isTypeScript ? 'tsx' : 'jsx']: isTSFile ? false : true,
}, },
transform: { transform: {
...@@ -119,8 +123,6 @@ async function loaderTransform(parentTrace, source, inputSourceMap) { ...@@ -119,8 +123,6 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
// Make the loader async // Make the loader async
const filename = this.resourcePath const filename = this.resourcePath
const isTypeScript = filename.endsWith('.ts') || filename.endsWith('.tsx')
let loaderOptions = getOptions(this) || {} let loaderOptions = getOptions(this) || {}
const { isServer, pagesDir, hasReactRefresh } = loaderOptions const { isServer, pagesDir, hasReactRefresh } = loaderOptions
...@@ -131,7 +133,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) { ...@@ -131,7 +133,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
const swcOptions = getSWCOptions({ const swcOptions = getSWCOptions({
pagesDir, pagesDir,
isTypeScript, filename,
isServer: isServer, isServer: isServer,
isPageFile, isPageFile,
development: this.mode === 'development', development: this.mode === 'development',
......
...@@ -1172,9 +1172,13 @@ export async function release(task) { ...@@ -1172,9 +1172,13 @@ export async function release(task) {
// notification helper // notification helper
function notify(msg) { function notify(msg) {
return notifier.notify({ try {
title: '▲ Next', notifier.notify({
message: msg, title: '▲ Next',
icon: false, message: msg,
}) icon: false,
})
} catch (err) {
// notifier can fail on M1 machines
}
} }
// eslint-disable-next-line
const myVar = <any>'test'
export default myVar
class MyClass<T> {
value: T
constructor(value: T) {
this.value = value
}
}
const instance = new MyClass<string>('Hello World from Generic')
export default instance.value
import value from '../components/angle-bracket-type-assertions'
export default () => <div id="value">{value}</div>
import value from '../components/generics'
export default () => <div id="value">{value}</div>
...@@ -49,6 +49,16 @@ describe('TypeScript Features', () => { ...@@ -49,6 +49,16 @@ describe('TypeScript Features', () => {
expect($('#cookies').text()).toBe('{}') expect($('#cookies').text()).toBe('{}')
}) })
it('should render the generics page', async () => {
const $ = await get$('/generics')
expect($('#value').text()).toBe('Hello World from Generic')
})
it('should render the angle bracket type assertions page', async () => {
const $ = await get$('/angle-bracket-type-assertions')
expect($('#value').text()).toBe('test')
})
it('should resolve files in correct order', async () => { it('should resolve files in correct order', async () => {
const $ = await get$('/hello') const $ = await get$('/hello')
expect($('#imported-value').text()).toBe('OK') expect($('#imported-value').text()).toBe('OK')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment