@stylexjs/babel-plugin 0.2.0-beta.20 → 0.2.0-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,8 +28,7 @@ export { traverse, NodePath };
28
28
  export type Visitor<S> = _Visitor<S>;
29
29
 
30
30
  export type Node = t.Node;
31
- export type ParseResult<Result: { +[string]: mixed } = { +[string]: mixed }> =
32
- _ParseResult<Result>;
31
+ export type ParseResult<Result: t.Node = t.Node> = _ParseResult<Result>;
33
32
  declare export var version: string;
34
33
  declare export var DEFAULT_EXTENSIONS: ['.js', '.jsx', '.es6', '.es', '.mjs'];
35
34
 
@@ -586,7 +585,7 @@ declare export var parse: ((
586
585
  declare export function parseSync(
587
586
  code: string,
588
587
  options?: TransformOptions,
589
- ): ParseResult<> | null;
588
+ ): ParseResult<t.File> | null;
590
589
 
591
590
  /**
592
591
  * Given some code, parse it using Babel's standard behavior.
@@ -595,7 +594,7 @@ declare export function parseSync(
595
594
  declare export function parseAsync(
596
595
  code: string,
597
596
  options?: TransformOptions,
598
- ): Promise<ParseResult<> | null>;
597
+ ): Promise<ParseResult<t.File> | null>;
599
598
 
600
599
  /**
601
600
  * Resolve Babel's options fully, resulting in an options object where:
@@ -246,7 +246,8 @@ export type ParseError = {
246
246
  reasonCode: string,
247
247
  };
248
248
 
249
- export type ParseResult<Result: { +[string]: mixed }> = {
250
- ...Result,
251
- errors: ParseError[],
252
- };
249
+ export type ParseResult<Result: t.Node = t.Node> =
250
+ | Result
251
+ | {
252
+ +errors: $ReadOnlyArray<ParseError>,
253
+ };
package/lib/index.js CHANGED
@@ -2744,8 +2744,8 @@ function isReferencedIdentifier(path, props) {
2744
2744
  return path.isReferencedIdentifier(props);
2745
2745
  }
2746
2746
 
2747
- const VALID_CALLEES = ["String", "Number", "Math"];
2748
- const INVALID_METHODS = ["random"];
2747
+ const VALID_CALLEES = ["String", "Number", "Math", "Object", "Array"];
2748
+ const INVALID_METHODS = ["random", "assign", "defineProperties", "defineProperty", "freeze", "seal", "splice"];
2749
2749
  function isValidCallee(val) {
2750
2750
  return VALID_CALLEES.includes(val);
2751
2751
  }
@@ -2762,12 +2762,13 @@ function evaluateImportedFile(filePath, namedExport, state) {
2762
2762
  const ast = core.parseSync(fileContents, {
2763
2763
  babelrc: true
2764
2764
  });
2765
- if (!ast) {
2765
+ if (!ast || ast.errors || !t__namespace.isNode(ast)) {
2766
2766
  state.confident = false;
2767
2767
  return;
2768
2768
  }
2769
+ const astNode = ast;
2769
2770
  let result;
2770
- traverse(ast, {
2771
+ traverse(astNode, {
2771
2772
  ExportNamedDeclaration(path) {
2772
2773
  const declaration = path.get("declaration");
2773
2774
  if (isVariableDeclaration(declaration)) {
@@ -2849,6 +2850,35 @@ function evaluateCached(path, state) {
2849
2850
  }
2850
2851
  function _evaluate(path, state) {
2851
2852
  if (!state.confident) return;
2853
+ if (isArrowFunctionExpression(path)) {
2854
+ const body = path.get("body");
2855
+ const params = path.get("params");
2856
+ const identParams = params.filter(param => isIdentifier(param)).map(paramPath => paramPath.node.name);
2857
+ if (isExpression(body) && identParams.length === params.length) {
2858
+ const expr = body;
2859
+ return function () {
2860
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
2861
+ args[_key] = arguments[_key];
2862
+ }
2863
+ const identifierEntries = identParams.map((ident, index) => [ident, args[index]]);
2864
+ const identifiersObj = Object.fromEntries(identifierEntries);
2865
+ const result = evaluate(expr, state.traversalState, {
2866
+ ...state.functions,
2867
+ identifiers: {
2868
+ ...state.functions.identifiers,
2869
+ ...identifiersObj
2870
+ }
2871
+ });
2872
+ return result.value;
2873
+ };
2874
+ }
2875
+ }
2876
+ if (isIdentifier(path)) {
2877
+ const name = path.node.name;
2878
+ if (Object.keys(state.functions?.identifiers ?? {}).includes(name)) {
2879
+ return state.functions.identifiers[name];
2880
+ }
2881
+ }
2852
2882
  if (isSequenceExpression(path)) {
2853
2883
  const exprs = path.get("expressions");
2854
2884
  return evaluateCached(exprs[exprs.length - 1], state);
@@ -3153,6 +3183,15 @@ function _evaluate(path, state) {
3153
3183
  const val = object.node.value;
3154
3184
  func = val[property.node.name];
3155
3185
  }
3186
+ const parsedObj = evaluate(object, state.traversalState, state.functions);
3187
+ if (parsedObj.confident && isIdentifier(property)) {
3188
+ func = parsedObj.value[property.node.name];
3189
+ context = parsedObj.value;
3190
+ }
3191
+ if (parsedObj.confident && isStringLiteral(property)) {
3192
+ func = parsedObj.value[property.node.value];
3193
+ context = parsedObj.value;
3194
+ }
3156
3195
  }
3157
3196
  if (func) {
3158
3197
  if (func.takesPath) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stylexjs/babel-plugin",
3
- "version": "0.2.0-beta.20",
3
+ "version": "0.2.0-beta.21",
4
4
  "description": "StyleX babel plugin.",
5
5
  "main": "lib/index.js",
6
6
  "repository": "https://github.com/facebook/stylex",
@@ -12,7 +12,7 @@
12
12
  "test": "jest"
13
13
  },
14
14
  "dependencies": {
15
- "@stylexjs/shared": "0.2.0-beta.20"
15
+ "@stylexjs/shared": "0.2.0-beta.21"
16
16
  },
17
17
  "peerDependencies": {
18
18
  "@babel/core": "^7.19.6",