Skip to content
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
1 change: 0 additions & 1 deletion flow-typed/environments/bom.js
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,6 @@ declare class SharedWorker extends EventTarget {
declare function importScripts(...urls: Array<string | TrustedScriptURL>): void;

declare class WorkerGlobalScope extends EventTarget {
self: this;
location: WorkerLocation;
navigator: WorkerNavigator;
close(): void;
Expand Down
2 changes: 1 addition & 1 deletion flow-typed/environments/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ declare class HTMLCollection<+Elem: Element> {
length: number;
item(nameOrIndex?: any, optionalIndex?: any): Elem | null;
namedItem(name: string): Elem | null;
[index: number | string]: Elem;
+[index: number | string]: Elem;
}

// from https://www.w3.org/TR/custom-elements/#extensions-to-document-interface-to-register
Expand Down
8 changes: 4 additions & 4 deletions flow-typed/environments/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,7 @@ type http$agentOptions = {
declare class http$Agent<+SocketT = net$Socket> {
constructor(options: http$agentOptions): void;
destroy(): void;
freeSockets: { [name: string]: $ReadOnlyArray<SocketT>, ... };
+freeSockets: { +[name: string]: $ReadOnlyArray<SocketT>, ... };
getName(options: {
host: string,
port: number,
Expand All @@ -1914,11 +1914,11 @@ declare class http$Agent<+SocketT = net$Socket> {
}): string;
maxFreeSockets: number;
maxSockets: number;
requests: {
[name: string]: $ReadOnlyArray<http$ClientRequest<SocketT>>,
+requests: {
+[name: string]: $ReadOnlyArray<http$ClientRequest<SocketT>>,
...
};
sockets: { [name: string]: $ReadOnlyArray<SocketT>, ... };
+sockets: { +[name: string]: $ReadOnlyArray<SocketT>, ... };
}

declare class http$IncomingMessage<SocketT = net$Socket>
Expand Down
6 changes: 3 additions & 3 deletions flow-typed/npm/@babel/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ declare module '@babel/traverse' {
[K in keyof _NodeToTuple<T>]: NodePath<_NodeToTuple<T>[K]>,
};

type TParentPath<T: Node> = T extends t.Program ? null : NodePath<>;
type TParentPath<+T: Node> = T extends t.Program ? null : NodePath<>;

interface object {}

Expand All @@ -300,14 +300,14 @@ declare module '@babel/traverse' {
state: any;
opts: object;
skipKeys: object;
parentPath: TParentPath<T>;
+parentPath: TParentPath<T>;
context: TraversalContext;
container: object | $ReadOnlyArray<object>;
listKey: string;
inList: boolean;
parentKey: string;
key: string | number;
node: T;
+node: T;
scope: Scope;

type: T extends null | void
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"eslint-plugin-ft-flow": "^3.0.11",
"eslint-plugin-headers": "~1.2.1",
"eslint-plugin-react": "^7.37.1",
"flow-bin": "^0.291.0",
"flow-bin": "^0.295.0",
"flow-typed": "^4.1.1",
"hermes-eslint": "^0.32.1",
"husky": "^8.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

jest.autoMockOff();

import { transformSync } from '@babel/core';
import jsx from '@babel/plugin-syntax-jsx';
import stylexPlugin from '../src/index';
import path from 'path';

function transform(source, opts = {}) {
return transformSync(source, {
filename: opts.filename,
parserOpts: {
sourceType: 'module',
flow: 'all',
},
babelrc: false,
plugins: [
jsx,
[
stylexPlugin,
{
unstable_moduleResolution: {
rootDir: path.parse(process.cwd()).root,
type: 'commonJS',
},
...opts,
runtimeInjection: true,
},
],
],
}).code;
}

describe('@stylexjs/babel-plugin', () => {
describe('[transform] stylex.attrs() call', () => {
test('empty stylex.attrs call', () => {
expect(
transform(`
import stylex from 'stylex';
stylex.attrs();
`),
).toMatchInlineSnapshot(`
"import stylex from 'stylex';
({});"
`);
});

test('basic stylex attrs call', () => {
expect(
transform(`
import stylex from 'stylex';
const styles = stylex.create({
red: {
color: 'red',
}
});
stylex.attrs(styles.red);
`),
).toMatchInlineSnapshot(`
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
var _inject2 = _inject;
import stylex from 'stylex';
_inject2({
ltr: ".x1e2nbdu{color:red}",
priority: 3000
});
({
class: "x1e2nbdu"
});"
`);
});

test('attrs call within jsx spread', () => {
expect(
transform(`
import stylex from 'stylex';
const styles = stylex.create({
red: {
color: 'red',
}
});
function Foo() {
return <div {...stylex.attrs(styles.red)} />;
}
`),
).toMatchInlineSnapshot(`
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
var _inject2 = _inject;
import stylex from 'stylex';
_inject2({
ltr: ".x1e2nbdu{color:red}",
priority: 3000
});
function Foo() {
return <div class="x1e2nbdu" />;
}"
`);
});
});
});
4 changes: 4 additions & 0 deletions packages/@stylexjs/babel-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import transformStylexCall, {
} from './visitors/stylex-merge';
import transformStylexProps from './visitors/stylex-props';
import { skipStylexPropsChildren } from './visitors/stylex-props';
import transformStylexAttrs from './visitors/stylex-attrs';
import { skipStylexAttrsChildren } from './visitors/stylex-attrs';
import transformStyleXViewTransitionClass from './visitors/stylex-view-transition-class';
import transformStyleXDefaultMarker from './visitors/stylex-default-marker';
import {
Expand Down Expand Up @@ -158,12 +160,14 @@ function styleXTransform(): PluginObj<> {
// should be kept.
skipStylexMergeChildren(path, state);
skipStylexPropsChildren(path, state);
skipStylexAttrsChildren(path, state);
},
});
path.traverse({
CallExpression(path: NodePath<t.CallExpression>) {
transformStylexCall(path, state);
transformStylexProps(path, state);
transformStylexAttrs(path, state);
},
});

Expand Down
Loading
Loading