Skip to content

Commit 03397ac

Browse files
committed
Move throwErrorAt to YAMLException as throwAt
1 parent d9dd2c7 commit 03397ac

3 files changed

Lines changed: 36 additions & 38 deletions

File tree

‎src/common/exception.ts‎

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,39 @@ class YAMLException extends Error {
5151
toString (compact?: boolean) {
5252
return `${this.name}: ${formatError(this, compact)}`
5353
}
54-
}
5554

56-
/**
57-
* Build a YAMLException with a source snippet and throw it. `source` is the
58-
* raw input text (no parser sentinel); `position` is an offset into it.
59-
*
60-
* @internal
61-
*/
62-
function throwErrorAt (source: string, position: number, message: string, filename = ''): never {
63-
let line = 0
64-
let lineStart = 0
65-
66-
for (let index = 0; index < position; index++) {
67-
const ch = source.charCodeAt(index)
68-
69-
if (ch === 0x0A/* LF */) {
70-
line++
71-
lineStart = index + 1
72-
} else if (ch === 0x0D/* CR */) {
73-
line++
74-
if (source.charCodeAt(index + 1) === 0x0A/* LF */) index++
75-
lineStart = index + 1
55+
/**
56+
* Builds a YAMLException with a source snippet and throws it. `source` is
57+
* the raw input text; `position` is an offset into it.
58+
*/
59+
static throwAt (source: string, position: number, message: string, filename = ''): never {
60+
let line = 0
61+
let lineStart = 0
62+
63+
for (let index = 0; index < position; index++) {
64+
const ch = source.charCodeAt(index)
65+
66+
if (ch === 0x0A/* LF */) {
67+
line++
68+
lineStart = index + 1
69+
} else if (ch === 0x0D/* CR */) {
70+
line++
71+
if (source.charCodeAt(index + 1) === 0x0A/* LF */) index++
72+
lineStart = index + 1
73+
}
7674
}
77-
}
7875

79-
const mark: SnippetMark = {
80-
name: filename,
81-
buffer: source,
82-
position,
83-
line,
84-
column: position - lineStart
85-
}
76+
const mark: SnippetMark = {
77+
name: filename,
78+
buffer: source,
79+
position,
80+
line,
81+
column: position - lineStart
82+
}
8683

87-
mark.snippet = makeSnippet(mark)
88-
throw new YAMLException(message, mark)
84+
mark.snippet = makeSnippet(mark)
85+
throw new YAMLException(message, mark)
86+
}
8987
}
9088

91-
export { YAMLException, throwErrorAt }
89+
export { YAMLException }

‎src/parser/constructor.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
type ScalarTagDefinition,
1717
type SequenceTagDefinition
1818
} from '../tag.ts'
19-
import { YAMLException, throwErrorAt } from '../common/exception.ts'
19+
import { YAMLException } from '../common/exception.ts'
2020
import { tagNameFull } from '../common/tagname.ts'
2121

2222
const NO_RANGE = -1
@@ -137,7 +137,7 @@ function eventPosition (event: Event) {
137137
}
138138

139139
function throwError (state: ConstructorState, message: string): never {
140-
throwErrorAt(state.source, state.position, message, state.filename)
140+
YAMLException.throwAt(state.source, state.position, message, state.filename)
141141
}
142142

143143
function finalizeCollection (
@@ -150,7 +150,7 @@ function finalizeCollection (
150150
return tag.finalize(carrier)
151151
} catch (error) {
152152
if (error instanceof YAMLException) throw error
153-
throwErrorAt(
153+
YAMLException.throwAt(
154154
state.source,
155155
position,
156156
error instanceof Error ? error.message : String(error),

‎src/parser/parser.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
type DocumentDirective,
1111
type TagHandlers
1212
} from './events.ts'
13-
import { throwErrorAt } from '../common/exception.ts'
13+
import { YAMLException } from '../common/exception.ts'
1414

1515
const NO_RANGE = -1
1616
const HAS_OWN = Object.prototype.hasOwnProperty
@@ -247,7 +247,7 @@ function restoreState (state: ParserState, snapshot: ParserSnapshot) {
247247
}
248248

249249
function throwError (state: ParserState, message: string): never {
250-
throwErrorAt(state.input.slice(0, state.length), state.position, message, state.filename)
250+
YAMLException.throwAt(state.input.slice(0, state.length), state.position, message, state.filename)
251251
}
252252

253253
function isEol (c: number) {
@@ -1443,7 +1443,7 @@ function parseEvents (input: string, options: ParserOptions): Event[] {
14431443
}
14441444

14451445
const nullpos = input.indexOf('\0')
1446-
if (nullpos !== -1) throwErrorAt(input, nullpos, 'null byte is not allowed in input', state.filename)
1446+
if (nullpos !== -1) YAMLException.throwAt(input, nullpos, 'null byte is not allowed in input', state.filename)
14471447

14481448
if (state.input.charCodeAt(state.position) === 0xFEFF) state.position++
14491449

0 commit comments

Comments
 (0)