Skip to content

Commit 8925c5f

Browse files
committed
Add a sanity check in plz-utilities.ts
1 parent 0f8deb6 commit 8925c5f

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

src/language/unparsing/plz-utilities.ts

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -195,40 +195,50 @@ const unparseSugaredIndex = (
195195
)
196196
if (either.isLeft(objectUnparseResult)) {
197197
return objectUnparseResult
198-
}
198+
} else {
199+
if (typeof expression.query !== 'object') {
200+
// TODO: It would be nice if this were provably impossible.
201+
return either.makeLeft({
202+
kind: 'unserializableValue',
203+
message: 'Invalid index expression',
204+
})
205+
} else {
206+
const keyPath = Object.entries(expression.query).reduce(
207+
(accumulator: KeyPath | 'invalid', [key, value]) => {
208+
if (accumulator === 'invalid') {
209+
return accumulator
210+
} else {
211+
if (
212+
key === String(accumulator.length) &&
213+
typeof value === 'string'
214+
) {
215+
return [...accumulator, value]
216+
} else {
217+
return 'invalid'
218+
}
219+
}
220+
},
221+
[],
222+
)
199223

200-
const keyPath = Object.entries(expression.query).reduce(
201-
(accumulator: KeyPath | 'invalid', [key, value]) => {
202-
if (accumulator === 'invalid') {
203-
return accumulator
224+
if (
225+
keyPath === 'invalid' ||
226+
Object.keys(expression.query).length !== keyPath.length
227+
) {
228+
return either.makeLeft({
229+
kind: 'unserializableValue',
230+
message: 'invalid key path',
231+
})
204232
} else {
205-
if (key === String(accumulator.length) && typeof value === 'string') {
206-
return [...accumulator, value]
207-
} else {
208-
return 'invalid'
209-
}
233+
const { dot } = punctuation(kleur)
234+
return either.makeRight(
235+
objectUnparseResult.value
236+
.concat(dot)
237+
.concat(keyPath.map(quoteKeyPathComponentIfNecessary).join(dot)),
238+
)
210239
}
211-
},
212-
[],
213-
)
214-
215-
if (
216-
keyPath === 'invalid' ||
217-
Object.keys(expression.query).length !== keyPath.length
218-
) {
219-
return either.makeLeft({
220-
kind: 'unserializableValue',
221-
message: 'invalid key path',
222-
})
240+
}
223241
}
224-
225-
const { dot } = punctuation(kleur)
226-
227-
return either.makeRight(
228-
objectUnparseResult.value
229-
.concat(dot)
230-
.concat(keyPath.map(quoteKeyPathComponentIfNecessary).join(dot)),
231-
)
232242
}
233243

234244
const unparseSugaredLookup = (

0 commit comments

Comments
 (0)