Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Allow stylex.create nested within TS namespaces #841

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions packages/babel-plugin/__tests__/stylex-transform-create-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ const { transformSync } = require('@babel/core');
const flowPlugin = require('@babel/plugin-syntax-flow');
const stylexPlugin = require('../src/index');

function transform(source, opts = {}) {
const defaultParserOpts = {
flow: 'all',
};

function transform(source, opts = {}, parserOpts = defaultParserOpts) {
return transformSync(source, {
filename: opts.filename,
parserOpts: {
flow: 'all',
},
parserOpts,
babelrc: false,
plugins: [
flowPlugin,
Expand Down Expand Up @@ -1303,6 +1305,40 @@ describe('@stylexjs/babel-plugin', () => {
};"
`);
});

test('typescript namespace transform', () => {
expect(
transform(
`
import stylex from 'stylex';
namespace A {
export const styles = stylex.create({
default: {
color: 'red',
}
})
}
stylex.props(A.styles);
`,
{},
{ plugins: ['typescript'] },
),
).toMatchInlineSnapshot(`
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
var _inject2 = _inject;
import stylex from 'stylex';
_inject2(".x1e2nbdu{color:red}", 3000);
namespace A {
export const styles = {
default: {
color: "x1e2nbdu",
$$css: true
}
};
}
stylex.props(A.styles);"
`);
});
});

describe('[transform] stylex.create() with functions', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ const defaultOpts = {
// test environment still.
const rootDir = '/stylex/packages/';

function transform(source, opts = defaultOpts) {
const defaultParserOpts = {
flow: 'all',
};

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 }]],
}).code;
}

let defineVarsOutput = '';

const createTheme = `{
Expand Down Expand Up @@ -692,4 +695,35 @@ describe('@stylexjs/babel-plugin stylex.createTheme with literals', () => {
`);
});
});

describe('[transform] typescript namespace', () => {
expect(
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 {
export const buttonTheme = {
bgColor: "var(--xgck17p)",
bgColorDisabled: "var(--xpegid5)",
cornerRadius: "var(--xrqfjmn)",
fgColor: "var(--x4y59db)",
__themeName__: "x568ih9"
};
export const buttonThemePositive = {
$$css: true,
x568ih9: "xtrlmmh x568ih9"
};
}"
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ 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 }]],
});
Expand Down Expand Up @@ -771,5 +773,35 @@ describe('@stylexjs/babel-plugin', () => {
};"
`);
});

test('transform typescript namespace', () => {
expect(
transform(
`
import stylex from 'stylex';
namespace A {
export const buttonTheme = stylex.defineVars({
bgColor: {
default: 'grey'
}
})
}
`,
{ dev: true, ...defaultOpts },
{ plugins: ['typescript'] },
).code,
).toMatchInlineSnapshot(`
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
var _inject2 = _inject;
import stylex from 'stylex';
_inject2(":root, .x568ih9{--xgck17p:grey;}", 0);
namespace A {
export const buttonTheme = {
bgColor: "var(--xgck17p)",
__themeName__: "x568ih9"
};
}"
`);
});
});
});
3 changes: 2 additions & 1 deletion packages/babel-plugin/src/visitors/stylex-create/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ function validateStyleXCreate(path: NodePath<t.CallExpression>) {
const nearestStatement = findNearestStatementAncestor(path);
if (
!pathUtils.isProgram(nearestStatement.parentPath) &&
!pathUtils.isExportNamedDeclaration(nearestStatement.parentPath)
!pathUtils.isExportNamedDeclaration(nearestStatement.parentPath) &&
!pathUtils.isTSModuleBlock(nearestStatement.parentPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. This should fix the issue for now!

) {
throw path.buildCodeFrameError(messages.ONLY_TOP_LEVEL, SyntaxError);
}
Expand Down
Loading