Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 1, 2024
1 parent 908d6ff commit 46cd8ba
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 38 deletions.
9 changes: 3 additions & 6 deletions src/ast/nodes/JSXAttribute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import { stringifyObjectKeyIfNeeded } from '../../utils/identifierHelpers';
import type { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import type JSXElement from './JSXElement';
Expand All @@ -15,13 +16,9 @@ export default class JSXAttribute extends NodeBase {
name!: JSXIdentifier | JSXNamespacedName;
value!: Literal | JSXExpressionContainer | JSXElement | JSXFragment | null;

render(
code: MagicString,
options: RenderOptions,
{ jsxMode = 'preserve' }: NodeRenderOptions = {}
): void {
render(code: MagicString, options: RenderOptions, { jsxMode }: NodeRenderOptions = BLANK): void {
super.render(code, options);
if (jsxMode !== 'preserve') {
if ((['classic', 'automatic'] as (string | undefined)[]).includes(jsxMode)) {
const { name, value } = this;
const key =
name instanceof JSXIdentifier ? name.name : `${name.namespace.name}:${name.name.name}`;
Expand Down
11 changes: 4 additions & 7 deletions src/ast/nodes/JSXIdentifier.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type MagicString from 'magic-string';
import type { NormalizedJsxOptions } from '../../rollup/types';
import type { RenderOptions } from '../../utils/renderHelpers';
import type JSXClosingElement from './JSXClosingElement';
import type JSXMemberExpression from './JSXMemberExpression';
import type JSXOpeningElement from './JSXOpeningElement';
import type * as NodeType from './NodeType';
import IdentifierBase from './shared/IdentifierBase';

Expand Down Expand Up @@ -54,11 +52,9 @@ export default class JSXIdentifier extends IdentifierBase {
switch (this.parent.type) {
case 'JSXOpeningElement':
case 'JSXClosingElement': {
return (this.parent as JSXOpeningElement | JSXClosingElement).name === this
? this.name.startsWith(this.name.charAt(0).toUpperCase())
? IdentifierType.Reference
: IdentifierType.NativeElementName
: IdentifierType.Other;
return this.name.startsWith(this.name.charAt(0).toUpperCase())
? IdentifierType.Reference
: IdentifierType.NativeElementName;
}
case 'JSXMemberExpression': {
return (this.parent as JSXMemberExpression).object === this
Expand All @@ -70,6 +66,7 @@ export default class JSXIdentifier extends IdentifierBase {
return IdentifierType.Other;
}
default: {
/* istanbul ignore next */
throw new Error(`Unexpected parent node type for JSXIdentifier: ${this.parent.type}`);
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/ast/scopes/GlobalScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ export default class GlobalScope extends Scope {
this.variables.set('undefined', new UndefinedVariable());
}

findGlobal(name: string): Variable {
return this.findVariable(name);
}

findVariable(name: string): Variable {
let variable = this.variables.get(name);
if (!variable) {
Expand Down
9 changes: 0 additions & 9 deletions src/ast/scopes/Scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ export default class Scope {
return this.variables.has(name);
}

/**
* This allows us to protect the name of a variable by pretending we also
* include a global variable of the same name.
*/
findGlobal(_name: string): void {
/* istanbul ignore next */
throw new Error('Internal Error: findGlobal needs to be implemented by a subclass');
}

findVariable(_name: string): Variable {
/* istanbul ignore next */
throw new Error('Internal Error: findVariable needs to be implemented by a subclass');
Expand Down
13 changes: 2 additions & 11 deletions src/utils/options/mergeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {
RollupCache,
RollupOptions
} from '../../rollup/types';
import { EMPTY_ARRAY } from '../blank';
import { ensureArray } from '../ensureArray';
import { getLogger } from '../logger';
import { LOGLEVEL_INFO } from '../logging';
Expand Down Expand Up @@ -140,7 +139,6 @@ function mergeInputOptions(
config,
overrides,
'jsx',
['preserve'],
objectifyOptionWithPresets(jsxPresets, 'jsx', URL_JSX, 'false, ')
),
logLevel: getOption('logLevel'),
Expand All @@ -159,7 +157,6 @@ function mergeInputOptions(
config,
overrides,
'treeshake',
EMPTY_ARRAY,
objectifyOptionWithPresets(treeshakePresets, 'treeshake', URL_TREESHAKE, 'false, true, ')
),
watch: getWatch(config, overrides)
Expand All @@ -181,13 +178,8 @@ const getObjectOption = <T extends object>(
config: T,
overrides: T,
name: keyof T,
nonObjectValues: readonly unknown[],
objectifyValue = objectifyOption
): any => {
const primitiveValue = overrides[name] ?? config[name];
if (nonObjectValues.includes(primitiveValue)) {
return primitiveValue;
}
const commandOption = normalizeObjectOptionValue(overrides[name], objectifyValue);
const configOption = normalizeObjectOptionValue(config[name], objectifyValue);
if (commandOption !== undefined) {
Expand All @@ -197,7 +189,7 @@ const getObjectOption = <T extends object>(
};

export const getWatch = (config: InputOptions, overrides: InputOptions) =>
config.watch !== false && getObjectOption(config, overrides, 'watch', EMPTY_ARRAY);
config.watch !== false && getObjectOption(config, overrides, 'watch');

export const isWatchEnabled = (optionValue: unknown): boolean => {
if (Array.isArray(optionValue)) {
Expand Down Expand Up @@ -236,7 +228,7 @@ async function mergeOutputOptions(
): Promise<OutputOptions> {
const getOption = (name: keyof OutputOptions): any => overrides[name] ?? config[name];
const outputOptions: CompleteOutputOptions<keyof OutputOptions> = {
amd: getObjectOption(config, overrides, 'amd', EMPTY_ARRAY),
amd: getObjectOption(config, overrides, 'amd'),
assetFileNames: getOption('assetFileNames'),
banner: getOption('banner'),
chunkFileNames: getOption('chunkFileNames'),
Expand All @@ -259,7 +251,6 @@ async function mergeOutputOptions(
config,
overrides,
'generatedCode',
EMPTY_ARRAY,
objectifyOptionWithPresets(
generatedCodePresets,
'output.generatedCode',
Expand Down
2 changes: 1 addition & 1 deletion src/utils/options/normalizeInputOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const getJsx = (config: InputOptions): NormalizedInputOptions['jsx'] => {
case 'automatic': {
return {
factory: factory || 'React.createElement',
importSource: importSource || null,
importSource: importSource || 'react',
jsxImportSource: configWithPreset.jsxImportSource || 'react/jsx-runtime',
mode: 'automatic'
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = defineTest({
description: 'transpiles JSX for react',
options: {
external: ['react', 'react/jsx-runtime'],
jsx: { mode: 'automatic' }
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
import react from 'react';

const Foo = () => {};
const obj = { key: '2' };

console.log(/*#__PURE__*/jsx(Foo, {}));
console.log(/*#__PURE__*/jsx(Fragment, { children: /*#__PURE__*/jsx(Foo, {}) }));
console.log(/*#__PURE__*/jsxs(Foo, { children: [/*#__PURE__*/jsx(Foo, {}), /*#__PURE__*/jsx(Foo, {})] }));
console.log(/*#__PURE__*/jsxs(Fragment, { children: [/*#__PURE__*/jsx(Foo, {}), /*#__PURE__*/jsx(Foo, {})] }));
console.log(/*#__PURE__*/react.createElement(Foo, Object.assign({}, obj, { key: "1" })));
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Foo = () => {};
const obj = { key: '2' };

console.log(<Foo />);
console.log(<><Foo/></>);
console.log(<Foo><Foo/><Foo/></Foo>);
console.log(<><Foo/><Foo/></>);
console.log(<Foo {...obj} key="1" />);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = defineTest({
description: 'transpiles JSX for react',
options: {
external: ['react', 'react/jsx-runtime'],
jsx: { mode: 'classic' }
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Foo = () => {};
const obj = { key: '2' };

console.log(/*#__PURE__*/React.createElement(Foo, null));
console.log(/*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Foo, null)));
console.log(/*#__PURE__*/React.createElement(Foo, null, /*#__PURE__*/React.createElement(Foo, null), /*#__PURE__*/React.createElement(Foo, null)));
console.log(/*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Foo, null), /*#__PURE__*/React.createElement(Foo, null)));
console.log(/*#__PURE__*/React.createElement(Foo, Object.assign({}, obj, { key: "1" })));
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Foo = () => {};
const obj = { key: '2' };

console.log(<Foo />);
console.log(<><Foo/></>);
console.log(<Foo><Foo/><Foo/></Foo>);
console.log(<><Foo/><Foo/></>);
console.log(<Foo {...obj} key="1" />);

0 comments on commit 46cd8ba

Please sign in to comment.