# ParseContext

CLASS in `@skmtc/core 0.63.4`

The context of the parse phase, the first of the three phases. It turns an
OpenAPI 3.0, 3.1 or 3.2 document into the objects that generators read.

The engine makes it for each run. A generator needs one only in its tests,
for `parse` or `toSchemaV3`.

Problems that the parser finds in the document are in `issues`.
`parse` throws when the input is not an OpenAPI document of one of
these versions.

## Signature

```ts
import { ParseContext } from '@skmtc/core'

class ParseContext
```

## Constructor

### constructor

```ts
constructor(args: ConstructorArgs)
```

## Properties

- `issues` (ParseIssue[])
- `logger` (Logger)
- `silent` (boolean)
- `protocol` (ProtocolState)
- `currentStackTrail` (StackTrail | undefined): The StackTrail of the currently-traversed position. Set by factories via `ParseContext.withStackTrail` just before constructing the node so the `OasBase` base can snapshot it. `undefined` outside an active `withStackTrail` scope.
- `system` (SystemSettings): How the OpenAPI parsers treat what they cannot read faithfully (`skmtc.config.json#settings.parse`). Each setting left out is `strict`.
- `oasDocument` (OasDocument): The document the parse fills.
- `documentObject` (OpenAPIV3.Document): The input document, as the parse reads it: under `parse.intersections: 'cycles'`, each entry it gives marks its kept refs.
- `keptRefs` (KeptRefs | undefined): The refs of the document that a merge keeps as refs (`parse.intersections`); `undefined` under `merge`.
- `parsedDocument` (SkmtcParsedDocument): Convenience: returns the `SkmtcParsedDocument` wrapper. Useful for parser code that needs to hand a document to `OasRef` without manually constructing the wrapper.

## Methods

### parse

```ts
parse(stackTrail: StackTrail): SkmtcParsedDocument
```

Runs the parse step and returns the result wrapped in
`SkmtcParsedDocument`. The walk runs under `stackTrail`, so
issue locations start with its frames; pruning removes those frames
before it reads a trail as a document address.

### removeErroredItems

```ts
removeErroredItems(): void
```

Removes every item that depends on a ref with a registered error.

OAS: a component with an error is removed, even when the error is in
one of its properties. Each consumer of a removed component is removed
too. A consumer that is itself a component (its trail starts
`components:<bucket>:<name>`) has its own consumers removed next, at any
depth.

Each removed consumer yields one `INVALID_DEPENDENCY_REF` at its
location. The message names the ref it depends on and the error that
started the removal; that error is the `cause`.

A `paths:P:M:…` consumer trail resolves to the whole operation, so an
operation is removed even when the ref is only in a response it does
not generate from.

### withStackTrail

```ts
withStackTrail<T>(stackTrail: StackTrail, fn: () => T): T
```

Run `fn` with `currentStackTrail` set to the given trail; restore
the previous value afterwards (try/finally semantics). Factories
wrap schema-construction in this so `OasBase` can snapshot the
trail off the context without each factory threading it
explicitly into the constructor.

### registerRef

```ts
registerRef(consumer: StackTrail, refKey: string): void
```

Register a `$ref`-style consumer site, keyed by a ref string such as
`#/components/schemas/User`.

### registerOasRef

```ts
registerOasRef<T extends OasRefData['refType']>(
  consumer: StackTrail,
  ref: OasRef<T>
): void
```

Register an OAS `$ref` at its consumer site, as `registerRef`
does, under the key `toRefKey` gives its `$ref`, and keep the site,
so that once the document is parsed `#checkRefs` can resolve the ref
there. The sites that write the same `$ref` as the same kind keep the
first such ref (`#refGroups`).

### toDanglingAllOfMember

```ts
toDanglingAllOfMember(args: ToDanglingAllOfMemberArgs): OpenAPIV3.SchemaObject
```

What a merge reads in place of a `$ref` that names nothing: an `allOf`
member, or in a `oneOf` or `anyOf`, a member's own `allOf` member or a
member merged with the keywords beside the union. When
`parse.danglingRefs` lists `allOfMember`, it is `{}`, which
adds nothing to the merge (`A & unknown` is `A`), and a `DANGLING_REF`
warning at `stackTrail`: the model has no properties from that member.
Otherwise it throws `DanglingRefError`, and what holds the `allOf` is
removed. It is separate from `schema`: here core decides, at parse, and
the generator never sees the member.

### registerRefError

```ts
registerRefError(
  error: unknown,
  refKey: string | undefined,
  location?: string
): void
```

Register an error against a ref key - typically called when the
target of a `$ref` (or type reference) fails to parse. `undefined`
`refKey` is a deliberate no-op: callers may pass the result of
`StackTrail.toStackRef()` directly, which is `undefined` for
non-component stack trails. `location` is where the error was logged;
each `INVALID_DEPENDENCY_REF` it causes names it.

### logIssueAt

```ts
logIssueAt(issue: LogIssueAtArgs, parent?: unknown): void
```

Universal issue recorder. Pushes to `issues` and (when not silent)
mirrors to the logger. The surface methods funnel here.

The log line's message is `<TYPE> at <document location>: <message>`,
and its `issue` field holds the issue's fields.

`parent` is an optional log-time-only field - surface methods
forward the surrounding object so log readers see its shape, not
just the leaf address. Stringified here so the logger payload
stays JSON-clonable. It is intentionally **not** stored on the
persisted `ParseIssue` (which would require clone-safe
serialization across the worker boundary and would bloat the
manifest).

### logIssue

```ts
logIssue(args: LogIssueArgs): void
```

### logIssueNoKey

```ts
logIssueNoKey(args: LogIssueNoKeyArgs): void
```

### log

```ts
log(args: LogAtArgs): void
```

Thin convenience for recording an issue at a pre-computed string
`location` rather than threading a `StackTrail`. Use this
for issues whose natural address isn't a tree position, such as
catch-all error paths where the parsed entity doesn't exist yet (the
parse threw before producing one).

For tree-position issues, prefer `logIssueNoKey` so the
stack trail composes with the surrounding traces.

Internally constructs a `StackTrail` with `StackTrail.fromString(location)`
and delegates to `logIssueNoKey` - the underlying issue
recording logic (protocol tagging, optional ref-error
auto-registration, logger mirror with parent context) is shared.

### logSkippedFields

```ts
logSkippedFields(args: LogSkippedValuesArgs): void
```

Records one warning per unrecognized key under `parent`.

Each skipped key is traced as a child of `stackTrail` so the
resulting issue locations point at the offending property, not
the parent. `parentType` is used in the message text
(`Unexpected property 'foo' in 'SchemaObject'`); `type` defaults
to `UNEXPECTED_PROPERTY`.
