8000 [eslint-plugin] support validImports options for all rules by javascripter · Pull Request #1085 · facebook/stylex · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[eslint-plugin] support validImports options for all rules #1085

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

Merged
merged 1 commit into from
Jun 3, 2025

Conversation

javascripter
Copy link
Contributor
@javascripter javascripter commented May 28, 2025

What changed / motivation ?

Continuing on #1039, this PR adds validImports option to all eslint rules with support for both the string and object syntax uniformly, for RSD compatibility and other custom aliases.

I've also updated API docs and the README to document the options. I expect documentation rework to be made in another PR (pending #1057) so made the change minimal.

Main changes:

  • added validImports option to all rules
  • refactored the import name tracking logic to src/utils/createImportTracker.js to reduce duplication
  • added unit tests to test for custom aliases
  • documented changes

Linked PR/Issues

Fixes #1056

Additional Context

I ran the following tests locally and confirmed existing and new tests are passing:

npm run test

Screenshots, Tests, Anything Else

Pre-flight checklist

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 28, 2025
@@ -24,11 +24,17 @@ const invalidExportWithDefineVars =
ruleTester.run('stylex-enforce-extension', rule.default, {
valid: [
{
code: 'export const vars = stylex.defineVars({});',
code: `
import stylex from 'stylex';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The sideeffect of createImportTracker refactoring is that import statements must now be present in the file, not just specifying it in validImports option as we now collect import statements and check against the collected values.

Technically a breaking change but should not affect real code as imports should exist anyway.

'ExportNamedDeclaration, ExportDefaultDeclaration'(node: Node): void {
checkExports(node);
reportErrors(node);
},
'Program:exit'() {
importTracker.clear();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not too sure this cleanup is strictly necessary, but aligned it with packages/@stylexjs/eslint-plugin/src/stylex-sort-keys.js which does clean up.

.forEach(parseStylexImportStyle);
// stylex.create can only be singular variable declarations at the root
// of the file so we can look directly on Program and populate our set.
.filter(
Copy link
Contributor Author
@javascripter javascripter May 28, 2025

Choose a reason for hiding this comment

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

we need to ensure we collect import statements before variable detection happens, so this can't happen in ImportDeclaration and needs to be done in Program

}

return {
ImportDeclaration: handleImportDeclaration,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because of the conflict with ImportDeclaration type, function name cannot be ImportDeclaration so it was named handleImportDeclaration and aliased as ImportDeclaration here.

@@ -16,7 +16,7 @@ sidebar_position: 2
type Options = {
// Possible strings where you can import stylex from
//
// Default: ['@stylexjs/stylex']
// Default: ['stylex', '@stylexjs/stylex']
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Existing code already had stylex in validImports by default so I aligned the default for all rules and reflected it in the docs too.

@javascripter javascripter marked this pull request as ready for review May 28, 2025 16:36
Copy link
Contributor
@necolas necolas left a comment

Choose a reason for hiding this comment

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

Thanks this looks great. There's just some minor changes requested about test import syntax and removing support for default export, since there isn't one in the runtime.

Copy link
Contributor
@necolas necolas left a comment

Choose a reason for hiding this comment

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

LGTM. I think as you mentioned, we can restore the default import support for internal use case since it's a one line change. Thanks for this great PR!

@necolas necolas force-pushed the eslint-valid-imports branch from bee2b7e to 8985f91 Compare June 3, 2025 17:04
@necolas necolas merged commit 6e309e1 into facebook:main Jun 3, 2025
7 of 9 checks passed
@javascripter javascripter deleted the eslint-valid-imports branch June 4, 2025 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[eslint-plugin] Should work with importSources
3 participants
0