From 7eda9c2da3b6f7f8a3fed3e2607ddc941d3d0135 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 27 Jun 2022 00:40:12 -0400 Subject: [PATCH] fix --- src/test/helpers.ts | 2 ++ src/test/transpilers.spec.ts | 62 +++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/test/helpers.ts b/src/test/helpers.ts index c4ea9e8a4..da86bddc2 100644 --- a/src/test/helpers.ts +++ b/src/test/helpers.ts @@ -99,6 +99,8 @@ export const tsSupportsStableNodeNextNode16 = // TS 4.5 is first version to understand .cts, .mts, .cjs, and .mjs extensions export const tsSupportsMtsCtsExtensions = semver.gte(ts.version, '4.5.0'); export const tsSupportsImportAssertions = semver.gte(ts.version, '4.5.0'); +// TS 4.1 added jsx=react-jsx and react-jsxdev: https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#react-17-jsx-factories +export const tsSupportsReact17JsxFactories = semver.gte(ts.version, '4.1.0'); //#endregion export const xfs = new NodeFS(fs); diff --git a/src/test/transpilers.spec.ts b/src/test/transpilers.spec.ts index 6920a2747..1d64186a0 100644 --- a/src/test/transpilers.spec.ts +++ b/src/test/transpilers.spec.ts @@ -3,7 +3,12 @@ // Should consolidate them here. import { context } from './testlib'; -import { ctxTsNode, testsDirRequire } from './helpers'; +import { + ctxTsNode, + testsDirRequire, + tsSupportsImportAssertions, + tsSupportsReact17JsxFactories, +} from './helpers'; import { createSwcOptions } from '../transpilers/swc'; import * as expect from 'expect'; import { outdent } from 'outdent'; @@ -76,8 +81,11 @@ test.suite('swc', (test) => { ); test(macro, 'react', undefined, undefined); - test(macro, 'react-jsx', 'automatic', undefined); - test(macro, 'react-jsxdev', 'automatic', true); + test.suite('react 17 jsx factories', (test) => { + test.runIf(tsSupportsReact17JsxFactories); + test(macro, 'react-jsx', 'automatic', undefined); + test(macro, 'react-jsxdev', 'automatic', true); + }); }); }); @@ -113,31 +121,35 @@ test.suite('swc', (test) => { input, `const div = /*#__PURE__*/ React.createElement("div", null);` ); - test( - compileMacro, - { jsx: 'react-jsx' }, - input, - outdent` - import { jsx as _jsx } from "react/jsx-runtime"; - const div = /*#__PURE__*/ _jsx("div", {}); - ` - ); - test( - compileMacro, - { jsx: 'react-jsxdev' }, - input, - outdent` - import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime"; - const div = /*#__PURE__*/ _jsxDEV("div", {}, void 0, false, { - fileName: "input.tsx", - lineNumber: 1, - columnNumber: 13 - }, this); - ` - ); + test.suite('react 17 jsx factories', (test) => { + test.runIf(tsSupportsReact17JsxFactories); + test( + compileMacro, + { jsx: 'react-jsx' }, + input, + outdent` + import { jsx as _jsx } from "react/jsx-runtime"; + const div = /*#__PURE__*/ _jsx("div", {}); + ` + ); + test( + compileMacro, + { jsx: 'react-jsxdev' }, + input, + outdent` + import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime"; + const div = /*#__PURE__*/ _jsxDEV("div", {}, void 0, false, { + fileName: "input.tsx", + lineNumber: 1, + columnNumber: 13 + }, this); + ` + ); + }); }); test.suite('preserves import assertions for json imports', (test) => { + test.runIf(tsSupportsImportAssertions); test( 'basic json import', compileMacro,