diff --git a/Cargo.lock b/Cargo.lock index 2536db0..4fb2f3f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,9 +44,9 @@ checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "js-sys" -version = "0.3.79" +version = "0.3.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6247da8b8658ad4e73a186e747fcc5fc2a29f979d6fe6269127fdb5fd08298d0" +checksum = "464a3709c7f55f1f721e5389aa6ea4e3bc6aba669353300af094b29ffbdde1d8" dependencies = [ "once_cell", "wasm-bindgen", @@ -63,12 +63,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "log" -version = "0.4.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" - [[package]] name = "memchr" version = "2.7.6" @@ -203,9 +197,9 @@ checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" [[package]] name = "wasm-bindgen" -version = "0.2.102" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ad224d2776649cfb4f4471124f8176e54c1cca67a88108e30a0cd98b90e7ad3" +checksum = "0d759f433fa64a2d763d1340820e46e111a7a5ab75f993d1852d70b03dbb80fd" dependencies = [ "cfg-if", "once_cell", @@ -214,25 +208,11 @@ dependencies = [ "wasm-bindgen-shared", ] -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.102" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a1364104bdcd3c03f22b16a3b1c9620891469f5e9f09bc38b2db121e593e732" -dependencies = [ - "bumpalo", - "log", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - [[package]] name = "wasm-bindgen-macro" -version = "0.2.102" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d7ab4ca3e367bb1ed84ddbd83cc6e41e115f8337ed047239578210214e36c76" +checksum = "48cb0d2638f8baedbc542ed444afc0644a29166f1595371af4fecf8ce1e7eeb3" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -240,22 +220,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.102" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a518014843a19e2dbbd0ed5dfb6b99b23fb886b14e6192a00803a3e14c552b0" +checksum = "cefb59d5cd5f92d9dcf80e4683949f15ca4b511f4ac0a6e14d4e1ac60c6ecd40" dependencies = [ + "bumpalo", "proc-macro2", "quote", "syn", - "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.102" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "255eb0aa4cc2eea3662a00c2bbd66e93911b7361d5e0fcd62385acfd7e15dcee" +checksum = "cbc538057e648b67f72a982e708d485b2efa771e1ac05fec311f9f63e5800db4" dependencies = [ "unicode-ident", ] diff --git a/README.md b/README.md index 3e715c4..c1630ab 100644 --- a/README.md +++ b/README.md @@ -75,15 +75,15 @@ customize this behaviour by passing options: ```ts import { parse, parseToValue } from "@david/jsonc-morph"; -// Disable specific extensions +// disable specific extensions const root = parse(text, { - allowComments: false, // Reject // and /* */ comments - allowTrailingCommas: false, // Reject trailing commas in arrays/objects - allowSingleQuotedStrings: false, // Reject 'single quoted' strings - allowHexadecimalNumbers: false, // Reject 0xFF style numbers - allowUnaryPlusNumbers: false, // Reject +42 style numbers - allowMissingCommas: false, // Reject missing commas between elements - allowLooseObjectPropertyNames: false, // Reject unquoted property names + allowComments: false, // reject // and /* */ comments + allowTrailingCommas: false, // reject trailing commas in arrays/objects + allowSingleQuotedStrings: false, // reject 'single quoted' strings + allowHexadecimalNumbers: false, // reject 0xFF style numbers + allowUnaryPlusNumbers: false, // reject +42 style numbers + allowMissingCommas: false, // reject missing commas between elements + allowLooseObjectPropertyNames: false, // reject unquoted property names }); // parseToValue accepts the same options @@ -96,12 +96,12 @@ For strict JSON parsing (only allow JSON), use `parseStrict` or ```ts import { parseStrict, parseToValueStrict } from "@david/jsonc-morph"; -// All extensions disabled by default +// all extensions disabled by default const root = parseStrict('{"name": "test"}'); -// Selectively enable specific extensions +// selectively enable specific extensions const rootWithComments = parseStrict(text, { allowComments: true }); -// Same for parseToValueStrict +// same for `parseToValueStrict` const value = parseToValueStrict('{"name": "test"}'); ``` diff --git a/deno.json b/deno.json index 0b699bb..363b95a 100644 --- a/deno.json +++ b/deno.json @@ -11,13 +11,14 @@ "Cargo.lock", "Cargo.toml", "deno.lock", + "target", "rust-toolchain.toml", "**/*_test.ts" ] }, "tasks": { "build:npm": "deno run -A scripts/build_npm.ts", - "build": "deno run -A jsr:@deno/wasmbuild@0.19.3 --inline && deno run -A scripts/fix_types.ts" + "build": "deno run -A jsr:@deno/wasmbuild@0.21.0 --inline && deno run -A scripts/fix_types.ts" }, "imports": { "@deno/dnt": "jsr:@deno/dnt@^0.42.3", diff --git a/mod.ts b/mod.ts index ce1b710..65ba369 100644 --- a/mod.ts +++ b/mod.ts @@ -9,6 +9,7 @@ export { ObjectProp, ObjectPropName, parse, + type ParseOptions, parseToValue, RootNode, StringLit, @@ -18,29 +19,18 @@ export { import { type JsonValue, parse, + type ParseOptions, parseToValue, type RootNode, } from "./lib/rs_lib.js"; -/** Options for strict JSON parsing (all JSONC extensions disabled by default). */ -export interface ParseStrictOptions { - /** Allow comments (defaults to `false` in strict mode). */ - allowComments?: boolean; - /** Allow trailing commas (defaults to `false` in strict mode). */ - allowTrailingCommas?: boolean; - /** Allow loose object property names (defaults to `false` in strict mode). */ - allowLooseObjectPropertyNames?: boolean; - /** Allow missing commas (defaults to `false` in strict mode). */ - allowMissingCommas?: boolean; - /** Allow single-quoted strings (defaults to `false` in strict mode). */ - allowSingleQuotedStrings?: boolean; - /** Allow hexadecimal numbers (defaults to `false` in strict mode). */ - allowHexadecimalNumbers?: boolean; - /** Allow unary plus on numbers (defaults to `false` in strict mode). */ - allowUnaryPlusNumbers?: boolean; -} +/** + * Options for strict JSON parsing (all JSONC extensions disabled by default). + * @deprecated Use `ParseOptions` instead - this is an alias for backwards compatibility. + */ +export type ParseStrictOptions = ParseOptions; -const STRICT_DEFAULTS: Required = { +const STRICT_DEFAULTS: Required = { allowComments: false, allowTrailingCommas: false, allowLooseObjectPropertyNames: false, @@ -60,7 +50,7 @@ const STRICT_DEFAULTS: Required = { */ export function parseStrict( text: string, - options?: ParseStrictOptions, + options?: ParseOptions, ): RootNode { return parse(text, { ...STRICT_DEFAULTS, ...options }); } @@ -76,7 +66,7 @@ export function parseStrict( */ export function parseToValueStrict( text: string, - options?: ParseStrictOptions, + options?: ParseOptions, ): JsonValue { return parseToValue(text, { ...STRICT_DEFAULTS, ...options }); } diff --git a/rs_lib/Cargo.toml b/rs_lib/Cargo.toml index e04c363..d2b66dd 100644 --- a/rs_lib/Cargo.toml +++ b/rs_lib/Cargo.toml @@ -8,7 +8,7 @@ crate-type = ["cdylib"] [dependencies] jsonc-parser = { version = "0.29.0", features = ["cst", "serde", "preserve_order", "error_unicode_width"] } -wasm-bindgen = "=0.2.102" +wasm-bindgen = "=0.2.106" js-sys = "0.3" serde = "1.0" serde-wasm-bindgen = "0.6" diff --git a/rs_lib/src/lib.rs b/rs_lib/src/lib.rs index 6edb87d..fab8b19 100644 --- a/rs_lib/src/lib.rs +++ b/rs_lib/src/lib.rs @@ -14,19 +14,38 @@ fn throw_error(msg: &str) -> JsValue { #[wasm_bindgen] extern "C" { - #[wasm_bindgen( - typescript_type = "{ allowComments?: boolean; allowTrailingCommas?: boolean; allowLooseObjectPropertyNames?: boolean; allowMissingCommas?: boolean; allowSingleQuotedStrings?: boolean; allowHexadecimalNumbers?: boolean; allowUnaryPlusNumbers?: boolean; }" - )] + #[wasm_bindgen(typescript_type = "ParseOptions")] pub type JsoncParseOptionsObject; - #[wasm_bindgen( - typescript_type = "string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }" - )] + #[wasm_bindgen(typescript_type = "JsonValue")] pub type JsonValue; } #[wasm_bindgen(typescript_custom_section)] const TS_APPEND_CONTENT: &'static str = r#" +/** + * Options for parsing JSONC. + * + * When used with `parse`/`parseToValue`, all options default to `true` (permissive). + * When used with `parseStrict`/`parseToValueStrict`, all options default to `false` (strict). + */ +export interface ParseOptions { + /** Allow comments. */ + allowComments?: boolean; + /** Allow trailing commas. */ + allowTrailingCommas?: boolean; + /** Allow loose object property names (unquoted keys). */ + allowLooseObjectPropertyNames?: boolean; + /** Allow missing commas between elements. */ + allowMissingCommas?: boolean; + /** Allow single-quoted strings. */ + allowSingleQuotedStrings?: boolean; + /** Allow hexadecimal numbers (e.g., 0xFF). */ + allowHexadecimalNumbers?: boolean; + /** Allow unary plus on numbers (e.g., +42). */ + allowUnaryPlusNumbers?: boolean; +} + export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }; "#; diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 1be126d..711b3a4 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.89.0" +channel = "1.92.0" components = ["clippy", "rustfmt"]