Skip to content

Rule: prefer-clean-model-structure #114

@sergeysova

Description

@sergeysova

This rule should improve the readability of the code.

The rule should check the order of definitions of units:

  1. First of all define domains
  2. Define events
  3. After events define derived events
  4. Next define stores
  5. After simple stores define derived stores
  6. Next define effects
  7. After effects define derived effects (attach)
  8. After all definitions write logic on sample's
  9. Do not mix custom mapper/functions with samples and definitions, move them down

This rule should ban using .on and .reset etc. methods on stores immediately after definition. The same for domains, effects, and events.

This rule should not be auto-fixable, because it's affects business-logic.

FAIL

// prefer-clean-model-structure: 'error'

const someHappened = createEvent()
const $data = createStore(0)
  .on(someHappened, (c) => c + 1)

const runMeFx = createEffect(() => {})

function calculate(a, b) {
  return a + b;
}

sample({ clock: someHappened, target: runMeFx })

export const anotherFx = attach({
  source: $data,
  async effect(data, arg) {
    return calculate(data, arg)
  },
})

sample({ clock: anotherFx.doneData, target: someHappened })

OKAY

// prefer-clean-model-structure: 'error'

const someHappened = createEvent()

const $data = createStore(0)

const runMeFx = createEffect(() => {})

export const anotherFx = attach({
  source: $data,
  async effect(data, arg) {
    return calculate(data, arg)
  },
})

$data.on(someHappened, (c) => c + 1)

sample({ clock: someHappened, target: runMeFx })

sample({ clock: anotherFx.doneData, target: someHappened })

function calculate(a, b) {
  return a + b;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions