Skip to content
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

💅 Transform a function with generics to arrow function by biome linter will not safe. #3292

Closed
1 task done
MTRo opened this issue Jun 26, 2024 · 2 comments · Fixed by #3300 · 4 remaining pull requests
Closed
1 task done

💅 Transform a function with generics to arrow function by biome linter will not safe. #3292

MTRo opened this issue Jun 26, 2024 · 2 comments · Fixed by #3300 · 4 remaining pull requests
Assignees
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug

Comments

@MTRo
Copy link

MTRo commented Jun 26, 2024

Environment information

CLI:
  Version:                      1.8.2
  Color support:                true

Platform:
  CPU Architecture:             x86_64
  OS:                           linux

Environment:
  BIOME_LOG_DIR:                unset
  NO_COLOR:                     unset
  TERM:                         "xterm-256color"
  JS_RUNTIME_VERSION:           "v20.15.0"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         "npm/10.7.0"

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

Linter:
  JavaScript enabled:           true
  JSON enabled:                 true
  CSS enabled:                  false
  Recommended:                  true
  All:                          false
  Enabled rules:
  performance/noDelete
  suspicious/noCatchAssign
  suspicious/noUnsafeNegation
  complexity/useLiteralKeys
  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
  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
  suspicious/noRedeclare
  correctness/noStringCaseMismatch
  correctness/noSetterReturn
  correctness/noInvalidConstructorSuper
  suspicious/noImplicitAnyLet
  suspicious/noFallthroughSwitchClause
  suspicious/noUnsafeDeclarationMerging
  correctness/noUnreachable
  a11y/useKeyWithClickEvents
  suspicious/noDuplicateObjectKeys
  complexity/noUselessThisAlias
  complexity/noThisInStatic
  complexity/useOptionalChain
  correctness/noInnerDeclarations
  style/noParameterAssign
  suspicious/noDuplicateCase
  a11y/useValidAnchor
  complexity/useRegexLiterals
  correctness/noSelfAssign
  style/noUselessElse
  style/useShorthandFunctionType
  suspicious/noShadowRestrictedNames
  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
  a11y/useAltText
  correctness/noEmptyCharacterClassInRegex
  suspicious/noSuspiciousSemicolonInJsx
  suspicious/noSparseArray
  a11y/useIframeTitle
  complexity/noBannedTypes
  a11y/noSvgWithoutTitle
  correctness/noVoidElementsWithChildren
  style/useAsConstAssertion
  correctness/useJsxKeyInIterable
  style/useExportType
  complexity/noUselessLoneBlockStatements
  suspicious/noPrototypeBuiltins
  suspicious/noMisleadingInstantiator
  suspicious/noDebugger
  style/noArguments
  a11y/useValidAriaValues
  suspicious/noCommentText
  suspicious/noThenProperty
  suspicious/noDuplicateJsxProps
  suspicious/noGlobalAssign
  a11y/noPositiveTabindex
  correctness/noEmptyPattern
  complexity/noExcessiveNestedTestSuites
  security/noDangerouslySetInnerHtmlWithChildren
  a11y/useKeyWithMouseEvents
  suspicious/noExtraNonNullAssertion
  suspicious/useGetterReturn
  correctness/noRenderReturnValue
  correctness/useExhaustiveDependencies
  security/noGlobalEval
  style/noNonNullAssertion
  a11y/noRedundantRoles
  complexity/useFlatMap
  correctness/useIsNan
  correctness/noUnusedVariables
  style/useConst
  suspicious/noGlobalIsFinite
  suspicious/noSelfCompare
  suspicious/noAsyncPromiseExecutor
  security/noDangerouslySetInnerHtml
  style/useNodejsImportProtocol
  a11y/noDistractingElements
  suspicious/noArrayIndexKey
  complexity/noWith
  suspicious/noDuplicateClassMembers
  complexity/noExtraBooleanCast
  performance/noAccumulatingSpread
  a11y/useValidAriaProps
  a11y/noRedundantAlt
  correctness/noChildrenProp
  suspicious/noConfusingLabels
  suspicious/noConfusingVoidType
  suspicious/noFocusedTests
  a11y/useButtonType
  a11y/noAriaUnsupportedElements
  correctness/noFlatMapIdentity
  a11y/noBlankTarget
  a11y/useHeadingContent
  correctness/useValidForDirection
  correctness/noVoidTypeReturn
  correctness/noInvalidUseBeforeDeclaration
  a11y/noAriaHiddenOnFocusable
  a11y/useAnchorContent
  complexity/noUselessRename
  correctness/noInvalidNewBuiltin
  style/useNumberNamespace
  complexity/noUselessConstructor
  a11y/noAccessKey
  style/useExponentiationOperator
  style/noUnusedTemplateLiteral
  complexity/noUselessSwitchCase
  style/useSingleVarDeclarator
  suspicious/noExportsInTest
  a11y/noNoninteractiveElementToInteractiveRole
  style/noCommaOperator
  suspicious/useIsArray
  a11y/noHeaderScope
  complexity/noUselessFragments
  suspicious/noMisrefactoredShorthandAssign
  complexity/noForEach
  correctness/noUnusedImports
  suspicious/noClassAssign
  suspicious/noFunctionAssign

Workspace:
  Open Documents:               0

Rule name

lint/complexity/useArrowFunction

Playground link

https://biomejs.dev/playground/?code=CgAKAGMAbwBuAHMAdAAgAHcAaQB0AGgARABlAGYAYQB1AGwAdAAgAD0AIABmAHUAbgBjAHQAaQBvAG4AIAA8AFQAPgAoAHYAYQBsAHUAZQA6ACAAVAAsACAAZABlAGYAYQB1AGwAdABWAGEAbAB1AGUAOgAgAFQAKQAgAHsACgAgACAAcgBlAHQAdQByAG4AIAB2AGEAbAB1AGUAIAA%2FAD8AIABkAGUAZgBhAHUAbAB0AFYAYQBsAHUAZQA7AAoAfQA7AAoACgAvAC8AIABjAG8AbgBzAHQAIAB3AGkAdABoAEQAZQBmAGEAdQBsAHQAIAA9ACAAPABUAD4AKAB2AGEAbAB1AGUAOgAgAFQALAAgAGQAZQBmAGEAdQBsAHQAVgBhAGwAdQBlADoAIABUACkAIAA9AD4AIAB2AGEAbAB1AGUAIAA%2FAD8AIABkAGUAZgBhAHUAbAB0AFYAYQBsAHUAZQA7AAoA

Expected result

Hi there, I discovered some weird behavior with Biome linter, so I want to share it.

When I apply to biome check --write to this code below (please don't mind that the functions actual doing.)

const withDefault = function <T>(value: T, defaultValue: T) {
    return value ?? defaultValue;
};

Biome linter suggest (also will be replaced) to this

const withDefault = <T>(value: T, defaultValue: T) => value ?? defaultValue;

However, unfortunately it seems probably wrong. And I actually expect to replace like this ( <T,> )

const withDefault = <T,>(value: T, defaultValue: T) => value ?? defaultValue;

Here is Typescript playground, and you can see how typescript recognize those code.

https://www.typescriptlang.org/play/?#code/FAehAIGMHsDsGcAu4DuBLRALAIgUwGYCGArgDbIC84+xskiac4APACoB8AFAG6GnG4AXOFYAacABMCJcgDU+A4awCU4AN6gI4beABOuRMV2xwvfrnAB+S5OllE88wG5N4AL4vgMBMnRY8RPYAqrC4AB4ADrj0uBLgVGyiXGaKIuJSgXIKQiKqFOym2VY2GTIO2Z7eSKgYOHbkAKKR0Yix8SwcPNlK6fXl5kp5BSkW1raZ-QIuQA

And also this post said what should suggest to.

https://stackoverflow.com/questions/32308370/what-is-the-syntax-for-typescript-arrow-functions-with-generics

Thanks!

Code of Conduct

  • I agree to follow Biome's Code of Conduct
@Sec-ant
Copy link
Member

Sec-ant commented Jun 26, 2024

const withDefault = <T>(value: T, defaultValue: T) => value ?? defaultValue; is correct syntax if JSX is disabled: https://www.typescriptlang.org/play/?jsx=0#code/FAehAIGMHsDsGcAu4DuBLRALAIgUwGYCGArgDbIC84+xskiac4APACoB8AFAG6GnG4AXOFYAacABMCJcgDU+A4awCU4AN6gI4beABOuRMV2xwvfrnAB+S5OllE88wG5N4AL4vgMBMnRY8RPYAqrC4AB4ADrj0uBLgVGyiXGaKIuJSgXIKQiKqFOym2VY2GTIO2Z7eSKgYOHbkAKKR0Yix8SwcPNlK6fXl5kp5BSkW1raZ-QIuQA

I see two options:

  1. Check the file extension (.ts or .tsx) to decide whether to use <T> or <T,>.
  2. Always use <T,> because our formatter can format it into <T> if it is a TypeScript file with JSX disabled: https://biomejs.dev/playground/?code=YwBvAG4AcwB0ACAAdwBpAHQAaABEAGUAZgBhAHUAbAB0ACAAPQAgADwAVAAsAD4AKAB2AGEAbAB1AGUAOgAgAFQALAAgAGQAZQBmAGEAdQBsAHQAVgBhAGwAdQBlADoAIABUACkAIAA9AD4AIAB2AGEAbAB1AGUAIAA%2FAD8AIABkAGUAZgBhAHUAbAB0AFYAYQBsAHUAZQA7AA%3D%3D&jsx=false

I think we should go with option 1 because JSX can only appear in an .tsx file.

@Sec-ant Sec-ant added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages S-Bug-confirmed Status: report has been confirmed as a valid bug S-Needs discussion Status: needs a discussion to understand criteria and removed S-Needs discussion Status: needs a discussion to understand criteria labels Jun 26, 2024
@MTRo
Copy link
Author

MTRo commented Jun 27, 2024

Sorry my late response. It sounds good idea!
Because change always to <T,> will bit confuse someone (like me)
I still have no idea why I need weird comma :p

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