Skip to content

Commit

Permalink
refactor: Scope class to generate function scope with unique names
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 10, 2020
1 parent 9436ab2 commit 8d77a1d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 35 deletions.
2 changes: 2 additions & 0 deletions lib/compile/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Scope from "./scope"
import ucs2length from "./ucs2length"
import {toQuotedString} from "./util"
const equal = require("fast-deep-equal")
Expand Down Expand Up @@ -92,6 +93,7 @@ function compile(schema, root, localRefs, baseId) {
schemaPath: "",
errSchemaPath: "#",
errorPath: '""',
scope: new Scope(),
MissingRefError: errorClasses.MissingRef,
RULES: RULES,
validate: validateGenerator,
Expand Down
13 changes: 13 additions & 0 deletions lib/compile/scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default class Scope {
_names: {[key: string]: number}

constructor() {
this._names = {}
}

getName(prefix: string): string {
if (!this._names[prefix]) this._names[prefix] = 0
const num = this._names[prefix]++
return `${prefix}_${num}`
}
}
9 changes: 3 additions & 6 deletions lib/keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,8 @@ function ruleCode(
let out = ""
const $data = $defData && it.opts.$data && schema && schema.$data
if ($data) {
// TODO stop using it.level and maybe it.dataLevel
// schemaCode = it.getName("schema")
schemaCode = `schema${it.level}`
// TODO replace with const once it.level replaced with unique names
out += `var ${schemaCode} = ${getData(
schemaCode = it.scope.getName("schema")
out += `const ${schemaCode} = ${getData(
$data,
it.dataLevel,
it.dataPathArr
Expand All @@ -169,8 +166,8 @@ function ruleCode(
$data,
schema,
schemaCode,
scope: it.scope,
usePattern: it.usePattern,
level: it.level,
opts: it.opts,
}
// TODO check that code called "fail" or another valid way to return code
Expand Down
5 changes: 3 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Cache from "./cache"
import Scope from "./compile/scope"

interface Options {
$data?: boolean
Expand Down Expand Up @@ -103,6 +104,7 @@ export interface CompilationContext {
schemaPath: string
errorPath: string
errSchemaPath: string
scope: Scope
createErrors?: boolean // TODO maybe remove later
baseId: string
async: boolean
Expand Down Expand Up @@ -166,13 +168,12 @@ export interface KeywordContext {
fail: (condition: string) => void
write: (str: string) => void
usePattern: (str: string) => string
scope: Scope
keyword: string
data: string
$data?: string | false
schema: any
schemaCode: string | number | boolean
// TODO replace level with namespace
level: number
opts: Options
}

Expand Down
30 changes: 3 additions & 27 deletions lib/vocabularies/validation/multipleOf.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
// {{# def.definitions }}
// {{# def.errors }}
// {{# def.setupKeyword }}
// {{# def.$data }}

// {{# def.numberKeyword }}

// var division{{=$lvl}};
// if ({{?$isData}}
// {{=$schemaValue}} !== undefined && (
// typeof {{=$schemaValue}} != 'number' ||
// {{?}}
// (division{{=$lvl}} = {{=$data}} / {{=$schemaValue}},
// {{? it.opts.multipleOfPrecision }}
// Math.abs(Math.round(division{{=$lvl}}) - division{{=$lvl}}) > 1e-{{=it.opts.multipleOfPrecision}}
// {{??}}
// division{{=$lvl}} !== parseInt(division{{=$lvl}})
// {{?}}
// )
// {{?$isData}} ) {{?}} ) {
// {{# def.error:'multipleOf' }}
// } {{? $breakOnError }} else { {{?}}

import {KeywordDefinition} from "../../types"
import {appendSchema, dataNotType} from "../util"

Expand All @@ -31,15 +8,14 @@ const def: KeywordDefinition = {
type: "number",
schemaType: SCH_TYPE,
$data: true,
code({write, fail, data, $data, schemaCode, level, opts}) {
code({write, fail, scope, data, $data, schemaCode, opts}) {
const dnt = dataNotType(schemaCode, SCH_TYPE, $data)
const res = `division${level}`
const res = scope.getName("res")
const prec = opts.multipleOfPrecision
const invalid = prec
? `Math.abs(Math.round(${res}) - ${res}) > 1e-${prec}`
: `${res} !== parseInt(${res})`
// TODO replace with let
write(`var ${res};`)
write(`let ${res};`)
fail(dnt + `(${res} = ${data}/${schemaCode}, ${invalid})`)
},
error: {
Expand Down

0 comments on commit 8d77a1d

Please sign in to comment.