biomejs / biome

A toolchain for web projects, aimed to provide functionalities to maintain them. Biome offers formatter and linter, usable via CLI and LSP.
https://biomejs.dev
Apache License 2.0
14.32k stars 445 forks source link

πŸ’… React TypeScript issue on `The React variable is undeclared` #3998

Open elawad opened 2 days ago

elawad commented 2 days ago

Environment information

CLI:
  Version:                      1.9.1
  Color support:                true

Platform:
  CPU Architecture:             aarch64
  OS:                           macos

Environment:
  BIOME_LOG_PATH:               unset
  BIOME_LOG_PREFIX_NAME:        unset
  BIOME_CONFIG_PATH:            unset
  NO_COLOR:                     unset
  TERM:                         "xterm-256color"
  JS_RUNTIME_VERSION:           "v20.16.0"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         "yarn/1.22.22"

Biome Configuration:
  Status:                       Loaded successfully
  Formatter disabled:           false
  Linter disabled:              false
  Organize imports disabled:    false
  VCS disabled:                 false

Linter:
  JavaScript enabled:           true
  JSON enabled:                 true
  CSS enabled:                  true
  GraphQL enabled:              false
  Recommended:                  false
  All:                          false
  Enabled rules:
  performance/noDelete
  suspicious/noCatchAssign
  suspicious/noUnsafeNegation
  complexity/useLiteralKeys
  suspicious/noClassAssign
  style/useImportType
  complexity/noMultipleSpacesInRegularExpressionLiterals
  a11y/useValidLang
  complexity/noUselessEmptyExport
  suspicious/useNamespaceKeyword
  suspicious/useValidTypeof
  a11y/useValidAriaRole
  correctness/noConstantCondition
  a11y/useAriaActivedescendantWithTabindex
  suspicious/noAssignInExpressions
  style/useDefaultParameterLast
  complexity/noEmptyTypeParameters
  correctness/noConstructorReturn
  style/useSelfClosingElements
  suspicious/noDuplicateParameters
  suspicious/noDuplicateSelectorsKeyframeBlock
  correctness/noUnknownProperty
  style/useTemplate
  correctness/noUnusedLabels
  complexity/noUselessTernary
  correctness/noUnreachableSuper
  suspicious/noCompareNegZero
  suspicious/noExplicitAny
  correctness/noSwitchDeclarations
  a11y/noAutofocus
  correctness/noUnsafeOptionalChaining
  correctness/noConstAssign
  suspicious/noControlCharactersInRegex
  complexity/noUselessTypeConstraint
  style/noVar
  suspicious/noDoubleEquals
  suspicious/noRedundantUseStrict
  style/useLiteralEnumMembers
  suspicious/noGlobalIsNan
  suspicious/noEmptyInterface
  suspicious/noConstEnum
  suspicious/noMisleadingCharacterClass
  correctness/noPrecisionLoss
  a11y/noLabelWithoutControl
  suspicious/noRedeclare
  correctness/noStringCaseMismatch
  correctness/noSetterReturn
  correctness/noInvalidConstructorSuper
  suspicious/noImplicitAnyLet
  suspicious/noFallthroughSwitchClause
  suspicious/noUnsafeDeclarationMerging
  complexity/noUselessThisAlias
  correctness/noUnreachable
  a11y/useKeyWithClickEvents
  suspicious/noDuplicateObjectKeys
  complexity/noThisInStatic
  complexity/useOptionalChain
  correctness/noInnerDeclarations
  style/noParameterAssign
  suspicious/noDuplicateCase
  a11y/useValidAnchor
  complexity/useRegexLiterals
  correctness/noSelfAssign
  correctness/noInvalidBuiltinInstantiation
  style/noUselessElse
  style/useShorthandFunctionType
  suspicious/noShadowRestrictedNames
  correctness/noInvalidDirectionInLinearGradient
  suspicious/noImportantInKeyframe
  a11y/useMediaCaption
  complexity/noUselessLabel
  complexity/noUselessCatch
  correctness/noUnsafeFinally
  a11y/useAriaPropsForRole
  correctness/noNonoctalDecimalEscape
  style/useEnumInitializers
  a11y/useHtmlLang
  suspicious/noDuplicateTestHooks
  complexity/noStaticOnlyClass
  style/useWhile
  complexity/useArrowFunction
  style/noInferrableTypes
  a11y/noNoninteractiveTabindex
  complexity/useSimpleNumberKeys
  correctness/useYield
  a11y/noInteractiveElementToNoninteractiveRole
  style/useNumericLiterals
  correctness/noUnnecessaryContinue
  suspicious/noApproximativeNumericConstant
  suspicious/noImportAssign
  suspicious/noLabelVar
  correctness/noGlobalObjectCalls
  suspicious/useDefaultSwitchClauseLast
  correctness/noUnknownUnit
  a11y/useAltText
  correctness/noEmptyCharacterClassInRegex
  suspicious/noSparseArray
  a11y/useIframeTitle
  complexity/noBannedTypes
  a11y/noSvgWithoutTitle
  correctness/noVoidElementsWithChildren
  style/useAsConstAssertion
  correctness/useJsxKeyInIterable
  style/useExportType
  complexity/noUselessLoneBlockStatements
  suspicious/noDebugger
  style/noArguments
  a11y/useValidAriaValues
  suspicious/noCommentText
  a11y/useFocusableInteractive
  correctness/noUnmatchableAnbSelector
  suspicious/noGlobalAssign
  suspicious/noDuplicateJsxProps
  suspicious/noMisleadingInstantiator
  a11y/noPositiveTabindex
  correctness/noEmptyPattern
  complexity/noExcessiveNestedTestSuites
  security/noDangerouslySetInnerHtmlWithChildren
  a11y/useKeyWithMouseEvents
  suspicious/noExtraNonNullAssertion
  suspicious/noShorthandPropertyOverrides
  correctness/noRenderReturnValue
  correctness/useExhaustiveDependencies
  security/noGlobalEval
  style/noNonNullAssertion
  a11y/noRedundantRoles
  complexity/useFlatMap
  correctness/useIsNan
  correctness/noUnusedVariables
  style/useConst
  suspicious/noGlobalIsFinite
  suspicious/noSelfCompare
  suspicious/noAsyncPromiseExecutor
  suspicious/noDuplicateFontNames
  suspicious/noThenProperty
  suspicious/useGetterReturn
  security/noDangerouslySetInnerHtml
  style/useNodejsImportProtocol
  a11y/noDistractingElements
  suspicious/noArrayIndexKey
  complexity/noWith
  suspicious/noDuplicateClassMembers
  complexity/noExtraBooleanCast
  performance/noAccumulatingSpread
  a11y/useValidAriaProps
  a11y/noRedundantAlt
  correctness/noChildrenProp
  correctness/noUnknownFunction
  correctness/noInvalidPositionAtImportRule
  suspicious/noConfusingLabels
  suspicious/noPrototypeBuiltins
  suspicious/noConfusingVoidType
  suspicious/noFocusedTests
  a11y/useButtonType
  a11y/useSemanticElements
  a11y/noAriaUnsupportedElements
  correctness/noInvalidGridAreas
  correctness/noFlatMapIdentity
  suspicious/noSuspiciousSemicolonInJsx
  a11y/noBlankTarget
  a11y/useHeadingContent
  correctness/useValidForDirection
  correctness/noVoidTypeReturn
  correctness/noInvalidUseBeforeDeclaration
  a11y/noAriaHiddenOnFocusable
  a11y/useGenericFontNames
  correctness/noUnknownMediaFeatureName
  a11y/useAnchorContent
  complexity/noUselessRename
  style/useNumberNamespace
  complexity/noUselessConstructor
  a11y/noAccessKey
  style/useExponentiationOperator
  style/noUnusedTemplateLiteral
  complexity/noUselessSwitchCase
  correctness/noUndeclaredVariables
  style/useSingleVarDeclarator
  suspicious/noExportsInTest
  a11y/noNoninteractiveElementToInteractiveRole
  style/noCommaOperator
  suspicious/noConsoleLog
  suspicious/noDuplicateAtImportRules
  suspicious/useIsArray
  a11y/noHeaderScope
  complexity/noUselessFragments
  suspicious/noMisrefactoredShorthandAssign
  suspicious/noEmptyBlock
  complexity/noForEach
  correctness/noUnusedImports
  suspicious/noFunctionAssign

Rule name

noUndeclaredVariables

Playground link

https://biomejs.dev/playground/?lintRules=all&code=ZgB1AG4AYwB0AGkAbwBuACAATQB5AEEAcABwACgAKQAgAHsACgAgACAAZgB1AG4AYwB0AGkAbwBuACAAaABhAG4AZABsAGUAcgAoAGUAOgAgAFIAZQBhAGMAdAAuAEYAbwByAG0ARQB2AGUAbgB0ACkAIAB7AAoAIAAgACAAIAByAGUAdAB1AHIAbgAgAGUACgAgACAAfQAKAAoAIAAgAHIAZQB0AHUAcgBuACAAKAAKACAAIAAgACAAPABmAG8AcgBtACAAbwBuAFMAdQBiAG0AaQB0AD0AewAoAGUAKQAgAD0APgAgAGgAYQBuAGQAbABlAHIAKABlACkAfQA%2BAAoAIAAgACAAIAA8AC8AZgBvAHIAbQA%2BAAoAIAAgACkACgB9AAoACgBlAHgAcABvAHIAdAAgAHsAIABNAHkAQQBwAHAAIAB9AA%3D%3D

Expected result

Hello, we're migrating a few React 18/TS/EsLint projects into Biome. With React 18 you could use global React Types as React.JSX.Element, React.FormEvent, etc without an import statement.

But when we enable noUndeclaredVariables in Biome to check for possible bugs, we're getting a false positive on The React variable is undeclared.

A workaround is to explicitly add an import type React from 'react' on every file with React types. But we have hundreds of files that would need this. And it was working before migrating over.

Is there a better solution for this issue?

Thanks, and we love Biome!

Code of Conduct

Conaclos commented 2 days ago

Hi! You can explicitly set a global using the javascript.globals configuration.

elawad commented 2 days ago

We did try globals, though that introduces other issues.

// biome.json
"javascript": {
  "globals": ["React"]
}

For instance, in a non-TS .jsx file you can add this without an import.

// App.jsx
function App() {
  const [count, setCount] = React.useState(0);
}

Biome will allow it due to globals, but the app will error during runtime.

One other option was just to disable noUndeclaredVariables in Biome overrides for *.tsx files.

Conaclos commented 2 days ago

Have you suggestions to solve the issue? We could add metadata to globals (if it is a type, a writable flobal, ...).

Also, we have a jsxRuntime configuration. i am unsure if we could leverage it in some way.

arendjr commented 2 days ago

I think the jsxRuntime should apply here indeed, but not sure if it does πŸ˜…

I’m also used to just disabling this rule, since it’s kinda redundant if you also use tsc.

elawad commented 2 days ago

I don't have any suggestions unfortunately. But disabling it seems to be ok, since TypeScript will handle undeclared issues on its own in .tsx files.

We do run biome check and tsc --noEmit in git hooks already. So that will find both types of issues.

Conaclos commented 2 days ago

This one of the reason noUndeclaredVariabels is still not recommended. It reports too much false positive. We hope to improve it once we have proper type info.

elawad commented 2 days ago

Thanks @Conaclos, it does help in .js/.jsx files. As we're used to it coming from ESLint. But using overrides was the perfect solution here.

Conaclos commented 2 days ago

It seems that there are more false positives with types than variables. We could add an option to ignore undeclared types. We could allow the option by default and allow the rule by default in Biome 2.0.

Whar do you think?

elawad commented 2 days ago

That's a good idea. Since types are already checked by TypeScript tsc.