Skip to content

Commit

Permalink
refactor: change code nodes from interfaces to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Sep 28, 2020
1 parent 837930f commit f20fb44
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 312 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
"@typescript-eslint/no-floating-promises": "off",
"@typescript-eslint/no-implied-eval": "off",
"@typescript-eslint/no-invalid-this": "off",
"@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/no-unnecessary-condition": "warn",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
Expand Down
2 changes: 1 addition & 1 deletion lib/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function requiredOptions(o: Options): RequiredInstanceOptions {
const strict = o.strict ?? true
const strictLog = strict ? "log" : false
const _optz = o.code?.optimize
const optimize = _optz === true || _optz === undefined ? 2 : _optz || 0
const optimize = _optz === true || _optz === undefined ? 1 : _optz || 0
return {
strict,
strictTypes: o.strictTypes ?? strictLog,
Expand Down
29 changes: 3 additions & 26 deletions lib/compile/codegen/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,9 @@ export function str(strs: TemplateStringsArray, ...args: (CodeArg | string[])[])
}

export function addCodeArg(code: CodeItem[], arg: CodeArg | string[]): void {
if (arg instanceof _CodeOrName) {
if (arg instanceof Name) code.push(arg)
else code.push(...arg._items)
} else {
code.push(interpolate(arg))
}
if (arg instanceof _Code) code.push(...arg._items)
else if (arg instanceof Name) code.push(arg)
else code.push(interpolate(arg))
}

function optimize(expr: CodeItem[]): void {
Expand Down Expand Up @@ -139,26 +136,6 @@ function mergeExprItems(a: CodeItem, b: CodeItem): CodeItem | undefined {
return
}

export function updateUsedNames(
src: Code | {names?: UsedNames},
names: UsedNames,
inc: 1 | -1 = 1
): void {
if (src instanceof Name) {
const n = src.str
names[n] = (names[n] || 0) + inc
} else if (src.names) {
for (const n in src.names) {
names[n] = (names[n] || 0) + inc * (src.names[n] || 0)
}
}
}

export function usedNames(e?: SafeExpr): UsedNames | undefined {
if (e instanceof _CodeOrName) return e.names
return undefined
}

export function strConcat(c1: Code, c2: Code): Code {
return c2.emptyStr() ? c1 : c1.emptyStr() ? c2 : str`${c1}${c2}`
}
Expand Down
Loading

0 comments on commit f20fb44

Please sign in to comment.