@ata-project/unplugin 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -183,9 +183,13 @@ A call is replaced only when all of this is true, and left to the runtime
183
183
  otherwise:
184
184
 
185
185
  - `Validator` is a named import from `ata-validator`;
186
- - the call has one argument, and it is an object literal, a top-level `const`
187
- bound to one, the default import of a relative `.json` file, or
188
- `defineSchema(...)` around one of those;
186
+ - the schema argument is an object literal, a top-level `const` bound to one,
187
+ the default import of a relative `.json` file, or `defineSchema(...)` around
188
+ one of those;
189
+ - there is no second argument, or it is `{ useDefaults: false }` (or
190
+ `{ useDefaults: true }`), written as a literal or a top-level `const`; that
191
+ one option needs ata-validator 1.37.0, and older versions keep such calls on
192
+ the runtime;
189
193
  - the result goes into a `const` that is not exported and is only used as
190
194
  `name.validate(...)`, `name.isValidObject(...)`, `name.validateJSON(...)` or
191
195
  `name.isValidJSON(...)`;
@@ -193,7 +197,7 @@ otherwise:
193
197
  `errorMessage`s and shapes its code generator cannot express, which the
194
198
  runtime answers with its interpreted engine.
195
199
 
196
- Anything with options (`new Validator(schema, { coerceTypes: true })`), a schema
200
+ Any other option (`new Validator(schema, { coerceTypes: true })`), a schema
197
201
  built at run time, or an instance passed around stays as written.
198
202
 
199
203
  ## How it works
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ata-project/unplugin",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Compile JSON, JS and TS schema files to ata-validator standalone modules at build time, in Vite, Webpack, Rollup, Rolldown, esbuild and Rspack.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -78,7 +78,7 @@
78
78
  },
79
79
  "devDependencies": {
80
80
  "@rspack/core": "^1.7.0",
81
- "ata-validator": "^1.36.0",
81
+ "ata-validator": "^1.37.0",
82
82
  "esbuild": "^0.28.0",
83
83
  "rolldown": "^1.2.8",
84
84
  "rollup": "^4.60.0",
@@ -185,8 +185,24 @@ function inlineModule(src, index) {
185
185
  return `const __ataCompiled${index} = (() => {\n${body}\nreturn { validate, isValid };\n})();\n`
186
186
  }
187
187
 
188
+ // The options of `new Validator(schema, options)` as a third argument for
189
+ // fromCompiled(), or NOT_STATIC when the call has to stay on the runtime: options
190
+ // that are not a literal the build can read, or that name anything this
191
+ // ata-validator's wrapper does not reproduce. `useDefaults: true` is the default
192
+ // and needs no argument.
193
+ function compiledOptionsArg(node, ctx, supported) {
194
+ const opts = staticValue(node, ctx)
195
+ if (opts === NOT_STATIC || opts === null || typeof opts !== 'object' || Array.isArray(opts)) return NOT_STATIC
196
+ for (const [key, value] of Object.entries(opts)) {
197
+ if (!supported.includes(key)) return NOT_STATIC
198
+ if (key === 'useDefaults' && typeof value !== 'boolean') return NOT_STATIC
199
+ }
200
+ return opts.useDefaults === false ? '{"useDefaults":false}' : ''
201
+ }
202
+
188
203
  export function compileAway(code, id, ata) {
189
204
  const { compiledModuleFor, compiledSchemaFor } = ata
205
+ const supportedOptions = Array.isArray(ata.compiledOptions) ? ata.compiledOptions : []
190
206
  if (!code.includes('ata-validator')) return null
191
207
  let ast
192
208
  try {
@@ -221,7 +237,13 @@ export function compileAway(code, id, ata) {
221
237
  for (const r of refs) {
222
238
  if (r.node.name !== validatorName) continue
223
239
  const expr = r.parent
224
- if (!expr || expr.type !== 'NewExpression' || r.key !== 'callee' || expr.arguments.length !== 1) continue
240
+ if (!expr || expr.type !== 'NewExpression' || r.key !== 'callee') continue
241
+ if (expr.arguments.length !== 1 && expr.arguments.length !== 2) continue
242
+ let optionsArg = ''
243
+ if (expr.arguments.length === 2) {
244
+ optionsArg = compiledOptionsArg(expr.arguments[1], ctx, supportedOptions)
245
+ if (optionsArg === NOT_STATIC) continue
246
+ }
225
247
  if (!replaceableBinding(expr, ctx)) continue
226
248
  const schema = staticValue(expr.arguments[0], ctx)
227
249
  if (schema === NOT_STATIC || schema === null || typeof schema !== 'object' || Array.isArray(schema)) continue
@@ -230,7 +252,7 @@ export function compileAway(code, id, ata) {
230
252
  if (!src) continue
231
253
  const index = modules.length
232
254
  modules.push(inlineModule(src, index))
233
- s.overwrite(expr.start, expr.end, `__ataFromCompiled(__ataCompiled${index}, ${JSON.stringify(compiledSchemaFor(schema))})`)
255
+ s.overwrite(expr.start, expr.end, `__ataFromCompiled(__ataCompiled${index}, ${JSON.stringify(compiledSchemaFor(schema))}${optionsArg ? ', ' + optionsArg : ''})`)
234
256
  done.add(expr)
235
257
  }
236
258
  if (done.size === 0) return null
package/src/core.js CHANGED
@@ -69,6 +69,9 @@ async function loadAta() {
69
69
  toStandaloneModule: build.toStandaloneModule,
70
70
  compiledModuleFor: canCompileAway ? build.compiledModuleFor : null,
71
71
  compiledSchemaFor: canCompileAway ? build.compiledSchemaFor : null,
72
+ // The Validator options fromCompiled() reproduces, from ata-validator
73
+ // 1.37.0; before it, only calls without options are replaced.
74
+ compiledOptions: canCompileAway && Array.isArray(build.compiledOptions) ? build.compiledOptions : [],
72
75
  }
73
76
  }
74
77