Hoc scope

From openreachtech/hora-skills-ort-core · View interactive version

Hoc scope is an AI agent skill from the GitHub repository openreachtech/hora-skills-ort-core. Conventions for how class members refer to one another across the static and instance sides. Use when a member refers to another member, to its own class, or to a property of `this`. How an instance reaches its own class through `#get:Ctor` belongs to the Ctor convention; what a class may hold, to t

How to use this skill

Skill file

Classes: Shared / Scope

This summarizes conventions related to scope references among class members.

References between static members should use `this`

(Using this ensures that even when called from an inheriting subclass, the subclass's own definition is referenced.)

method, this handed over where a class is expected. Hardcoding the class name anchors the call

to the base class even when a subclass made it, which breaks polymorphism.

// NG
static create ({
  characters = RandomTextGenerator.DEFAULT_CHARACTERS,
} = {}) {
  return new this({ characters })
}

// OK
static create ({
  characters = this.DEFAULT_CHARACTERS,
} = {}) {
  return new this({ characters })
}

When referencing a static member from an instance member, go through `#get:Ctor`

defined, typed and overridden, and why this.constructor is never written outside it, belong

to /hoc-classes-ctor.

inheriting subclass on the subclass's own static definition.

Do not destructure `this`

this never stands alone on the right of a destructuring assignment. That an instance is

never taken apart belongs to /hoc-statements; it is read here for this, where a property is

read where it is used, as this.alpha, rather than taken out into a local.

// NG: the property taken into a local first
const {
  maxDocumentDepth,
} = this

return depths.filter(it =>
  it > maxDocumentDepth
)

// OK: read where it is used
return depths.filter(it =>
  it > this.maxDocumentDepth
)

the property and nothing keeps them together — and the reader has to carry the alias to

understand the lines below it.

held because some logic needs it in hand; give the class the member that performs that

logic, and the property is read inside it as this.alpha again.

Binding `this` itself is permitted

**What the rule turns away is a property taken out; binding the object to a name is a

different act and is allowed.** It creates no second name for a value — the one name still

refers to the one object — and it is what carries the scope into a body that would otherwise

lose it.

// OK: the receiver carried into a class expression, whose methods re-bind `this`
static onto (TargetCtor) {
  const registry = BoundCtorRegistry.create({
    BaseCtor: TargetCtor,
  })

  const OwnCtor = this

  return registry.ensureBoundCtor({
    bindings: [
      OwnCtor,
    ],
    deriver: ({ Ctor }) => class extends Ctor {
      /** @override */
      static get boundSource () {
        return OwnCtor
      }
    },
  })
}

called on, and an arrow function's lexical this does not reach into it. The binding above

is the only way the enclosing receiver arrives there.

member was called on. self says only that something was aliased.

Reading the property is also the faster one

Measured on Node v22.23.2, medians over 11–12 rounds:

| Shape | this.value | const { value } = this | Difference |

| :-- | --: | --: | --: |

| Three reads inside a hot loop | 2.78 ms | 2.87 ms | +3% |

| Two reads per call, 3,000,000 calls | 66.17 ms | 68.28 ms | +3% |

| filter() over 8 elements, 300,000 calls | 21.74 ms | 25.87 ms | +19% |

callback**. A monomorphic property load folds into an inline cache, while the

destructured binding is created on every call and, once a closure captures it, lives in

the context object.

The type checker is what usually induces it

A property typed number | null is narrowed by a guard, **and the narrowing does not reach

inside a callback** — the comparison there is reported as possibly null (TS18047). Taking

the property into a local is what makes that message go away, which is why the local gets

written.

// The narrowing does not cross into the callback
someMethod ({ depths }) {
  if (!this.maxDepth) {
    return []
  }

  return depths.filter(it =>
    it > this.maxDepth // TS18047: 'maxDepth' is possibly 'null'
  )
}

// The member carries the guard and the comparison in one body
exceedsMaxDepth ({ depth }) {
  if (!this.maxDepth) {
    return false
  }

  return depth > this.maxDepth
}

the same function body, so the narrowing holds and nothing is copied.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.