Skip to content

Commit 1ab037d

Browse files
committed
style: Include explicit type declarations on all public APIs
1 parent 4354c42 commit 1ab037d

21 files changed

Lines changed: 81 additions & 46 deletions

src/compose/composer.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ export class Composer<
133133
*
134134
* Mostly useful at the end of input for an empty stream.
135135
*/
136-
streamInfo() {
136+
streamInfo(): {
137+
comment: string
138+
directives: Directives
139+
errors: YAMLParseError[]
140+
warnings: YAMLWarning[]
141+
} {
137142
return {
138143
comment: parsePrelude(this.prelude).comment,
139144
directives: this.directives,
@@ -148,13 +153,19 @@ export class Composer<
148153
* @param forceDoc - If the stream contains no document, still emit a final document including any comments and directives that would be applied to a subsequent document.
149154
* @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
150155
*/
151-
*compose(tokens: Iterable<Token>, forceDoc = false, endOffset = -1) {
156+
*compose(
157+
tokens: Iterable<Token>,
158+
forceDoc = false,
159+
endOffset = -1
160+
): Generator<Document.Parsed<Contents, Strict>, void, unknown> {
152161
for (const token of tokens) yield* this.next(token)
153162
yield* this.end(forceDoc, endOffset)
154163
}
155164

156165
/** Advance the composer by one CST token. */
157-
*next(token: Token) {
166+
*next(
167+
token: Token
168+
): Generator<Document.Parsed<Contents, Strict>, void, unknown> {
158169
if (process.env.LOG_STREAM) console.dir(token, { depth: null })
159170
switch (token.type) {
160171
case 'directive':
@@ -245,7 +256,10 @@ export class Composer<
245256
* @param forceDoc - If the stream contains no document, still emit a final document including any comments and directives that would be applied to a subsequent document.
246257
* @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
247258
*/
248-
*end(forceDoc = false, endOffset = -1) {
259+
*end(
260+
forceDoc = false,
261+
endOffset = -1
262+
): Generator<Document.Parsed<Contents, Strict>, void, unknown> {
249263
if (this.doc) {
250264
this.decorate(this.doc, true)
251265
yield this.doc

src/doc/Document.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export class Document<
265265
key: unknown,
266266
value: unknown,
267267
options: CreateNodeOptions = {}
268-
) {
268+
): Pair<K, V> {
269269
const k = this.createNode(key, null, options) as K
270270
const v = this.createNode(value, null, options) as V
271271
return new Pair(k, v)

src/doc/directives.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class Directives {
5353
* During parsing, get a Directives instance for the current document and
5454
* update the stream state according to the current version's spec.
5555
*/
56-
atDocument() {
56+
atDocument(): Directives {
5757
const res = new Directives(this.yaml, this.tags)
5858
switch (this.yaml.version) {
5959
case '1.1':
@@ -78,7 +78,7 @@ export class Directives {
7878
add(
7979
line: string,
8080
onError: (offset: number, message: string, warning?: boolean) => void
81-
) {
81+
): boolean {
8282
if (this.atNextDocument) {
8383
this.yaml = { explicit: Directives.defaultYaml.explicit, version: '1.1' }
8484
this.tags = Object.assign({}, Directives.defaultTags)
@@ -124,7 +124,7 @@ export class Directives {
124124
* @returns Resolved tag, which may also be the non-specific tag `'!'` or a
125125
* `'!local'` tag, or `null` if unresolvable.
126126
*/
127-
tagName(source: string, onError: (message: string) => void) {
127+
tagName(source: string, onError: (message: string) => void): string | null {
128128
if (source === '!') return '!' // non-specific tag
129129

130130
if (source[0] !== '!') {
@@ -164,15 +164,15 @@ export class Directives {
164164
* Given a fully resolved tag, returns its printable string form,
165165
* taking into account current tag prefixes and defaults.
166166
*/
167-
tagString(tag: string) {
167+
tagString(tag: string): string {
168168
for (const [handle, prefix] of Object.entries(this.tags)) {
169169
if (tag.startsWith(prefix))
170170
return handle + escapeTagName(tag.substring(prefix.length))
171171
}
172172
return tag[0] === '!' ? tag : `!<${tag}>`
173173
}
174174

175-
toString(doc?: Document) {
175+
toString(doc?: Document): string {
176176
const lines = this.yaml.explicit
177177
? [`%YAML ${this.yaml.version || '1.2'}`]
178178
: []

src/nodes/Alias.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class Alias extends NodeBase {
4949
return found
5050
}
5151

52-
toJSON(_arg?: unknown, ctx?: ToJSContext) {
52+
toJSON(_arg?: unknown, ctx?: ToJSContext): unknown {
5353
if (!ctx) return { source: this.source }
5454
const { anchors, doc, maxAliasCount } = ctx
5555
const source = this.resolve(doc)
@@ -85,7 +85,7 @@ export class Alias extends NodeBase {
8585
ctx?: StringifyContext,
8686
_onComment?: () => void,
8787
_onChompKeep?: () => void
88-
) {
88+
): string {
8989
const src = `*${this.source}`
9090
if (ctx) {
9191
anchorIsValid(this.source)

src/nodes/Collection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export abstract class Collection extends NodeBase {
163163
else return isCollection(node) ? node.getIn(rest, keepScalar) : undefined
164164
}
165165

166-
hasAllNullValues(allowScalar?: boolean) {
166+
hasAllNullValues(allowScalar?: boolean): boolean {
167167
return this.items.every(node => {
168168
if (!isPair(node)) return false
169169
const n = node.value

src/nodes/Pair.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import type { StringifyContext } from '../stringify/stringify.ts'
66
import { stringifyPair } from '../stringify/stringifyPair.ts'
77
import { addPairToJSMap } from './addPairToJSMap.ts'
88
import { isNode, NODE_TYPE, PAIR } from './identity.ts'
9+
import type { Node } from './Node.ts'
910
import type { ToJSContext } from './toJS.ts'
1011

1112
export function createPair(
1213
key: unknown,
1314
value: unknown,
1415
ctx: CreateNodeContext
15-
) {
16+
): Pair<Node, Node> {
1617
const k = createNode(key, undefined, ctx)
1718
const v = createNode(value, undefined, ctx)
1819
return new Pair(k, v)

src/nodes/Scalar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class Scalar<T = unknown> extends NodeBase {
6161
return ctx?.keep ? this.value : toJS(this.value, arg, ctx)
6262
}
6363

64-
toString() {
64+
toString(): string {
6565
return String(this.value)
6666
}
6767
}

src/nodes/YAMLMap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type MapLike =
2020
export function findPair<K = unknown, V = unknown>(
2121
items: Iterable<Pair<K, V>>,
2222
key: unknown
23-
) {
23+
): Pair<K, V> | undefined {
2424
const k = isScalar(key) ? key.value : key
2525
for (const it of items) {
2626
if (isPair(it)) {
@@ -57,7 +57,7 @@ export class YAMLMap<K = unknown, V = unknown> extends Collection {
5757
* A generic collection parsing method that can be extended
5858
* to other node classes that inherit from YAMLMap
5959
*/
60-
static from(schema: Schema, obj: unknown, ctx: CreateNodeContext) {
60+
static from(schema: Schema, obj: unknown, ctx: CreateNodeContext): YAMLMap {
6161
const { keepUndefined, replacer } = ctx
6262
const map = new this(schema)
6363
const add = (key: unknown, value: unknown) => {

src/nodes/YAMLSeq.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class YAMLSeq<T = unknown> extends Collection {
9898
else this.items[idx] = value
9999
}
100100

101-
toJSON(_?: unknown, ctx?: ToJSContext) {
101+
toJSON(_?: unknown, ctx?: ToJSContext): unknown[] {
102102
const seq: unknown[] = []
103103
if (ctx?.onCreate) ctx.onCreate(seq)
104104
let i = 0
@@ -121,7 +121,7 @@ export class YAMLSeq<T = unknown> extends Collection {
121121
})
122122
}
123123

124-
static from(schema: Schema, obj: unknown, ctx: CreateNodeContext) {
124+
static from(schema: Schema, obj: unknown, ctx: CreateNodeContext): YAMLSeq {
125125
const { replacer } = ctx
126126
const seq = new this(schema)
127127
if (obj && Symbol.iterator in Object(obj)) {

src/nodes/addPairToJSMap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function addPairToJSMap(
1111
ctx: ToJSContext | undefined,
1212
map: MapLike,
1313
{ key, value }: Pair
14-
) {
14+
): MapLike {
1515
if (isNode(key) && key.addToJSMap) key.addToJSMap(ctx, map, value)
1616
// TODO: Should drop this special case for bare << handling
1717
else if (isMergeKey(ctx, key)) addMergeToJSMap(ctx, map, value)

0 commit comments

Comments
 (0)