biome icon indicating copy to clipboard operation
biome copied to clipboard

feat(lint): add rule `useComponentsOnlyModule`

Open GunseiKPaseri opened this issue 1 year ago • 1 comments

Summary

Implement useComponentsOnlyModule( inspired eslint-plugin-react-refresh)

Closes https://github.com/biomejs/biome/issues/3560

This rule applies only to .jsx files. (Since .js files are also treated as .jsx, it is necessary to check the file extension.) It scans the declarations in the file and enumerates the following:

  • Component declarations that are not exported
  • Exported components
  • Exported non-components

The determination of whether something is a component or not is done simply by checking if the function or variable name is in PascalCase. (Exceptionally, cases where standard React functions like memo are used are also considered as components.) The enumeration of exports is achieved by searching for JsExport and using a newly implemented utility (get_exported_items). This utility extracts the names of declarations and the expressions being exported within JsExport. The declaration names are used to determine whether they are components, and the expressions are used for exception handling specific to React.

Based on the count of each of these, it outputs appropriate errors to ensure that the condition "components must always be exported, and only components should be exported" is met.

When attempting to export both components and non-components simultaneously, it instructs to move the export of non-components to a separate file. If there are components that are not exported, it instructs to move the component to a new file (with a slightly different error message depending on whether there are other exported components). No error is output in other cases. Options have been implemented to ignore the export of constants (allowConstantExport), to treat specific non-PascalCase exports as components (allowExportNames), ~~and to enforce the check on .js files as well (checkJS)~~. checkJS has not been implemented.

Test Plan

  • The added tests function correctly

GunseiKPaseri avatar Aug 02 '24 17:08 GunseiKPaseri

CodSpeed Performance Report

Merging #3576 will degrade performances by 7.96%

Comparing GunseiKPaseri:for-react-refresh (e3e6881) with main (0a80daa)

Summary

⚡ 1 improvements ❌ 1 regressions ✅ 105 untouched benchmarks

:warning: Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main GunseiKPaseri:for-react-refresh Change
js_analyzer[index_3894593175024091846.js] 37.3 ms 40.5 ms -7.96%
db_17847247775464589309.json[cached] 14.2 ms 12.8 ms +10.52%

codspeed-hq[bot] avatar Aug 02 '24 18:08 codspeed-hq[bot]

@GunseiKPaseri Thank you for your update! I will take a look tonight.

chansuke avatar Aug 16 '24 13:08 chansuke