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
125 changes: 124 additions & 1 deletion tools/oxlint-plugins/serverFacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,42 @@ function deriveSection(facts: SectionFacts) {
function deriveSection(facts: SectionFacts) {
const a = facts.canTopUp && !facts.isLoading
const b = facts.autoTopUpEnabled && facts.hasSeats
const c = facts.canTopUp && options.canTopUp
}
`
},
{
name: 'known scope limit: a fact capability gated by a capability-named member of an unnamed object',
code: `
function deriveSection(facts: SectionFacts) {
return facts.canTopUp && options.canTopUp
}
`
},
{
name: 'a served refusal rendered next to its capability',
code: `
function describe(facts: AddCreditsFacts) {
const a = !facts.canTopUp && facts.canTopUpRefusal
const b = facts.canTopUpRefusal ?? facts.canTopUpReason
const c = facts.canChangeSeats || facts.canChangeSeatsDenial
const d = !facts.canTopUp && facts.canTopUpRefusal && !isLoading
const e = !this.facts.canTopUp && this.facts.canTopUpRefusal
}
`
},
{
name: 'detail fields of a tracked capability source rendered together',
code: `
const capabilities = useBillingCapabilities()
const detail = capabilities.canTopUpRefusal.value ?? capabilities.canTopUpReason.value
const refusal = !capabilities.canTopUp.value && capabilities.canTopUpRefusal.value
`
},
{
name: 'capability-named members of an object nested inside a facts object',
code: `
function deriveSection(facts: SectionFacts) {
return facts.dialog.canClose && facts.dialog.canOpen
}
`
},
Expand All @@ -209,6 +244,94 @@ function deriveSection(facts: SectionFacts) {
{ messageId: 'combined', data: { operator: '||' } }
]
},
{
name: 'two capability members of a facts object held under another object',
code: `
function deriveSection(props: Props, section: Section) {
if (props.facts.canManageAutoTopUp && props.facts.canTopUp) return 'card'
if (section.billingFacts.canTopUp || section.billingFacts.canChangeSeats) return 'seats'
return this.facts.canTopUp && this.facts.canChangeSeats
}
`,
errors: [
{ messageId: 'combined', data: { operator: '&&' } },
{ messageId: 'combined', data: { operator: '||' } },
{ messageId: 'combined', data: { operator: '&&' } }
]
},
{
name: 'capability members of two different fact objects combined',
code: `
function deriveSection(billingFacts: BillingFacts, seatFacts: SeatFacts) {
if (billingFacts.canTopUp && seatFacts.canChangeSeats) return 'card'
return facts.canTopUp || capabilities.canManageBilling
}
`,
errors: [
{ messageId: 'combined', data: { operator: '&&' } },
{ messageId: 'combined', data: { operator: '||' } }
]
},
{
name: 'capability refs of an untracked capabilities object combined',
code: `
function deriveSection(capabilities: BillingCapabilities) {
return capabilities.canTopUp.value && !capabilities.canManageAutoTopUp.value
}
`,
errors: [{ messageId: 'combined', data: { operator: '&&' } }]
},
{
name: 'a capability combined with other state beside its served refusal',
code: `
const capabilities = useBillingCapabilities()
const a = !capabilities.canTopUp.value && capabilities.canTopUpRefusal.value && isCloud
const b = capabilities.canTopUp.value && options.canTopUpReason
`,
errors: [
{ messageId: 'combined', data: { operator: '&&' } },
{ messageId: 'combined', data: { operator: '&&' } }
]
},
{
name: 'a capability gated by a served detail of another capability or holder',
code: `
const capabilities = useBillingCapabilities()
const a = capabilities.canTopUp.value && !billingFacts.canManageBillingReason
const b = !capabilities.canTopUp.value && billingFacts.canTopUpRefusal
const c = capabilities.canTopUp.value && capabilities.canManageBillingRefusal.value
`,
errors: [
{ messageId: 'combined', data: { operator: '&&' } },
{ messageId: 'combined', data: { operator: '&&' } },
{ messageId: 'combined', data: { operator: '&&' } }
]
},
{
name: 'a capability gated by its own served refusal as a boolean',
code: `
const capabilities = useBillingCapabilities()
const a = capabilities.canTopUp.value || !capabilities.canTopUpRefusal.value
`,
errors: [{ messageId: 'combined', data: { operator: '||' } }]
},
{
name: 'a fact-object capability gated by a served detail of another capability or holder',
code: `
function f(billingFacts: BillingFacts, seatFacts: SeatFacts) {
return !billingFacts.canTopUp && seatFacts.canTopUpRefusal
}
function g(facts: SectionFacts) {
const a = facts.canTopUp && !facts.canManageBillingReason
const b = facts.canTopUp && !facts.canTopUpRefusal
}
`,
errors: [
{ messageId: 'combined', data: { operator: '&&' } },
{ messageId: 'combined', data: { operator: '&&' } },
{ messageId: 'combined', data: { operator: '&&' } }
]
},
{
name: '#15675 capability chosen by isCloud with a role fallback',
code: `
Expand Down
114 changes: 87 additions & 27 deletions tools/oxlint-plugins/serverFacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ type VisitedNode<K extends keyof Visitor> = Parameters<
type Expression = VisitedNode<'LogicalExpression'>['left']
type AstNode = Expression['parent']
type Ternary = VisitedNode<'ConditionalExpression'>
type Member = Extract<AstNode, { type: 'MemberExpression' }>

const CAPABILITY_SOURCES: ReadonlySet<string> = new Set([
'useBillingCapabilities'
])
const CAPABILITY_BINDING = /^can[A-Z]\w*$/
const CAPABILITY_FIELD = /^can_[a-z_]+$/
const CAPABILITY_DETAIL = /(?:Refusal|Denial|Reason)$/
const SERVER_FACT_OWNER = /^(?:\w*Facts|facts|\w*Capabilities|capabilities)$/
const PENDING_FACT_WRAPPER = 'pendingServerFact'
const LOCAL_UI_STATE: ReadonlySet<string> = new Set([
Expand Down Expand Up @@ -433,6 +435,7 @@ function oneSideIsLocal(
function memberPath(node: AstNode): string | undefined {
const expression = unwrap(node)
if (expression.type === 'Identifier') return expression.name
if (expression.type === 'ThisExpression') return 'this'
if (expression.type !== 'MemberExpression' || expression.computed) {
return undefined
}
Expand All @@ -442,29 +445,50 @@ function memberPath(node: AstNode): string | undefined {
: `${object}.${expression.property.name}`
}

function capabilityMemberOwner(node: AstNode): string | undefined {
const inner = negated(node)
if (inner) return capabilityMemberOwner(inner)
const expression = unwrap(node)
if (expression.type !== 'MemberExpression' || expression.computed) {
return undefined
}
if (expression.property.name === 'value') {
return capabilityMemberOwner(expression.object)
}
const owner = memberPath(expression.object)
return CAPABILITY_BINDING.test(expression.property.name) &&
owner !== undefined &&
SERVER_FACT_OWNER.test(owner.split('.')[0])
? owner
: undefined
function isCapabilityMemberName(property: string): boolean {
return CAPABILITY_BINDING.test(property) && !CAPABILITY_DETAIL.test(property)
}

function isFactObjectCapability(node: AstNode): boolean {
if (node.type !== 'MemberExpression' || node.computed) return false
return (
isCapabilityMemberName(node.property.name) &&
SERVER_FACT_OWNER.test(factName(node.object) ?? '')
)
}

function isCapabilityDetail(node: AstNode): boolean {
return (
node.type === 'MemberExpression' &&
!node.computed &&
CAPABILITY_BINDING.test(node.property.name) &&
CAPABILITY_DETAIL.test(node.property.name)
)
}

function combinesSiblingCapabilities(operands: AstNode[]): boolean {
const owners = operands.flatMap(
(operand) => capabilityMemberOwner(operand) ?? []
function memberKey(member: Member): string | undefined {
if (member.computed) return undefined
const holder = memberPath(member.object)
return holder && `${holder}.${member.property.name}`
}

function isFactObjectField(node: AstNode): boolean {
return (
node.type === 'MemberExpression' &&
!node.computed &&
CAPABILITY_BINDING.test(node.property.name) &&
SERVER_FACT_OWNER.test(factName(node.object) ?? '')
)
}

function combinesFactObjectCapabilities(operands: AstNode[]): boolean {
const fields = operands.flatMap(
(operand) =>
capabilityMember(negated(operand) ?? operand, isFactObjectField) ?? []
)
return (
fields.length >= 2 && fields.some((field) => !isCapabilityDetail(field))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
)
return new Set(owners).size < owners.length
}

interface LiteralDefault {
Expand Down Expand Up @@ -497,18 +521,26 @@ function isNarrowedTernary(node: Ternary, isCapability: IsCapability): boolean {
: impliesCapability(fallback.other, isCapability)
}

function capabilityOwner(
function capabilityMember(
node: AstNode,
isCapability: IsCapability
): string | undefined {
): Member | undefined {
const expression = unwrap(node)
if (expression.type !== 'MemberExpression' || expression.computed) {
return undefined
}
if (expression.property.name === 'value') {
return capabilityOwner(expression.object, isCapability)
return capabilityMember(expression.object, isCapability)
}
return isCapability(expression) ? memberPath(expression.object) : undefined
return isCapability(expression) ? expression : undefined
}

function capabilityOwner(
node: AstNode,
isCapability: IsCapability
): string | undefined {
const member = capabilityMember(node, isCapability)
return member && memberPath(member.object)
}

// `caps ? caps.can_top_up : false` is `caps?.can_top_up ?? false`.
Expand Down Expand Up @@ -547,11 +579,35 @@ export const noCapabilityRecombination: Rule = {
const property = node.property.name
return (
CAPABILITY_FIELD.test(property) ||
(CAPABILITY_BINDING.test(property) &&
(isCapabilityMemberName(property) &&
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
capabilityObjects.has(factName(node.object) ?? ''))
)
}

const isPairableCapability = (node: AstNode): boolean =>
isCapabilityReference(node) || isFactObjectCapability(node)

const capabilityKeys = (operands: AstNode[]): Set<string> =>
new Set(
operands.flatMap((operand) => {
const member = capabilityMember(
negated(operand) ?? operand,
isPairableCapability
)
const key = member && memberKey(member)
return key ?? []
})
)

const isDetailOfPairedCapability = (
operand: AstNode,
paired: Set<string>
): boolean => {
const detail = capabilityMember(operand, isCapabilityDetail)
const key = detail && memberKey(detail)?.replace(CAPABILITY_DETAIL, '')
return key !== undefined && paired.has(key)
}

const isCapabilitySource = (init: AstNode | null): boolean =>
isCapabilitySourceCall(init) ||
(init !== null && capabilityObjects.has(factName(init) ?? ''))
Expand Down Expand Up @@ -636,12 +692,16 @@ export const noCapabilityRecombination: Rule = {
LogicalExpression(node) {
if (unwrap(node.parent).type === 'LogicalExpression') return
const operands = logicalOperands(node)
const factOperands = operands.filter((operand) => !isLiteral(operand))
const paired = capabilityKeys(operands)
const factOperands = operands.filter(
(operand) =>
!isLiteral(operand) && !isDetailOfPairedCapability(operand, paired)
)
if (
factOperands.length < 2 ||
!(
operands.some(containsCapability) ||
combinesSiblingCapabilities(operands)
combinesFactObjectCapabilities(factOperands)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
) ||
onlyNarrowedByLocalState(node) ||
isInsidePendingFact(node)
Expand Down
Loading