Skip to content
Source
Sign inSign up
TanStack/showcase
A read-only demo of every feature
Every page of this repository is open, and nothing can change. Take a guided tour of what TanStack Source does.

addTask accepts a priority that is not a number #9

Send to my agent
OpenAlemTuzlak opened this issue 1 comment
AlemTuzlakcommented

Steps to reproduce:

  1. tanstack-source added labelsbug

  2. tanstack-sourcecommented

    Reproduced on main (8c5e0f6). addTask accepts any priority that is not a number, not just negative numbers.

    What happens

    src/tasks.ts validates the priority with a single comparison:

    export function addTask(list: TaskList, title: string, priority = 3): TaskList {
      const name = title.trim()
      if (name === '') throw new Error('A task needs a title.')
      if (priority < 0) throw new Error('A priority is 0 or more.')
      ...

    The only guard is if (priority < 0). Every non-number falls through it, because nothing that is not a number compares as < 0: Number.isNaN(NaN) is false, 'high' < 0 is false ('high' becomes NaN), null < 0 is false (null becomes 0), and an object or array becomes NaN. The call then succeeds and stores the value in a field typed as priority: number.

    Concretely, with addTask([], 'Docs', 'high'):

    • No error is thrown; the task is created with priority: 'high'.
    • Task.priority (number) and the JSDoc "Lower numbers come first." are violated.
    • Downstream code breaks: openTasks sorts with a.priority - b.priority, so 'high' - 1 is NaN and the "most important first" order is not kept — a docs task with priority 'high' even sorts before a task with priority 1.

    Tested values that are all accepted: 'high', '5', NaN, null, true, { level: 1 }.

    Suspected files

    • src/tasks.ts — addTask: the guard if (priority < 0) does not reject non-numbers; this is the place to fix (for example if (typeof priority !== 'number' || !Number.isInteger(priority) || priority < 0) throw ...).
    • test/tasks.test.ts — has a test for a negative priority (refuses a negative priority) but none for a non-number priority.
    • src/tasks.ts — openTasks shows the user-visible damage from a stored non-number priority (a.priority - b.priority is NaN).

    Repro

    Path: repro/issue.test.mjs Command: node --test repro/issue.test.mjs

    import assert from 'node:assert/strict'
    import { test } from 'node:test'
    import { addTask, openTasks } from '../src/index.ts'
    
    // Issue #9: addTask accepts a priority that is not a number.
    // The guard in src/tasks.ts is only `if (priority < 0)`, so any non-number
    // (which never compares below 0) is stored on the task.
    
    const notNumbers = ['high', '5', NaN, null, true, { level: 1 }]
    
    test('addTask refuses a priority that is not a number', () => {
      const accepted = notNumbers.filter((priority) => {
        try {
          addTask([], 'Docs', priority)
          return true
        } catch {
          return false
        }
      })
      assert.deepEqual(
        accepted,
        [],
        `addTask accepted these non-number priorities: ${accepted.map((p) => JSON.stringify(p)).join(', ')}`,
      )
    })
    
    test('a string priority is stored on the task', () => {
      const list = addTask([], 'Docs', 'high')
      assert.equal(typeof list[0].priority, 'number', `stored priority: ${JSON.stringify(list[0].priority)}`)
    })
    
    test('a non-number priority breaks openTasks ordering', () => {
      const list = addTask(addTask([], 'Docs', 'high'), 'Ship it', 1)
      // openTasks sorts with a.priority - b.priority; 'high' - 1 is NaN,
      // so the "most important first" order is not guaranteed.
      const titles = openTasks(list).map((task) => task.title)
      assert.deepEqual(titles, ['Ship it', 'Docs'])
    })

    Output (end):

    ✖ addTask refuses a priority that is not a number
      AssertionError: addTask accepted these non-number priorities: "high", "5", null, null, true, {"level":1}
    ✖ a string priority is stored on the task
      AssertionError: stored priority: "high" — 'string' !== 'number'
    ✖ a non-number priority breaks openTasks ordering
      AssertionError: expected [ 'Ship it', 'Docs' ], actual [ 'Docs', 'Ship it' ]
    ℹ tests 3
    ℹ pass 0
    ℹ fail 3

    TanStack Source bot, investigate agent

  3. tanstack-source published a comment(spam 4%, injection 16%)

  4. AlemTuzlak added labelsfactory:fix

  5. AlemTuzlak pinned this issue

Sign in to join this conversation.