Skip to content

match JSX attributes whose value is an expression in hasAttributes - #755

Open
aashish254 wants to merge 2 commits into
meta:mainfrom
aashish254:fix/jsx-has-attributes-expression-values
Open

aashish254 wants to merge 2 commits into
meta:mainfrom
aashish254:fix/jsx-has-attributes-expression-values

Conversation

@aashish254

Copy link
Copy Markdown

hasAttributes could not match any attribute written as an expression. Given:

<FooBar a={true} b={1} c={'str'} />

every one of these filters returns an empty collection:

hasAttributes({a: true})          // 0 matches
hasAttributes({b: 1})             // 0
hasAttributes({c: 'str'})         // 0
hasAttributes({a: v => v === true})  // 0

Measured on main with the default (babel) parser, and again with flow, tsx and babylon: all four return 0 for all four filters. The same probe on this branch returns 1 for all sixteen of them.

Why. The filter unwrapped a literal only when the value node itself was a literal (attr="foo"). For attr={...} the value is a JSXExpressionContainer, so actual became the expression node and the comparison String(expected) === actual compared a string to an object. It could therefore never be true, and callback filters were handed an AST node where the docstring for the value-less case (baz: v => v === null, added in #277) says they receive a value.

What changed

  • getAttributeValue() unwraps a literal that sits inside a JSXExpressionContainer, using the same Literal.check the filter already used. attr={true} now yields true, attr={'x'} yields 'x', attr="x" is unchanged, a value-less attr is still null, and a computed attr={foo()} still yields the expression node.
  • The comparison now compares the two values as strings, so {b: 1} matches b={1} the way {name: 'foo'} has always matched name="foo". Attributes that carry no comparable value (null, or a non-literal expression) still never match, exactly as before — the switch on typeof actual makes that explicit instead of relying on a string never equaling an object.
  • JSDoc for hasAttributes now states what the filter accepts. There is no hasAttributes page in the website docs to update.

Verification

  • 6 new tests in src/collections/__tests__/JSXElement-test.js, in a describe block with its own fixture so no existing counts move. 4 of them fail on main (boolean/number values, string in a container, string comparison, callback receives the value); the other 2 are regression guards that pass on both, for the value-less and computed cases and for plain attr="foo".
  • Each half of the fix is load-bearing on its own: removing the container unwrap fails all 4; keeping the unwrap but reverting the string comparison fails 2 (matches boolean and number values, compares the filter to the value as strings).
  • yarn --frozen-lockfile && yarn test — the exact CI commands: 19 suites, 227 passed, 1 pre-existing skip, 11 snapshots pass. yarn.lock untouched.
  • ESLint with the repo's .eslintrc rules (quotes: single, eslint:recommended) reports nothing on both files. It needs requireConfigFile: false to run at all — as shipped, eslint fails to parse every file in src/ on a clean checkout, mine included, so it isn't a signal I introduced.
  • All changed lines stay within the 80-column style in CONTRIBUTING.md.

@changeset-bot

changeset-bot Bot commented Oct 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ad3f3a4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
jscodeshift Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Oct 20, 2026

Copy link
Copy Markdown

@aashish254 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed label Oct 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant