Skip to content

Commit

Permalink
chore: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Jan 8, 2025
1 parent d31965e commit 2a00aef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 59 deletions.
38 changes: 12 additions & 26 deletions packages/babel-plugin/__tests__/stylex-transform-create-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,17 @@ const { transformSync } = require('@babel/core');
const flowPlugin = require('@babel/plugin-syntax-flow');
const stylexPlugin = require('../src/index');

function transform(source, opts = {}) {
return transformSync(source, {
filename: opts.filename,
parserOpts: {
flow: 'all',
},
babelrc: false,
plugins: [
flowPlugin,
[
stylexPlugin,
{
runtimeInjection: true,
unstable_moduleResolution: { type: 'haste' },
...opts,
},
],
],
}).code;
}
const defaultParserOpts = {
flow: 'all',
};

function transformTypescript(source, opts = {}) {
function transform(source, opts = {}, parserOpts = defaultParserOpts) {
return transformSync(source, {
filename: opts.filename,
parserOpts: {
plugins: ['typescript'],
},
parserOpts,
babelrc: false,
plugins: [
flowPlugin,
[
stylexPlugin,
{
Expand Down Expand Up @@ -1326,7 +1308,8 @@ describe('@stylexjs/babel-plugin', () => {

test('typescript namespace transform', () => {
expect(
transformTypescript(`
transform(
`
import stylex from 'stylex';
namespace A {
export const styles = stylex.create({
Expand All @@ -1336,7 +1319,10 @@ describe('@stylexjs/babel-plugin', () => {
})
}
stylex.props(A.styles);
`),
`,
{},
{ plugins: ['typescript'] },
),
).toMatchInlineSnapshot(`
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
var _inject2 = _inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,14 @@ const defaultOpts = {
// test environment still.
const rootDir = '/stylex/packages/';

function transform(source, opts = defaultOpts) {
return transformSync(source, {
filename: opts.filename || '/stylex/packages/TestTheme.stylex.js',
parserOpts: {
flow: 'all',
},
babelrc: false,
plugins: [[stylexPlugin, { ...defaultOpts, ...opts }]],
}).code;
}
const defaultParserOpts = {
flow: 'all',
};

function transformTypescript(source, opts = {}) {
function transform(source, opts = defaultOpts, parserOpts = defaultParserOpts) {
return transformSync(source, {
filename: opts.filename || '/stylex/packages/TestTheme.stylex.js',
parserOpts: {
plugins: ['typescript'],
},
parserOpts,
babelrc: false,
plugins: [[stylexPlugin, { ...defaultOpts, ...opts }]],
}).code;
Expand Down Expand Up @@ -707,13 +698,17 @@ describe('@stylexjs/babel-plugin stylex.createTheme with literals', () => {

describe('[transform] typescript namespace', () => {
expect(
transformTypescript(`
transform(
`
import stylex from 'stylex';
namespace A {
export const buttonTheme = stylex.defineVars(${createTheme});
export const buttonThemePositive = stylex.createTheme(buttonTheme, ${createThemeWithDifferentOrder});
}
`),
`,
{},
{ plugins: ['typescript'] },
),
).toMatchInlineSnapshot(`
"import stylex from 'stylex';
namespace A {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,23 @@ const defaultOpts = {
debug: false,
};

const defaultParserOpts = {
flow: 'all',
};

// NOTE: While `rootDir` is optional now, It is needed in the
// test environment still.
const rootDir = '/stylex/packages/';

function transform(source, opts = defaultOpts) {
function transform(source, opts = defaultOpts, parserOpts = defaultParserOpts) {
return transformSync(source, {
filename: opts.filename || '/stylex/packages/TestTheme.stylex.js',
parserOpts: {
flow: 'all',
},
parserOpts,
babelrc: false,
plugins: [[stylexPlugin, { ...defaultOpts, ...opts }]],
});
}

function transformTypescript(source, opts = {}) {
return transformSync(source, {
filename: opts.filename || '/stylex/packages/TestTheme.stylex.js',
parserOpts: {
plugins: ['typescript'],
},
babelrc: false,
plugins: [[stylexPlugin, { ...defaultOpts, ...opts }]],
}).code;
}

describe('@stylexjs/babel-plugin', () => {
describe('[transform] stylex.defineVars()', () => {
test('transforms variables object', () => {
Expand Down Expand Up @@ -785,7 +776,7 @@ describe('@stylexjs/babel-plugin', () => {

test('transform typescript namespace', () => {
expect(
transformTypescript(
transform(
`
import stylex from 'stylex';
namespace A {
Expand All @@ -797,7 +788,8 @@ describe('@stylexjs/babel-plugin', () => {
}
`,
{ dev: true, ...defaultOpts },
),
{ plugins: ['typescript'] },
).code,
).toMatchInlineSnapshot(`
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
var _inject2 = _inject;
Expand Down

0 comments on commit 2a00aef

Please sign in to comment.