Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
fix regexp for numeric template literals (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Oct 19, 2023
1 parent 9ce724e commit 4b522a3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-kids-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

fix regexp for numeric template literals
8 changes: 6 additions & 2 deletions src/Arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ export const go = (ast: AST.AST, constraints?: Constraints): Arbitrary<any> => {
return (fc) => fc.oneof(fc.object(), fc.array(fc.anything()))
case "TemplateLiteral": {
return (fc) => {
const components = [fc.constant(ast.head)]
const components: Array<FastCheck.Arbitrary<string | number>> = [fc.constant(ast.head)]
for (const span of ast.spans) {
components.push(fc.string({ maxLength: 5 }))
if (AST.isStringKeyword(span.type)) {
components.push(fc.string({ maxLength: 5 }))
} else {
components.push(fc.float({ noDefaultInfinity: true }))
}
components.push(fc.constant(span.literal))
}
return fc.tuple(...components).map((spans) => spans.join(""))
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ const getTemplateLiteralRegex = (ast: AST.TemplateLiteral): RegExp => {
if (AST.isStringKeyword(span.type)) {
pattern += ".*"
} else if (AST.isNumberKeyword(span.type)) {
pattern += "-?\\d+(\\.\\d+)?"
pattern += "[+-]?\\d*\\.?\\d+(?:[Ee][+-]?\\d+)?"
}
pattern += span.literal
}
Expand Down
5 changes: 5 additions & 0 deletions test/Arbitrary/Arbitrary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ describe("Arbitrary/Arbitrary", () => {
propertyTo(schema)
})

it("templateLiteral. a${number}", () => {
const schema = S.templateLiteral(S.literal("a"), S.number)
propertyTo(schema)
})

it("templateLiteral. a", () => {
const schema = S.templateLiteral(S.literal("a"))
propertyTo(schema)
Expand Down
18 changes: 16 additions & 2 deletions test/Schema/templateLiteral.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,22 @@ describe("Schema/templateLiteral", () => {

it("a${number}", async () => {
const schema = S.templateLiteral(S.literal("a"), S.number)
await Util.expectParseSuccess(schema, "a1", "a1")
await Util.expectParseSuccess(schema, "a1.2", "a1.2")
await Util.expectParseSuccess(schema, "a1")
await Util.expectParseSuccess(schema, "a1.2")

await Util.expectParseSuccess(schema, "a-1.401298464324817e-45")
await Util.expectParseSuccess(schema, "a1.401298464324817e-45")
await Util.expectParseSuccess(schema, "a+1.401298464324817e-45")
await Util.expectParseSuccess(schema, "a-1.401298464324817e+45")
await Util.expectParseSuccess(schema, "a1.401298464324817e+45")
await Util.expectParseSuccess(schema, "a+1.401298464324817e+45")

await Util.expectParseSuccess(schema, "a-1.401298464324817E-45")
await Util.expectParseSuccess(schema, "a1.401298464324817E-45")
await Util.expectParseSuccess(schema, "a+1.401298464324817E-45")
await Util.expectParseSuccess(schema, "a-1.401298464324817E+45")
await Util.expectParseSuccess(schema, "a1.401298464324817E+45")
await Util.expectParseSuccess(schema, "a+1.401298464324817E+45")

await Util.expectParseFailure(
schema,
Expand Down

0 comments on commit 4b522a3

Please sign in to comment.