Skip to content

Commit e0620ab

Browse files
committed
chore: fmt
I gave oxfmt a shot, and I wasn't happy with the results at all. Especially in places with dense and complex code, applying opinionated format is stupid and just doesn't work. It made the `parseString` function really hard to read because it squished everything together. I still kept the stuff it did right :)
1 parent 96114cb commit e0620ab

22 files changed

Lines changed: 230 additions & 285 deletions

bench/parse.bench.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ summary(() => {
8888
}
8989
})
9090

91-
bench('deno\'s @std/toml', function* () {
91+
bench("deno's @std/toml", function* () {
9292
yield {
9393
[0]() {
9494
return toml

bench/stringify.bench.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ summary(() => {
7575
}
7676
})
7777

78-
bench('deno\'s @std/toml', function* () {
78+
bench("deno's @std/toml", function* () {
7979
yield {
8080
[0]() {
8181
return toml

src/date.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class TomlDate extends Date {
3838
#hasTime = false
3939
#offset: Offset = null
4040

41-
constructor (date: string | Date) {
41+
constructor(date: string | Date) {
4242
let hasDate = true
4343
let hasTime = true
4444
let offset: Offset = 'Z'
@@ -75,23 +75,23 @@ export class TomlDate extends Date {
7575
}
7676
}
7777

78-
isDateTime () {
78+
isDateTime() {
7979
return this.#hasDate && this.#hasTime
8080
}
8181

82-
isLocal () {
82+
isLocal() {
8383
return !this.#hasDate || !this.#hasTime || !this.#offset
8484
}
8585

86-
isDate () {
86+
isDate() {
8787
return this.#hasDate && !this.#hasTime
8888
}
8989

90-
isTime () {
90+
isTime() {
9191
return this.#hasTime && !this.#hasDate
9292
}
9393

94-
isValid () {
94+
isValid() {
9595
return this.#hasDate || this.#hasTime
9696
}
9797

@@ -124,26 +124,26 @@ export class TomlDate extends Date {
124124
return offsetDate.toISOString().slice(0, -1) + this.#offset
125125
}
126126

127-
static wrapAsOffsetDateTime (jsDate: Date, offset = 'Z') {
127+
static wrapAsOffsetDateTime(jsDate: Date, offset = 'Z') {
128128
let date = new TomlDate(jsDate)
129129
date.#offset = offset
130130
return date
131131
}
132132

133-
static wrapAsLocalDateTime (jsDate: Date) {
133+
static wrapAsLocalDateTime(jsDate: Date) {
134134
let date = new TomlDate(jsDate)
135135
date.#offset = null
136136
return date
137137
}
138138

139-
static wrapAsLocalDate (jsDate: Date) {
139+
static wrapAsLocalDate(jsDate: Date) {
140140
let date = new TomlDate(jsDate)
141141
date.#hasTime = false
142142
date.#offset = null
143143
return date
144144
}
145145

146-
static wrapAsLocalTime (jsDate: Date) {
146+
static wrapAsLocalTime(jsDate: Date) {
147147
let date = new TomlDate(jsDate)
148148
date.#hasDate = false
149149
date.#offset = null

src/error.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ type TomlErrorOptions = ErrorOptions & {
3131
ptr: number
3232
}
3333

34-
function getLineColFromPtr (string: string, ptr: number): [ number, number ] {
34+
function getLineColFromPtr(string: string, ptr: number): [number, number] {
3535
let lines = string.slice(0, ptr).split(/\r\n|\n|\r/g)
36-
return [ lines.length, lines.pop()!.length + 1 ]
36+
return [lines.length, lines.pop()!.length + 1]
3737
}
3838

39-
function makeCodeBlock (string: string, line: number, column: number) {
39+
function makeCodeBlock(string: string, line: number, column: number) {
4040
let lines = string.split(/\r\n|\n|\r/g)
4141
let codeblock = ''
4242

@@ -65,8 +65,8 @@ export class TomlError extends Error {
6565
column: number
6666
codeblock: string
6767

68-
constructor (message: string, options: TomlErrorOptions) {
69-
const [ line, column ] = getLineColFromPtr(options.toml, options.ptr)
68+
constructor(message: string, options: TomlErrorOptions) {
69+
const [line, column] = getLineColFromPtr(options.toml, options.ptr)
7070
const codeblock = makeCodeBlock(options.toml, line, column)
7171

7272
super(`Invalid TOML document: ${message}\n\n${codeblock}`, options)

src/extract.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { parseArray, parseInlineTable } from './struct.js'
3131
import { skipVoid, skipUntil, skipComment, type TomlValue } from './util.js'
3232
import { TomlError } from './error.js'
3333

34-
function sliceAndTrimEndOf (str: string, startPtr: number, endPtr: number): [ string, number ] {
34+
function sliceAndTrimEndOf(str: string, startPtr: number, endPtr: number): [string, number] {
3535
let value = str.slice(startPtr, endPtr)
3636

3737
let commentIdx = value.indexOf('#')
@@ -42,25 +42,20 @@ function sliceAndTrimEndOf (str: string, startPtr: number, endPtr: number): [ st
4242
value = value.slice(0, commentIdx)
4343
}
4444

45-
return [ value.trimEnd(), commentIdx ]
45+
return [value.trimEnd(), commentIdx]
4646
}
4747

48-
export function extractValue (
49-
str: string, ptr: number,
50-
end: string | undefined,
51-
depth: number,
52-
integersAsBigInt: IntegersAsBigInt,
53-
): [ TomlValue, number ] {
48+
export function extractValue(str: string, ptr: number, end: string | undefined, depth: number, integersAsBigInt: IntegersAsBigInt): [TomlValue, number] {
5449
if (depth === 0) {
5550
throw new TomlError('document contains excessively nested structures. aborting.', {
5651
toml: str,
57-
ptr: ptr
52+
ptr: ptr,
5853
})
5954
}
6055

6156
let c = str[ptr]
6257
if (c === '[' || c === '{') {
63-
let [ value, endPtr ] = c === '['
58+
let [value, endPtr] = c === '['
6459
? parseArray(str, ptr, depth, integersAsBigInt)
6560
: parseInlineTable(str, ptr, depth, integersAsBigInt)
6661

@@ -75,7 +70,7 @@ export function extractValue (
7570
}
7671
}
7772

78-
return [ value, endPtr ]
73+
return [value, endPtr]
7974
}
8075

8176
if (c === '"' || c === "'") {
@@ -93,15 +88,15 @@ export function extractValue (
9388
if (str[endPtr] === ',') endPtr++
9489
}
9590

96-
return [ parsed, endPtr ]
91+
return [parsed, endPtr]
9792
}
9893

9994
let endPtr = skipUntil(str, ptr, ',', end)
10095
let slice = sliceAndTrimEndOf(str, ptr, endPtr - (str[endPtr - 1] === ',' ? 1 : 0))
10196
if (!slice[0]) {
10297
throw new TomlError('incomplete key-value declaration: no value specified', {
10398
toml: str,
104-
ptr: ptr
99+
ptr: ptr,
105100
})
106101
}
107102

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ export { parse, stringify, TomlDate, TomlError }
3838

3939
export type {
4040
/** @deprecated use TomlValue instead */
41-
TomlValue as TomlPrimitive
41+
TomlValue as TomlPrimitive,
4242
} from './util.js'

src/parse.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ import { TomlError } from './error.js'
3434

3535
const enum Type { DOTTED, EXPLICIT, ARRAY, ARRAY_DOTTED }
3636

37-
type MetaState = { t: Type, d: boolean, i: number, c: MetaRecord }
37+
type MetaState = { t: Type; d: boolean; i: number; c: MetaRecord }
3838
type MetaRecord = { [k: string]: MetaState }
39-
type PeekResult = [ string, TomlTable, MetaRecord ] | null
39+
type PeekResult = [string, TomlTable, MetaRecord] | null
4040

41-
function peekTable (key: string[], table: TomlTable, meta: MetaRecord, type: Type): PeekResult {
41+
function peekTable(key: string[], table: TomlTable, meta: MetaRecord, type: Type): PeekResult {
4242
let t: any = table
4343
let m = meta
4444
let k: string
@@ -111,17 +111,17 @@ function peekTable (key: string[], table: TomlTable, meta: MetaRecord, type: Typ
111111
return null
112112
}
113113

114-
return [ k!, t, state.c ]
114+
return [k!, t, state.c]
115115
}
116116

117117
export interface ParseOptions {
118118
maxDepth?: number
119119
integersAsBigInt?: IntegersAsBigInt
120120
}
121121

122-
export function parse(toml: string, options?: ParseOptions & { integersAsBigInt: Exclude<IntegersAsBigInt, undefined | false> }): TomlTable;
123-
export function parse(toml: string, options?: ParseOptions): TomlTableWithoutBigInt;
124-
export function parse(toml: string, { maxDepth = 1000, integersAsBigInt }: ParseOptions = {}): TomlTable {
122+
export function parse(toml: string, options?: ParseOptions & { integersAsBigInt: Exclude<IntegersAsBigInt, undefined | false> }): TomlTable
123+
export function parse(toml: string, options?: ParseOptions): TomlTableWithoutBigInt
124+
export function parse(toml: string, { maxDepth = 1000, integersAsBigInt }: ParseOptions = {}): TomlTable {
125125
let res = {}
126126
let meta = {}
127127

@@ -131,7 +131,7 @@ export function parse(toml: string, { maxDepth = 1000, integersAsBigInt }: Parse
131131
for (let ptr = skipVoid(toml, 0); ptr < toml.length;) {
132132
if (toml[ptr] === '[') {
133133
let isTableArray = toml[++ptr] === '['
134-
let k = parseKey(toml, ptr += +isTableArray, ']')
134+
let k = parseKey(toml, (ptr += +isTableArray), ']')
135135

136136
if (isTableArray) {
137137
if (toml[k[1] - 1] !== ']') {
@@ -165,16 +165,16 @@ export function parse(toml: string, { maxDepth = 1000, integersAsBigInt }: Parse
165165
})
166166
}
167167

168-
let v = extractValue(toml, k[1], void 0, maxDepth, integersAsBigInt);
169-
p[1][p[0]] = v[0];
170-
ptr = v[1];
168+
let v = extractValue(toml, k[1], void 0, maxDepth, integersAsBigInt)
169+
p[1][p[0]] = v[0]
170+
ptr = v[1]
171171
}
172172

173173
ptr = skipVoid(toml, ptr, true)
174174
if (toml[ptr] && toml[ptr] !== '\n' && toml[ptr] !== '\r') {
175175
throw new TomlError('each key-value declaration must be followed by an end-of-line', {
176176
toml: toml,
177-
ptr: ptr
177+
ptr: ptr,
178178
})
179179
}
180180
ptr = skipVoid(toml, ptr)

src/primitive.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let INT_REGEX = /^((0x[0-9a-fA-F](_?[0-9a-fA-F])*)|(([+-]|0[ob])?\d(_?\d)*))$/
3434
let FLOAT_REGEX = /^[+-]?\d(_?\d)*(\.\d(_?\d)*)?([eE][+-]?\d(_?\d)*)?$/
3535
let LEADING_ZERO = /^[+-]?0[0-9_]/
3636

37-
export function parseString (str: string, ptr: number): [string, number] {
37+
export function parseString(str: string, ptr: number): [string, number] {
3838
let c = str[ptr++]!
3939
let first = c
4040
let isLiteral = c === "'"
@@ -115,13 +115,13 @@ export function parseString (str: string, ptr: number): [string, number] {
115115
// If we're in a newline escape still, then there's nothing to add.
116116
// Also try to avoid concat if there's nothing to add to parsed, or nothing has been added to parsed.
117117
state ? parsed : parsed + str.slice(sliceStart, i),
118-
i + (isMultiline ? 3 : 1)
118+
i + (isMultiline ? 3 : 1),
119119
]
120120
}
121121

122122
else if (!state) {
123123
if (!isLiteral && c === '\\') {
124-
parsed += str.slice(sliceStart, sliceStart = i)
124+
parsed += str.slice(sliceStart, (sliceStart = i))
125125
state = 1
126126
}
127127
}
@@ -190,7 +190,7 @@ export function parseString (str: string, ptr: number): [string, number] {
190190

191191
export type IntegersAsBigInt = undefined | boolean | 'asNeeded'
192192

193-
export function parseValue (value: string, toml: string, ptr: number, integersAsBigInt: IntegersAsBigInt): boolean | number | bigint | TomlDate {
193+
export function parseValue(value: string, toml: string, ptr: number, integersAsBigInt: IntegersAsBigInt): boolean | number | bigint | TomlDate {
194194
// Constant values
195195
if (value === 'true') return true
196196
if (value === 'false') return false

src/stringify.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
let BARE_KEY = /^[a-z0-9-_]+$/i
3030

3131
type ExtendedType = ReturnType<typeof extendedTypeOf>
32-
function extendedTypeOf (obj: any) {
32+
function extendedTypeOf(obj: any) {
3333
let type = typeof obj
3434
if (type === 'object') {
3535
if (Array.isArray(obj)) return 'array'
@@ -39,19 +39,19 @@ function extendedTypeOf (obj: any) {
3939
return type
4040
}
4141

42-
function isArrayOfTables (obj: any[]) {
42+
function isArrayOfTables(obj: any[]) {
4343
for (let i = 0; i < obj.length; i++) {
4444
if (extendedTypeOf(obj[i]) !== 'object') return false
4545
}
4646

4747
return obj.length != 0
4848
}
4949

50-
function formatString (s: string) {
50+
function formatString(s: string) {
5151
return JSON.stringify(s).replace(/\x7f/g, '\\u007f')
5252
}
5353

54-
function stringifyValue (val: any, type: ExtendedType, depth: number, numberAsFloat: boolean) {
54+
function stringifyValue(val: any, type: ExtendedType, depth: number, numberAsFloat: boolean) {
5555
if (depth === 0) {
5656
throw new Error('Could not stringify the object: maximum object depth exceeded')
5757
}
@@ -89,7 +89,7 @@ function stringifyValue (val: any, type: ExtendedType, depth: number, numberAsFl
8989
}
9090
}
9191

92-
function stringifyInlineTable (obj: any, depth: number, numberAsFloat: boolean) {
92+
function stringifyInlineTable(obj: any, depth: number, numberAsFloat: boolean) {
9393
let keys = Object.keys(obj)
9494
if (keys.length === 0) return '{}'
9595

@@ -106,7 +106,7 @@ function stringifyInlineTable (obj: any, depth: number, numberAsFloat: boolean)
106106
return res + ' }'
107107
}
108108

109-
function stringifyArray (array: any[], depth: number, numberAsFloat: boolean) {
109+
function stringifyArray(array: any[], depth: number, numberAsFloat: boolean) {
110110
if (array.length === 0) return '[]'
111111

112112
let res = '[ '
@@ -122,7 +122,7 @@ function stringifyArray (array: any[], depth: number, numberAsFloat: boolean) {
122122
return res + ' ]'
123123
}
124124

125-
function stringifyArrayTable (array: any[], key: string, depth: number, numberAsFloat: boolean) {
125+
function stringifyArrayTable(array: any[], key: string, depth: number, numberAsFloat: boolean) {
126126
if (depth === 0) {
127127
throw new Error('Could not stringify the object: maximum object depth exceeded')
128128
}
@@ -136,7 +136,7 @@ function stringifyArrayTable (array: any[], key: string, depth: number, numberAs
136136
return res
137137
}
138138

139-
function stringifyTable (tableKey: string | 0, obj: any, prefix: string, depth: number, numberAsFloat: boolean) {
139+
function stringifyTable(tableKey: string | 0, obj: any, prefix: string, depth: number, numberAsFloat: boolean) {
140140
if (depth === 0) {
141141
throw new Error('Could not stringify the object: maximum object depth exceeded')
142142
}

0 commit comments

Comments
 (0)