Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .oxlintrc.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"automation/no-manual-tagged-construction": "error",
"automation/no-service-option": "error",
"automation/prefer-effect-match": "error",
"automation/prefer-option-from-nullable": "error",
"automation/prefer-tagged-error-handling": "error",
"automation/no-ambient-nondeterminism": "error",
"anti-slop/no-reflect-apply": "error",
Expand Down Expand Up @@ -81,6 +82,7 @@
"automation/no-manual-tagged-construction": "off",
"automation/no-service-option": "off",
"automation/prefer-effect-match": "off",
"automation/prefer-option-from-nullable": "off",
"automation/prefer-tagged-error-handling": "off",
"automation/no-ambient-nondeterminism": "off",
},
Expand Down
2 changes: 2 additions & 0 deletions tools/oxlint/automation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import noServiceOption from './rules/no-service-option.ts'
import noShadowedStandardArrayStatic from './rules/no-shadowed-standard-array-static.ts'
import noSilentErrorSwallow from './rules/no-silent-error-swallow.ts'
import preferEffectMatch from './rules/prefer-effect-match.ts'
import preferOptionFromNullable from './rules/prefer-option-from-nullable.ts'
import preferTaggedErrorHandling from './rules/prefer-tagged-error-handling.ts'

export default eslintCompatPlugin({
Expand All @@ -21,6 +22,7 @@ export default eslintCompatPlugin({
'no-shadowed-standard-array-static': noShadowedStandardArrayStatic,
'no-silent-error-swallow': noSilentErrorSwallow,
'prefer-effect-match': preferEffectMatch,
'prefer-option-from-nullable': preferOptionFromNullable,
'prefer-tagged-error-handling': preferTaggedErrorHandling,
},
})
68 changes: 68 additions & 0 deletions tools/oxlint/automation/rules/prefer-option-from-nullable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { defineRule } from '@oxlint/plugins'

// Vendored from typeonce-dev/ai-automation (rules/oxlint/src/rules/prefer-option-from-nullable.ts).
// Adapted to Fold's defineRule/messageId convention.

type Node = {
readonly type: string
readonly [key: string]: unknown
}

const isNode = (value: unknown): value is Node =>
typeof value === 'object' && value !== null && 'type' in value && typeof value.type === 'string'

const isNullLiteral = (node: unknown): boolean => isNode(node) && node.type === 'Literal' && node.value === null

const isOptionMemberCall = (name: string, node: unknown): boolean => {
if (!isNode(node) || node.type !== 'CallExpression') return false

const callee = node.callee
const member =
isNode(callee) && callee.type === 'MemberExpression'
? callee
: isNode(callee) && callee.type === 'TSInstantiationExpression' && isNode(callee.expression)
? callee.expression
: undefined

return (
isNode(member) &&
member.type === 'MemberExpression' &&
isNode(member.object) &&
member.object.type === 'Identifier' &&
member.object.name === 'Option' &&
isNode(member.property) &&
member.property.type === 'Identifier' &&
member.property.name === name
)
}

export default defineRule({
meta: {
type: 'problem',
docs: {
description:
'Use Option.fromNullable instead of ternaries that choose between Option.some and Option.none.',
},
messages: {
preferFromNullable:
'Use Option.fromNullable instead of a nullable ternary with Option.some and Option.none.',
},
},
createOnce(context) {
return {
ConditionalExpression(node) {
if (
node.test.type !== 'BinaryExpression' ||
(node.test.operator !== '!==' && node.test.operator !== '!=') ||
(!isNullLiteral(node.test.left) && !isNullLiteral(node.test.right))
) {
return
}

if (isOptionMemberCall('some', node.consequent) && isOptionMemberCall('none', node.alternate)) {
context.report({ node, messageId: 'preferFromNullable' })
}
},
}
},
})
Loading