json-schema-to-typescript
Advanced tools
Comparing version 9.0.1 to 9.1.0
# Changelog | ||
## 9.1.0 | ||
- d88a514 Improve deduping logic for `anyOf` (#273) | ||
- 8f3f101 Bugfixes for CLI | ||
- d0ad44b Improve normalizer performance (#286) | ||
## 9.0.0 | ||
@@ -4,0 +10,0 @@ |
@@ -169,3 +169,3 @@ #!/usr/bin/env node | ||
case 2: | ||
outputPath = utils_1.pathTransform(argOut, file).replace('.json', '.d.ts'); | ||
outputPath = utils_1.pathTransform(argOut, argIn, file); | ||
_b = [file]; | ||
@@ -172,0 +172,0 @@ return [4 /*yield*/, processFile(file, argv)]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var cli_color_1 = require("cli-color"); | ||
var stringify = require("json-stringify-safe"); | ||
var lodash_1 = require("lodash"); | ||
@@ -46,4 +45,4 @@ var utils_1 = require("./utils"); | ||
}); | ||
rules.set('Default top level `id`', function (schema, rootSchema, fileName) { | ||
if (!schema.id && stringify(schema) === stringify(rootSchema)) { | ||
rules.set('Default top level `id`', function (schema, _rootSchema, fileName, _options, isRoot) { | ||
if (isRoot && !schema.id) { | ||
schema.id = utils_1.toSafeString(utils_1.justName(fileName)); | ||
@@ -103,3 +102,3 @@ } | ||
rules.forEach(function (rule, key) { | ||
utils_1.traverse(_schema, function (schema) { return rule(schema, _schema, filename, options); }); | ||
utils_1.traverse(_schema, function (schema, isRoot) { return rule(schema, _schema, filename, options, isRoot); }, true); | ||
utils_1.log(cli_color_1.whiteBright.bgYellow('normalizer'), "Applied rule: \"" + key + "\""); | ||
@@ -106,0 +105,0 @@ }); |
@@ -28,3 +28,4 @@ "use strict"; | ||
// [A, B, B] -> [A, B] | ||
ast.params = lodash_1.uniqBy(ast.params, function (_) { return _.type + "------" + stringify(_.params); }); | ||
var shouldTakeStandaloneNameIntoAccount_1 = ast.params.filter(function (_) { return _.standaloneName; }).length > 1; | ||
ast.params = lodash_1.uniqBy(ast.params, function (_) { return "\n " + _.type + "-\n " + (shouldTakeStandaloneNameIntoAccount_1 ? _.standaloneName : '') + "-\n " + stringify(_.params) + "\n "; }); | ||
return Object.assign(ast, { | ||
@@ -31,0 +32,0 @@ params: ast.params.map(function (_) { return optimize(_, processed); }) |
@@ -10,3 +10,3 @@ import { JSONSchema } from './types/JSONSchema'; | ||
export declare function mapDeep(object: object, fn: (value: object, key?: string) => object, key?: string): object; | ||
export declare function traverse(schema: JSONSchema, callback: (schema: JSONSchema) => void): void; | ||
export declare function traverse(schema: JSONSchema, callback: (schema: JSONSchema, isRoot: boolean) => void, isRoot: boolean): void; | ||
/** | ||
@@ -32,2 +32,2 @@ * Eg. `foo/bar/baz.json` => `baz` | ||
export declare function escapeBlockComment(schema: JSONSchema): void; | ||
export declare function pathTransform(o: string, i: string): string; | ||
export declare function pathTransform(outputPath: string, inputPath: string, filePath: string): string; |
@@ -91,3 +91,3 @@ "use strict"; | ||
if (obj[k] && typeof obj[k] === 'object' && !Array.isArray(obj[k])) { | ||
traverse(obj[k], callback); | ||
traverse(obj[k], callback, false); | ||
} | ||
@@ -97,6 +97,6 @@ }); | ||
function traverseArray(arr, callback) { | ||
arr.forEach(function (i) { return traverse(i, callback); }); | ||
arr.forEach(function (i) { return traverse(i, callback, false); }); | ||
} | ||
function traverse(schema, callback) { | ||
callback(schema); | ||
function traverse(schema, callback, isRoot) { | ||
callback(schema, isRoot); | ||
if (schema.anyOf) { | ||
@@ -118,3 +118,3 @@ traverseArray(schema.anyOf, callback); | ||
if (schema.additionalProperties && typeof schema.additionalProperties === 'object') { | ||
traverse(schema.additionalProperties, callback); | ||
traverse(schema.additionalProperties, callback, false); | ||
} | ||
@@ -127,7 +127,7 @@ if (schema.items) { | ||
else { | ||
traverse(items, callback); | ||
traverse(items, callback, false); | ||
} | ||
} | ||
if (schema.additionalItems && typeof schema.additionalItems === 'object') { | ||
traverse(schema.additionalItems, callback); | ||
traverse(schema.additionalItems, callback, false); | ||
} | ||
@@ -141,3 +141,3 @@ if (schema.dependencies) { | ||
if (schema.not) { | ||
traverse(schema.not, callback); | ||
traverse(schema.not, callback, false); | ||
} | ||
@@ -259,12 +259,9 @@ // technically you can put definitions on any key | ||
*/ | ||
function pathTransform(o, i) { | ||
var outPathList = o.split('/'); | ||
var inPathList = i.split('/'); | ||
var intersection = outPathList.filter(function (x) { return inPathList.includes(x); }); | ||
var symmetricDifference = outPathList | ||
.filter(function (x) { return !inPathList.includes(x); }) | ||
.concat(inPathList.filter(function (x) { return !outPathList.includes(x); })); | ||
return path_1.join.apply(void 0, __spreadArrays(intersection, symmetricDifference)); | ||
function pathTransform(outputPath, inputPath, filePath) { | ||
var inPathList = path_1.normalize(inputPath).split(path_1.sep); | ||
var filePathList = path_1.dirname(path_1.normalize(filePath)).split(path_1.sep); | ||
var filePathRel = filePathList.filter(function (f, i) { return f !== inPathList[i]; }); | ||
return path_1.join.apply(void 0, __spreadArrays([path_1.normalize(outputPath)], filePathRel)); | ||
} | ||
exports.pathTransform = pathTransform; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "json-schema-to-typescript", | ||
"version": "9.0.1", | ||
"version": "9.1.0", | ||
"description": "compile json schema to typescript typings", | ||
@@ -20,3 +20,3 @@ "main": "dist/src/index.js", | ||
"tdd": "concurrently -r -p '' -k 'npm run watch' 'npm run watch:test'", | ||
"test": "ava -c 1 --timeout=300s --verbose", | ||
"test": "ava --timeout=300s --verbose", | ||
"stresstest": "seq 1 10 | xargs -I{} npm test", | ||
@@ -23,0 +23,0 @@ "prepublishOnly": "npm test", |
@@ -99,3 +99,3 @@ #!/usr/bin/env node | ||
} else { | ||
const outputPath = pathTransform(argOut, file).replace('.json', '.d.ts') | ||
const outputPath = pathTransform(argOut, argIn, file) | ||
return [file, await processFile(file, argv), outputPath] as const | ||
@@ -102,0 +102,0 @@ } |
import {whiteBright} from 'cli-color' | ||
import stringify = require('json-stringify-safe') | ||
import {cloneDeep} from 'lodash' | ||
@@ -8,3 +7,3 @@ import {JSONSchema, JSONSchemaTypeName, NormalizedJSONSchema} from './types/JSONSchema' | ||
type Rule = (schema: JSONSchema, rootSchema: JSONSchema, fileName: string, options: Options) => void | ||
type Rule = (schema: JSONSchema, rootSchema: JSONSchema, fileName: string, options: Options, isRoot: boolean) => void | ||
const rules = new Map<string, Rule>() | ||
@@ -58,4 +57,4 @@ | ||
rules.set('Default top level `id`', (schema, rootSchema, fileName) => { | ||
if (!schema.id && stringify(schema) === stringify(rootSchema)) { | ||
rules.set('Default top level `id`', (schema, _rootSchema, fileName, _options, isRoot) => { | ||
if (isRoot && !schema.id) { | ||
schema.id = toSafeString(justName(fileName)) | ||
@@ -123,3 +122,3 @@ } | ||
rules.forEach((rule, key) => { | ||
traverse(_schema, schema => rule(schema, _schema, filename, options)) | ||
traverse(_schema, (schema, isRoot) => rule(schema, _schema, filename, options, isRoot), true) | ||
log(whiteBright.bgYellow('normalizer'), `Applied rule: "${key}"`) | ||
@@ -126,0 +125,0 @@ }) |
@@ -30,3 +30,11 @@ import {whiteBright} from 'cli-color' | ||
// [A, B, B] -> [A, B] | ||
ast.params = uniqBy(ast.params, _ => `${_.type}------${stringify((_ as any).params)}`) | ||
const shouldTakeStandaloneNameIntoAccount = ast.params.filter(_ => _.standaloneName).length > 1 | ||
ast.params = uniqBy( | ||
ast.params, | ||
_ => ` | ||
${_.type}- | ||
${shouldTakeStandaloneNameIntoAccount ? _.standaloneName : ''}- | ||
${stringify((_ as any).params)} | ||
` | ||
) | ||
@@ -33,0 +41,0 @@ return Object.assign(ast, { |
@@ -273,3 +273,7 @@ import {whiteBright} from 'cli-color' | ||
*/ | ||
function standaloneName(schema: JSONSchema, keyNameFromDefinition: string | undefined, usedNames: UsedNames) { | ||
function standaloneName( | ||
schema: JSONSchema, | ||
keyNameFromDefinition: string | undefined, | ||
usedNames: UsedNames | ||
): string | undefined { | ||
const name = schema.title || schema.id || keyNameFromDefinition | ||
@@ -276,0 +280,0 @@ if (name) { |
import {whiteBright} from 'cli-color' | ||
import {deburr, isPlainObject, mapValues, trim, upperFirst} from 'lodash' | ||
import {basename, extname, join} from 'path' | ||
import {basename, dirname, extname, join, normalize, sep} from 'path' | ||
import {JSONSchema} from './types/JSONSchema' | ||
@@ -80,14 +80,18 @@ | ||
]) | ||
function traverseObjectKeys(obj: Record<string, JSONSchema>, callback: (schema: JSONSchema) => void) { | ||
function traverseObjectKeys(obj: Record<string, JSONSchema>, callback: (schema: JSONSchema, isRoot: boolean) => void) { | ||
Object.keys(obj).forEach(k => { | ||
if (obj[k] && typeof obj[k] === 'object' && !Array.isArray(obj[k])) { | ||
traverse(obj[k], callback) | ||
traverse(obj[k], callback, false) | ||
} | ||
}) | ||
} | ||
function traverseArray(arr: JSONSchema[], callback: (schema: JSONSchema) => void) { | ||
arr.forEach(i => traverse(i, callback)) | ||
function traverseArray(arr: JSONSchema[], callback: (schema: JSONSchema, isRoot: boolean) => void) { | ||
arr.forEach(i => traverse(i, callback, false)) | ||
} | ||
export function traverse(schema: JSONSchema, callback: (schema: JSONSchema) => void): void { | ||
callback(schema) | ||
export function traverse( | ||
schema: JSONSchema, | ||
callback: (schema: JSONSchema, isRoot: boolean) => void, | ||
isRoot: boolean | ||
): void { | ||
callback(schema, isRoot) | ||
@@ -110,3 +114,3 @@ if (schema.anyOf) { | ||
if (schema.additionalProperties && typeof schema.additionalProperties === 'object') { | ||
traverse(schema.additionalProperties, callback) | ||
traverse(schema.additionalProperties, callback, false) | ||
} | ||
@@ -118,7 +122,7 @@ if (schema.items) { | ||
} else { | ||
traverse(items, callback) | ||
traverse(items, callback, false) | ||
} | ||
} | ||
if (schema.additionalItems && typeof schema.additionalItems === 'object') { | ||
traverse(schema.additionalItems, callback) | ||
traverse(schema.additionalItems, callback, false) | ||
} | ||
@@ -132,3 +136,3 @@ if (schema.dependencies) { | ||
if (schema.not) { | ||
traverse(schema.not, callback) | ||
traverse(schema.not, callback, false) | ||
} | ||
@@ -245,12 +249,8 @@ | ||
*/ | ||
export function pathTransform(o: string, i: string): string { | ||
const outPathList = o.split('/') | ||
const inPathList = i.split('/') | ||
export function pathTransform(outputPath: string, inputPath: string, filePath: string): string { | ||
const inPathList = normalize(inputPath).split(sep) | ||
const filePathList = dirname(normalize(filePath)).split(sep) | ||
const filePathRel = filePathList.filter((f, i) => f !== inPathList[i]) | ||
const intersection = outPathList.filter(x => inPathList.includes(x)) | ||
const symmetricDifference = outPathList | ||
.filter(x => !inPathList.includes(x)) | ||
.concat(inPathList.filter(x => !outPathList.includes(x))) | ||
return join(...intersection, ...symmetricDifference) | ||
return join(normalize(outputPath), ...filePathRel) | ||
} |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1602084
53
39371