From 3f72238bdbe444b703c96957f442758affddfea3 Mon Sep 17 00:00:00 2001 From: aerialist7 Date: Mon, 13 Jun 2022 17:45:47 +0400 Subject: [PATCH 1/7] Esmification --- .eslintrc.yml | 2 + lib/bundle.js | 10 +- lib/dereference.js | 11 +- lib/index.js | 48 +++-- lib/normalize-args.js | 6 +- lib/options.js | 17 +- lib/parse.js | 11 +- lib/parsers/binary.js | 4 +- lib/parsers/json.js | 6 +- lib/parsers/text.js | 6 +- lib/parsers/yaml.js | 8 +- lib/pointer.js | 9 +- lib/ref.js | 14 +- lib/refs.js | 10 +- lib/resolve-external.js | 14 +- lib/resolvers/file.js | 12 +- lib/resolvers/http.js | 9 +- lib/util/errors.js | 52 +++-- lib/util/plugins.js | 246 ++++++++++----------- lib/util/url.js | 468 ++++++++++++++++++++-------------------- package.json | 1 + 21 files changed, 469 insertions(+), 495 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 8ce416a4..1db9efd1 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -4,6 +4,8 @@ root: true extends: "@jsdevtools" +parserOptions: + sourceType: module env: node: true browser: true diff --git a/lib/bundle.js b/lib/bundle.js index d5578bf1..c4a21444 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -1,10 +1,8 @@ -"use strict"; +import $Ref from "./ref"; +import Pointer from "./pointer"; +import url from "./util/url"; -const $Ref = require("./ref"); -const Pointer = require("./pointer"); -const url = require("./util/url"); - -module.exports = bundle; +export default bundle; /** * Bundles all external JSON references into the main JSON schema, thus resulting in a schema that diff --git a/lib/dereference.js b/lib/dereference.js index db63d4c3..c81c5b2e 100644 --- a/lib/dereference.js +++ b/lib/dereference.js @@ -1,11 +1,10 @@ -"use strict"; +import $Ref from "./ref"; +import Pointer from "./pointer"; +import { ono } from "@jsdevtools/ono"; -const $Ref = require("./ref"); -const Pointer = require("./pointer"); -const { ono } = require("@jsdevtools/ono"); -const url = require("./util/url"); +import url from "./util/url"; -module.exports = dereference; +export default dereference; /** * Crawls the JSON schema, finds all JSON references, and dereferences them. diff --git a/lib/index.js b/lib/index.js index fb5d5f32..ddc26194 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,26 +1,34 @@ /* eslint-disable no-unused-vars */ -"use strict"; +import $Refs from "./refs"; +import _parse from "./parse"; +import normalizeArgs from "./normalize-args"; +import resolveExternal from "./resolve-external"; +import _bundle from "./bundle"; +import _dereference from "./dereference"; +import url from "./util/url"; +import { + InvalidPointerError, + isHandledError, + JSONParserError, + JSONParserErrorGroup, + MissingPointerError, + ParserError, + ResolverError, + UnmatchedParserError, + UnmatchedResolverError +} from "./util/errors"; -const $Refs = require("./refs"); -const _parse = require("./parse"); -const normalizeArgs = require("./normalize-args"); -const resolveExternal = require("./resolve-external"); -const _bundle = require("./bundle"); -const _dereference = require("./dereference"); -const url = require("./util/url"); -const { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, JSONParserErrorGroup } = require("./util/errors"); -const maybe = require("call-me-maybe"); -const { ono } = require("@jsdevtools/ono"); +import maybe from "call-me-maybe"; +import { ono } from "@jsdevtools/ono"; -module.exports = $RefParser; -module.exports.default = $RefParser; -module.exports.JSONParserError = JSONParserError; -module.exports.InvalidPointerError = InvalidPointerError; -module.exports.MissingPointerError = MissingPointerError; -module.exports.ResolverError = ResolverError; -module.exports.ParserError = ParserError; -module.exports.UnmatchedParserError = UnmatchedParserError; -module.exports.UnmatchedResolverError = UnmatchedResolverError; +export default $RefParser; +export { JSONParserError }; +export { InvalidPointerError }; +export { MissingPointerError }; +export { ResolverError }; +export { ParserError }; +export { UnmatchedParserError }; +export { UnmatchedResolverError }; /** * This class parses a JSON schema, builds a map of its JSON references and their resolved values, diff --git a/lib/normalize-args.js b/lib/normalize-args.js index 1d48fe4d..1fafcdbb 100644 --- a/lib/normalize-args.js +++ b/lib/normalize-args.js @@ -1,8 +1,6 @@ -"use strict"; +import Options from "./options"; -const Options = require("./options"); - -module.exports = normalizeArgs; +export default normalizeArgs; /** * Normalizes the given arguments, accounting for optional args. diff --git a/lib/options.js b/lib/options.js index 199362a6..a3c8dada 100644 --- a/lib/options.js +++ b/lib/options.js @@ -1,14 +1,11 @@ -/* eslint lines-around-comment: [2, {beforeBlockComment: false}] */ -"use strict"; +import jsonParser from "./parsers/json"; +import yamlParser from "./parsers/yaml"; +import textParser from "./parsers/text"; +import binaryParser from "./parsers/binary"; +import fileResolver from "./resolvers/file"; +import httpResolver from "./resolvers/http"; -const jsonParser = require("./parsers/json"); -const yamlParser = require("./parsers/yaml"); -const textParser = require("./parsers/text"); -const binaryParser = require("./parsers/binary"); -const fileResolver = require("./resolvers/file"); -const httpResolver = require("./resolvers/http"); - -module.exports = $RefParserOptions; +export default $RefParserOptions; /** * Options that determine how JSON schemas are parsed, resolved, and dereferenced. diff --git a/lib/parse.js b/lib/parse.js index b9160df6..b997b5ef 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1,11 +1,10 @@ -"use strict"; +import { ono } from "@jsdevtools/ono"; -const { ono } = require("@jsdevtools/ono"); -const url = require("./util/url"); -const plugins = require("./util/plugins"); -const { ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } = require("./util/errors"); +import url from "./util/url"; +import plugins from "./util/plugins"; +import { isHandledError, ParserError, ResolverError, UnmatchedParserError, UnmatchedResolverError } from "./util/errors"; -module.exports = parse; +export default parse; /** * Reads and parses the specified file path or URL. diff --git a/lib/parsers/binary.js b/lib/parsers/binary.js index be15073c..141fc3a8 100644 --- a/lib/parsers/binary.js +++ b/lib/parsers/binary.js @@ -1,8 +1,6 @@ -"use strict"; - let BINARY_REGEXP = /\.(jpeg|jpg|gif|png|bmp|ico)$/i; -module.exports = { +export default { /** * The order that this parser will run, in relation to other parsers. * diff --git a/lib/parsers/json.js b/lib/parsers/json.js index 34dd986f..0ed9b76f 100644 --- a/lib/parsers/json.js +++ b/lib/parsers/json.js @@ -1,8 +1,6 @@ -"use strict"; +import { ParserError } from "../util/errors"; -const { ParserError } = require("../util/errors"); - -module.exports = { +export default { /** * The order that this parser will run, in relation to other parsers. * diff --git a/lib/parsers/text.js b/lib/parsers/text.js index 03bcd96b..f235c97a 100644 --- a/lib/parsers/text.js +++ b/lib/parsers/text.js @@ -1,10 +1,8 @@ -"use strict"; - -const { ParserError } = require("../util/errors"); +import { ParserError } from "../util/errors"; let TEXT_REGEXP = /\.(txt|htm|html|md|xml|js|min|map|css|scss|less|svg)$/i; -module.exports = { +export default { /** * The order that this parser will run, in relation to other parsers. * diff --git a/lib/parsers/yaml.js b/lib/parsers/yaml.js index 94c4eca0..f2307a86 100644 --- a/lib/parsers/yaml.js +++ b/lib/parsers/yaml.js @@ -1,10 +1,8 @@ -"use strict"; +import { ParserError } from "../util/errors"; -const { ParserError } = require("../util/errors"); -const yaml = require("js-yaml"); -const { JSON_SCHEMA } = require("js-yaml"); +import yaml, { JSON_SCHEMA } from "js-yaml"; -module.exports = { +export default { /** * The order that this parser will run, in relation to other parsers. * diff --git a/lib/pointer.js b/lib/pointer.js index 299b2215..b8344ac7 100644 --- a/lib/pointer.js +++ b/lib/pointer.js @@ -1,10 +1,9 @@ -"use strict"; +import $Ref from "./ref"; +import url from "./util/url"; +import { InvalidPointerError, isHandledError, JSONParserError, MissingPointerError } from "./util/errors"; -module.exports = Pointer; +export default Pointer; -const $Ref = require("./ref"); -const url = require("./util/url"); -const { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } = require("./util/errors"); const slashes = /\//g; const tildes = /~/g; const escapedSlash = /~1/g; diff --git a/lib/ref.js b/lib/ref.js index b6693795..b8d6fec0 100644 --- a/lib/ref.js +++ b/lib/ref.js @@ -1,10 +1,8 @@ -"use strict"; +import Pointer from "./pointer"; +import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors"; +import url from "./util/url"; -module.exports = $Ref; - -const Pointer = require("./pointer"); -const { InvalidPointerError, isHandledError, normalizeError } = require("./util/errors"); -const { safePointerToPath, stripHash, getHash } = require("./util/url"); +export default $Ref; /** * This class represents a single JSON reference and its resolved value. @@ -129,13 +127,13 @@ $Ref.prototype.resolve = function (path, options, friendlyPath, pathFromRoot) { } if (err.path === null) { - err.path = safePointerToPath(getHash(pathFromRoot)); + err.path = url.safePointerToPath(url.getHash(pathFromRoot)); } if (err instanceof InvalidPointerError) { // this is a special case - InvalidPointerError is thrown when dereferencing external file, // but the issue is caused by the source file that referenced the file that undergoes dereferencing - err.source = decodeURI(stripHash(pathFromRoot)); + err.source = decodeURI(url.stripHash(pathFromRoot)); } this.addError(err); diff --git a/lib/refs.js b/lib/refs.js index 82e37841..2cff7e6a 100644 --- a/lib/refs.js +++ b/lib/refs.js @@ -1,13 +1,13 @@ -"use strict"; +import { ono } from "@jsdevtools/ono"; -const { ono } = require("@jsdevtools/ono"); -const $Ref = require("./ref"); -const url = require("./util/url"); +import $Ref from "./ref"; +import url from "./util/url"; const isWindows = /^win/.test(globalThis.process?.platform); const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath; -module.exports = $Refs; +export default $Refs; + /** * This class is a map of JSON references and their resolved values. diff --git a/lib/resolve-external.js b/lib/resolve-external.js index 9af29bb2..af192fda 100644 --- a/lib/resolve-external.js +++ b/lib/resolve-external.js @@ -1,12 +1,10 @@ -"use strict"; +import $Ref from "./ref"; +import Pointer from "./pointer"; +import parse from "./parse"; +import url from "./util/url"; +import { isHandledError } from "./util/errors"; -const $Ref = require("./ref"); -const Pointer = require("./pointer"); -const parse = require("./parse"); -const url = require("./util/url"); -const { isHandledError } = require("./util/errors"); - -module.exports = resolveExternal; +export default resolveExternal; /** * Crawls the JSON schema, finds all external JSON references, and resolves their values. diff --git a/lib/resolvers/file.js b/lib/resolvers/file.js index 45577ad7..8a38eb88 100644 --- a/lib/resolvers/file.js +++ b/lib/resolvers/file.js @@ -1,10 +1,10 @@ -"use strict"; -const fs = require("fs"); -const { ono } = require("@jsdevtools/ono"); -const url = require("../util/url"); -const { ResolverError } = require("../util/errors"); +import fs from "fs"; +import { ono } from "@jsdevtools/ono"; -module.exports = { +import url from "../util/url"; +import { ResolverError } from "../util/errors"; + +export default { /** * The order that this resolver will run, in relation to other resolvers. * diff --git a/lib/resolvers/http.js b/lib/resolvers/http.js index 7e5f10d8..2c4041a9 100644 --- a/lib/resolvers/http.js +++ b/lib/resolvers/http.js @@ -1,10 +1,9 @@ -"use strict"; -const { ono } = require("@jsdevtools/ono"); -const url = require("../util/url"); -const { ResolverError } = require("../util/errors"); +import { ono } from "@jsdevtools/ono"; +import url from "../util/url"; +import { ResolverError } from "../util/errors"; -module.exports = { +export default { /** * The order that this resolver will run, in relation to other resolvers. * diff --git a/lib/util/errors.js b/lib/util/errors.js index 4cdb1c0a..3201f33a 100644 --- a/lib/util/errors.js +++ b/lib/util/errors.js @@ -1,10 +1,8 @@ -"use strict"; +import { Ono } from "@jsdevtools/ono"; -const { Ono } = require("@jsdevtools/ono"); +import url from "./url"; -const { stripHash, toFileSystemPath } = require("./url"); - -const JSONParserError = exports.JSONParserError = class JSONParserError extends Error { +export class JSONParserError extends Error { constructor (message, source) { super(); @@ -19,16 +17,16 @@ const JSONParserError = exports.JSONParserError = class JSONParserError extends get footprint () { return `${this.path}+${this.source}+${this.code}+${this.message}`; } -}; +} setErrorName(JSONParserError); -const JSONParserErrorGroup = exports.JSONParserErrorGroup = class JSONParserErrorGroup extends Error { +export class JSONParserErrorGroup extends Error { constructor (parser) { super(); this.files = parser; - this.message = `${this.errors.length} error${this.errors.length > 1 ? "s" : ""} occurred while reading '${toFileSystemPath(parser.$refs._root$Ref.path)}'`; + this.message = `${this.errors.length} error${this.errors.length > 1 ? "s" : ""} occurred while reading '${url.toFileSystemPath(parser.$refs._root$Ref.path)}'`; Ono.extend(this); } @@ -48,31 +46,31 @@ const JSONParserErrorGroup = exports.JSONParserErrorGroup = class JSONParserErro get errors () { return JSONParserErrorGroup.getParserErrors(this.files); } -}; +} setErrorName(JSONParserErrorGroup); -const ParserError = exports.ParserError = class ParserError extends JSONParserError { +export class ParserError extends JSONParserError { constructor (message, source) { super(`Error parsing ${source}: ${message}`, source); this.code = "EPARSER"; } -}; +} setErrorName(ParserError); -const UnmatchedParserError = exports.UnmatchedParserError = class UnmatchedParserError extends JSONParserError { +export class UnmatchedParserError extends JSONParserError { constructor (source) { super(`Could not find parser for "${source}"`, source); this.code = "EUNMATCHEDPARSER"; } -}; +} setErrorName(UnmatchedParserError); -const ResolverError = exports.ResolverError = class ResolverError extends JSONParserError { +export class ResolverError extends JSONParserError { constructor (ex, source) { super(ex.message || `Error reading file "${source}"`, source); @@ -82,37 +80,37 @@ const ResolverError = exports.ResolverError = class ResolverError extends JSONPa this.ioErrorCode = String(ex.code); } } -}; +} setErrorName(ResolverError); -const UnmatchedResolverError = exports.UnmatchedResolverError = class UnmatchedResolverError extends JSONParserError { +export class UnmatchedResolverError extends JSONParserError { constructor (source) { super(`Could not find resolver for "${source}"`, source); this.code = "EUNMATCHEDRESOLVER"; } -}; +} setErrorName(UnmatchedResolverError); -const MissingPointerError = exports.MissingPointerError = class MissingPointerError extends JSONParserError { +export class MissingPointerError extends JSONParserError { constructor (token, path) { - super(`Token "${token}" does not exist.`, stripHash(path)); + super(`Token "${token}" does not exist.`, url.stripHash(path)); this.code = "EMISSINGPOINTER"; } -}; +} setErrorName(MissingPointerError); -const InvalidPointerError = exports.InvalidPointerError = class InvalidPointerError extends JSONParserError { +export class InvalidPointerError extends JSONParserError { constructor (pointer, path) { - super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, stripHash(path)); + super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, url.stripHash(path)); this.code = "EINVALIDPOINTER"; } -}; +} setErrorName(InvalidPointerError); @@ -123,14 +121,14 @@ function setErrorName (err) { }); } -exports.isHandledError = function (err) { +export function isHandledError (err) { return err instanceof JSONParserError || err instanceof JSONParserErrorGroup; -}; +} -exports.normalizeError = function (err) { +export function normalizeError (err) { if (err.path === null) { err.path = []; } return err; -}; +} diff --git a/lib/util/plugins.js b/lib/util/plugins.js index 24d69349..69352b6f 100644 --- a/lib/util/plugins.js +++ b/lib/util/plugins.js @@ -1,126 +1,3 @@ -"use strict"; - -/** - * Returns the given plugins as an array, rather than an object map. - * All other methods in this module expect an array of plugins rather than an object map. - * - * @param {object} plugins - A map of plugin objects - * @return {object[]} - */ -exports.all = function (plugins) { - return Object.keys(plugins) - .filter((key) => { - return typeof plugins[key] === "object"; - }) - .map((key) => { - plugins[key].name = key; - return plugins[key]; - }); -}; - -/** - * Filters the given plugins, returning only the ones return `true` for the given method. - * - * @param {object[]} plugins - An array of plugin objects - * @param {string} method - The name of the filter method to invoke for each plugin - * @param {object} file - A file info object, which will be passed to each method - * @return {object[]} - */ -exports.filter = function (plugins, method, file) { - return plugins - .filter((plugin) => { - return !!getResult(plugin, method, file); - }); -}; - -/** - * Sorts the given plugins, in place, by their `order` property. - * - * @param {object[]} plugins - An array of plugin objects - * @returns {object[]} - */ -exports.sort = function (plugins) { - for (let plugin of plugins) { - plugin.order = plugin.order || Number.MAX_SAFE_INTEGER; - } - - return plugins.sort((a, b) => { return a.order - b.order; }); -}; - -/** - * Runs the specified method of the given plugins, in order, until one of them returns a successful result. - * Each method can return a synchronous value, a Promise, or call an error-first callback. - * If the promise resolves successfully, or the callback is called without an error, then the result - * is immediately returned and no further plugins are called. - * If the promise rejects, or the callback is called with an error, then the next plugin is called. - * If ALL plugins fail, then the last error is thrown. - * - * @param {object[]} plugins - An array of plugin objects - * @param {string} method - The name of the method to invoke for each plugin - * @param {object} file - A file info object, which will be passed to each method - * @returns {Promise} - */ -exports.run = function (plugins, method, file, $refs) { - let plugin, lastError, index = 0; - - return new Promise(((resolve, reject) => { - runNextPlugin(); - - function runNextPlugin () { - plugin = plugins[index++]; - if (!plugin) { - // There are no more functions, so re-throw the last error - return reject(lastError); - } - - try { - // console.log(' %s', plugin.name); - let result = getResult(plugin, method, file, callback, $refs); - if (result && typeof result.then === "function") { - // A promise was returned - result.then(onSuccess, onError); - } - else if (result !== undefined) { - // A synchronous result was returned - onSuccess(result); - } - else if (index === plugins.length) { - throw new Error("No promise has been returned or callback has been called."); - } - } - catch (e) { - onError(e); - } - } - - function callback (err, result) { - if (err) { - onError(err); - } - else { - onSuccess(result); - } - } - - function onSuccess (result) { - // console.log(' success'); - resolve({ - plugin, - result - }); - } - - function onError (error) { - // console.log(' %s', err.message || err); - lastError = { - plugin, - error, - }; - runNextPlugin(); - } - })); -}; - /** * Returns the value of the given property. * If the property is a function, then the result of the function is returned. @@ -157,3 +34,126 @@ function getResult (obj, prop, file, callback, $refs) { return value; } + +export default { + /** + * Returns the given plugins as an array, rather than an object map. + * All other methods in this module expect an array of plugins rather than an object map. + * + * @param {object} plugins - A map of plugin objects + * @return {object[]} + */ + all (plugins) { + return Object.keys(plugins) + .filter((key) => { + return typeof plugins[key] === "object"; + }) + .map((key) => { + plugins[key].name = key; + return plugins[key]; + }); + }, + + /** + * Filters the given plugins, returning only the ones return `true` for the given method. + * + * @param {object[]} plugins - An array of plugin objects + * @param {string} method - The name of the filter method to invoke for each plugin + * @param {object} file - A file info object, which will be passed to each method + * @return {object[]} + */ + filter (plugins, method, file) { + return plugins + .filter((plugin) => { + return !!getResult(plugin, method, file); + }); + }, + + /** + * Sorts the given plugins, in place, by their `order` property. + * + * @param {object[]} plugins - An array of plugin objects + * @returns {object[]} + */ + sort (plugins) { + for (let plugin of plugins) { + plugin.order = plugin.order || Number.MAX_SAFE_INTEGER; + } + + return plugins.sort((a, b) => { return a.order - b.order; }); + }, + + /** + * Runs the specified method of the given plugins, in order, until one of them returns a successful result. + * Each method can return a synchronous value, a Promise, or call an error-first callback. + * If the promise resolves successfully, or the callback is called without an error, then the result + * is immediately returned and no further plugins are called. + * If the promise rejects, or the callback is called with an error, then the next plugin is called. + * If ALL plugins fail, then the last error is thrown. + * + * @param {object[]} plugins - An array of plugin objects + * @param {string} method - The name of the method to invoke for each plugin + * @param {object} file - A file info object, which will be passed to each method + * @returns {Promise} + */ + run (plugins, method, file, $refs) { + let plugin, lastError, index = 0; + + return new Promise(((resolve, reject) => { + runNextPlugin(); + + function runNextPlugin () { + plugin = plugins[index++]; + if (!plugin) { + // There are no more functions, so re-throw the last error + return reject(lastError); + } + + try { + // console.log(' %s', plugin.name); + let result = getResult(plugin, method, file, callback, $refs); + if (result && typeof result.then === "function") { + // A promise was returned + result.then(onSuccess, onError); + } + else if (result !== undefined) { + // A synchronous result was returned + onSuccess(result); + } + else if (index === plugins.length) { + throw new Error("No promise has been returned or callback has been called."); + } + } + catch (e) { + onError(e); + } + } + + function callback (err, result) { + if (err) { + onError(err); + } + else { + onSuccess(result); + } + } + + function onSuccess (result) { + // console.log(' success'); + resolve({ + plugin, + result + }); + } + + function onError (error) { + // console.log(' %s', err.message || err); + lastError = { + plugin, + error, + }; + runNextPlugin(); + } + })); + }, +}; diff --git a/lib/util/url.js b/lib/util/url.js index 20d9621e..10957a8e 100644 --- a/lib/util/url.js +++ b/lib/util/url.js @@ -1,4 +1,4 @@ -"use strict"; +import { parse, resolve } from "url"; const nodePath = require("path"); const projectDir = nodePath.resolve(__dirname, "..", ".."); @@ -6,7 +6,6 @@ const projectDir = nodePath.resolve(__dirname, "..", ".."); let isWindows = /^win/.test(globalThis.process?.platform), forwardSlashPattern = /\//g, protocolPattern = /^(\w{2,}):\/\//i, - url = module.exports, jsonPointerSlash = /~1/g, jsonPointerTilde = /~0/g; @@ -25,271 +24,260 @@ let urlDecodePatterns = [ /\%40/g, "@" ]; -exports.parse = (u) => new URL(u); +export default { + /** + * Returns the current working directory (in Node) or the current page URL (in browsers). + * + * @returns {string} + */ + cwd () { + if (typeof window !== "undefined") { + return location.href; + } -/** - * Returns resolved target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF. - * - * @return {string} - */ -exports.resolve = function resolve (from, to) { - let resolvedUrl = new URL(to, new URL(from, "resolve://")); - if (resolvedUrl.protocol === "resolve:") { - // `from` is a relative URL. - let { pathname, search, hash } = resolvedUrl; - return pathname + search + hash; - } - return resolvedUrl.toString(); -}; + let path = process.cwd(); -/** - * Returns the current working directory (in Node) or the current page URL (in browsers). - * - * @returns {string} - */ -exports.cwd = function cwd () { - if (typeof window !== "undefined") { - return location.href; - } + let lastChar = path.slice(-1); + if (lastChar === "/" || lastChar === "\\") { + return path; + } + else { + return path + "/"; + } + }, - let path = process.cwd(); + /** + * Returns the protocol of the given URL, or `undefined` if it has no protocol. + * + * @param {string} path + * @returns {?string} + */ + getProtocol (path) { + let match = protocolPattern.exec(path); + if (match) { + return match[1].toLowerCase(); + } + }, - let lastChar = path.slice(-1); - if (lastChar === "/" || lastChar === "\\") { - return path; - } - else { - return path + "/"; - } -}; + /** + * Returns the lowercased file extension of the given URL, + * or an empty string if it has no extension. + * + * @param {string} path + * @returns {string} + */ + getExtension (path) { + let lastDot = path.lastIndexOf("."); + if (lastDot >= 0) { + return this.stripQuery(path.substr(lastDot).toLowerCase()); + } + return ""; + }, -/** - * Returns the protocol of the given URL, or `undefined` if it has no protocol. - * - * @param {string} path - * @returns {?string} - */ -exports.getProtocol = function getProtocol (path) { - let match = protocolPattern.exec(path); - if (match) { - return match[1].toLowerCase(); - } -}; + /** + * Removes the query, if any, from the given path. + * + * @param {string} path + * @returns {string} + */ + stripQuery (path) { + let queryIndex = path.indexOf("?"); + if (queryIndex >= 0) { + path = path.substr(0, queryIndex); + } + return path; + }, -/** - * Returns the lowercased file extension of the given URL, - * or an empty string if it has no extension. - * - * @param {string} path - * @returns {string} - */ -exports.getExtension = function getExtension (path) { - let lastDot = path.lastIndexOf("."); - if (lastDot >= 0) { - return url.stripQuery(path.substr(lastDot).toLowerCase()); - } - return ""; -}; + /** + * Returns the hash (URL fragment), of the given path. + * If there is no hash, then the root hash ("#") is returned. + * + * @param {string} path + * @returns {string} + */ + getHash (path) { + let hashIndex = path.indexOf("#"); + if (hashIndex >= 0) { + return path.substr(hashIndex); + } + return "#"; + }, -/** - * Removes the query, if any, from the given path. - * - * @param {string} path - * @returns {string} - */ -exports.stripQuery = function stripQuery (path) { - let queryIndex = path.indexOf("?"); - if (queryIndex >= 0) { - path = path.substr(0, queryIndex); - } - return path; -}; + /** + * Removes the hash (URL fragment), if any, from the given path. + * + * @param {string} path + * @returns {string} + */ + stripHash (path) { + let hashIndex = path.indexOf("#"); + if (hashIndex >= 0) { + path = path.substr(0, hashIndex); + } + return path; + }, -/** - * Returns the hash (URL fragment), of the given path. - * If there is no hash, then the root hash ("#") is returned. - * - * @param {string} path - * @returns {string} - */ -exports.getHash = function getHash (path) { - let hashIndex = path.indexOf("#"); - if (hashIndex >= 0) { - return path.substr(hashIndex); - } - return "#"; -}; + /** + * Determines whether the given path is an HTTP(S) URL. + * + * @param {string} path + * @returns {boolean} + */ + isHttp (path) { + let protocol = this.getProtocol(path); + if (protocol === "http" || protocol === "https") { + return true; + } + else if (protocol === undefined) { + // There is no protocol. If we're running in a browser, then assume it's HTTP. + return typeof window !== "undefined"; + } + else { + // It's some other protocol, such as "ftp://", "mongodb://", etc. + return false; + } + }, -/** - * Removes the hash (URL fragment), if any, from the given path. - * - * @param {string} path - * @returns {string} - */ -exports.stripHash = function stripHash (path) { - let hashIndex = path.indexOf("#"); - if (hashIndex >= 0) { - path = path.substr(0, hashIndex); - } - return path; -}; + /** + * Determines whether the given path is a filesystem path. + * This includes "file://" URLs. + * + * @param {string} path + * @returns {boolean} + */ + isFileSystemPath (path) { + if (typeof window !== "undefined") { + // We're running in a browser, so assume that all paths are URLs. + // This way, even relative paths will be treated as URLs rather than as filesystem paths + return false; + } -/** - * Determines whether the given path is an HTTP(S) URL. - * - * @param {string} path - * @returns {boolean} - */ -exports.isHttp = function isHttp (path) { - let protocol = url.getProtocol(path); - if (protocol === "http" || protocol === "https") { - return true; - } - else if (protocol === undefined) { - // There is no protocol. If we're running in a browser, then assume it's HTTP. - return typeof window !== "undefined"; - } - else { - // It's some other protocol, such as "ftp://", "mongodb://", etc. - return false; - } -}; + let protocol = this.getProtocol(path); + return protocol === undefined || protocol === "file"; + }, -/** - * Determines whether the given path is a filesystem path. - * This includes "file://" URLs. - * - * @param {string} path - * @returns {boolean} - */ -exports.isFileSystemPath = function isFileSystemPath (path) { - if (process.browser) { - // We're running in a browser, so assume that all paths are URLs. - // This way, even relative paths will be treated as URLs rather than as filesystem paths - return false; - } + /** + * Converts a filesystem path to a properly-encoded URL. + * + * This is intended to handle situations where JSON Schema $Ref Parser is called + * with a filesystem path that contains characters which are not allowed in URLs. + * + * @example + * The following filesystem paths would be converted to the following URLs: + * + * <"!@#$%^&*+=?'>.json ==> %3C%22!@%23$%25%5E&*+=%3F\'%3E.json + * C:\\My Documents\\File (1).json ==> C:/My%20Documents/File%20(1).json + * file://Project #42/file.json ==> file://Project%20%2342/file.json + * + * @param {string} path + * @returns {string} + */ + fromFileSystemPath (path) { + // Step 1: On Windows, replace backslashes with forward slashes, + // rather than encoding them as "%5C" + if (isWindows) { + const hasProjectDir = path.toUpperCase().includes(projectDir.replace(/\\/g, "\\").toUpperCase()); + const hasProjectUri = path.toUpperCase().includes(projectDir.replace(/\\/g, "/").toUpperCase()); + if (hasProjectDir || hasProjectUri) { + path = path.replace(/\\/g, "/"); + } + else { + path = `${projectDir}/${path}`.replace(/\\/g, "/"); + } + } - let protocol = url.getProtocol(path); - return protocol === undefined || protocol === "file"; -}; + // Step 2: `encodeURI` will take care of MOST characters + path = encodeURI(path); -/** - * Converts a filesystem path to a properly-encoded URL. - * - * This is intended to handle situations where JSON Schema $Ref Parser is called - * with a filesystem path that contains characters which are not allowed in URLs. - * - * @example - * The following filesystem paths would be converted to the following URLs: - * - * <"!@#$%^&*+=?'>.json ==> %3C%22!@%23$%25%5E&*+=%3F\'%3E.json - * C:\\My Documents\\File (1).json ==> C:/My%20Documents/File%20(1).json - * file://Project #42/file.json ==> file://Project%20%2342/file.json - * - * @param {string} path - * @returns {string} - */ -exports.fromFileSystemPath = function fromFileSystemPath (path) { - // Step 1: On Windows, replace backslashes with forward slashes, - // rather than encoding them as "%5C" - if (isWindows) { - const hasProjectDir = path.toUpperCase().includes(projectDir.replace(/\\/g, "\\").toUpperCase()); - const hasProjectUri = path.toUpperCase().includes(projectDir.replace(/\\/g, "/").toUpperCase()); - if (hasProjectDir || hasProjectUri) { - path = path.replace(/\\/g, "/"); - } - else { - path = `${projectDir}/${path}`.replace(/\\/g, "/"); + // Step 3: Manually encode characters that are not encoded by `encodeURI`. + // This includes characters such as "#" and "?", which have special meaning in URLs, + // but are just normal characters in a filesystem path. + for (let i = 0; i < urlEncodePatterns.length; i += 2) { + path = path.replace(urlEncodePatterns[i], urlEncodePatterns[i + 1]); } - } - // Step 2: `encodeURI` will take care of MOST characters - path = encodeURI(path); - - // Step 3: Manually encode characters that are not encoded by `encodeURI`. - // This includes characters such as "#" and "?", which have special meaning in URLs, - // but are just normal characters in a filesystem path. - for (let i = 0; i < urlEncodePatterns.length; i += 2) { - path = path.replace(urlEncodePatterns[i], urlEncodePatterns[i + 1]); - } + return path; + }, - return path; -}; + /** + * Converts a URL to a local filesystem path. + * + * @param {string} path + * @param {boolean} [keepFileProtocol] - If true, then "file://" will NOT be stripped + * @returns {string} + */ + toFileSystemPath (path, keepFileProtocol) { + // Step 1: `decodeURI` will decode characters such as Cyrillic characters, spaces, etc. + path = decodeURI(path); -/** - * Converts a URL to a local filesystem path. - * - * @param {string} path - * @param {boolean} [keepFileProtocol] - If true, then "file://" will NOT be stripped - * @returns {string} - */ -exports.toFileSystemPath = function toFileSystemPath (path, keepFileProtocol) { - // Step 1: `decodeURI` will decode characters such as Cyrillic characters, spaces, etc. - path = decodeURI(path); + // Step 2: Manually decode characters that are not decoded by `decodeURI`. + // This includes characters such as "#" and "?", which have special meaning in URLs, + // but are just normal characters in a filesystem path. + for (let i = 0; i < urlDecodePatterns.length; i += 2) { + path = path.replace(urlDecodePatterns[i], urlDecodePatterns[i + 1]); + } - // Step 2: Manually decode characters that are not decoded by `decodeURI`. - // This includes characters such as "#" and "?", which have special meaning in URLs, - // but are just normal characters in a filesystem path. - for (let i = 0; i < urlDecodePatterns.length; i += 2) { - path = path.replace(urlDecodePatterns[i], urlDecodePatterns[i + 1]); - } + // Step 3: If it's a "file://" URL, then format it consistently + // or convert it to a local filesystem path + let isFileUrl = path.substr(0, 7).toLowerCase() === "file://"; + if (isFileUrl) { + // Strip-off the protocol, and the initial "/", if there is one + path = path[7] === "/" ? path.substr(8) : path.substr(7); - // Step 3: If it's a "file://" URL, then format it consistently - // or convert it to a local filesystem path - let isFileUrl = path.substr(0, 7).toLowerCase() === "file://"; - if (isFileUrl) { - // Strip-off the protocol, and the initial "/", if there is one - path = path[7] === "/" ? path.substr(8) : path.substr(7); + // insert a colon (":") after the drive letter on Windows + if (isWindows && path[1] === "/") { + path = path[0] + ":" + path.substr(1); + } - // insert a colon (":") after the drive letter on Windows - if (isWindows && path[1] === "/") { - path = path[0] + ":" + path.substr(1); + if (keepFileProtocol) { + // Return the consistently-formatted "file://" URL + path = "file:///" + path; + } + else { + // Convert the "file://" URL to a local filesystem path. + // On Windows, it will start with something like "C:/". + // On Posix, it will start with "/" + isFileUrl = false; + path = isWindows ? path : "/" + path; + } } - if (keepFileProtocol) { - // Return the consistently-formatted "file://" URL - path = "file:///" + path; - } - else { - // Convert the "file://" URL to a local filesystem path. - // On Windows, it will start with something like "C:/". - // On Posix, it will start with "/" - isFileUrl = false; - path = isWindows ? path : "/" + path; + // Step 4: Normalize Windows paths (unless it's a "file://" URL) + if (isWindows && !isFileUrl) { + // Replace forward slashes with backslashes + path = path.replace(forwardSlashPattern, "\\"); + + // Capitalize the drive letter + if (path.substr(1, 2) === ":\\") { + path = path[0].toUpperCase() + path.substr(1); + } } - } - // Step 4: Normalize Windows paths (unless it's a "file://" URL) - if (isWindows && !isFileUrl) { - // Replace forward slashes with backslashes - path = path.replace(forwardSlashPattern, "\\"); + return path; + }, - // Capitalize the drive letter - if (path.substr(1, 2) === ":\\") { - path = path[0].toUpperCase() + path.substr(1); + /** + * Converts a $ref pointer to a valid JSON Path. + * + * @param {string} pointer + * @returns {Array} + */ + safePointerToPath (pointer) { + if (pointer.length <= 1 || pointer[0] !== "#" || pointer[1] !== "/") { + return []; } - } - return path; -}; + return pointer + .slice(2) + .split("/") + .map((value) => { + return decodeURIComponent(value) + .replace(jsonPointerSlash, "/") + .replace(jsonPointerTilde, "~"); + }); + }, -/** - * Converts a $ref pointer to a valid JSON Path. - * - * @param {string} pointer - * @returns {Array} - */ -exports.safePointerToPath = function safePointerToPath (pointer) { - if (pointer.length <= 1 || pointer[0] !== "#" || pointer[1] !== "/") { - return []; - } + parse: parse, - return pointer - .slice(2) - .split("/") - .map((value) => { - return decodeURIComponent(value) - .replace(jsonPointerSlash, "/") - .replace(jsonPointerTilde, "~"); - }); + resolve: resolve, }; diff --git a/package.json b/package.json index 716e811a..065b1e35 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "files": [ "lib" ], + "type": "module", "scripts": { "build": "cp LICENSE *.md dist", "clean": "shx rm -rf .nyc_output coverage", From 899add3363d668c34f6c8a9c66cff75c13498e93 Mon Sep 17 00:00:00 2001 From: aerialist7 Date: Wed, 15 Jun 2022 23:52:25 +0400 Subject: [PATCH 2/7] Esmification. Tests. Lint fix --- karma.conf.js | 2 -- lib/util/url.js | 4 ++-- test/fixtures/mocha.js | 2 -- test/fixtures/polyfill.js | 2 -- test/specs/__({[ % & $ # @ ` ~ ,)}]__/dereferenced.js | 2 -- test/specs/__({[ % & $ # @ ` ~ ,)}]__/parsed.js | 2 -- .../__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js | 2 -- test/specs/absolute-root/absolute-root.spec.js | 2 -- test/specs/absolute-root/bundled.js | 2 -- test/specs/absolute-root/dereferenced.js | 2 -- test/specs/absolute-root/parsed.js | 2 -- test/specs/blank/blank.spec.js | 2 -- test/specs/blank/dereferenced.js | 2 -- test/specs/blank/parsed.js | 2 -- test/specs/callbacks.spec.js | 2 -- test/specs/circular-extended/bundled.js | 2 -- test/specs/circular-extended/circular-extended.spec.js | 2 -- test/specs/circular-extended/dereferenced.js | 2 -- test/specs/circular-extended/parsed.js | 2 -- .../circular-external-direct/circular-external-direct.spec.js | 2 -- test/specs/circular-external-direct/dereferenced.js | 2 -- test/specs/circular-external-direct/parsed.js | 2 -- test/specs/circular-external/bundled.js | 2 -- test/specs/circular-external/circular-external.spec.js | 2 -- test/specs/circular-external/dereferenced.js | 2 -- test/specs/circular-external/parsed.js | 2 -- test/specs/circular-multi/bundled.js | 2 -- test/specs/circular-multi/circular-multi.spec.js | 2 -- test/specs/circular/circular.spec.js | 2 -- test/specs/circular/dereferenced.js | 2 -- test/specs/circular/parsed.js | 2 -- test/specs/date-strings/date-strings.spec.js | 2 -- test/specs/date-strings/parsed.js | 2 -- test/specs/deep-circular/bundled.js | 2 -- test/specs/deep-circular/deep-circular.spec.js | 2 -- test/specs/deep-circular/dereferenced.js | 2 -- test/specs/deep-circular/parsed.js | 2 -- test/specs/deep/bundled.js | 2 -- test/specs/deep/deep.spec.js | 2 -- test/specs/deep/dereferenced.js | 2 -- test/specs/deep/parsed.js | 2 -- test/specs/empty/empty.spec.js | 2 -- test/specs/error-source/error-source.spec.js | 2 -- test/specs/exports.spec.js | 2 -- test/specs/external-from-internal/bundled.js | 2 -- test/specs/external-from-internal/dereferenced.js | 2 -- .../external-from-internal/external-from-internal.spec.js | 2 -- test/specs/external-from-internal/parsed.js | 2 -- test/specs/external-multiple/bundled.js | 2 -- test/specs/external-multiple/dereferenced.js | 2 -- test/specs/external-multiple/external-multiple.spec.js | 2 -- test/specs/external-multiple/parsed.js | 2 -- test/specs/external-partial/bundled.js | 2 -- test/specs/external-partial/dereferenced.js | 2 -- test/specs/external-partial/external-partial.spec.js | 2 -- test/specs/external-partial/parsed.js | 2 -- test/specs/external/bundled.js | 2 -- test/specs/external/dereferenced.js | 2 -- test/specs/external/external.spec.js | 2 -- test/specs/external/parsed.js | 2 -- test/specs/http.spec.js | 2 -- test/specs/internal/bundled.js | 2 -- test/specs/internal/dereferenced.js | 2 -- test/specs/internal/internal.spec.js | 2 -- test/specs/internal/parsed.js | 2 -- test/specs/invalid-pointers/invalid-pointers.js | 2 -- test/specs/invalid/invalid.spec.js | 2 -- test/specs/missing-pointers/missing-pointers.spec.js | 2 -- test/specs/no-refs/no-refs.spec.js | 2 -- test/specs/no-refs/parsed.js | 2 -- test/specs/object-source-with-path/bundled.js | 2 -- test/specs/object-source-with-path/dereferenced.js | 2 -- .../object-source-with-path/object-source-with-path.spec.js | 2 -- test/specs/object-source-with-path/parsed.js | 2 -- test/specs/object-source/bundled.js | 2 -- test/specs/object-source/dereferenced.js | 2 -- test/specs/object-source/object-source.spec.js | 2 -- test/specs/object-source/parsed.js | 2 -- test/specs/parsers/dereferenced.js | 2 -- test/specs/parsers/parsed.js | 2 -- test/specs/parsers/parsers.spec.js | 2 -- test/specs/ref-in-excluded-path/dereferenced.js | 2 -- test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js | 2 -- test/specs/refs.spec.js | 2 -- test/specs/resolvers/dereferenced.js | 2 -- test/specs/resolvers/parsed.js | 2 -- test/specs/resolvers/resolvers.spec.js | 2 -- test/specs/root/bundled.js | 2 -- test/specs/root/dereferenced.js | 2 -- test/specs/root/parsed.js | 2 -- test/specs/root/root.spec.js | 2 -- test/specs/substrings/bundled.js | 2 -- test/specs/substrings/dereferenced.js | 2 -- test/specs/substrings/parsed.js | 2 -- test/specs/substrings/substrings.spec.js | 2 -- test/specs/util/url.spec.js | 2 -- test/utils/helper.js | 2 -- test/utils/path.js | 2 -- 98 files changed, 2 insertions(+), 196 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 0f9b7050..f224911a 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -2,8 +2,6 @@ // https://karma-runner.github.io/0.12/config/configuration-file.html // https://jstools.dev/karma-config/ -"use strict"; - const { karmaConfig } = require("@jsdevtools/karma-config"); const { host } = require("@jsdevtools/host-environment"); diff --git a/lib/util/url.js b/lib/util/url.js index 10957a8e..ec5dda62 100644 --- a/lib/util/url.js +++ b/lib/util/url.js @@ -277,7 +277,7 @@ export default { }); }, - parse: parse, + parse, - resolve: resolve, + resolve, }; diff --git a/test/fixtures/mocha.js b/test/fixtures/mocha.js index 2d9625c6..5e758542 100644 --- a/test/fixtures/mocha.js +++ b/test/fixtures/mocha.js @@ -1,5 +1,3 @@ -"use strict"; - const { host } = require("@jsdevtools/host-environment"); // Mocha configuration diff --git a/test/fixtures/polyfill.js b/test/fixtures/polyfill.js index 5c126415..f973e75f 100644 --- a/test/fixtures/polyfill.js +++ b/test/fixtures/polyfill.js @@ -1,5 +1,3 @@ -"use strict"; - const { host } = require("@jsdevtools/host-environment"); // Load the Babel Polyfills for old browsers. diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/dereferenced.js b/test/specs/__({[ % & $ # @ ` ~ ,)}]__/dereferenced.js index c011e237..01011242 100644 --- a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/dereferenced.js +++ b/test/specs/__({[ % & $ # @ ` ~ ,)}]__/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/parsed.js b/test/specs/__({[ % & $ # @ ` ~ ,)}]__/parsed.js index 61153c01..eedaede1 100644 --- a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/parsed.js +++ b/test/specs/__({[ % & $ # @ ` ~ ,)}]__/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js b/test/specs/__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js index cf5f83c6..643d807b 100644 --- a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js +++ b/test/specs/__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const $RefParser = require("../../.."); const helper = require("../../utils/helper"); const path = require("../../utils/path"); diff --git a/test/specs/absolute-root/absolute-root.spec.js b/test/specs/absolute-root/absolute-root.spec.js index 45541690..9589ac5b 100644 --- a/test/specs/absolute-root/absolute-root.spec.js +++ b/test/specs/absolute-root/absolute-root.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const { resolve } = require("path"); const $RefParser = require("../../.."); diff --git a/test/specs/absolute-root/bundled.js b/test/specs/absolute-root/bundled.js index db7919ec..635d392a 100644 --- a/test/specs/absolute-root/bundled.js +++ b/test/specs/absolute-root/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/absolute-root/dereferenced.js b/test/specs/absolute-root/dereferenced.js index d82f1ca5..3d0e2c30 100644 --- a/test/specs/absolute-root/dereferenced.js +++ b/test/specs/absolute-root/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/absolute-root/parsed.js b/test/specs/absolute-root/parsed.js index d6c1349f..f3aaf050 100644 --- a/test/specs/absolute-root/parsed.js +++ b/test/specs/absolute-root/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/blank/blank.spec.js b/test/specs/blank/blank.spec.js index a8f59a14..1ef6bc92 100644 --- a/test/specs/blank/blank.spec.js +++ b/test/specs/blank/blank.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { host } = require("@jsdevtools/host-environment"); const { expect } = require("chai"); const $RefParser = require("../../.."); diff --git a/test/specs/blank/dereferenced.js b/test/specs/blank/dereferenced.js index 6dc06e5c..10986093 100644 --- a/test/specs/blank/dereferenced.js +++ b/test/specs/blank/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { json: undefined, yaml: undefined, diff --git a/test/specs/blank/parsed.js b/test/specs/blank/parsed.js index d55e6748..523c5638 100644 --- a/test/specs/blank/parsed.js +++ b/test/specs/blank/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/callbacks.spec.js b/test/specs/callbacks.spec.js index ecd43ebb..3b9a24f2 100644 --- a/test/specs/callbacks.spec.js +++ b/test/specs/callbacks.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../lib"); const helper = require("../utils/helper"); diff --git a/test/specs/circular-extended/bundled.js b/test/specs/circular-extended/bundled.js index 77352209..e3988e85 100644 --- a/test/specs/circular-extended/bundled.js +++ b/test/specs/circular-extended/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - const bundledSchema = module.exports = { self: { diff --git a/test/specs/circular-extended/circular-extended.spec.js b/test/specs/circular-extended/circular-extended.spec.js index 14d8fd12..8827b31e 100644 --- a/test/specs/circular-extended/circular-extended.spec.js +++ b/test/specs/circular-extended/circular-extended.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/circular-extended/dereferenced.js b/test/specs/circular-extended/dereferenced.js index 527b1a75..97c7f767 100644 --- a/test/specs/circular-extended/dereferenced.js +++ b/test/specs/circular-extended/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - const dereferencedSchema = module.exports = { self: { diff --git a/test/specs/circular-extended/parsed.js b/test/specs/circular-extended/parsed.js index e9054c2c..a44602d9 100644 --- a/test/specs/circular-extended/parsed.js +++ b/test/specs/circular-extended/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { self: { diff --git a/test/specs/circular-external-direct/circular-external-direct.spec.js b/test/specs/circular-external-direct/circular-external-direct.spec.js index 408ec73f..bdbb1490 100644 --- a/test/specs/circular-external-direct/circular-external-direct.spec.js +++ b/test/specs/circular-external-direct/circular-external-direct.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const chai = require("chai"); const chaiSubset = require("chai-subset"); chai.use(chaiSubset); diff --git a/test/specs/circular-external-direct/dereferenced.js b/test/specs/circular-external-direct/dereferenced.js index 30ac5ddc..3565623b 100644 --- a/test/specs/circular-external-direct/dereferenced.js +++ b/test/specs/circular-external-direct/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { $ref: "./circular-external-direct-root.yaml#/foo", diff --git a/test/specs/circular-external-direct/parsed.js b/test/specs/circular-external-direct/parsed.js index 8ffbbd6e..67be0e20 100644 --- a/test/specs/circular-external-direct/parsed.js +++ b/test/specs/circular-external-direct/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/circular-external/bundled.js b/test/specs/circular-external/bundled.js index 39ec2ca9..e7b31b03 100644 --- a/test/specs/circular-external/bundled.js +++ b/test/specs/circular-external/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/circular-external/circular-external.spec.js b/test/specs/circular-external/circular-external.spec.js index bf60c30f..3b29b1fd 100644 --- a/test/specs/circular-external/circular-external.spec.js +++ b/test/specs/circular-external/circular-external.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/circular-external/dereferenced.js b/test/specs/circular-external/dereferenced.js index 5d952c5d..6d3f9e9b 100644 --- a/test/specs/circular-external/dereferenced.js +++ b/test/specs/circular-external/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - const dereferencedSchema = module.exports = { definitions: { diff --git a/test/specs/circular-external/parsed.js b/test/specs/circular-external/parsed.js index b9367703..65c75080 100644 --- a/test/specs/circular-external/parsed.js +++ b/test/specs/circular-external/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/circular-multi/bundled.js b/test/specs/circular-multi/bundled.js index 780ce18d..e6dcd535 100644 --- a/test/specs/circular-multi/bundled.js +++ b/test/specs/circular-multi/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { $schema: "http://json-schema.org/draft-07/schema#", properties: { diff --git a/test/specs/circular-multi/circular-multi.spec.js b/test/specs/circular-multi/circular-multi.spec.js index f7889693..222b56ae 100644 --- a/test/specs/circular-multi/circular-multi.spec.js +++ b/test/specs/circular-multi/circular-multi.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const path = require("../../utils/path"); diff --git a/test/specs/circular/circular.spec.js b/test/specs/circular/circular.spec.js index 2ba5b886..54001ca9 100644 --- a/test/specs/circular/circular.spec.js +++ b/test/specs/circular/circular.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/circular/dereferenced.js b/test/specs/circular/dereferenced.js index 519893dc..3cff0449 100644 --- a/test/specs/circular/dereferenced.js +++ b/test/specs/circular/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - const dereferencedSchema = module.exports = { self: { diff --git a/test/specs/circular/parsed.js b/test/specs/circular/parsed.js index fff22719..e118905c 100644 --- a/test/specs/circular/parsed.js +++ b/test/specs/circular/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { self: { diff --git a/test/specs/date-strings/date-strings.spec.js b/test/specs/date-strings/date-strings.spec.js index 7de4acce..4f0a4754 100644 --- a/test/specs/date-strings/date-strings.spec.js +++ b/test/specs/date-strings/date-strings.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/date-strings/parsed.js b/test/specs/date-strings/parsed.js index c9228a93..4963049e 100644 --- a/test/specs/date-strings/parsed.js +++ b/test/specs/date-strings/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { title: "Date Strings", diff --git a/test/specs/deep-circular/bundled.js b/test/specs/deep-circular/bundled.js index d1ba471d..f9fc6b66 100644 --- a/test/specs/deep-circular/bundled.js +++ b/test/specs/deep-circular/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Deep Schema", diff --git a/test/specs/deep-circular/deep-circular.spec.js b/test/specs/deep-circular/deep-circular.spec.js index 00ae32bb..87e6e0da 100644 --- a/test/specs/deep-circular/deep-circular.spec.js +++ b/test/specs/deep-circular/deep-circular.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/deep-circular/dereferenced.js b/test/specs/deep-circular/dereferenced.js index 1da07d24..8d8a200e 100644 --- a/test/specs/deep-circular/dereferenced.js +++ b/test/specs/deep-circular/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - let name = { required: [ "first", diff --git a/test/specs/deep-circular/parsed.js b/test/specs/deep-circular/parsed.js index 7c4d7186..c8063db5 100644 --- a/test/specs/deep-circular/parsed.js +++ b/test/specs/deep-circular/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/deep/bundled.js b/test/specs/deep/bundled.js index b47d6922..330f1076 100644 --- a/test/specs/deep/bundled.js +++ b/test/specs/deep/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { type: "object", diff --git a/test/specs/deep/deep.spec.js b/test/specs/deep/deep.spec.js index 30d9ac60..f2a0ab2e 100644 --- a/test/specs/deep/deep.spec.js +++ b/test/specs/deep/deep.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/deep/dereferenced.js b/test/specs/deep/dereferenced.js index 59035087..0b3f02e2 100644 --- a/test/specs/deep/dereferenced.js +++ b/test/specs/deep/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - let name = { required: [ "first", diff --git a/test/specs/deep/parsed.js b/test/specs/deep/parsed.js index 24218bb2..06d43d02 100644 --- a/test/specs/deep/parsed.js +++ b/test/specs/deep/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/empty/empty.spec.js b/test/specs/empty/empty.spec.js index f3815726..999a870e 100644 --- a/test/specs/empty/empty.spec.js +++ b/test/specs/empty/empty.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../../lib"); const helper = require("../../utils/helper"); diff --git a/test/specs/error-source/error-source.spec.js b/test/specs/error-source/error-source.spec.js index 4315c8bf..30c05497 100644 --- a/test/specs/error-source/error-source.spec.js +++ b/test/specs/error-source/error-source.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const chai = require("chai"); const chaiSubset = require("chai-subset"); chai.use(chaiSubset); diff --git a/test/specs/exports.spec.js b/test/specs/exports.spec.js index 1c87693d..0a3c096d 100644 --- a/test/specs/exports.spec.js +++ b/test/specs/exports.spec.js @@ -1,6 +1,4 @@ /* eslint-disable require-await */ -"use strict"; - const { expect } = require("chai"); const commonJSExport = require("../../"); const { default: defaultExport } = require("../../"); diff --git a/test/specs/external-from-internal/bundled.js b/test/specs/external-from-internal/bundled.js index 9b8caa3e..a3bce65a 100644 --- a/test/specs/external-from-internal/bundled.js +++ b/test/specs/external-from-internal/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { internal1: { $ref: "#/external1" diff --git a/test/specs/external-from-internal/dereferenced.js b/test/specs/external-from-internal/dereferenced.js index edf7d382..adddf134 100644 --- a/test/specs/external-from-internal/dereferenced.js +++ b/test/specs/external-from-internal/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { internal1: { diff --git a/test/specs/external-from-internal/external-from-internal.spec.js b/test/specs/external-from-internal/external-from-internal.spec.js index 75a36d22..b3d830ee 100644 --- a/test/specs/external-from-internal/external-from-internal.spec.js +++ b/test/specs/external-from-internal/external-from-internal.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/external-from-internal/parsed.js b/test/specs/external-from-internal/parsed.js index f4553079..657a66ad 100644 --- a/test/specs/external-from-internal/parsed.js +++ b/test/specs/external-from-internal/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/external-multiple/bundled.js b/test/specs/external-multiple/bundled.js index 782dd3dd..bc84683d 100644 --- a/test/specs/external-multiple/bundled.js +++ b/test/specs/external-multiple/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { type: "object", required: ["user", "token"], diff --git a/test/specs/external-multiple/dereferenced.js b/test/specs/external-multiple/dereferenced.js index d2bcf464..eb4e9052 100644 --- a/test/specs/external-multiple/dereferenced.js +++ b/test/specs/external-multiple/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { type: "object", required: ["user", "token"], diff --git a/test/specs/external-multiple/external-multiple.spec.js b/test/specs/external-multiple/external-multiple.spec.js index bbefe176..9cefc0cd 100644 --- a/test/specs/external-multiple/external-multiple.spec.js +++ b/test/specs/external-multiple/external-multiple.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/external-multiple/parsed.js b/test/specs/external-multiple/parsed.js index 29bafd16..38350713 100644 --- a/test/specs/external-multiple/parsed.js +++ b/test/specs/external-multiple/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { type: "object", diff --git a/test/specs/external-partial/bundled.js b/test/specs/external-partial/bundled.js index 93e97832..92b4cd21 100644 --- a/test/specs/external-partial/bundled.js +++ b/test/specs/external-partial/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/external-partial/dereferenced.js b/test/specs/external-partial/dereferenced.js index e56ea468..fd593769 100644 --- a/test/specs/external-partial/dereferenced.js +++ b/test/specs/external-partial/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/external-partial/external-partial.spec.js b/test/specs/external-partial/external-partial.spec.js index 2ab233b4..d35ab92a 100644 --- a/test/specs/external-partial/external-partial.spec.js +++ b/test/specs/external-partial/external-partial.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/external-partial/parsed.js b/test/specs/external-partial/parsed.js index e3043c06..dd475524 100644 --- a/test/specs/external-partial/parsed.js +++ b/test/specs/external-partial/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/external/bundled.js b/test/specs/external/bundled.js index db7919ec..635d392a 100644 --- a/test/specs/external/bundled.js +++ b/test/specs/external/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/external/dereferenced.js b/test/specs/external/dereferenced.js index d82f1ca5..3d0e2c30 100644 --- a/test/specs/external/dereferenced.js +++ b/test/specs/external/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/external/external.spec.js b/test/specs/external/external.spec.js index f1a8ec4b..78e28e3c 100644 --- a/test/specs/external/external.spec.js +++ b/test/specs/external/external.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/external/parsed.js b/test/specs/external/parsed.js index d6c1349f..f3aaf050 100644 --- a/test/specs/external/parsed.js +++ b/test/specs/external/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/http.spec.js b/test/specs/http.spec.js index cc51f041..3aa36943 100644 --- a/test/specs/http.spec.js +++ b/test/specs/http.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { host } = require("@jsdevtools/host-environment"); const { expect } = require("chai"); const $RefParser = require("../../lib"); diff --git a/test/specs/internal/bundled.js b/test/specs/internal/bundled.js index dc923eec..af9f0b24 100644 --- a/test/specs/internal/bundled.js +++ b/test/specs/internal/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/internal/dereferenced.js b/test/specs/internal/dereferenced.js index 1e971e0d..f3524ffe 100644 --- a/test/specs/internal/dereferenced.js +++ b/test/specs/internal/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/internal/internal.spec.js b/test/specs/internal/internal.spec.js index f43875e5..0a07b4b5 100644 --- a/test/specs/internal/internal.spec.js +++ b/test/specs/internal/internal.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/internal/parsed.js b/test/specs/internal/parsed.js index b9bf16a6..ed84fe03 100644 --- a/test/specs/internal/parsed.js +++ b/test/specs/internal/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/invalid-pointers/invalid-pointers.js b/test/specs/invalid-pointers/invalid-pointers.js index bc62d936..f20224fa 100644 --- a/test/specs/invalid-pointers/invalid-pointers.js +++ b/test/specs/invalid-pointers/invalid-pointers.js @@ -1,5 +1,3 @@ -"use strict"; - const chai = require("chai"); const chaiSubset = require("chai-subset"); chai.use(chaiSubset); diff --git a/test/specs/invalid/invalid.spec.js b/test/specs/invalid/invalid.spec.js index 18eeb57e..1f4488a9 100644 --- a/test/specs/invalid/invalid.spec.js +++ b/test/specs/invalid/invalid.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { host } = require("@jsdevtools/host-environment"); const chai = require("chai"); const chaiSubset = require("chai-subset"); diff --git a/test/specs/missing-pointers/missing-pointers.spec.js b/test/specs/missing-pointers/missing-pointers.spec.js index c9ee5bc3..d12ed193 100644 --- a/test/specs/missing-pointers/missing-pointers.spec.js +++ b/test/specs/missing-pointers/missing-pointers.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const chai = require("chai"); const chaiSubset = require("chai-subset"); chai.use(chaiSubset); diff --git a/test/specs/no-refs/no-refs.spec.js b/test/specs/no-refs/no-refs.spec.js index fe87615e..aa374523 100644 --- a/test/specs/no-refs/no-refs.spec.js +++ b/test/specs/no-refs/no-refs.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/no-refs/parsed.js b/test/specs/no-refs/parsed.js index 2cadce72..bcdc5a6c 100644 --- a/test/specs/no-refs/parsed.js +++ b/test/specs/no-refs/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { required: [ diff --git a/test/specs/object-source-with-path/bundled.js b/test/specs/object-source-with-path/bundled.js index 7a2e5898..dc0addc8 100644 --- a/test/specs/object-source-with-path/bundled.js +++ b/test/specs/object-source-with-path/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/object-source-with-path/dereferenced.js b/test/specs/object-source-with-path/dereferenced.js index e5a5eb39..ea5a9d81 100644 --- a/test/specs/object-source-with-path/dereferenced.js +++ b/test/specs/object-source-with-path/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/object-source-with-path/object-source-with-path.spec.js b/test/specs/object-source-with-path/object-source-with-path.spec.js index 5393f8b1..5caa9211 100644 --- a/test/specs/object-source-with-path/object-source-with-path.spec.js +++ b/test/specs/object-source-with-path/object-source-with-path.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/object-source-with-path/parsed.js b/test/specs/object-source-with-path/parsed.js index a12db7e7..152104e8 100644 --- a/test/specs/object-source-with-path/parsed.js +++ b/test/specs/object-source-with-path/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/object-source/bundled.js b/test/specs/object-source/bundled.js index 7a2e5898..dc0addc8 100644 --- a/test/specs/object-source/bundled.js +++ b/test/specs/object-source/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/object-source/dereferenced.js b/test/specs/object-source/dereferenced.js index e5a5eb39..ea5a9d81 100644 --- a/test/specs/object-source/dereferenced.js +++ b/test/specs/object-source/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/object-source/object-source.spec.js b/test/specs/object-source/object-source.spec.js index a6121e82..f4b7563c 100644 --- a/test/specs/object-source/object-source.spec.js +++ b/test/specs/object-source/object-source.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/object-source/parsed.js b/test/specs/object-source/parsed.js index fe034a4f..9f6fa301 100644 --- a/test/specs/object-source/parsed.js +++ b/test/specs/object-source/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - const path = require("../../utils/path"); module.exports = diff --git a/test/specs/parsers/dereferenced.js b/test/specs/parsers/dereferenced.js index 0d7e4997..2e8b6695 100644 --- a/test/specs/parsers/dereferenced.js +++ b/test/specs/parsers/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { defaultParsers: { definitions: { diff --git a/test/specs/parsers/parsed.js b/test/specs/parsers/parsed.js index 8dc803ab..e425405c 100644 --- a/test/specs/parsers/parsed.js +++ b/test/specs/parsers/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/parsers/parsers.spec.js b/test/specs/parsers/parsers.spec.js index 506d1cf7..2c4b15e5 100644 --- a/test/specs/parsers/parsers.spec.js +++ b/test/specs/parsers/parsers.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const chai = require("chai"); const chaiSubset = require("chai-subset"); chai.use(chaiSubset); diff --git a/test/specs/ref-in-excluded-path/dereferenced.js b/test/specs/ref-in-excluded-path/dereferenced.js index bd995c99..885e287d 100644 --- a/test/specs/ref-in-excluded-path/dereferenced.js +++ b/test/specs/ref-in-excluded-path/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { components: { examples: { diff --git a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js index a462932e..8499e692 100644 --- a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js +++ b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const path = require("../../utils/path"); diff --git a/test/specs/refs.spec.js b/test/specs/refs.spec.js index be9bbdc9..55e706df 100644 --- a/test/specs/refs.spec.js +++ b/test/specs/refs.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { host } = require("@jsdevtools/host-environment"); const { expect } = require("chai"); const $RefParser = require("../../lib"); diff --git a/test/specs/resolvers/dereferenced.js b/test/specs/resolvers/dereferenced.js index abdba1ec..1b223ce3 100644 --- a/test/specs/resolvers/dereferenced.js +++ b/test/specs/resolvers/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/resolvers/parsed.js b/test/specs/resolvers/parsed.js index 975496de..d4bc1d3c 100644 --- a/test/specs/resolvers/parsed.js +++ b/test/specs/resolvers/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { definitions: { diff --git a/test/specs/resolvers/resolvers.spec.js b/test/specs/resolvers/resolvers.spec.js index 0d341ecc..c965acea 100644 --- a/test/specs/resolvers/resolvers.spec.js +++ b/test/specs/resolvers/resolvers.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const chai = require("chai"); const chaiSubset = require("chai-subset"); chai.use(chaiSubset); diff --git a/test/specs/root/bundled.js b/test/specs/root/bundled.js index c16becab..d87f6766 100644 --- a/test/specs/root/bundled.js +++ b/test/specs/root/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Extending a root $ref", diff --git a/test/specs/root/dereferenced.js b/test/specs/root/dereferenced.js index c16becab..d87f6766 100644 --- a/test/specs/root/dereferenced.js +++ b/test/specs/root/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Extending a root $ref", diff --git a/test/specs/root/parsed.js b/test/specs/root/parsed.js index 3fcce63c..6a369422 100644 --- a/test/specs/root/parsed.js +++ b/test/specs/root/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/root/root.spec.js b/test/specs/root/root.spec.js index e15cb31c..34f45da1 100644 --- a/test/specs/root/root.spec.js +++ b/test/specs/root/root.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/substrings/bundled.js b/test/specs/substrings/bundled.js index 96dc4c0e..2066cf10 100644 --- a/test/specs/substrings/bundled.js +++ b/test/specs/substrings/bundled.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/substrings/dereferenced.js b/test/specs/substrings/dereferenced.js index e79bfaa8..5bbabf82 100644 --- a/test/specs/substrings/dereferenced.js +++ b/test/specs/substrings/dereferenced.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { title: "Person", diff --git a/test/specs/substrings/parsed.js b/test/specs/substrings/parsed.js index bdfb3419..41cd00f2 100644 --- a/test/specs/substrings/parsed.js +++ b/test/specs/substrings/parsed.js @@ -1,5 +1,3 @@ -"use strict"; - module.exports = { schema: { diff --git a/test/specs/substrings/substrings.spec.js b/test/specs/substrings/substrings.spec.js index ea7b3437..dae5ab52 100644 --- a/test/specs/substrings/substrings.spec.js +++ b/test/specs/substrings/substrings.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const { expect } = require("chai"); const $RefParser = require("../../.."); const helper = require("../../utils/helper"); diff --git a/test/specs/util/url.spec.js b/test/specs/util/url.spec.js index b0b464c9..7bfd4660 100644 --- a/test/specs/util/url.spec.js +++ b/test/specs/util/url.spec.js @@ -1,5 +1,3 @@ -"use strict"; - const chai = require("chai"); const chaiSubset = require("chai-subset"); chai.use(chaiSubset); diff --git a/test/utils/helper.js b/test/utils/helper.js index e201bf64..03127f3d 100644 --- a/test/utils/helper.js +++ b/test/utils/helper.js @@ -1,5 +1,3 @@ -"use strict"; - const $RefParser = require("../../lib"); const { host } = require("@jsdevtools/host-environment"); const { expect } = require("chai"); diff --git a/test/utils/path.js b/test/utils/path.js index 58a69bd0..d9fdedce 100644 --- a/test/utils/path.js +++ b/test/utils/path.js @@ -1,5 +1,3 @@ -"use strict"; - const { host } = require("@jsdevtools/host-environment"); const isWindows = /^win/.test(globalThis.process?.platform); From 04159698c70b0d5139bbb93fe348e5d8ddabc82d Mon Sep 17 00:00:00 2001 From: Phil Sturgeon <67381+philsturgeon@users.noreply.github.com> Date: Mon, 7 Nov 2022 10:50:32 +0000 Subject: [PATCH 3/7] chore: switched tests to import --- .eslintrc.yml | 12 +- test/fixtures/mocha.js | 2 +- test/fixtures/polyfill.js | 4 +- .../special-characters.spec.js | 38 --- .../specs/absolute-root/absolute-root.spec.js | 64 ++--- test/specs/blank/blank.spec.js | 58 ++--- test/specs/callbacks.spec.js | 20 +- .../circular-extended.spec.js | 150 ++++++------ .../circular-external-direct.spec.js | 22 +- .../circular-external.spec.js | 42 ++-- .../circular-multi/circular-multi.spec.js | 10 +- test/specs/circular/circular.spec.js | 140 +++++------ test/specs/date-strings/date-strings.spec.js | 22 +- .../specs/deep-circular/deep-circular.spec.js | 38 +-- test/specs/deep/deep.spec.js | 34 +-- .../dereference-callback.spec.js | 8 +- test/specs/empty/empty.spec.js | 30 +-- test/specs/error-source/error-source.spec.js | 36 +-- test/specs/exports.spec.js | 16 +- .../external-from-internal.spec.js | 60 ++--- .../external-multiple.spec.js | 32 +-- .../external-partial/external-partial.spec.js | 36 +-- test/specs/external/external.spec.js | 72 +++--- test/specs/http.spec.js | 6 +- test/specs/internal/internal.spec.js | 28 +-- .../invalid-pointers/invalid-pointers.js | 26 +- test/specs/invalid/invalid.spec.js | 93 ++++--- .../missing-pointers/missing-pointers.spec.js | 28 +-- test/specs/no-refs/no-refs.spec.js | 24 +- .../object-source-with-path.spec.js | 48 ++-- .../specs/object-source/object-source.spec.js | 42 ++-- test/specs/object-source/parsed.js | 138 +++++------ test/specs/parsers/parsers.spec.js | 118 +++++---- .../ref-in-excluded-path.spec.js | 10 +- test/specs/refs.spec.js | 190 +++++++-------- test/specs/resolvers/resolvers.spec.js | 228 +++++++++--------- test/specs/root/root.spec.js | 36 +-- .../__({[ % & $ # @ ` ~ ,)}]__.yaml | 0 .../__({[ % & $ # @ ` ~ ,)}]__.json | 0 .../dereferenced.js | 0 .../parsed.js | 0 .../special-characters.spec.js | 38 +++ test/specs/substrings/substrings.spec.js | 34 +-- test/specs/util/url.spec.js | 14 +- 44 files changed, 1007 insertions(+), 1040 deletions(-) delete mode 100644 test/specs/__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js rename test/specs/{__({[ % & $ # @ ` ~ ,)}]__ => special-characters}/__({[ % & $ # @ ` ~ ,)}]__.yaml (100%) rename test/specs/{__({[ % & $ # @ ` ~ ,)}]__ => special-characters}/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json (100%) rename test/specs/{__({[ % & $ # @ ` ~ ,)}]__ => special-characters}/dereferenced.js (100%) rename test/specs/{__({[ % & $ # @ ` ~ ,)}]__ => special-characters}/parsed.js (100%) create mode 100644 test/specs/special-characters/special-characters.spec.js diff --git a/.eslintrc.yml b/.eslintrc.yml index 1db9efd1..189a8ae9 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -3,11 +3,15 @@ # https://jstools.dev/eslint-config/ root: true -extends: "@jsdevtools" -parserOptions: - sourceType: module env: node: true browser: true +parserOptions: + sourceType: module +extends: "@jsdevtools" rules: - "@typescript-eslint/no-explicit-any": ["off"] + # Added to help with ESMification + '@typescript-eslint/no-require-imports': 2 + + # TODO Remove this hack, I was just desperate to get this 'working'. -Phil + '@typescript-eslint/no-explicit-any': 'off' diff --git a/test/fixtures/mocha.js b/test/fixtures/mocha.js index 5e758542..4cc73667 100644 --- a/test/fixtures/mocha.js +++ b/test/fixtures/mocha.js @@ -1,4 +1,4 @@ -const { host } = require("@jsdevtools/host-environment"); +import { host } from "@jsdevtools/host-environment"; // Mocha configuration if (host.browser) { diff --git a/test/fixtures/polyfill.js b/test/fixtures/polyfill.js index f973e75f..92e59426 100644 --- a/test/fixtures/polyfill.js +++ b/test/fixtures/polyfill.js @@ -1,9 +1,10 @@ -const { host } = require("@jsdevtools/host-environment"); +import { host } from "@jsdevtools/host-environment"; // Load the Babel Polyfills for old browsers. // NOTE: It's important that we ONLY do this when needed, // to ensure that our code works _without_ polyfills everywhere else if (host.browser.IE) { + // eslint-disable-next-line @typescript-eslint/no-require-imports require("@babel/polyfill"); } @@ -14,5 +15,6 @@ import("node-fetch").then(({ default: fetch }) => { }); if (!globalThis.AbortController) { + // eslint-disable-next-line @typescript-eslint/no-require-imports globalThis.AbortController = require("node-abort-controller").AbortController; } diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js b/test/specs/__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js deleted file mode 100644 index 643d807b..00000000 --- a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/special-characters.spec.js +++ /dev/null @@ -1,38 +0,0 @@ -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const { expect } = require("chai"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); - -describe("File names with special characters", () => { - it("should parse successfully", async () => { - let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); - expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")]); - }); - - it("should resolve successfully", helper.testResolve( - path.rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml"), - path.abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml"), parsedSchema.schema, - path.abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json"), parsedSchema.file - )); - - it("should dereference successfully", async () => { - let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); - expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema); - // The "circular" flag should NOT be set - expect(parser.$refs.circular).to.equal(false); - }); - - it("should bundle successfully", async () => { - let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); - expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema); - }); -}); diff --git a/test/specs/absolute-root/absolute-root.spec.js b/test/specs/absolute-root/absolute-root.spec.js index 9589ac5b..2740b17e 100644 --- a/test/specs/absolute-root/absolute-root.spec.js +++ b/test/specs/absolute-root/absolute-root.spec.js @@ -1,12 +1,12 @@ -const { expect } = require("chai"); -const { resolve } = require("path"); -const $RefParser = require("../../.."); -const path = require("../../utils/path"); -const helper = require("../../utils/helper"); -const url = require("../../../lib/util/url"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import { resolve } from "path"; +import $RefParser from "../../.."; +import { abs, url as _url } from "../../utils/path"; +import { testResolve } from "../../utils/helper"; +import { cwd } from "../../../lib/util/url"; +import { schema as _schema, definitions, name, requiredString } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("When executed in the context of root directory", () => { // Store the OS root directory @@ -14,7 +14,7 @@ describe("When executed in the context of root directory", () => { // Store references to the original methods const originalProcessCwd = process.cwd; - const originalUrlCwd = url.cwd; + const originalUrlCwd = cwd; /** * A mock `process.cwd()` implementation that always returns the root diretory @@ -37,52 +37,52 @@ describe("When executed in the context of root directory", () => { } beforeEach("Mock process.cwd and url.cwd", () => { - url.cwd = mockUrlCwd; + cwd = mockUrlCwd; }); afterEach("Restore process.cwd and url.cwd", () => { - url.cwd = originalUrlCwd; + cwd = originalUrlCwd; process.cwd = originalProcessCwd; // already restored by the finally block above, but just in case }); it("should parse successfully from an absolute path", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.abs("specs/absolute-root/absolute-root.yaml")); + const schema = await parser.parse(abs("specs/absolute-root/absolute-root.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); + expect(schema).to.deep.equal(_schema); expect(parser.$refs.paths()).to.deep.equal([ - path.abs("specs/absolute-root/absolute-root.yaml") + abs("specs/absolute-root/absolute-root.yaml") ]); }); it("should parse successfully from a url", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.url("specs/absolute-root/absolute-root.yaml")); + const schema = await parser.parse(_url("specs/absolute-root/absolute-root.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.url("specs/absolute-root/absolute-root.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([_url("specs/absolute-root/absolute-root.yaml")]); }); - it("should resolve successfully from an absolute path", helper.testResolve( - path.abs("specs/absolute-root/absolute-root.yaml"), - path.abs("specs/absolute-root/absolute-root.yaml"), parsedSchema.schema, - path.abs("specs/absolute-root/definitions/definitions.json"), parsedSchema.definitions, - path.abs("specs/absolute-root/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/absolute-root/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully from an absolute path", testResolve( + abs("specs/absolute-root/absolute-root.yaml"), + abs("specs/absolute-root/absolute-root.yaml"), _schema, + abs("specs/absolute-root/definitions/definitions.json"), definitions, + abs("specs/absolute-root/definitions/name.yaml"), name, + abs("specs/absolute-root/definitions/required-string.yaml"), requiredString )); - it("should resolve successfully from a url", helper.testResolve( - path.url("specs/absolute-root/absolute-root.yaml"), - path.url("specs/absolute-root/absolute-root.yaml"), parsedSchema.schema, - path.url("specs/absolute-root/definitions/definitions.json"), parsedSchema.definitions, - path.url("specs/absolute-root/definitions/name.yaml"), parsedSchema.name, - path.url("specs/absolute-root/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully from a url", testResolve( + _url("specs/absolute-root/absolute-root.yaml"), + _url("specs/absolute-root/absolute-root.yaml"), _schema, + _url("specs/absolute-root/definitions/definitions.json"), definitions, + _url("specs/absolute-root/definitions/name.yaml"), name, + _url("specs/absolute-root/definitions/required-string.yaml"), requiredString )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.abs("specs/absolute-root/absolute-root.yaml")); + const schema = await parser.dereference(abs("specs/absolute-root/absolute-root.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -98,7 +98,7 @@ describe("When executed in the context of root directory", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.abs("specs/absolute-root/absolute-root.yaml")); + const schema = await parser.bundle(abs("specs/absolute-root/absolute-root.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/blank/blank.spec.js b/test/specs/blank/blank.spec.js index 1ef6bc92..e6443a17 100644 --- a/test/specs/blank/blank.spec.js +++ b/test/specs/blank/blank.spec.js @@ -1,10 +1,10 @@ -const { host } = require("@jsdevtools/host-environment"); -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); +import { host } from "@jsdevtools/host-environment"; +import { expect } from "chai"; +import { parse as _parse, dereference, bundle } from "../../.."; +import { shouldNotGetCalled, testResolve, convertNodeBuffersToPOJOs } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, yaml as _yaml, json as _json, text, binary as _binary, unknown } from "./parsed"; +import dereferencedSchema from "./dereferenced"; describe("Blank files", () => { let windowOnError, testDone; @@ -25,8 +25,8 @@ describe("Blank files", () => { describe("main file", () => { it("should throw an error for a blank YAML file", async () => { try { - await $RefParser.parse(path.rel("specs/blank/files/blank.yaml")); - helper.shouldNotGetCalled(); + await _parse(rel("specs/blank/files/blank.yaml")); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -37,8 +37,8 @@ describe("Blank files", () => { it('should throw a different error if "parse.yaml.allowEmpty" is disabled', async () => { try { - await $RefParser.parse(path.rel("specs/blank/files/blank.yaml"), { parse: { yaml: { allowEmpty: false }}}); - helper.shouldNotGetCalled(); + await _parse(rel("specs/blank/files/blank.yaml"), { parse: { yaml: { allowEmpty: false }}}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -50,8 +50,8 @@ describe("Blank files", () => { it("should throw an error for a blank JSON file", async () => { try { - await $RefParser.parse(path.rel("specs/blank/files/blank.json"), { parse: { json: { allowEmpty: false }}}); - helper.shouldNotGetCalled(); + await _parse(rel("specs/blank/files/blank.json"), { parse: { json: { allowEmpty: false }}}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -63,36 +63,36 @@ describe("Blank files", () => { describe("referenced files", () => { it("should parse successfully", async () => { - let schema = await $RefParser.parse(path.rel("specs/blank/blank.yaml")); - expect(schema).to.deep.equal(parsedSchema.schema); + let schema = await _parse(rel("specs/blank/blank.yaml")); + expect(schema).to.deep.equal(_schema); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/blank/blank.yaml"), - path.abs("specs/blank/blank.yaml"), parsedSchema.schema, - path.abs("specs/blank/files/blank.yaml"), parsedSchema.yaml, - path.abs("specs/blank/files/blank.json"), parsedSchema.json, - path.abs("specs/blank/files/blank.txt"), parsedSchema.text, - path.abs("specs/blank/files/blank.png"), parsedSchema.binary, - path.abs("specs/blank/files/blank.foo"), parsedSchema.unknown + it("should resolve successfully", testResolve( + rel("specs/blank/blank.yaml"), + abs("specs/blank/blank.yaml"), _schema, + abs("specs/blank/files/blank.yaml"), _yaml, + abs("specs/blank/files/blank.json"), _json, + abs("specs/blank/files/blank.txt"), text, + abs("specs/blank/files/blank.png"), _binary, + abs("specs/blank/files/blank.foo"), unknown )); it("should dereference successfully", async () => { - let schema = await $RefParser.dereference(path.rel("specs/blank/blank.yaml")); - schema.binary = helper.convertNodeBuffersToPOJOs(schema.binary); + let schema = await dereference(rel("specs/blank/blank.yaml")); + schema.binary = convertNodeBuffersToPOJOs(schema.binary); expect(schema).to.deep.equal(dereferencedSchema); }); it("should bundle successfully", async () => { - let schema = await $RefParser.bundle(path.rel("specs/blank/blank.yaml")); - schema.binary = helper.convertNodeBuffersToPOJOs(schema.binary); + let schema = await bundle(rel("specs/blank/blank.yaml")); + schema.binary = convertNodeBuffersToPOJOs(schema.binary); expect(schema).to.deep.equal(dereferencedSchema); }); it('should throw an error if "allowEmpty" is disabled', async () => { try { - await $RefParser.dereference(path.rel("specs/blank/blank.yaml"), { parse: { binary: { allowEmpty: false }}}); - helper.shouldNotGetCalled(); + await dereference(rel("specs/blank/blank.yaml"), { parse: { binary: { allowEmpty: false }}}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); diff --git a/test/specs/callbacks.spec.js b/test/specs/callbacks.spec.js index 3b9a24f2..e6ce7e8d 100644 --- a/test/specs/callbacks.spec.js +++ b/test/specs/callbacks.spec.js @@ -1,8 +1,8 @@ -const { expect } = require("chai"); -const $RefParser = require("../../lib"); -const helper = require("../utils/helper"); -const path = require("../utils/path"); -const { ParserError } = require("../../lib/util/errors"); +import { expect } from "chai"; +import $RefParser from "../../lib"; +import { shouldNotGetCalled } from "../utils/helper"; +import { rel } from "../utils/path"; +import { ParserError } from "../../lib/util/errors"; describe("Callback & Promise syntax", () => { for (let method of ["parse", "resolve", "dereference", "bundle"]) { @@ -17,7 +17,7 @@ describe("Callback & Promise syntax", () => { function testCallbackSuccess (method) { return function (done) { let parser = new $RefParser(); - parser[method](path.rel("specs/internal/internal.yaml"), (err, result) => { + parser[method](rel("specs/internal/internal.yaml"), (err, result) => { try { expect(err).to.equal(null); expect(result).to.be.an("object"); @@ -39,7 +39,7 @@ describe("Callback & Promise syntax", () => { function testCallbackError (method) { return function (done) { - $RefParser[method](path.rel("specs/invalid/invalid.yaml"), (err, result) => { + $RefParser[method](rel("specs/invalid/invalid.yaml"), (err, result) => { try { expect(err).to.be.an.instanceOf(ParserError); expect(result).to.equal(undefined); @@ -55,7 +55,7 @@ describe("Callback & Promise syntax", () => { function testPromiseSuccess (method) { return function () { let parser = new $RefParser(); - return parser[method](path.rel("specs/internal/internal.yaml")) + return parser[method](rel("specs/internal/internal.yaml")) .then((result) => { expect(result).to.be.an("object"); @@ -71,8 +71,8 @@ describe("Callback & Promise syntax", () => { function testPromiseError (method) { return function () { - return $RefParser[method](path.rel("specs/invalid/invalid.yaml")) - .then(helper.shouldNotGetCalled) + return $RefParser[method](rel("specs/invalid/invalid.yaml")) + .then(shouldNotGetCalled) .catch((err) => { expect(err).to.be.an.instanceOf(ParserError); }); diff --git a/test/specs/circular-extended/circular-extended.spec.js b/test/specs/circular-extended/circular-extended.spec.js index 8827b31e..3ed16d4d 100644 --- a/test/specs/circular-extended/circular-extended.spec.js +++ b/test/specs/circular-extended/circular-extended.spec.js @@ -1,44 +1,44 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { self, thing, ancestor, personWithSpouse, pet, animals, indirect, parentWithChildren, childWithParents, indirectAncestor, parentWithChild, childWithChildren } from "./parsed"; +import { self as _self, ancestor as _ancestor, indirect as _indirect, indirectAncestor as _indirectAncestor } from "./dereferenced"; +import { self as __self, ancestor as __ancestor, indirect as __indirect, indirectAncestor as __indirectAncestor } from "./bundled"; describe("Schema with circular $refs that extend each other", () => { describe("$ref to self", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-extended/circular-extended-self.yaml")); + const schema = await parser.parse(rel("specs/circular-extended/circular-extended-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.self); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-extended/circular-extended-self.yaml")]); + expect(schema).to.deep.equal(self); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-extended/circular-extended-self.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular-extended/circular-extended-self.yaml"), - path.abs("specs/circular-extended/circular-extended-self.yaml"), parsedSchema.self, - path.abs("specs/circular-extended/definitions/thing.yaml"), parsedSchema.thing + it("should resolve successfully", testResolve( + rel("specs/circular-extended/circular-extended-self.yaml"), + abs("specs/circular-extended/circular-extended-self.yaml"), self, + abs("specs/circular-extended/definitions/thing.yaml"), thing )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-self.yaml")); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.self); + expect(schema).to.deep.equal(_self); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-self.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-self.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.self); + expect(schema).to.deep.equal(_self); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); @@ -47,8 +47,8 @@ describe("Schema with circular $refs that extend each other", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular-extended/circular-extended-self.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular-extended/circular-extended-self.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -63,9 +63,9 @@ describe("Schema with circular $refs that extend each other", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-extended/circular-extended-self.yaml")); + const schema = await parser.bundle(rel("specs/circular-extended/circular-extended-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(bundledSchema.self); + expect(schema).to.deep.equal(__self); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -75,28 +75,28 @@ describe("Schema with circular $refs that extend each other", () => { describe("$ref to ancestor", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-extended/circular-extended-ancestor.yaml")); + const schema = await parser.parse(rel("specs/circular-extended/circular-extended-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.ancestor); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-extended/circular-extended-ancestor.yaml")]); + expect(schema).to.deep.equal(ancestor); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-extended/circular-extended-ancestor.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular-extended/circular-extended-ancestor.yaml"), - path.abs("specs/circular-extended/circular-extended-ancestor.yaml"), parsedSchema.ancestor, - path.abs("specs/circular-extended/definitions/person-with-spouse.yaml"), parsedSchema.personWithSpouse, - path.abs("specs/circular-extended/definitions/pet.yaml"), parsedSchema.pet, - path.abs("specs/circular-extended/definitions/animals.yaml"), parsedSchema.animals + it("should resolve successfully", testResolve( + rel("specs/circular-extended/circular-extended-ancestor.yaml"), + abs("specs/circular-extended/circular-extended-ancestor.yaml"), ancestor, + abs("specs/circular-extended/definitions/person-with-spouse.yaml"), personWithSpouse, + abs("specs/circular-extended/definitions/pet.yaml"), pet, + abs("specs/circular-extended/definitions/animals.yaml"), animals )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-ancestor.yaml")); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.ancestor.fullyDereferenced); + expect(schema).to.deep.equal(_ancestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -108,9 +108,9 @@ describe("Schema with circular $refs that extend each other", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-ancestor.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-ancestor.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.ancestor.ignoreCircular$Refs); + expect(schema).to.deep.equal(_ancestor.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); @@ -119,8 +119,8 @@ describe("Schema with circular $refs that extend each other", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular-extended/circular-extended-ancestor.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular-extended/circular-extended-ancestor.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -135,9 +135,9 @@ describe("Schema with circular $refs that extend each other", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-extended/circular-extended-ancestor.yaml")); + const schema = await parser.bundle(rel("specs/circular-extended/circular-extended-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(bundledSchema.ancestor); + expect(schema).to.deep.equal(__ancestor); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -147,29 +147,29 @@ describe("Schema with circular $refs that extend each other", () => { describe("indirect circular $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-extended/circular-extended-indirect.yaml")); + const schema = await parser.parse(rel("specs/circular-extended/circular-extended-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirect); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-extended/circular-extended-indirect.yaml")]); + expect(schema).to.deep.equal(indirect); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-extended/circular-extended-indirect.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular-extended/circular-extended-indirect.yaml"), - path.abs("specs/circular-extended/circular-extended-indirect.yaml"), parsedSchema.indirect, - path.abs("specs/circular-extended/definitions/parent-with-children.yaml"), parsedSchema.parentWithChildren, - path.abs("specs/circular-extended/definitions/child-with-parents.yaml"), parsedSchema.childWithParents, - path.abs("specs/circular-extended/definitions/pet.yaml"), parsedSchema.pet, - path.abs("specs/circular-extended/definitions/animals.yaml"), parsedSchema.animals + it("should resolve successfully", testResolve( + rel("specs/circular-extended/circular-extended-indirect.yaml"), + abs("specs/circular-extended/circular-extended-indirect.yaml"), indirect, + abs("specs/circular-extended/definitions/parent-with-children.yaml"), parentWithChildren, + abs("specs/circular-extended/definitions/child-with-parents.yaml"), childWithParents, + abs("specs/circular-extended/definitions/pet.yaml"), pet, + abs("specs/circular-extended/definitions/animals.yaml"), animals )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect.yaml")); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirect.fullyDereferenced); + expect(schema).to.deep.equal(_indirect.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -183,9 +183,9 @@ describe("Schema with circular $refs that extend each other", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-indirect.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirect.ignoreCircular$Refs); + expect(schema).to.deep.equal(_indirect.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); @@ -194,8 +194,8 @@ describe("Schema with circular $refs that extend each other", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular-extended/circular-extended-indirect.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -210,9 +210,9 @@ describe("Schema with circular $refs that extend each other", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-extended/circular-extended-indirect.yaml")); + const schema = await parser.bundle(rel("specs/circular-extended/circular-extended-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(bundledSchema.indirect); + expect(schema).to.deep.equal(__indirect); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -222,29 +222,29 @@ describe("Schema with circular $refs that extend each other", () => { describe("indirect circular and ancestor $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); + const schema = await parser.parse(rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirectAncestor); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-extended/circular-extended-indirect-ancestor.yaml")]); + expect(schema).to.deep.equal(indirectAncestor); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-extended/circular-extended-indirect-ancestor.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), - path.abs("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), parsedSchema.indirectAncestor, - path.abs("specs/circular-extended/definitions/parent-with-child.yaml"), parsedSchema.parentWithChild, - path.abs("specs/circular-extended/definitions/child-with-children.yaml"), parsedSchema.childWithChildren, - path.abs("specs/circular-extended/definitions/pet.yaml"), parsedSchema.pet, - path.abs("specs/circular-extended/definitions/animals.yaml"), parsedSchema.animals + it("should resolve successfully", testResolve( + rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), + abs("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), indirectAncestor, + abs("specs/circular-extended/definitions/parent-with-child.yaml"), parentWithChild, + abs("specs/circular-extended/definitions/child-with-children.yaml"), childWithChildren, + abs("specs/circular-extended/definitions/pet.yaml"), pet, + abs("specs/circular-extended/definitions/animals.yaml"), animals )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirectAncestor.fullyDereferenced); + expect(schema).to.deep.equal(_indirectAncestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -258,9 +258,9 @@ describe("Schema with circular $refs that extend each other", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirectAncestor.ignoreCircular$Refs); + expect(schema).to.deep.equal(_indirectAncestor.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); @@ -269,8 +269,8 @@ describe("Schema with circular $refs that extend each other", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -285,9 +285,9 @@ describe("Schema with circular $refs that extend each other", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); + const schema = await parser.bundle(rel("specs/circular-extended/circular-extended-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(bundledSchema.indirectAncestor); + expect(schema).to.deep.equal(__indirectAncestor); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); diff --git a/test/specs/circular-external-direct/circular-external-direct.spec.js b/test/specs/circular-external-direct/circular-external-direct.spec.js index bdbb1490..b1938ae9 100644 --- a/test/specs/circular-external-direct/circular-external-direct.spec.js +++ b/test/specs/circular-external-direct/circular-external-direct.spec.js @@ -1,19 +1,19 @@ -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../../lib"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); +import $RefParser from "../../../lib"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema } from "./parsed"; +import dereferencedSchema from "./dereferenced"; describe("Schema with direct circular (recursive) external $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-external-direct/circular-external-direct-root.yaml")); + const schema = await parser.parse(rel("specs/circular-external-direct/circular-external-direct-root.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-external-direct/circular-external-direct-root.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-external-direct/circular-external-direct-root.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -21,7 +21,7 @@ describe("Schema with direct circular (recursive) external $refs", () => { it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-external-direct/circular-external-direct-root.yaml")); + const schema = await parser.dereference(rel("specs/circular-external-direct/circular-external-direct-root.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // The "circular" flag should be set diff --git a/test/specs/circular-external/circular-external.spec.js b/test/specs/circular-external/circular-external.spec.js index 3b29b1fd..9ed3f678 100644 --- a/test/specs/circular-external/circular-external.spec.js +++ b/test/specs/circular-external/circular-external.spec.js @@ -1,35 +1,35 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, pet, child, parent, person } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with circular (recursive) external $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular-external/circular-external.yaml")); + const schema = await parser.parse(rel("specs/circular-external/circular-external.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular-external/circular-external.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular-external/circular-external.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular-external/circular-external.yaml"), - path.abs("specs/circular-external/circular-external.yaml"), parsedSchema.schema, - path.abs("specs/circular-external/definitions/pet.yaml"), parsedSchema.pet, - path.abs("specs/circular-external/definitions/child.yaml"), parsedSchema.child, - path.abs("specs/circular-external/definitions/parent.yaml"), parsedSchema.parent, - path.abs("specs/circular-external/definitions/person.yaml"), parsedSchema.person + it("should resolve successfully", testResolve( + rel("specs/circular-external/circular-external.yaml"), + abs("specs/circular-external/circular-external.yaml"), _schema, + abs("specs/circular-external/definitions/pet.yaml"), pet, + abs("specs/circular-external/definitions/child.yaml"), child, + abs("specs/circular-external/definitions/parent.yaml"), parent, + abs("specs/circular-external/definitions/person.yaml"), person )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular-external/circular-external.yaml")); + const schema = await parser.dereference(rel("specs/circular-external/circular-external.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // The "circular" flag should be set @@ -44,8 +44,8 @@ describe("Schema with circular (recursive) external $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular-external/circular-external.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular-external/circular-external.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -60,7 +60,7 @@ describe("Schema with circular (recursive) external $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-external/circular-external.yaml")); + const schema = await parser.bundle(rel("specs/circular-external/circular-external.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); // The "circular" flag should NOT be set diff --git a/test/specs/circular-multi/circular-multi.spec.js b/test/specs/circular-multi/circular-multi.spec.js index 222b56ae..4a23bd23 100644 --- a/test/specs/circular-multi/circular-multi.spec.js +++ b/test/specs/circular-multi/circular-multi.spec.js @@ -1,13 +1,13 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const path = require("../../utils/path"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { rel } from "../../utils/path"; +import bundledSchema from "./bundled"; describe("multiple circular $refs at the same depth in the schema", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular-multi/definitions/root.json")); + const schema = await parser.bundle(rel("specs/circular-multi/definitions/root.json")); expect(schema).to.deep.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/circular/circular.spec.js b/test/specs/circular/circular.spec.js index 54001ca9..2e6c697e 100644 --- a/test/specs/circular/circular.spec.js +++ b/test/specs/circular/circular.spec.js @@ -1,33 +1,33 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); +import { expect } from "chai"; +import $RefParser, { dereference as _dereference } from "../../.."; +import { testResolve, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { self, ancestor, indirect, indirectAncestor } from "./parsed"; +import { self as _self, ancestor as _ancestor, indirect as _indirect, indirectAncestor as _indirectAncestor } from "./dereferenced"; describe("Schema with circular (recursive) $refs", () => { describe("$ref to self", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular/circular-self.yaml")); + const schema = await parser.parse(rel("specs/circular/circular-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.self); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular/circular-self.yaml")]); + expect(schema).to.deep.equal(self); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular/circular-self.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular/circular-self.yaml"), - path.abs("specs/circular/circular-self.yaml"), parsedSchema.self + it("should resolve successfully", testResolve( + rel("specs/circular/circular-self.yaml"), + abs("specs/circular/circular-self.yaml"), self )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-self.yaml")); + const schema = await parser.dereference(rel("specs/circular/circular-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.self); + expect(schema).to.deep.equal(_self); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -35,11 +35,11 @@ describe("Schema with circular (recursive) $refs", () => { }); it("should double dereference successfully", async () => { - const firstPassSchema = await $RefParser.dereference(path.rel("specs/circular/circular-self.yaml")); + const firstPassSchema = await _dereference(rel("specs/circular/circular-self.yaml")); let parser = new $RefParser(); const schema = await parser.dereference(firstPassSchema); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.self); + expect(schema).to.deep.equal(_self); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -48,9 +48,9 @@ describe("Schema with circular (recursive) $refs", () => { it('should produce the same results if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-self.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular/circular-self.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.self); + expect(schema).to.deep.equal(_self); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); }); @@ -59,8 +59,8 @@ describe("Schema with circular (recursive) $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular/circular-self.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular/circular-self.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -75,9 +75,9 @@ describe("Schema with circular (recursive) $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular/circular-self.yaml")); + const schema = await parser.bundle(rel("specs/circular/circular-self.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.self); + expect(schema).to.deep.equal(self); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -87,25 +87,25 @@ describe("Schema with circular (recursive) $refs", () => { describe("$ref to ancestor", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular/circular-ancestor.yaml")); + const schema = await parser.parse(rel("specs/circular/circular-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.ancestor); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular/circular-ancestor.yaml")]); + expect(schema).to.deep.equal(ancestor); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular/circular-ancestor.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular/circular-ancestor.yaml"), - path.abs("specs/circular/circular-ancestor.yaml"), parsedSchema.ancestor + it("should resolve successfully", testResolve( + rel("specs/circular/circular-ancestor.yaml"), + abs("specs/circular/circular-ancestor.yaml"), ancestor )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-ancestor.yaml")); + const schema = await parser.dereference(rel("specs/circular/circular-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.ancestor.fullyDereferenced); + expect(schema).to.deep.equal(_ancestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -115,10 +115,10 @@ describe("Schema with circular (recursive) $refs", () => { it("should double dereference successfully", async () => { let parser = new $RefParser(); - const firstPassSchema = await $RefParser.dereference(path.rel("specs/circular/circular-ancestor.yaml")); + const firstPassSchema = await _dereference(rel("specs/circular/circular-ancestor.yaml")); const schema = await parser.dereference(firstPassSchema); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.ancestor.fullyDereferenced); + expect(schema).to.deep.equal(_ancestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -128,9 +128,9 @@ describe("Schema with circular (recursive) $refs", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-ancestor.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular/circular-ancestor.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.ancestor.ignoreCircular$Refs); + expect(schema).to.deep.equal(_ancestor.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -141,8 +141,8 @@ describe("Schema with circular (recursive) $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular/circular-ancestor.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular/circular-ancestor.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -157,9 +157,9 @@ describe("Schema with circular (recursive) $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular/circular-ancestor.yaml")); + const schema = await parser.bundle(rel("specs/circular/circular-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.ancestor); + expect(schema).to.deep.equal(ancestor); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -169,25 +169,25 @@ describe("Schema with circular (recursive) $refs", () => { describe("indirect circular $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular/circular-indirect.yaml")); + const schema = await parser.parse(rel("specs/circular/circular-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirect); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular/circular-indirect.yaml")]); + expect(schema).to.deep.equal(indirect); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular/circular-indirect.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular/circular-indirect.yaml"), - path.abs("specs/circular/circular-indirect.yaml"), parsedSchema.indirect + it("should resolve successfully", testResolve( + rel("specs/circular/circular-indirect.yaml"), + abs("specs/circular/circular-indirect.yaml"), indirect )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-indirect.yaml")); + const schema = await parser.dereference(rel("specs/circular/circular-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirect.fullyDereferenced); + expect(schema).to.deep.equal(_indirect.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -199,10 +199,10 @@ describe("Schema with circular (recursive) $refs", () => { it("should double dereference successfully", async () => { let parser = new $RefParser(); - const firstPassSchema = await $RefParser.dereference(path.rel("specs/circular/circular-indirect.yaml")); + const firstPassSchema = await _dereference(rel("specs/circular/circular-indirect.yaml")); const schema = await parser.dereference(firstPassSchema); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirect.fullyDereferenced); + expect(schema).to.deep.equal(_indirect.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -214,9 +214,9 @@ describe("Schema with circular (recursive) $refs", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-indirect.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular/circular-indirect.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirect.ignoreCircular$Refs); + expect(schema).to.deep.equal(_indirect.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -227,8 +227,8 @@ describe("Schema with circular (recursive) $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular/circular-indirect.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular/circular-indirect.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -243,9 +243,9 @@ describe("Schema with circular (recursive) $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular/circular-indirect.yaml")); + const schema = await parser.bundle(rel("specs/circular/circular-indirect.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirect); + expect(schema).to.deep.equal(indirect); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); @@ -255,25 +255,25 @@ describe("Schema with circular (recursive) $refs", () => { describe("indirect circular and ancestor $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/circular/circular-indirect-ancestor.yaml")); + const schema = await parser.parse(rel("specs/circular/circular-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirectAncestor); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/circular/circular-indirect-ancestor.yaml")]); + expect(schema).to.deep.equal(indirectAncestor); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/circular/circular-indirect-ancestor.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/circular/circular-indirect-ancestor.yaml"), - path.abs("specs/circular/circular-indirect-ancestor.yaml"), parsedSchema.indirectAncestor + it("should resolve successfully", testResolve( + rel("specs/circular/circular-indirect-ancestor.yaml"), + abs("specs/circular/circular-indirect-ancestor.yaml"), indirectAncestor )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-indirect-ancestor.yaml")); + const schema = await parser.dereference(rel("specs/circular/circular-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirectAncestor.fullyDereferenced); + expect(schema).to.deep.equal(_indirectAncestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -285,10 +285,10 @@ describe("Schema with circular (recursive) $refs", () => { it("should double dereference successfully", async () => { let parser = new $RefParser(); - const firstPassSchema = await parser.dereference(path.rel("specs/circular/circular-indirect-ancestor.yaml")); + const firstPassSchema = await parser.dereference(rel("specs/circular/circular-indirect-ancestor.yaml")); const schema = await parser.dereference(firstPassSchema); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirectAncestor.fullyDereferenced); + expect(schema).to.deep.equal(_indirectAncestor.fullyDereferenced); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -300,9 +300,9 @@ describe("Schema with circular (recursive) $refs", () => { it('should not dereference circular $refs if "options.$refs.circular" is "ignore"', async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/circular/circular-indirect-ancestor.yaml"), { dereference: { circular: "ignore" }}); + const schema = await parser.dereference(rel("specs/circular/circular-indirect-ancestor.yaml"), { dereference: { circular: "ignore" }}); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(dereferencedSchema.indirectAncestor.ignoreCircular$Refs); + expect(schema).to.deep.equal(_indirectAncestor.ignoreCircular$Refs); // The "circular" flag should be set expect(parser.$refs.circular).to.equal(true); // Reference equality @@ -313,8 +313,8 @@ describe("Schema with circular (recursive) $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/circular/circular-indirect-ancestor.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/circular/circular-indirect-ancestor.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -329,9 +329,9 @@ describe("Schema with circular (recursive) $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/circular/circular-indirect-ancestor.yaml")); + const schema = await parser.bundle(rel("specs/circular/circular-indirect-ancestor.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.indirectAncestor); + expect(schema).to.deep.equal(indirectAncestor); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); diff --git a/test/specs/date-strings/date-strings.spec.js b/test/specs/date-strings/date-strings.spec.js index 4f0a4754..78d85be4 100644 --- a/test/specs/date-strings/date-strings.spec.js +++ b/test/specs/date-strings/date-strings.spec.js @@ -1,20 +1,20 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema } from "./parsed"; describe("Schema with date strings", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/date-strings/date-strings.yaml")); + const schema = await parser.parse(rel("specs/date-strings/date-strings.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/date-strings/date-strings.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/date-strings/date-strings.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/date-strings/date-strings.yaml"), - path.abs("specs/date-strings/date-strings.yaml"), parsedSchema.schema, + it("should resolve successfully", testResolve( + rel("specs/date-strings/date-strings.yaml"), + abs("specs/date-strings/date-strings.yaml"), _schema, )); }); diff --git a/test/specs/deep-circular/deep-circular.spec.js b/test/specs/deep-circular/deep-circular.spec.js index 87e6e0da..687dc2f0 100644 --- a/test/specs/deep-circular/deep-circular.spec.js +++ b/test/specs/deep-circular/deep-circular.spec.js @@ -1,33 +1,33 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, name, requiredString } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with deeply-nested circular $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/deep-circular/deep-circular.yaml")); + const schema = await parser.parse(rel("specs/deep-circular/deep-circular.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/deep-circular/deep-circular.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/deep-circular/deep-circular.yaml")]); // The "circular" flag should NOT be set // (it only gets set by `dereference`) expect(parser.$refs.circular).to.equal(false); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/deep-circular/deep-circular.yaml"), - path.abs("specs/deep-circular/deep-circular.yaml"), parsedSchema.schema, - path.abs("specs/deep-circular/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/deep-circular/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully", testResolve( + rel("specs/deep-circular/deep-circular.yaml"), + abs("specs/deep-circular/deep-circular.yaml"), _schema, + abs("specs/deep-circular/definitions/name.yaml"), name, + abs("specs/deep-circular/definitions/required-string.yaml"), requiredString )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/deep-circular/deep-circular.yaml")); + const schema = await parser.dereference(rel("specs/deep-circular/deep-circular.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // The "circular" flag should be set @@ -45,8 +45,8 @@ describe("Schema with deeply-nested circular $refs", () => { let parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/deep-circular/deep-circular.yaml"), { dereference: { circular: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/deep-circular/deep-circular.yaml"), { dereference: { circular: false }}); + shouldNotGetCalled(); } catch (err) { // A ReferenceError should have been thrown @@ -68,7 +68,7 @@ describe("Schema with deeply-nested circular $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/deep-circular/deep-circular.yaml")); + const schema = await parser.bundle(rel("specs/deep-circular/deep-circular.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); // The "circular" flag should NOT be set diff --git a/test/specs/deep/deep.spec.js b/test/specs/deep/deep.spec.js index f2a0ab2e..b23689d1 100644 --- a/test/specs/deep/deep.spec.js +++ b/test/specs/deep/deep.spec.js @@ -1,30 +1,30 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, name, requiredString } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with deeply-nested $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/deep/deep.yaml")); + const schema = await parser.parse(rel("specs/deep/deep.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/deep/deep.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/deep/deep.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/deep/deep.yaml"), - path.abs("specs/deep/deep.yaml"), parsedSchema.schema, - path.abs("specs/deep/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/deep/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully", testResolve( + rel("specs/deep/deep.yaml"), + abs("specs/deep/deep.yaml"), _schema, + abs("specs/deep/definitions/name.yaml"), name, + abs("specs/deep/definitions/required-string.yaml"), requiredString )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/deep/deep.yaml")); + const schema = await parser.dereference(rel("specs/deep/deep.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -39,7 +39,7 @@ describe("Schema with deeply-nested $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/deep/deep.yaml")); + const schema = await parser.bundle(rel("specs/deep/deep.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/dereference-callback/dereference-callback.spec.js b/test/specs/dereference-callback/dereference-callback.spec.js index b73585ef..3c9b550e 100644 --- a/test/specs/dereference-callback/dereference-callback.spec.js +++ b/test/specs/dereference-callback/dereference-callback.spec.js @@ -1,8 +1,6 @@ -"use strict"; - -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const { rel } = require("../../utils/path"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { rel } from "../../utils/path"; describe("Schema with a $ref", () => { it("should call onDereference", async () => { diff --git a/test/specs/empty/empty.spec.js b/test/specs/empty/empty.spec.js index 999a870e..07df9b56 100644 --- a/test/specs/empty/empty.spec.js +++ b/test/specs/empty/empty.spec.js @@ -1,47 +1,47 @@ -const { expect } = require("chai"); -const $RefParser = require("../../../lib"); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); +import { expect } from "chai"; +import $RefParser, { parse as _parse } from "../../../lib"; +import { testResolve, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; describe("Empty schema", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/empty/empty.json")); + const schema = await parser.parse(rel("specs/empty/empty.json")); expect(schema).to.be.an("object"); expect(schema).to.be.empty; // eslint-disable-line no-unused-expressions expect(parser.schema).to.equal(schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/empty/empty.json")]); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/empty/empty.json")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/empty/empty.json"), - path.abs("specs/empty/empty.json"), {} + it("should resolve successfully", testResolve( + rel("specs/empty/empty.json"), + abs("specs/empty/empty.json"), {} )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/empty/empty.json")); + const schema = await parser.dereference(rel("specs/empty/empty.json")); expect(schema).to.be.an("object"); expect(schema).to.be.empty; // eslint-disable-line no-unused-expressions expect(parser.schema).to.equal(schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/empty/empty.json")]); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/empty/empty.json")]); // The "circular" flag should NOT be set expect(parser.$refs.circular).to.equal(false); }); it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/empty/empty.json")); + const schema = await parser.bundle(rel("specs/empty/empty.json")); expect(schema).to.be.an("object"); expect(schema).to.be.empty; // eslint-disable-line no-unused-expressions expect(parser.schema).to.equal(schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/empty/empty.json")]); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/empty/empty.json")]); }); it('should throw an error if "parse.json.allowEmpty" is disabled', async () => { try { - await $RefParser.parse(path.rel("specs/empty/empty.json"), { parse: { json: { allowEmpty: false }}}); - helper.shouldNotGetCalled(); + await _parse(rel("specs/empty/empty.json"), { parse: { json: { allowEmpty: false }}}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); diff --git a/test/specs/error-source/error-source.spec.js b/test/specs/error-source/error-source.spec.js index 30c05497..5fac6fe6 100644 --- a/test/specs/error-source/error-source.spec.js +++ b/test/specs/error-source/error-source.spec.js @@ -1,12 +1,12 @@ -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const { InvalidPointerError, ResolverError, MissingPointerError } = require("../../../lib/util/errors"); +import $RefParser from "../../.."; +import { shouldNotGetCalled } from "../../utils/helper"; +import { abs, unixify } from "../../utils/path"; +import { InvalidPointerError, ResolverError, MissingPointerError } from "../../../lib/util/errors"; describe("Report correct error source and path for", () => { @@ -14,7 +14,7 @@ describe("Report correct error source and path for", () => { const parser = new $RefParser(); try { await parser.dereference({ foo: { bar: { $ref: "I do not exist" }}}, { continueOnError: true }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err.errors).to.containSubset([ @@ -31,14 +31,14 @@ describe("Report correct error source and path for", () => { it("schema with a local reference pointing at property with broken external reference", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.abs("specs/error-source/broken-external.json"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(abs("specs/error-source/broken-external.json"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err.errors).to.containSubset([ { name: ResolverError.name, - source: path.unixify(path.abs("specs/error-source/broken-external.json")), + source: unixify(abs("specs/error-source/broken-external.json")), path: ["components", "schemas", "testSchema", "properties", "test"], message: message => typeof message === "string", }, @@ -49,20 +49,20 @@ describe("Report correct error source and path for", () => { it("schema with a missing local pointer and reference pointing at external file with broken external", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.abs("specs/error-source/invalid-external.json"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(abs("specs/error-source/invalid-external.json"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err.errors).to.containSubset([ { name: MissingPointerError.name, - source: path.unixify(path.abs("specs/error-source/invalid-external.json")), + source: unixify(abs("specs/error-source/invalid-external.json")), path: ["foo", "bar"], message: message => typeof message === "string", }, { name: ResolverError.name, - source: path.unixify(path.abs("specs/error-source/broken-external.json")), + source: unixify(abs("specs/error-source/broken-external.json")), path: ["components", "schemas", "testSchema", "properties", "test"], message: message => typeof message === "string", }, @@ -73,14 +73,14 @@ describe("Report correct error source and path for", () => { it("schema with an invalid pointer", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.abs("specs/error-source/invalid-pointer.json"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(abs("specs/error-source/invalid-pointer.json"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err.errors).to.containSubset([ { name: InvalidPointerError.name, - source: path.unixify(path.abs("specs/error-source/invalid-pointer.json")), + source: unixify(abs("specs/error-source/invalid-pointer.json")), path: ["foo", "baz"], message: message => typeof message === "string", }, diff --git a/test/specs/exports.spec.js b/test/specs/exports.spec.js index 0a3c096d..2503d66e 100644 --- a/test/specs/exports.spec.js +++ b/test/specs/exports.spec.js @@ -1,16 +1,8 @@ /* eslint-disable require-await */ -const { expect } = require("chai"); -const commonJSExport = require("../../"); -const { default: defaultExport } = require("../../"); -const { - JSONParserError, - InvalidPointerError, - MissingPointerError, - ResolverError, - ParserError, - UnmatchedParserError, - UnmatchedResolverError, -} = require("../../"); +import { expect } from "chai"; +import commonJSExport from "../../"; +import { default as defaultExport } from "../../"; +import { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError } from "../../"; describe("json-schema-ref-parser package exports", () => { diff --git a/test/specs/external-from-internal/external-from-internal.spec.js b/test/specs/external-from-internal/external-from-internal.spec.js index b3d830ee..7f4eac95 100644 --- a/test/specs/external-from-internal/external-from-internal.spec.js +++ b/test/specs/external-from-internal/external-from-internal.spec.js @@ -1,10 +1,10 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve } from "../../utils/helper"; +import { abs, rel, url } from "../../utils/path"; +import { schema as _schema, definitions } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; /** * This test is from PR #62 @@ -13,49 +13,49 @@ const bundledSchema = require("./bundled"); describe("Schema with two external refs to the same value and internal ref before", () => { it("should parse successfully from an absolute path", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.abs("specs/external-from-internal/external-from-internal.yaml")); + const schema = await parser.parse(abs("specs/external-from-internal/external-from-internal.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external-from-internal/external-from-internal.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external-from-internal/external-from-internal.yaml")]); }); it("should parse successfully from a relative path", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/external-from-internal/external-from-internal.yaml")); + const schema = await parser.parse(rel("specs/external-from-internal/external-from-internal.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external-from-internal/external-from-internal.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external-from-internal/external-from-internal.yaml")]); }); it("should parse successfully from a url", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.url("specs/external-from-internal/external-from-internal.yaml")); + const schema = await parser.parse(url("specs/external-from-internal/external-from-internal.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.url("specs/external-from-internal/external-from-internal.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([url("specs/external-from-internal/external-from-internal.yaml")]); }); - it("should resolve successfully from an absolute path", helper.testResolve( - path.abs("specs/external-from-internal/external-from-internal.yaml"), - path.abs("specs/external-from-internal/external-from-internal.yaml"), parsedSchema.schema, - path.abs("specs/external-from-internal/definitions.yaml"), parsedSchema.definitions + it("should resolve successfully from an absolute path", testResolve( + abs("specs/external-from-internal/external-from-internal.yaml"), + abs("specs/external-from-internal/external-from-internal.yaml"), _schema, + abs("specs/external-from-internal/definitions.yaml"), definitions )); - it("should resolve successfully from a relative path", helper.testResolve( - path.rel("specs/external-from-internal/external-from-internal.yaml"), - path.abs("specs/external-from-internal/external-from-internal.yaml"), parsedSchema.schema, - path.abs("specs/external-from-internal/definitions.yaml"), parsedSchema.definitions + it("should resolve successfully from a relative path", testResolve( + rel("specs/external-from-internal/external-from-internal.yaml"), + abs("specs/external-from-internal/external-from-internal.yaml"), _schema, + abs("specs/external-from-internal/definitions.yaml"), definitions )); - it("should resolve successfully from a url", helper.testResolve( - path.url("specs/external-from-internal/external-from-internal.yaml"), - path.url("specs/external-from-internal/external-from-internal.yaml"), parsedSchema.schema, - path.url("specs/external-from-internal/definitions.yaml"), parsedSchema.definitions + it("should resolve successfully from a url", testResolve( + url("specs/external-from-internal/external-from-internal.yaml"), + url("specs/external-from-internal/external-from-internal.yaml"), _schema, + url("specs/external-from-internal/definitions.yaml"), definitions )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/external-from-internal/external-from-internal.yaml")); + const schema = await parser.dereference(rel("specs/external-from-internal/external-from-internal.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -75,7 +75,7 @@ describe("Schema with two external refs to the same value and internal ref befor it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/external-from-internal/external-from-internal.yaml")); + const schema = await parser.bundle(rel("specs/external-from-internal/external-from-internal.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/external-multiple/external-multiple.spec.js b/test/specs/external-multiple/external-multiple.spec.js index 9cefc0cd..377e9605 100644 --- a/test/specs/external-multiple/external-multiple.spec.js +++ b/test/specs/external-multiple/external-multiple.spec.js @@ -1,29 +1,29 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve } from "../../utils/helper"; +import { abs, rel } from "../../utils/path"; +import { schema as _schema, definitions } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with multiple external $refs to different parts of a file", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.abs("specs/external-multiple/external-multiple.yaml")); + const schema = await parser.parse(abs("specs/external-multiple/external-multiple.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external-multiple/external-multiple.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external-multiple/external-multiple.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/external-multiple/external-multiple.yaml"), - path.abs("specs/external-multiple/external-multiple.yaml"), parsedSchema.schema, - path.abs("specs/external-multiple/definitions.yaml"), parsedSchema.definitions + it("should resolve successfully", testResolve( + rel("specs/external-multiple/external-multiple.yaml"), + abs("specs/external-multiple/external-multiple.yaml"), _schema, + abs("specs/external-multiple/definitions.yaml"), definitions )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/external-multiple/external-multiple.yaml")); + const schema = await parser.dereference(rel("specs/external-multiple/external-multiple.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -34,7 +34,7 @@ describe("Schema with multiple external $refs to different parts of a file", () it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/external-multiple/external-multiple.yaml")); + const schema = await parser.bundle(rel("specs/external-multiple/external-multiple.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/external-partial/external-partial.spec.js b/test/specs/external-partial/external-partial.spec.js index d35ab92a..82bf21e0 100644 --- a/test/specs/external-partial/external-partial.spec.js +++ b/test/specs/external-partial/external-partial.spec.js @@ -1,31 +1,31 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, definitions, name, requiredString } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with $refs to parts of external files", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/external-partial/external-partial.yaml")); + const schema = await parser.parse(rel("specs/external-partial/external-partial.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external-partial/external-partial.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external-partial/external-partial.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/external-partial/external-partial.yaml"), - path.abs("specs/external-partial/external-partial.yaml"), parsedSchema.schema, - path.abs("specs/external-partial/definitions/definitions.json"), parsedSchema.definitions, - path.abs("specs/external-partial/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/external-partial/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully", testResolve( + rel("specs/external-partial/external-partial.yaml"), + abs("specs/external-partial/external-partial.yaml"), _schema, + abs("specs/external-partial/definitions/definitions.json"), definitions, + abs("specs/external-partial/definitions/name.yaml"), name, + abs("specs/external-partial/definitions/required-string.yaml"), requiredString )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/external-partial/external-partial.yaml")); + const schema = await parser.dereference(rel("specs/external-partial/external-partial.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -37,7 +37,7 @@ describe("Schema with $refs to parts of external files", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/external-partial/external-partial.yaml")); + const schema = await parser.bundle(rel("specs/external-partial/external-partial.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/external/external.spec.js b/test/specs/external/external.spec.js index 78e28e3c..514d7f1f 100644 --- a/test/specs/external/external.spec.js +++ b/test/specs/external/external.spec.js @@ -1,63 +1,63 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve } from "../../utils/helper"; +import { abs, rel, url } from "../../utils/path"; +import { schema as _schema, definitions, name, requiredString } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with external $refs", () => { it("should parse successfully from an absolute path", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.abs("specs/external/external.yaml")); + const schema = await parser.parse(abs("specs/external/external.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external/external.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external/external.yaml")]); }); it("should parse successfully from a relative path", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/external/external.yaml")); + const schema = await parser.parse(rel("specs/external/external.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external/external.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/external/external.yaml")]); }); it("should parse successfully from a url", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.url("specs/external/external.yaml")); + const schema = await parser.parse(url("specs/external/external.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.url("specs/external/external.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([url("specs/external/external.yaml")]); }); - it("should resolve successfully from an absolute path", helper.testResolve( - path.abs("specs/external/external.yaml"), - path.abs("specs/external/external.yaml"), parsedSchema.schema, - path.abs("specs/external/definitions/definitions.json"), parsedSchema.definitions, - path.abs("specs/external/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/external/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully from an absolute path", testResolve( + abs("specs/external/external.yaml"), + abs("specs/external/external.yaml"), _schema, + abs("specs/external/definitions/definitions.json"), definitions, + abs("specs/external/definitions/name.yaml"), name, + abs("specs/external/definitions/required-string.yaml"), requiredString )); - it("should resolve successfully from a relative path", helper.testResolve( - path.rel("specs/external/external.yaml"), - path.abs("specs/external/external.yaml"), parsedSchema.schema, - path.abs("specs/external/definitions/definitions.json"), parsedSchema.definitions, - path.abs("specs/external/definitions/name.yaml"), parsedSchema.name, - path.abs("specs/external/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully from a relative path", testResolve( + rel("specs/external/external.yaml"), + abs("specs/external/external.yaml"), _schema, + abs("specs/external/definitions/definitions.json"), definitions, + abs("specs/external/definitions/name.yaml"), name, + abs("specs/external/definitions/required-string.yaml"), requiredString )); - it("should resolve successfully from a url", helper.testResolve( - path.url("specs/external/external.yaml"), - path.url("specs/external/external.yaml"), parsedSchema.schema, - path.url("specs/external/definitions/definitions.json"), parsedSchema.definitions, - path.url("specs/external/definitions/name.yaml"), parsedSchema.name, - path.url("specs/external/definitions/required-string.yaml"), parsedSchema.requiredString + it("should resolve successfully from a url", testResolve( + url("specs/external/external.yaml"), + url("specs/external/external.yaml"), _schema, + url("specs/external/definitions/definitions.json"), definitions, + url("specs/external/definitions/name.yaml"), name, + url("specs/external/definitions/required-string.yaml"), requiredString )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/external/external.yaml")); + const schema = await parser.dereference(rel("specs/external/external.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -73,7 +73,7 @@ describe("Schema with external $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/external/external.yaml")); + const schema = await parser.bundle(rel("specs/external/external.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/http.spec.js b/test/specs/http.spec.js index 3aa36943..568cf060 100644 --- a/test/specs/http.spec.js +++ b/test/specs/http.spec.js @@ -1,6 +1,6 @@ -const { host } = require("@jsdevtools/host-environment"); -const { expect } = require("chai"); -const $RefParser = require("../../lib"); +import { host } from "@jsdevtools/host-environment"; +import { expect } from "chai"; +import $RefParser from "../../lib"; const isWindows = /^win/.test(globalThis.process?.platform); diff --git a/test/specs/internal/internal.spec.js b/test/specs/internal/internal.spec.js index 0a07b4b5..83190ef8 100644 --- a/test/specs/internal/internal.spec.js +++ b/test/specs/internal/internal.spec.js @@ -1,28 +1,28 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import parsedSchema from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with internal $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/internal/internal.yaml")); + const schema = await parser.parse(rel("specs/internal/internal.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(parsedSchema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/internal/internal.yaml")]); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/internal/internal.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/internal/internal.yaml"), - path.abs("specs/internal/internal.yaml"), parsedSchema + it("should resolve successfully", testResolve( + rel("specs/internal/internal.yaml"), + abs("specs/internal/internal.yaml"), parsedSchema )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/internal/internal.yaml")); + const schema = await parser.dereference(rel("specs/internal/internal.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -38,7 +38,7 @@ describe("Schema with internal $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/internal/internal.yaml")); + const schema = await parser.bundle(rel("specs/internal/internal.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/invalid-pointers/invalid-pointers.js b/test/specs/invalid-pointers/invalid-pointers.js index f20224fa..e5d3deac 100644 --- a/test/specs/invalid-pointers/invalid-pointers.js +++ b/test/specs/invalid-pointers/invalid-pointers.js @@ -1,17 +1,17 @@ -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../../lib"); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const { JSONParserErrorGroup, InvalidPointerError } = require("../../../lib/util/errors"); +import $RefParser, { dereference } from "../../../lib"; +import { shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs, unixify } from "../../utils/path"; +import { JSONParserErrorGroup, InvalidPointerError } from "../../../lib/util/errors"; describe("Schema with invalid pointers", () => { it("should throw an error for an invalid pointer", async () => { try { - await $RefParser.dereference(path.rel("specs/invalid-pointers/invalid.json")); - helper.shouldNotGetCalled(); + await dereference(rel("specs/invalid-pointers/invalid.json")); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(InvalidPointerError); @@ -22,19 +22,19 @@ describe("Schema with invalid pointers", () => { it("should throw a grouped error for an invalid pointer if continueOnError is true", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/invalid-pointers/invalid.json"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/invalid-pointers/invalid.json"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); expect(err.files).to.equal(parser); - expect(err.message).to.equal(`1 error occurred while reading '${path.abs("specs/invalid-pointers/invalid.json")}'`); + expect(err.message).to.equal(`1 error occurred while reading '${abs("specs/invalid-pointers/invalid.json")}'`); expect(err.errors).to.containSubset([ { name: InvalidPointerError.name, message: "Invalid $ref pointer \"f\". Pointers must begin with \"#/\"", path: ["foo"], - source: path.unixify(path.abs("specs/invalid-pointers/invalid.json")), + source: unixify(abs("specs/invalid-pointers/invalid.json")), } ]); } diff --git a/test/specs/invalid/invalid.spec.js b/test/specs/invalid/invalid.spec.js index 1f4488a9..cc18fabf 100644 --- a/test/specs/invalid/invalid.spec.js +++ b/test/specs/invalid/invalid.spec.js @@ -1,12 +1,12 @@ -const { host } = require("@jsdevtools/host-environment"); -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import { host } from "@jsdevtools/host-environment"; +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../../lib"); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const { JSONParserErrorGroup, ParserError, ResolverError } = require("../../../lib/util/errors"); +import $RefParser, { dereference } from "../../../lib"; +import { shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { JSONParserErrorGroup, ParserError, ResolverError } from "../../../lib/util/errors"; const isWindows = /^win/.test(globalThis.process?.platform); const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath; @@ -15,8 +15,8 @@ describe("Invalid syntax", () => { describe("in main file", () => { it("should throw an error for an invalid file path", async () => { try { - await $RefParser.dereference("this file does not exist"); - helper.shouldNotGetCalled(); + await dereference("this file does not exist"); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ResolverError); @@ -29,8 +29,8 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid YAML file", async () => { try { - await $RefParser.dereference(path.rel("specs/invalid/invalid.yaml")); - helper.shouldNotGetCalled(); + await dereference(rel("specs/invalid/invalid.yaml")); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -41,8 +41,8 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid JSON file", async () => { try { - await $RefParser.dereference(path.rel("specs/invalid/invalid.json")); - helper.shouldNotGetCalled(); + await dereference(rel("specs/invalid/invalid.json")); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -53,8 +53,8 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid JSON file with YAML disabled", async () => { try { - await $RefParser.dereference(path.rel("specs/invalid/invalid.json"), { parse: { yaml: false }}); - helper.shouldNotGetCalled(); + await dereference(rel("specs/invalid/invalid.json"), { parse: { yaml: false }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -65,8 +65,8 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid YAML file with JSON and YAML disabled", async () => { try { - await $RefParser.dereference(path.rel("specs/invalid/invalid.yaml"), { parse: { yaml: false, json: false }}); - helper.shouldNotGetCalled(); + await dereference(rel("specs/invalid/invalid.yaml"), { parse: { yaml: false, json: false }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -79,7 +79,7 @@ describe("Invalid syntax", () => { const parser = new $RefParser(); try { await parser.dereference("this file does not exist", { continueOnError: true }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -101,13 +101,13 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid YAML file", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/invalid/invalid.yaml"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/invalid/invalid.yaml"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); expect(err.files).to.equal(parser); - expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${path.abs("specs/invalid/invalid.yaml")}'`); + expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${abs("specs/invalid/invalid.yaml")}'`); expect(err.errors.length).to.equal(1); expect(err.errors).to.containSubset([ { @@ -125,13 +125,13 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid JSON file", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/invalid/invalid.json"), { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/invalid/invalid.json"), { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); expect(err.files).to.equal(parser); - expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${path.abs("specs/invalid/invalid.json")}'`); + expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${abs("specs/invalid/invalid.json")}'`); expect(err.errors.length).to.equal(1); expect(err.errors).to.containSubset([ { @@ -149,13 +149,13 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid JSON file with YAML disabled", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.rel("specs/invalid/invalid.json"), { continueOnError: true, parse: { yaml: false }}); - helper.shouldNotGetCalled(); + await parser.dereference(rel("specs/invalid/invalid.json"), { continueOnError: true, parse: { yaml: false }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); expect(err.files).to.equal(parser); - expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${path.abs("specs/invalid/invalid.json")}'`); + expect(getPathFromOs(err.message)).to.equal(`1 error occurred while reading '${abs("specs/invalid/invalid.json")}'`); expect(err.errors.length).to.equal(1); expect(err.errors).to.containSubset([ { @@ -177,7 +177,7 @@ describe("Invalid syntax", () => { it("should not throw an error for an invalid YAML file with JSON and YAML disabled", async () => { const parser = new $RefParser(); - const result = await parser.dereference(path.rel("specs/invalid/invalid.yaml"), { continueOnError: true, parse: { yaml: false, json: false }}); + const result = await parser.dereference(rel("specs/invalid/invalid.yaml"), { continueOnError: true, parse: { yaml: false, json: false }}); expect(result).to.equal(null); }); }); @@ -186,8 +186,8 @@ describe("Invalid syntax", () => { describe("in referenced files", () => { it("should throw an error for an invalid YAML file", async () => { try { - await $RefParser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.yaml") }}); - helper.shouldNotGetCalled(); + await dereference({ foo: { $ref: rel("specs/invalid/invalid.yaml") }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -198,8 +198,8 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid JSON file", async () => { try { - await $RefParser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.json") }}); - helper.shouldNotGetCalled(); + await dereference({ foo: { $ref: rel("specs/invalid/invalid.json") }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -210,10 +210,10 @@ describe("Invalid syntax", () => { it("should throw an error for an invalid JSON file with YAML disabled", async () => { try { - await $RefParser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.json") }}, { + await dereference({ foo: { $ref: rel("specs/invalid/invalid.json") }}, { parse: { yaml: false } }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -223,10 +223,9 @@ describe("Invalid syntax", () => { }); it("should throw a grouped error for an invalid YAML file with JSON and YAML disabled", async () => { - const schema = await $RefParser - .dereference({ foo: { $ref: path.rel("specs/invalid/invalid.yaml") }}, { - parse: { yaml: false, json: false } - }); + const schema = await dereference({ foo: { $ref: rel("specs/invalid/invalid.yaml") }}, { + parse: { yaml: false, json: false } + }); // Because the JSON and YAML parsers were disabled, the invalid YAML file got parsed as plain text expect(schema).to.deep.equal({ @@ -239,7 +238,7 @@ describe("Invalid syntax", () => { try { const parser = new $RefParser(); await parser.dereference({ foo: { $ref: "this file does not exist" }}, { continueOnError: true }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -259,8 +258,8 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid YAML file", async () => { try { const parser = new $RefParser(); - await parser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.yaml") }}, { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference({ foo: { $ref: rel("specs/invalid/invalid.yaml") }}, { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -282,8 +281,8 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid JSON file", async () => { try { const parser = new $RefParser(); - await parser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.json") }}, { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference({ foo: { $ref: rel("specs/invalid/invalid.json") }}, { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -305,8 +304,8 @@ describe("Invalid syntax", () => { it("should throw a grouped error for an invalid JSON file with YAML disabled", async () => { try { const parser = new $RefParser(); - await parser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.json") }}, { continueOnError: true, parse: { yaml: false }}); - helper.shouldNotGetCalled(); + await parser.dereference({ foo: { $ref: rel("specs/invalid/invalid.json") }}, { continueOnError: true, parse: { yaml: false }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -332,7 +331,7 @@ describe("Invalid syntax", () => { it("should not throw an error for an invalid YAML file with JSON and YAML disabled", async () => { const parser = new $RefParser(); - const result = await parser.dereference({ foo: { $ref: path.rel("specs/invalid/invalid.yaml") }}, { continueOnError: true, parse: { yaml: false, json: false }}); + const result = await parser.dereference({ foo: { $ref: rel("specs/invalid/invalid.yaml") }}, { continueOnError: true, parse: { yaml: false, json: false }}); expect(result).to.deep.equal({ foo: ":\n" }); }); }); diff --git a/test/specs/missing-pointers/missing-pointers.spec.js b/test/specs/missing-pointers/missing-pointers.spec.js index d12ed193..64b046ed 100644 --- a/test/specs/missing-pointers/missing-pointers.spec.js +++ b/test/specs/missing-pointers/missing-pointers.spec.js @@ -1,17 +1,17 @@ -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../../lib"); -const { JSONParserErrorGroup, MissingPointerError } = require("../../../lib/util/errors"); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); +import $RefParser, { dereference } from "../../../lib"; +import { JSONParserErrorGroup, MissingPointerError } from "../../../lib/util/errors"; +import { shouldNotGetCalled } from "../../utils/helper"; +import { abs } from "../../utils/path"; describe("Schema with missing pointers", () => { it("should throw an error for missing pointer", async () => { try { - await $RefParser.dereference({ foo: { $ref: "#/baz" }}); - helper.shouldNotGetCalled(); + await dereference({ foo: { $ref: "#/baz" }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(MissingPointerError); @@ -21,8 +21,8 @@ describe("Schema with missing pointers", () => { it("should throw an error for missing pointer in external file", async () => { try { - await $RefParser.dereference({ foo: { $ref: path.abs("specs/missing-pointers/external-from-internal.yaml") }}); - helper.shouldNotGetCalled(); + await dereference({ foo: { $ref: abs("specs/missing-pointers/external-from-internal.yaml") }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(MissingPointerError); @@ -35,7 +35,7 @@ describe("Schema with missing pointers", () => { const parser = new $RefParser(); try { await parser.dereference({ foo: { $ref: "#/baz" }}, { continueOnError: true }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); @@ -56,8 +56,8 @@ describe("Schema with missing pointers", () => { it("should throw an error for missing pointer in external file", async () => { const parser = new $RefParser(); try { - await parser.dereference({ foo: { $ref: path.abs("specs/missing-pointers/external-from-internal.yaml") }}, { continueOnError: true }); - helper.shouldNotGetCalled(); + await parser.dereference({ foo: { $ref: abs("specs/missing-pointers/external-from-internal.yaml") }}, { continueOnError: true }); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); diff --git a/test/specs/no-refs/no-refs.spec.js b/test/specs/no-refs/no-refs.spec.js index aa374523..3d8aec93 100644 --- a/test/specs/no-refs/no-refs.spec.js +++ b/test/specs/no-refs/no-refs.spec.js @@ -1,26 +1,26 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import parsedSchema from "./parsed"; describe("Schema without any $refs", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/no-refs/no-refs.yaml")); + const schema = await parser.parse(rel("specs/no-refs/no-refs.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(parsedSchema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/no-refs/no-refs.yaml")]); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/no-refs/no-refs.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/no-refs/no-refs.yaml"), - path.abs("specs/no-refs/no-refs.yaml"), parsedSchema + it("should resolve successfully", testResolve( + rel("specs/no-refs/no-refs.yaml"), + abs("specs/no-refs/no-refs.yaml"), parsedSchema )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/no-refs/no-refs.yaml")); + const schema = await parser.dereference(rel("specs/no-refs/no-refs.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(parsedSchema); // The "circular" flag should NOT be set @@ -29,7 +29,7 @@ describe("Schema without any $refs", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/no-refs/no-refs.yaml")); + const schema = await parser.bundle(rel("specs/no-refs/no-refs.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(parsedSchema); }); diff --git a/test/specs/object-source-with-path/object-source-with-path.spec.js b/test/specs/object-source-with-path/object-source-with-path.spec.js index 5caa9211..dbdc883e 100644 --- a/test/specs/object-source-with-path/object-source-with-path.spec.js +++ b/test/specs/object-source-with-path/object-source-with-path.spec.js @@ -1,28 +1,28 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const internalRefsParsedSchema = require("../internal/parsed"); -const internalRefsDereferencedSchema = require("../internal/dereferenced"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { cloneDeep } from "../../utils/helper"; +import { abs, rel } from "../../utils/path"; +import internalRefsParsedSchema from "../internal/parsed"; +import internalRefsDereferencedSchema from "../internal/dereferenced"; +import { schema as _schema } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Object sources with file paths", () => { it("should dereference a single object", async () => { let parser = new $RefParser(); const schema = await parser.dereference( // This file doesn't actually need to exist. But its path will be used to resolve external $refs - path.abs("path/that/does/not/exist.yaml"), + abs("path/that/does/not/exist.yaml"), // This schema object does not contain any external $refs - helper.cloneDeep(internalRefsParsedSchema), + cloneDeep(internalRefsParsedSchema), // An options object MUST be passed (even if it's empty) {}); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(internalRefsDereferencedSchema); // The schema path should match the one we pass-in let expectedPaths = [ - path.abs("path/that/does/not/exist.yaml") + abs("path/that/does/not/exist.yaml") ]; expect(parser.$refs.paths()).to.have.same.members(expectedPaths); expect(parser.$refs.values()).to.have.keys(expectedPaths); @@ -39,9 +39,9 @@ describe("Object sources with file paths", () => { let parser = new $RefParser(); const schema = await parser.dereference( // This file doesn't actually need to exist. But its path will be used to resolve external $refs - path.abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), + abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), // This schema object contains external $refs - helper.cloneDeep(parsedSchema.schema), + cloneDeep(_schema), // An options object MUST be passed (even if it's empty) {}); expect(schema).to.equal(parser.schema); @@ -49,10 +49,10 @@ describe("Object sources with file paths", () => { // The schema path should match the one we passed-in. // All other paths should be the actual paths of referenced files. let expectedPaths = [ - path.abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), - path.abs("specs/object-source-with-path/definitions/definitions.json"), - path.abs("specs/object-source-with-path/definitions/name.yaml"), - path.abs("specs/object-source-with-path/definitions/required-string.yaml") + abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), + abs("specs/object-source-with-path/definitions/definitions.json"), + abs("specs/object-source-with-path/definitions/name.yaml"), + abs("specs/object-source-with-path/definitions/required-string.yaml") ]; expect(parser.$refs.paths()).to.have.same.members(expectedPaths); expect(parser.$refs.values()).to.have.keys(expectedPaths); @@ -71,9 +71,9 @@ describe("Object sources with file paths", () => { let parser = new $RefParser(); const schema = await parser.bundle( // This file doesn't actually need to exist. But its path will be used to resolve external $refs - path.rel("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), + rel("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), // This schema object contains external $refs - helper.cloneDeep(parsedSchema.schema), + cloneDeep(_schema), // An options object MUST be passed (even if it's empty) {}); expect(schema).to.equal(parser.schema); @@ -81,10 +81,10 @@ describe("Object sources with file paths", () => { // The schema path should match the one we passed-in. // All other paths should be the actual paths of referenced files. let expectedPaths = [ - path.abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), - path.abs("specs/object-source-with-path/definitions/definitions.json"), - path.abs("specs/object-source-with-path/definitions/name.yaml"), - path.abs("specs/object-source-with-path/definitions/required-string.yaml") + abs("specs/object-source-with-path/schema-file-that-does-not-exist.yaml"), + abs("specs/object-source-with-path/definitions/definitions.json"), + abs("specs/object-source-with-path/definitions/name.yaml"), + abs("specs/object-source-with-path/definitions/required-string.yaml") ]; expect(parser.$refs.paths()).to.have.same.members(expectedPaths); expect(parser.$refs.values()).to.have.keys(expectedPaths); diff --git a/test/specs/object-source/object-source.spec.js b/test/specs/object-source/object-source.spec.js index f4b7563c..395cb0aa 100644 --- a/test/specs/object-source/object-source.spec.js +++ b/test/specs/object-source/object-source.spec.js @@ -1,24 +1,24 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const internalRefsParsedSchema = require("../internal/parsed"); -const internalRefsDereferencedSchema = require("../internal/dereferenced"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { cloneDeep } from "../../utils/helper"; +import { cwd, abs } from "../../utils/path"; +import internalRefsParsedSchema from "../internal/parsed"; +import internalRefsDereferencedSchema from "../internal/dereferenced"; +import { schema as _schema } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; const isWindows = /^win/.test(globalThis.process?.platform); describe("Object sources (instead of file paths)", () => { it("should dereference a single object", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(helper.cloneDeep(internalRefsParsedSchema)); + const schema = await parser.dereference(cloneDeep(internalRefsParsedSchema)); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(internalRefsDereferencedSchema); // The schema path should be the current directory let expectedPaths = [ - path.cwd() + cwd() ]; if (!isWindows) { expect(parser.$refs.paths()).to.have.same.members(expectedPaths); @@ -35,15 +35,15 @@ describe("Object sources (instead of file paths)", () => { it("should dereference an object that references external files", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(helper.cloneDeep(parsedSchema.schema)); + const schema = await parser.dereference(cloneDeep(_schema)); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // The schema path should be the current directory, and all other paths should be absolute let expectedPaths = [ - path.cwd(), - path.abs("specs/object-source/definitions/definitions.json"), - path.abs("specs/object-source/definitions/name.yaml"), - path.abs("specs/object-source/definitions/required-string.yaml") + cwd(), + abs("specs/object-source/definitions/definitions.json"), + abs("specs/object-source/definitions/name.yaml"), + abs("specs/object-source/definitions/required-string.yaml") ]; if (!isWindows) { expect(parser.$refs.paths()).to.have.same.members(expectedPaths); @@ -62,15 +62,15 @@ describe("Object sources (instead of file paths)", () => { it("should bundle an object that references external files", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(helper.cloneDeep(parsedSchema.schema)); + const schema = await parser.bundle(cloneDeep(_schema)); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); // The schema path should be the current directory, and all other paths should be absolute let expectedPaths = [ - path.cwd(), - path.abs("specs/object-source/definitions/definitions.json"), - path.abs("specs/object-source/definitions/name.yaml"), - path.abs("specs/object-source/definitions/required-string.yaml") + cwd(), + abs("specs/object-source/definitions/definitions.json"), + abs("specs/object-source/definitions/name.yaml"), + abs("specs/object-source/definitions/required-string.yaml") ]; if (!isWindows) { expect(parser.$refs.paths()).to.have.same.members(expectedPaths); diff --git a/test/specs/object-source/parsed.js b/test/specs/object-source/parsed.js index 9f6fa301..ce12c145 100644 --- a/test/specs/object-source/parsed.js +++ b/test/specs/object-source/parsed.js @@ -1,85 +1,79 @@ -const path = require("../../utils/path"); - -module.exports = -{ - schema: { - definitions: { - // Because we're not specifying a path, the current directory (the "test" directory) - // will be assumed. So this path must be relative to the "test" directory. - $ref: path.rel("specs/object-source/definitions/definitions.json") - }, - required: [ - "name" - ], - type: "object", - properties: { - gender: { - enum: [ - "male", - "female" - ], - type: "string" - }, - age: { - minimum: 0, - type: "integer" - }, - name: { - $ref: "#/definitions/name" - } - }, - title: "Person" - }, +import { rel } from "../../utils/path"; +export const schema = { definitions: { - requiredString: { - $ref: "required-string.yaml" + // Because we're not specifying a path, the current directory (the "test" directory) + // will be assumed. So this path must be relative to the "test" directory. + $ref: rel("specs/object-source/definitions/definitions.json") + }, + required: [ + "name" + ], + type: "object", + properties: { + gender: { + enum: [ + "male", + "female" + ], + type: "string" }, - string: { - $ref: "#/requiredString/type" + age: { + minimum: 0, + type: "integer" }, name: { - $ref: "../definitions/name.yaml" + $ref: "#/definitions/name" } }, - + title: "Person" +}; +export const definitions = { + requiredString: { + $ref: "required-string.yaml" + }, + string: { + $ref: "#/requiredString/type" + }, name: { - required: [ - "first", - "last" - ], - type: "object", - properties: { - middle: { - minLength: { - $ref: "#/properties/first/minLength" - }, - type: { - $ref: "#/properties/first/type" - } - }, - prefix: { - minLength: 3, - $ref: "#/properties/last" - }, - last: { - $ref: "./required-string.yaml" - }, - suffix: { - $ref: "#/properties/prefix", - type: "string", - maxLength: 3 + $ref: "../definitions/name.yaml" + } +}; +export const name = { + required: [ + "first", + "last" + ], + type: "object", + properties: { + middle: { + minLength: { + $ref: "#/properties/first/minLength" }, - first: { - $ref: "../definitions/definitions.json#/requiredString" + type: { + $ref: "#/properties/first/type" } }, - title: "name" + prefix: { + minLength: 3, + $ref: "#/properties/last" + }, + last: { + $ref: "./required-string.yaml" + }, + suffix: { + $ref: "#/properties/prefix", + type: "string", + maxLength: 3 + }, + first: { + $ref: "../definitions/definitions.json#/requiredString" + } }, - - requiredString: { - minLength: 1, - type: "string", - title: "requiredString" - } + title: "name" +}; +export const requiredString = { + minLength: 1, + type: "string", + title: "requiredString" }; diff --git a/test/specs/parsers/parsers.spec.js b/test/specs/parsers/parsers.spec.js index 2c4b15e5..966f7d72 100644 --- a/test/specs/parsers/parsers.spec.js +++ b/test/specs/parsers/parsers.spec.js @@ -1,51 +1,49 @@ -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const { JSONParserErrorGroup, ParserError, UnmatchedParserError } = require("../../../lib/util/errors"); +import $RefParser, { parse as _parse, bundle, dereference } from "../../.."; +import { testResolve, convertNodeBuffersToPOJOs, shouldNotGetCalled } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema } from "./parsed"; +import { defaultParsers, binaryParser, staticParser as _staticParser, customParser } from "./dereferenced"; +import { JSONParserErrorGroup, ParserError, UnmatchedParserError } from "../../../lib/util/errors"; describe("References to non-JSON files", () => { it("should parse successfully", async () => { - const schema = await $RefParser - .parse(path.rel("specs/parsers/parsers.yaml")); - expect(schema).to.deep.equal(parsedSchema.schema); + const schema = await _parse(rel("specs/parsers/parsers.yaml")); + expect(schema).to.deep.equal(_schema); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/parsers/parsers.yaml"), - path.abs("specs/parsers/parsers.yaml"), parsedSchema.schema, - path.abs("specs/parsers/files/README.md"), dereferencedSchema.defaultParsers.definitions.markdown, - path.abs("specs/parsers/files/page.html"), dereferencedSchema.defaultParsers.definitions.html, - path.abs("specs/parsers/files/style.css"), dereferencedSchema.defaultParsers.definitions.css, - path.abs("specs/parsers/files/binary.png"), dereferencedSchema.defaultParsers.definitions.binary, - path.abs("specs/parsers/files/unknown.foo"), dereferencedSchema.defaultParsers.definitions.unknown, - path.abs("specs/parsers/files/empty"), dereferencedSchema.defaultParsers.definitions.empty + it("should resolve successfully", testResolve( + rel("specs/parsers/parsers.yaml"), + abs("specs/parsers/parsers.yaml"), _schema, + abs("specs/parsers/files/README.md"), defaultParsers.definitions.markdown, + abs("specs/parsers/files/page.html"), defaultParsers.definitions.html, + abs("specs/parsers/files/style.css"), defaultParsers.definitions.css, + abs("specs/parsers/files/binary.png"), defaultParsers.definitions.binary, + abs("specs/parsers/files/unknown.foo"), defaultParsers.definitions.unknown, + abs("specs/parsers/files/empty"), defaultParsers.definitions.empty )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/parsers/parsers.yaml")); + const schema = await parser.dereference(rel("specs/parsers/parsers.yaml")); expect(schema).to.equal(parser.schema); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.defaultParsers); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(defaultParsers); // The "circular" flag should NOT be set expect(parser.$refs.circular).to.equal(false); }); it("should bundle successfully", async () => { - const schema = await $RefParser - .bundle(path.rel("specs/parsers/parsers.yaml")); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.defaultParsers); + const schema = await bundle(rel("specs/parsers/parsers.yaml")); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(defaultParsers); }); it('should parse text as binary if "parse.text" is disabled', async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // Disable the text parser text: false, @@ -57,18 +55,18 @@ describe("References to non-JSON files", () => { } } }); - schema.definitions.markdown = helper.convertNodeBuffersToPOJOs(schema.definitions.markdown); - schema.definitions.html = helper.convertNodeBuffersToPOJOs(schema.definitions.html); - schema.definitions.css = helper.convertNodeBuffersToPOJOs(schema.definitions.css); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - schema.definitions.unknown = helper.convertNodeBuffersToPOJOs(schema.definitions.unknown); - schema.definitions.empty = helper.convertNodeBuffersToPOJOs(schema.definitions.empty); - expect(schema).to.deep.equal(dereferencedSchema.binaryParser); + schema.definitions.markdown = convertNodeBuffersToPOJOs(schema.definitions.markdown); + schema.definitions.html = convertNodeBuffersToPOJOs(schema.definitions.html); + schema.definitions.css = convertNodeBuffersToPOJOs(schema.definitions.css); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + schema.definitions.unknown = convertNodeBuffersToPOJOs(schema.definitions.unknown); + schema.definitions.empty = convertNodeBuffersToPOJOs(schema.definitions.empty); + expect(schema).to.deep.equal(binaryParser); }); it("should throw an error if no parser can be matched", async () => { try { - await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + await dereference(rel("specs/parsers/parsers.yaml"), { parse: { yaml: false, json: false, @@ -76,7 +74,7 @@ describe("References to non-JSON files", () => { binary: false, }, }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -87,7 +85,7 @@ describe("References to non-JSON files", () => { it("should throw an error if no parser returned a result", async () => { try { - await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + await dereference(rel("specs/parsers/parsers.yaml"), { parse: { yaml: { canParse: true, @@ -99,7 +97,7 @@ describe("References to non-JSON files", () => { binary: false, }, }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { // would time out otherwise @@ -110,8 +108,8 @@ describe("References to non-JSON files", () => { it('should throw an error if "parse.text" and "parse.binary" are disabled', async () => { try { - await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { parse: { text: false, binary: false }}); - helper.shouldNotGetCalled(); + await dereference(rel("specs/parsers/parsers.yaml"), { parse: { text: false, binary: false }}); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(ParserError); @@ -120,7 +118,7 @@ describe("References to non-JSON files", () => { }); it("should use a custom parser with static values", async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that always returns the same value staticParser: { @@ -130,11 +128,11 @@ describe("References to non-JSON files", () => { } } }); - expect(schema).to.deep.equal(dereferencedSchema.staticParser); + expect(schema).to.deep.equal(_staticParser); }); it("should use a custom parser that returns a value", async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that returns the contents of ".foo" files, in reverse reverseFooParser: { @@ -147,12 +145,12 @@ describe("References to non-JSON files", () => { } } }); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.customParser); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(customParser); }); it("should use a custom parser that calls a callback", async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that returns the contents of ".foo" files, in reverse reverseFooParser: { @@ -164,12 +162,12 @@ describe("References to non-JSON files", () => { } } }); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.customParser); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(customParser); }); it("should use a custom parser that returns a promise", async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that returns the contents of ".foo" files, in reverse reverseFooParser: { @@ -183,12 +181,12 @@ describe("References to non-JSON files", () => { } } }); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.customParser); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(customParser); }); it("should continue parsing if a custom parser fails", async () => { - const schema = await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + const schema = await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that always fails, // so the built-in parsers will be used as a fallback @@ -201,13 +199,13 @@ describe("References to non-JSON files", () => { } } }); - schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary); - expect(schema).to.deep.equal(dereferencedSchema.defaultParsers); + schema.definitions.binary = convertNodeBuffersToPOJOs(schema.definitions.binary); + expect(schema).to.deep.equal(defaultParsers); }); it("should normalize errors thrown by parsers", async () => { try { - await $RefParser.dereference(path.rel("specs/parsers/parsers.yaml"), { + await dereference(rel("specs/parsers/parsers.yaml"), { parse: { // A custom parser that always fails, // so the built-in parsers will be used as a fallback @@ -219,7 +217,7 @@ describe("References to non-JSON files", () => { } } }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(ParserError); @@ -231,7 +229,7 @@ describe("References to non-JSON files", () => { it("should throw a grouped error if no parser can be matched and continueOnError is true", async () => { try { const parser = new $RefParser(); - await parser.dereference(path.rel("specs/parsers/parsers.yaml"), { + await parser.dereference(rel("specs/parsers/parsers.yaml"), { parse: { yaml: false, json: false, @@ -240,7 +238,7 @@ describe("References to non-JSON files", () => { }, continueOnError: true, }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); diff --git a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js index 8499e692..9d6e8103 100644 --- a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js +++ b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js @@ -1,12 +1,12 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const path = require("../../utils/path"); -const dereferencedSchema = require("./dereferenced"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { rel } from "../../utils/path"; +import dereferencedSchema from "./dereferenced"; describe("Schema with literal $refs in examples", () => { it("should exclude the given paths from dereferencing", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/ref-in-excluded-path/ref-in-excluded-path.yaml"), { + const schema = await parser.dereference(rel("specs/ref-in-excluded-path/ref-in-excluded-path.yaml"), { dereference: { excludedPathMatcher: (schemaPath) => { return /\/example(\/|$|s\/[^\/]+\/value(\/|$))/.test(schemaPath); diff --git a/test/specs/refs.spec.js b/test/specs/refs.spec.js index 55e706df..c478e964 100644 --- a/test/specs/refs.spec.js +++ b/test/specs/refs.spec.js @@ -1,46 +1,45 @@ -const { host } = require("@jsdevtools/host-environment"); -const { expect } = require("chai"); -const $RefParser = require("../../lib"); -const helper = require("../utils/helper"); -const path = require("../utils/path"); -const parsedSchema = require("./external/parsed"); -const dereferencedSchema = require("./external/dereferenced"); -const bundledSchema = require("./external/bundled"); +import { host } from "@jsdevtools/host-environment"; +import { expect } from "chai"; +import $RefParser, { resolve } from "../../lib"; +import { shouldNotGetCalled } from "../utils/helper"; +import { abs, url } from "../utils/path"; +import { schema, definitions, name, requiredString } from "./external/parsed"; +import dereferencedSchema, { definitions as _definitions } from "./external/dereferenced"; +import bundledSchema, { definitions as __definitions } from "./external/bundled"; describe("$Refs object", () => { describe("paths", () => { it("should only contain the main file when calling `parse()`", async () => { let parser = new $RefParser(); - await parser.parse(path.abs("specs/external/external.yaml")); + await parser.parse(abs("specs/external/external.yaml")); let paths = parser.$refs.paths(); expect(paths).to.have.same.members([ - path.abs("specs/external/external.yaml") + abs("specs/external/external.yaml") ]); }); it("should contain all files when calling `resolve()`", async () => { let parser = new $RefParser(); - const $refs = await parser.resolve(path.abs("specs/external/external.yaml")); + const $refs = await parser.resolve(abs("specs/external/external.yaml")); expect($refs).to.equal(parser.$refs); let paths = $refs.paths(); expect(paths).to.have.same.members([ - path.abs("specs/external/external.yaml"), - path.abs("specs/external/definitions/definitions.json"), - path.abs("specs/external/definitions/name.yaml"), - path.abs("specs/external/definitions/required-string.yaml") + abs("specs/external/external.yaml"), + abs("specs/external/definitions/definitions.json"), + abs("specs/external/definitions/name.yaml"), + abs("specs/external/definitions/required-string.yaml") ]); }); it("should return only local files", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let paths = $refs.paths("file"); if (host.node) { expect(paths).to.have.same.members([ - path.abs("specs/external/external.yaml"), - path.abs("specs/external/definitions/definitions.json"), - path.abs("specs/external/definitions/name.yaml"), - path.abs("specs/external/definitions/required-string.yaml") + abs("specs/external/external.yaml"), + abs("specs/external/definitions/definitions.json"), + abs("specs/external/definitions/name.yaml"), + abs("specs/external/definitions/required-string.yaml") ]); } else { @@ -49,15 +48,14 @@ describe("$Refs object", () => { }); it("should return only URLs", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let paths = $refs.paths(["http"]); if (host.browser) { expect(paths).to.have.same.members([ - path.url("specs/external/external.yaml"), - path.url("specs/external/definitions/definitions.json"), - path.url("specs/external/definitions/name.yaml"), - path.url("specs/external/definitions/required-string.yaml") + url("specs/external/external.yaml"), + url("specs/external/definitions/definitions.json"), + url("specs/external/definitions/name.yaml"), + url("specs/external/definitions/required-string.yaml") ]); } else { @@ -68,57 +66,54 @@ describe("$Refs object", () => { describe("values", () => { it("should be the same as `toJSON()`", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); expect($refs.values).to.equal($refs.toJSON); }); it("should return the paths and values of all resolved files", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let expected = {}; - expected[path.abs("specs/external/external.yaml")] = parsedSchema.schema; - expected[path.abs("specs/external/definitions/definitions.json")] = parsedSchema.definitions; - expected[path.abs("specs/external/definitions/name.yaml")] = parsedSchema.name; - expected[path.abs("specs/external/definitions/required-string.yaml")] = parsedSchema.requiredString; + expected[abs("specs/external/external.yaml")] = schema; + expected[abs("specs/external/definitions/definitions.json")] = definitions; + expected[abs("specs/external/definitions/name.yaml")] = name; + expected[abs("specs/external/definitions/required-string.yaml")] = requiredString; let values = $refs.values(); expect(values).to.deep.equal(expected); }); it("should return the paths and values of all dereferenced files", async () => { let parser = new $RefParser(); - await parser.dereference(path.abs("specs/external/external.yaml")); + await parser.dereference(abs("specs/external/external.yaml")); let expected = {}; - expected[path.abs("specs/external/external.yaml")] = dereferencedSchema; - expected[path.abs("specs/external/definitions/definitions.json")] = dereferencedSchema.definitions; - expected[path.abs("specs/external/definitions/name.yaml")] = dereferencedSchema.definitions.name; - expected[path.abs("specs/external/definitions/required-string.yaml")] = dereferencedSchema.definitions["required string"]; + expected[abs("specs/external/external.yaml")] = dereferencedSchema; + expected[abs("specs/external/definitions/definitions.json")] = _definitions; + expected[abs("specs/external/definitions/name.yaml")] = _definitions.name; + expected[abs("specs/external/definitions/required-string.yaml")] = _definitions["required string"]; let values = parser.$refs.values(); expect(values).to.deep.equal(expected); }); it("should return the paths and values of all bundled files", async () => { let parser = new $RefParser(); - await parser.bundle(path.abs("specs/external/external.yaml")); + await parser.bundle(abs("specs/external/external.yaml")); let expected = {}; - expected[path.abs("specs/external/external.yaml")] = bundledSchema; - expected[path.abs("specs/external/definitions/definitions.json")] = bundledSchema.definitions; - expected[path.abs("specs/external/definitions/name.yaml")] = bundledSchema.definitions.name; - expected[path.abs("specs/external/definitions/required-string.yaml")] = bundledSchema.definitions["required string"]; + expected[abs("specs/external/external.yaml")] = bundledSchema; + expected[abs("specs/external/definitions/definitions.json")] = __definitions; + expected[abs("specs/external/definitions/name.yaml")] = __definitions.name; + expected[abs("specs/external/definitions/required-string.yaml")] = __definitions["required string"]; let values = parser.$refs.values(); expect(values).to.deep.equal(expected); }); it("should return only local files and values", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let values = $refs.values("file"); if (host.node) { let expected = {}; - expected[path.abs("specs/external/external.yaml")] = parsedSchema.schema; - expected[path.abs("specs/external/definitions/definitions.json")] = parsedSchema.definitions; - expected[path.abs("specs/external/definitions/name.yaml")] = parsedSchema.name; - expected[path.abs("specs/external/definitions/required-string.yaml")] = parsedSchema.requiredString; + expected[abs("specs/external/external.yaml")] = schema; + expected[abs("specs/external/definitions/definitions.json")] = definitions; + expected[abs("specs/external/definitions/name.yaml")] = name; + expected[abs("specs/external/definitions/required-string.yaml")] = requiredString; values = $refs.values(); expect(values).to.deep.equal(expected); } @@ -128,15 +123,14 @@ describe("$Refs object", () => { }); it("should return only URLs and values", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let values = $refs.values(["http"]); if (host.browser) { let expected = {}; - expected[path.url("specs/external/external.yaml")] = parsedSchema.schema; - expected[path.url("specs/external/definitions/definitions.json")] = parsedSchema.definitions; - expected[path.url("specs/external/definitions/name.yaml")] = parsedSchema.name; - expected[path.url("specs/external/definitions/required-string.yaml")] = parsedSchema.requiredString; + expected[url("specs/external/external.yaml")] = schema; + expected[url("specs/external/definitions/definitions.json")] = definitions; + expected[url("specs/external/definitions/name.yaml")] = name; + expected[url("specs/external/definitions/required-string.yaml")] = requiredString; values = $refs.values(); expect(values).to.deep.equal(expected); } @@ -148,17 +142,15 @@ describe("$Refs object", () => { describe("exists", () => { it("should work with absolute paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); - expect($refs.exists(path.abs("specs/external/external.yaml"))).to.equal(true); - expect($refs.exists(path.abs("specs/external/definitions/definitions.json"))).to.equal(true); - expect($refs.exists(path.abs("specs/external/definitions/name.yaml"))).to.equal(true); - expect($refs.exists(path.abs("specs/external/definitions/required-string.yaml"))).to.equal(true); + const $refs = await resolve(abs("specs/external/external.yaml")); + expect($refs.exists(abs("specs/external/external.yaml"))).to.equal(true); + expect($refs.exists(abs("specs/external/definitions/definitions.json"))).to.equal(true); + expect($refs.exists(abs("specs/external/definitions/name.yaml"))).to.equal(true); + expect($refs.exists(abs("specs/external/definitions/required-string.yaml"))).to.equal(true); }); it("should work with relative paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); expect($refs.exists("external.yaml")).to.equal(true); expect($refs.exists("definitions/definitions.json")).to.equal(true); expect($refs.exists("definitions/name.yaml")).to.equal(true); @@ -166,51 +158,46 @@ describe("$Refs object", () => { }); it("should return false if the $ref does not exist", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); expect($refs.exists("foo bar")).to.equal(false); }); }); describe("get", () => { it("should work with absolute paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); - expect($refs.get(path.abs("specs/external/external.yaml"))).to.deep.equal(parsedSchema.schema); - expect($refs.get(path.abs("specs/external/definitions/definitions.json"))).to.deep.equal(parsedSchema.definitions); - expect($refs.get(path.abs("specs/external/definitions/name.yaml"))).to.deep.equal(parsedSchema.name); - expect($refs.get(path.abs("specs/external/definitions/required-string.yaml"))).to.deep.equal(parsedSchema.requiredString); + const $refs = await resolve(abs("specs/external/external.yaml")); + expect($refs.get(abs("specs/external/external.yaml"))).to.deep.equal(schema); + expect($refs.get(abs("specs/external/definitions/definitions.json"))).to.deep.equal(definitions); + expect($refs.get(abs("specs/external/definitions/name.yaml"))).to.deep.equal(name); + expect($refs.get(abs("specs/external/definitions/required-string.yaml"))).to.deep.equal(requiredString); }); it("should work with relative paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); - expect($refs.get("external.yaml")).to.deep.equal(parsedSchema.schema); - expect($refs.get("definitions/definitions.json")).to.deep.equal(parsedSchema.definitions); - expect($refs.get("definitions/name.yaml")).to.deep.equal(parsedSchema.name); - expect($refs.get("definitions/required-string.yaml")).to.deep.equal(parsedSchema.requiredString); + const $refs = await resolve(abs("specs/external/external.yaml")); + expect($refs.get("external.yaml")).to.deep.equal(schema); + expect($refs.get("definitions/definitions.json")).to.deep.equal(definitions); + expect($refs.get("definitions/name.yaml")).to.deep.equal(name); + expect($refs.get("definitions/required-string.yaml")).to.deep.equal(requiredString); }); it("should get the entire file if there is no hash", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let value = $refs.get("definitions/name.yaml"); - expect(value).to.deep.equal(parsedSchema.name); + expect(value).to.deep.equal(name); }); it("should get the entire file if the hash is empty", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); let value = $refs.get("definitions/name.yaml#"); - expect(value).to.deep.equal(parsedSchema.name); + expect(value).to.deep.equal(name); }); it('should try to get an empty key if the hash is "#/"', async () => { - const $refs = await $RefParser.resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); try { $refs.get("definitions/name.yaml#/"); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(Error); @@ -219,8 +206,7 @@ describe("$Refs object", () => { }); it("should resolve values across multiple files if necessary", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); expect($refs.get("external.yaml#/properties/name/properties/first")).to.deep.equal({ title: "required string", type: "string", @@ -230,11 +216,11 @@ describe("$Refs object", () => { }); it("should throw an error if the file does not exist", async () => { - const $refs = await $RefParser.resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); try { $refs.get("foo-bar.yaml#/some/value"); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(Error); @@ -244,11 +230,11 @@ describe("$Refs object", () => { }); it("should throw an error if the JSON Pointer path does not exist", async () => { - const $refs = await $RefParser.resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); try { $refs.get("external.yaml#/foo/bar"); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(Error); @@ -259,33 +245,30 @@ describe("$Refs object", () => { describe("set", () => { it("should work with absolute paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); - let $ref = path.abs("specs/external/external.yaml") + "#/properties/name"; + const $refs = await resolve(abs("specs/external/external.yaml")); + let $ref = abs("specs/external/external.yaml") + "#/properties/name"; $refs.set($ref, { foo: "bar" }); expect($refs.get("external.yaml#/properties/name")).to.deep.equal({ foo: "bar" }); }); it("should work with relative paths", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); $refs.set("external.yaml#/properties/name", { foo: "bar" }); expect($refs.get("external.yaml#/properties/name")).to.deep.equal({ foo: "bar" }); }); it("should resolve values across multiple files if necessary", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); $refs.set("external.yaml#/properties/name/properties/first/title", "foo bar"); expect($refs.get("external.yaml#/properties/name/properties/first/title")).to.equal("foo bar"); }); it("should throw an error if the file does not exist", async () => { - const $refs = await $RefParser.resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); try { $refs.set("foo-bar.yaml#/some/path", "some value"); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(Error); @@ -295,8 +278,7 @@ describe("$Refs object", () => { }); it("should NOT throw an error if the JSON Pointer path does not exist (it creates the new value instead)", async () => { - const $refs = await $RefParser - .resolve(path.abs("specs/external/external.yaml")); + const $refs = await resolve(abs("specs/external/external.yaml")); $refs.set("external.yaml#/foo/bar/baz", { hello: "world" }); expect($refs.get("external.yaml#/foo/bar/baz")).to.deep.equal({ hello: "world" }); }); diff --git a/test/specs/resolvers/resolvers.spec.js b/test/specs/resolvers/resolvers.spec.js index c965acea..d0a6cdd5 100644 --- a/test/specs/resolvers/resolvers.spec.js +++ b/test/specs/resolvers/resolvers.spec.js @@ -1,25 +1,24 @@ -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const { ResolverError, UnmatchedResolverError, JSONParserErrorGroup } = require("../../../lib/util/errors"); +import $RefParser, { dereference } from "../../.."; +import { shouldNotGetCalled } from "../../utils/helper"; +import { abs, rel } from "../../utils/path"; +import parsedSchema from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import { ResolverError, UnmatchedResolverError, JSONParserErrorGroup } from "../../../lib/util/errors"; describe("options.resolve", () => { it('should not resolve external links if "resolve.external" is disabled', async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { resolve: { external: false }}); + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { resolve: { external: false }}); expect(schema).to.deep.equal(parsedSchema); }); it("should throw an error for unrecognized protocols", async () => { try { - await $RefParser.dereference(path.abs("specs/resolvers/resolvers.yaml")); - helper.shouldNotGetCalled(); + await dereference(abs("specs/resolvers/resolvers.yaml")); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.an.instanceOf(SyntaxError); @@ -28,147 +27,146 @@ describe("options.resolve", () => { }); it("should use a custom resolver with static values", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { - resolve: { - // A custom resolver for "foo://" URLs - foo: { - canRead: /^foo\:\/\//i, - read: { bar: { baz: "hello world" }} - }, - bar: { - canRead: /^bar\:\/\//i, - read: { Foo: { Baz: "hello world" }} - } + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { + resolve: { + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read: { bar: { baz: "hello world" }} + }, + bar: { + canRead: /^bar\:\/\//i, + read: { Foo: { Baz: "hello world" }} } - }); + } + }); expect(schema).to.deep.equal(dereferencedSchema); }); it("should use a custom resolver that returns a value", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { - resolve: { - // A custom resolver for "foo://" URLs - foo: { - canRead: /^foo\:\/\//i, - read (_file) { - return { bar: { baz: "hello world" }}; - } - }, - bar: { - canRead: /^bar\:\/\//i, - read (_file) { - return { Foo: { Baz: "hello world" }}; - } + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { + resolve: { + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read (_file) { + return { bar: { baz: "hello world" }}; + } + }, + bar: { + canRead: /^bar\:\/\//i, + read (_file) { + return { Foo: { Baz: "hello world" }}; } } - }); + } + }); expect(schema).to.deep.equal(dereferencedSchema); }); it("should return _file url as it's written", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { - resolve: { - // A custom resolver for "foo://" URLs - foo: { - canRead: /^foo\:\/\//i, - read (_file) { - return { bar: { baz: "hello world" }}; - } - }, - // A custom resolver with uppercase symbols - bar: { - canRead: /^bar\:\/\//i, - read (_file) { - expect(_file.url).to.be.equal("bar://Foo.Baz"); + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { + resolve: { + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read (_file) { + return { bar: { baz: "hello world" }}; + } + }, + // A custom resolver with uppercase symbols + bar: { + canRead: /^bar\:\/\//i, + read (_file) { + expect(_file.url).to.be.equal("bar://Foo.Baz"); - return { Foo: { Baz: "hello world" }}; - } + return { Foo: { Baz: "hello world" }}; } } - }); + } + }); expect(schema).to.deep.equal(dereferencedSchema); }); it("should use a custom resolver that calls a callback", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { + resolve: { + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read (_file, callback) { + callback(null, { bar: { baz: "hello world" }}); + } + }, + bar: { + canRead: /^bar\:\/\//i, + read (_file, callback) { + callback(null, { Foo: { Baz: "hello world" }}); + } + } + } + }); + expect(schema).to.deep.equal(dereferencedSchema); + }); + + if (typeof Promise === "function") { + it("should use a custom resolver that returns a promise", async () => { + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { resolve: { // A custom resolver for "foo://" URLs foo: { canRead: /^foo\:\/\//i, - read (_file, callback) { - callback(null, { bar: { baz: "hello world" }}); + read (_file) { + return Promise.resolve({ bar: { baz: "hello world" }}); } }, bar: { canRead: /^bar\:\/\//i, - read (_file, callback) { - callback(null, { Foo: { Baz: "hello world" }}); + read (_file) { + return Promise.resolve({ Foo: { Baz: "hello world" }}); } } } }); - expect(schema).to.deep.equal(dereferencedSchema); - }); - - if (typeof Promise === "function") { - it("should use a custom resolver that returns a promise", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { - resolve: { - // A custom resolver for "foo://" URLs - foo: { - canRead: /^foo\:\/\//i, - read (_file) { - return Promise.resolve({ bar: { baz: "hello world" }}); - } - }, - bar: { - canRead: /^bar\:\/\//i, - read (_file) { - return Promise.resolve({ Foo: { Baz: "hello world" }}); - } - } - } - }); expect(schema).to.deep.equal(dereferencedSchema); }); } it("should continue resolving if a custom resolver fails", async () => { - const schema = await $RefParser - .dereference(path.abs("specs/resolvers/resolvers.yaml"), { - resolve: { - // A custom resolver that always fails - badResolver: { - order: 1, - canRead: true, - read (_file) { - throw new Error("BOMB!!!"); - } - }, - // A custom resolver for "foo://" URLs - foo: { - canRead: /^foo\:\/\//i, - read: { bar: { baz: "hello world" }} - }, - bar: { - canRead: /^bar\:\/\//i, - read: { Foo: { Baz: "hello world" }} + const schema = await dereference(abs("specs/resolvers/resolvers.yaml"), { + resolve: { + // A custom resolver that always fails + badResolver: { + order: 1, + canRead: true, + read (_file) { + throw new Error("BOMB!!!"); } + }, + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read: { bar: { baz: "hello world" }} + }, + bar: { + canRead: /^bar\:\/\//i, + read: { Foo: { Baz: "hello world" }} } - }); + }, + // A custom resolver for "foo://" URLs + foo: { + canRead: /^foo\:\/\//i, + read: { bar: { baz: "hello world" }} + } + }); expect(schema).to.deep.equal(dereferencedSchema); }); it("should normalize errors thrown by resolvers", async () => { try { - await $RefParser.dereference({ $ref: path.abs("specs/resolvers/resolvers.yaml") }, { + await dereference({ $ref: abs("specs/resolvers/resolvers.yaml") }, { resolve: { // A custom resolver that always fails file: { @@ -180,7 +178,7 @@ describe("options.resolve", () => { } } }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(ResolverError); @@ -190,7 +188,7 @@ describe("options.resolve", () => { it("should throw an error if no resolver returned a result", async () => { try { - await $RefParser.dereference(path.rel("specs/resolvers/resolvers.yaml"), { + await dereference(rel("specs/resolvers/resolvers.yaml"), { resolve: { http: false, file: { @@ -202,7 +200,7 @@ describe("options.resolve", () => { } } }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { // would time out otherwise @@ -213,14 +211,14 @@ describe("options.resolve", () => { it("should throw a grouped error if no resolver can be matched and continueOnError is true", async () => { const parser = new $RefParser(); try { - await parser.dereference(path.abs("specs/resolvers/resolvers.yaml"), { + await parser.dereference(abs("specs/resolvers/resolvers.yaml"), { resolve: { file: false, http: false, }, continueOnError: true, }); - helper.shouldNotGetCalled(); + shouldNotGetCalled(); } catch (err) { expect(err).to.be.instanceof(JSONParserErrorGroup); diff --git a/test/specs/root/root.spec.js b/test/specs/root/root.spec.js index 34f45da1..1203146c 100644 --- a/test/specs/root/root.spec.js +++ b/test/specs/root/root.spec.js @@ -1,31 +1,31 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, root, extended, name } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("Schema with a top-level (root) $ref", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/root/root.yaml")); + const schema = await parser.parse(rel("specs/root/root.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/root/root.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/root/root.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/root/root.yaml"), - path.abs("specs/root/root.yaml"), parsedSchema.schema, - path.abs("specs/root/definitions/root.json"), parsedSchema.root, - path.abs("specs/root/definitions/extended.yaml"), parsedSchema.extended, - path.abs("specs/root/definitions/name.yaml"), parsedSchema.name + it("should resolve successfully", testResolve( + rel("specs/root/root.yaml"), + abs("specs/root/root.yaml"), _schema, + abs("specs/root/definitions/root.json"), root, + abs("specs/root/definitions/extended.yaml"), extended, + abs("specs/root/definitions/name.yaml"), name )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/root/root.yaml")); + const schema = await parser.dereference(rel("specs/root/root.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -36,7 +36,7 @@ describe("Schema with a top-level (root) $ref", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/root/root.yaml")); + const schema = await parser.bundle(rel("specs/root/root.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml b/test/specs/special-characters/__({[ % & $ # @ ` ~ ,)}]__.yaml similarity index 100% rename from test/specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml rename to test/specs/special-characters/__({[ % & $ # @ ` ~ ,)}]__.yaml diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json b/test/specs/special-characters/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json similarity index 100% rename from test/specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json rename to test/specs/special-characters/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/dereferenced.js b/test/specs/special-characters/dereferenced.js similarity index 100% rename from test/specs/__({[ % & $ # @ ` ~ ,)}]__/dereferenced.js rename to test/specs/special-characters/dereferenced.js diff --git a/test/specs/__({[ % & $ # @ ` ~ ,)}]__/parsed.js b/test/specs/special-characters/parsed.js similarity index 100% rename from test/specs/__({[ % & $ # @ ` ~ ,)}]__/parsed.js rename to test/specs/special-characters/parsed.js diff --git a/test/specs/special-characters/special-characters.spec.js b/test/specs/special-characters/special-characters.spec.js new file mode 100644 index 00000000..c98806a0 --- /dev/null +++ b/test/specs/special-characters/special-characters.spec.js @@ -0,0 +1,38 @@ +import $RefParser from "../../../lib"; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { expect } from "chai"; +import { schema as _schema, file } from "./parsed"; +import dereferencedSchema from "./dereferenced"; + +describe("File names with special characters", () => { + it("should parse successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.parse(rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")]); + }); + + it("should resolve successfully", testResolve( + rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml"), + abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml"), _schema, + abs("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.json"), file + )); + + it("should dereference successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.dereference(rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(dereferencedSchema); + // The "circular" flag should NOT be set + expect(parser.$refs.circular).to.equal(false); + }); + + it("should bundle successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.bundle(rel("specs/__({[ % & $ # @ ` ~ ,)}]__/__({[ % & $ # @ ` ~ ,)}]__.yaml")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(dereferencedSchema); + }); +}); diff --git a/test/specs/substrings/substrings.spec.js b/test/specs/substrings/substrings.spec.js index dae5ab52..189d78d4 100644 --- a/test/specs/substrings/substrings.spec.js +++ b/test/specs/substrings/substrings.spec.js @@ -1,30 +1,30 @@ -const { expect } = require("chai"); -const $RefParser = require("../../.."); -const helper = require("../../utils/helper"); -const path = require("../../utils/path"); -const parsedSchema = require("./parsed"); -const dereferencedSchema = require("./dereferenced"); -const bundledSchema = require("./bundled"); +import { expect } from "chai"; +import $RefParser from "../../.."; +import { testResolve } from "../../utils/helper"; +import { rel, abs } from "../../utils/path"; +import { schema as _schema, definitions, strings } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("$refs that are substrings of each other", () => { it("should parse successfully", async () => { let parser = new $RefParser(); - const schema = await parser.parse(path.rel("specs/substrings/substrings.yaml")); + const schema = await parser.parse(rel("specs/substrings/substrings.yaml")); expect(schema).to.equal(parser.schema); - expect(schema).to.deep.equal(parsedSchema.schema); - expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/substrings/substrings.yaml")]); + expect(schema).to.deep.equal(_schema); + expect(parser.$refs.paths()).to.deep.equal([abs("specs/substrings/substrings.yaml")]); }); - it("should resolve successfully", helper.testResolve( - path.rel("specs/substrings/substrings.yaml"), - path.abs("specs/substrings/substrings.yaml"), parsedSchema.schema, - path.abs("specs/substrings/definitions/definitions.json"), parsedSchema.definitions, - path.abs("specs/substrings/definitions/strings.yaml"), parsedSchema.strings + it("should resolve successfully", testResolve( + rel("specs/substrings/substrings.yaml"), + abs("specs/substrings/substrings.yaml"), _schema, + abs("specs/substrings/definitions/definitions.json"), definitions, + abs("specs/substrings/definitions/strings.yaml"), strings )); it("should dereference successfully", async () => { let parser = new $RefParser(); - const schema = await parser.dereference(path.rel("specs/substrings/substrings.yaml")); + const schema = await parser.dereference(rel("specs/substrings/substrings.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); // Reference equality @@ -37,7 +37,7 @@ describe("$refs that are substrings of each other", () => { it("should bundle successfully", async () => { let parser = new $RefParser(); - const schema = await parser.bundle(path.rel("specs/substrings/substrings.yaml")); + const schema = await parser.bundle(rel("specs/substrings/substrings.yaml")); expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(bundledSchema); }); diff --git a/test/specs/util/url.spec.js b/test/specs/util/url.spec.js index 7bfd4660..126eec91 100644 --- a/test/specs/util/url.spec.js +++ b/test/specs/util/url.spec.js @@ -1,22 +1,22 @@ -const chai = require("chai"); -const chaiSubset = require("chai-subset"); -chai.use(chaiSubset); +import chai, { use } from "chai"; +import chaiSubset from "chai-subset"; +use(chaiSubset); const { expect } = chai; -const $url = require("../../../lib/util/url"); +import { getExtension } from "../../../lib/util/url"; describe("Return the extension of a URL", () => { it("should return an empty string if there isn't any extension", async () => { - const extension = $url.getExtension("/file"); + const extension = getExtension("/file"); expect(extension).to.equal(""); }); it("should return the extension in lowercase", async () => { - const extension = $url.getExtension("/file.YML"); + const extension = getExtension("/file.YML"); expect(extension).to.equal(".yml"); }); it("should return the extension without the query", async () => { - const extension = $url.getExtension("/file.yml?foo=bar"); + const extension = getExtension("/file.yml?foo=bar"); expect(extension).to.equal(".yml"); }); }); From fba9acae9db2052fdbc3d0c3c3b6048fa483b12f Mon Sep 17 00:00:00 2001 From: Phil Sturgeon <67381+philsturgeon@users.noreply.github.com> Date: Mon, 7 Nov 2022 11:21:41 +0000 Subject: [PATCH 4/7] chore: slapping a few .js extensions on isnt going to do --- test/specs/absolute-root/absolute-root.spec.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/specs/absolute-root/absolute-root.spec.js b/test/specs/absolute-root/absolute-root.spec.js index 2740b17e..dccc2d2a 100644 --- a/test/specs/absolute-root/absolute-root.spec.js +++ b/test/specs/absolute-root/absolute-root.spec.js @@ -1,12 +1,12 @@ import { expect } from "chai"; import { resolve } from "path"; import $RefParser from "../../.."; -import { abs, url as _url } from "../../utils/path"; -import { testResolve } from "../../utils/helper"; -import { cwd } from "../../../lib/util/url"; -import { schema as _schema, definitions, name, requiredString } from "./parsed"; -import dereferencedSchema from "./dereferenced"; -import bundledSchema from "./bundled"; +import { abs, url as _url } from "../../utils/path.js"; +import { testResolve } from "../../utils/helper.js"; +import { cwd } from "../../../lib/util/url.js"; +import { schema as _schema, definitions, name, requiredString } from "./parsed.js"; +import dereferencedSchema from "./dereferenced.js"; +import bundledSchema from "./bundled.js"; describe("When executed in the context of root directory", () => { // Store the OS root directory From 89589f9dd7bfbf4ea6cec94034617660b59ccaa7 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon <67381+philsturgeon@users.noreply.github.com> Date: Mon, 7 Nov 2022 15:17:44 +0000 Subject: [PATCH 5/7] chore: avoid directory imports still getting issues, looks like export default { /** * Returns the current working directory (in Node) or the current page URL (in browsers). * * @returns {string} */ cwd () { does not actually make import { cwd } available? --- lib/bundle.js | 6 +- lib/dereference.js | 6 +- lib/index.js | 16 +-- lib/normalize-args.js | 2 +- lib/options.js | 12 +- lib/parse.js | 6 +- lib/parsers/json.js | 2 +- lib/parsers/text.js | 2 +- lib/parsers/yaml.js | 2 +- lib/pointer.js | 6 +- lib/ref.js | 6 +- lib/refs.js | 4 +- lib/resolve-external.js | 10 +- lib/resolvers/file.js | 4 +- lib/resolvers/http.js | 4 +- lib/util/errors.js | 2 +- lib/util/url.js | 22 ++- .../specs/absolute-root/absolute-root.spec.js | 2 +- test/specs/absolute-root/bundled.js | 3 +- test/specs/absolute-root/parsed.js | 131 +++++++++--------- .../circular-extended.spec.js | 2 +- .../circular-external.spec.js | 2 +- .../circular-multi/circular-multi.spec.js | 2 +- test/specs/date-strings/date-strings.spec.js | 2 +- .../specs/deep-circular/deep-circular.spec.js | 2 +- test/specs/deep/deep.spec.js | 2 +- .../dereference-callback.spec.js | 2 +- test/specs/error-source/error-source.spec.js | 2 +- .../external-from-internal.spec.js | 2 +- .../external-multiple.spec.js | 2 +- .../external-partial/external-partial.spec.js | 2 +- test/specs/external/external.spec.js | 2 +- test/specs/internal/internal.spec.js | 2 +- test/specs/no-refs/no-refs.spec.js | 2 +- .../object-source-with-path.spec.js | 2 +- .../specs/object-source/object-source.spec.js | 2 +- .../ref-in-excluded-path.spec.js | 2 +- test/specs/root/root.spec.js | 2 +- test/specs/substrings/substrings.spec.js | 2 +- test/utils/path.js | 41 +++--- 40 files changed, 168 insertions(+), 159 deletions(-) diff --git a/lib/bundle.js b/lib/bundle.js index c4a21444..30406cec 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -1,6 +1,6 @@ -import $Ref from "./ref"; -import Pointer from "./pointer"; -import url from "./util/url"; +import $Ref from "./ref.js"; +import Pointer from "./pointer.js"; +import url from "./util/url.js"; export default bundle; diff --git a/lib/dereference.js b/lib/dereference.js index c81c5b2e..f116d29b 100644 --- a/lib/dereference.js +++ b/lib/dereference.js @@ -1,8 +1,8 @@ -import $Ref from "./ref"; -import Pointer from "./pointer"; +import $Ref from "./ref.js"; +import Pointer from "./pointer.js"; import { ono } from "@jsdevtools/ono"; -import url from "./util/url"; +import url from "./util/url.js"; export default dereference; diff --git a/lib/index.js b/lib/index.js index ddc26194..25fd3225 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,11 +1,11 @@ /* eslint-disable no-unused-vars */ -import $Refs from "./refs"; -import _parse from "./parse"; -import normalizeArgs from "./normalize-args"; -import resolveExternal from "./resolve-external"; -import _bundle from "./bundle"; -import _dereference from "./dereference"; -import url from "./util/url"; +import $Refs from "./refs.js"; +import _parse from "./parse.js"; +import normalizeArgs from "./normalize-args.js"; +import resolveExternal from "./resolve-external.js"; +import _bundle from "./bundle.js"; +import _dereference from "./dereference.js"; +import url from "./util/url.js"; import { InvalidPointerError, isHandledError, @@ -16,7 +16,7 @@ import { ResolverError, UnmatchedParserError, UnmatchedResolverError -} from "./util/errors"; +} from "./util/errors.js"; import maybe from "call-me-maybe"; import { ono } from "@jsdevtools/ono"; diff --git a/lib/normalize-args.js b/lib/normalize-args.js index 1fafcdbb..4f5d0fcd 100644 --- a/lib/normalize-args.js +++ b/lib/normalize-args.js @@ -1,4 +1,4 @@ -import Options from "./options"; +import Options from "./options.js"; export default normalizeArgs; diff --git a/lib/options.js b/lib/options.js index a3c8dada..af2227e6 100644 --- a/lib/options.js +++ b/lib/options.js @@ -1,9 +1,9 @@ -import jsonParser from "./parsers/json"; -import yamlParser from "./parsers/yaml"; -import textParser from "./parsers/text"; -import binaryParser from "./parsers/binary"; -import fileResolver from "./resolvers/file"; -import httpResolver from "./resolvers/http"; +import jsonParser from "./parsers/json.js"; +import yamlParser from "./parsers/yaml.js"; +import textParser from "./parsers/text.js"; +import binaryParser from "./parsers/binary.js"; +import fileResolver from "./resolvers/file.js"; +import httpResolver from "./resolvers/http.js"; export default $RefParserOptions; diff --git a/lib/parse.js b/lib/parse.js index b997b5ef..121d0b61 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1,8 +1,8 @@ import { ono } from "@jsdevtools/ono"; -import url from "./util/url"; -import plugins from "./util/plugins"; -import { isHandledError, ParserError, ResolverError, UnmatchedParserError, UnmatchedResolverError } from "./util/errors"; +import url from "./util/url.js"; +import plugins from "./util/plugins.js"; +import { isHandledError, ParserError, ResolverError, UnmatchedParserError, UnmatchedResolverError } from "./util/errors.js"; export default parse; diff --git a/lib/parsers/json.js b/lib/parsers/json.js index 0ed9b76f..116ece19 100644 --- a/lib/parsers/json.js +++ b/lib/parsers/json.js @@ -1,4 +1,4 @@ -import { ParserError } from "../util/errors"; +import { ParserError } from "../util/errors.js"; export default { /** diff --git a/lib/parsers/text.js b/lib/parsers/text.js index f235c97a..a4975eb8 100644 --- a/lib/parsers/text.js +++ b/lib/parsers/text.js @@ -1,4 +1,4 @@ -import { ParserError } from "../util/errors"; +import { ParserError } from "../util/errors.js"; let TEXT_REGEXP = /\.(txt|htm|html|md|xml|js|min|map|css|scss|less|svg)$/i; diff --git a/lib/parsers/yaml.js b/lib/parsers/yaml.js index f2307a86..ecc39287 100644 --- a/lib/parsers/yaml.js +++ b/lib/parsers/yaml.js @@ -1,4 +1,4 @@ -import { ParserError } from "../util/errors"; +import { ParserError } from "../util/errors.js"; import yaml, { JSON_SCHEMA } from "js-yaml"; diff --git a/lib/pointer.js b/lib/pointer.js index b8344ac7..bfb47cd1 100644 --- a/lib/pointer.js +++ b/lib/pointer.js @@ -1,6 +1,6 @@ -import $Ref from "./ref"; -import url from "./util/url"; -import { InvalidPointerError, isHandledError, JSONParserError, MissingPointerError } from "./util/errors"; +import $Ref from "./ref.js"; +import url from "./util/url.js"; +import { InvalidPointerError, isHandledError, JSONParserError, MissingPointerError } from "./util/errors.js"; export default Pointer; diff --git a/lib/ref.js b/lib/ref.js index b8d6fec0..78ea16be 100644 --- a/lib/ref.js +++ b/lib/ref.js @@ -1,6 +1,6 @@ -import Pointer from "./pointer"; -import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors"; -import url from "./util/url"; +import Pointer from "./pointer.js"; +import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors.js"; +import url from "./util/url.js"; export default $Ref; diff --git a/lib/refs.js b/lib/refs.js index 2cff7e6a..7cf46a1a 100644 --- a/lib/refs.js +++ b/lib/refs.js @@ -1,7 +1,7 @@ import { ono } from "@jsdevtools/ono"; -import $Ref from "./ref"; -import url from "./util/url"; +import $Ref from "./ref.js"; +import url from "./util/url.js"; const isWindows = /^win/.test(globalThis.process?.platform); const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath; diff --git a/lib/resolve-external.js b/lib/resolve-external.js index af192fda..50f9fe16 100644 --- a/lib/resolve-external.js +++ b/lib/resolve-external.js @@ -1,8 +1,8 @@ -import $Ref from "./ref"; -import Pointer from "./pointer"; -import parse from "./parse"; -import url from "./util/url"; -import { isHandledError } from "./util/errors"; +import $Ref from "./ref.js"; +import Pointer from "./pointer.js"; +import parse from "./parse.js"; +import url from "./util/url.js"; +import { isHandledError } from "./util/errors.js"; export default resolveExternal; diff --git a/lib/resolvers/file.js b/lib/resolvers/file.js index 8a38eb88..d198deec 100644 --- a/lib/resolvers/file.js +++ b/lib/resolvers/file.js @@ -1,8 +1,8 @@ import fs from "fs"; import { ono } from "@jsdevtools/ono"; -import url from "../util/url"; -import { ResolverError } from "../util/errors"; +import url from "../util/url.js"; +import { ResolverError } from "../util/errors.js"; export default { /** diff --git a/lib/resolvers/http.js b/lib/resolvers/http.js index 2c4041a9..fdb8788d 100644 --- a/lib/resolvers/http.js +++ b/lib/resolvers/http.js @@ -1,7 +1,7 @@ import { ono } from "@jsdevtools/ono"; -import url from "../util/url"; -import { ResolverError } from "../util/errors"; +import url from "../util/url.js"; +import { ResolverError } from "../util/errors.js"; export default { /** diff --git a/lib/util/errors.js b/lib/util/errors.js index 3201f33a..19b24de6 100644 --- a/lib/util/errors.js +++ b/lib/util/errors.js @@ -1,6 +1,6 @@ import { Ono } from "@jsdevtools/ono"; -import url from "./url"; +import url from "./url.js"; export class JSONParserError extends Error { constructor (message, source) { diff --git a/lib/util/url.js b/lib/util/url.js index ec5dda62..73ba982f 100644 --- a/lib/util/url.js +++ b/lib/util/url.js @@ -1,7 +1,7 @@ -import { parse, resolve } from "url"; +import { parse } from "url"; +import { resolve } from "path"; -const nodePath = require("path"); -const projectDir = nodePath.resolve(__dirname, "..", ".."); +const projectDir = resolve(__dirname, "..", ".."); let isWindows = /^win/.test(globalThis.process?.platform), forwardSlashPattern = /\//g, @@ -25,6 +25,7 @@ let urlDecodePatterns = [ ]; export default { + /** * Returns the current working directory (in Node) or the current page URL (in browsers). * @@ -279,5 +280,18 @@ export default { parse, - resolve, + /** + * Returns resolved target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF. + * + * @return {string} + */ + resolve (from, to) { + let resolvedUrl = new URL(to, new URL(from, "resolve://")); + if (resolvedUrl.protocol === "resolve:") { + // `from` is a relative URL. + let { pathname, search, hash } = resolvedUrl; + return pathname + search + hash; + } + return resolvedUrl.toString(); + }, }; diff --git a/test/specs/absolute-root/absolute-root.spec.js b/test/specs/absolute-root/absolute-root.spec.js index dccc2d2a..fef9670b 100644 --- a/test/specs/absolute-root/absolute-root.spec.js +++ b/test/specs/absolute-root/absolute-root.spec.js @@ -1,6 +1,6 @@ import { expect } from "chai"; import { resolve } from "path"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { abs, url as _url } from "../../utils/path.js"; import { testResolve } from "../../utils/helper.js"; import { cwd } from "../../../lib/util/url.js"; diff --git a/test/specs/absolute-root/bundled.js b/test/specs/absolute-root/bundled.js index 635d392a..33e08de9 100644 --- a/test/specs/absolute-root/bundled.js +++ b/test/specs/absolute-root/bundled.js @@ -1,5 +1,4 @@ -module.exports = -{ +export default { title: "Person", type: "object", required: [ diff --git a/test/specs/absolute-root/parsed.js b/test/specs/absolute-root/parsed.js index f3aaf050..ebbb7e70 100644 --- a/test/specs/absolute-root/parsed.js +++ b/test/specs/absolute-root/parsed.js @@ -1,81 +1,78 @@ -module.exports = -{ - schema: { - definitions: { - $ref: "definitions/definitions.json" - }, - required: [ - "name" - ], - type: "object", - properties: { - gender: { - enum: [ - "male", - "female" - ], - type: "string" - }, - age: { - minimum: 0, - type: "integer" - }, - name: { - $ref: "#/definitions/name" - } - }, - title: "Person" - }, - +export const schema = { definitions: { - "required string": { - $ref: "required-string.yaml" + $ref: "definitions/definitions.json" + }, + required: [ + "name" + ], + type: "object", + properties: { + gender: { + enum: [ + "male", + "female" + ], + type: "string" }, - string: { - $ref: "#/required%20string/type" + age: { + minimum: 0, + type: "integer" }, name: { - $ref: "../definitions/name.yaml" + $ref: "#/definitions/name" } }, + title: "Person" +}; +export const definitions = { + "required string": { + $ref: "required-string.yaml" + }, + string: { + $ref: "#/required%20string/type" + }, name: { - required: [ - "first", - "last" - ], - type: "object", - properties: { - middle: { - minLength: { - $ref: "#/properties/first/minLength" - }, - type: { - $ref: "#/properties/first/type" - } - }, - prefix: { - minLength: 3, - $ref: "#/properties/last" - }, - last: { - $ref: "./required-string.yaml" - }, - suffix: { - $ref: "#/properties/prefix", - type: "string", - maxLength: 3 + $ref: "../definitions/name.yaml" + } +}; + +export const name = { + required: [ + "first", + "last" + ], + type: "object", + properties: { + middle: { + minLength: { + $ref: "#/properties/first/minLength" }, - first: { - $ref: "../definitions/definitions.json#/required string" + type: { + $ref: "#/properties/first/type" } }, - title: "name" + prefix: { + minLength: 3, + $ref: "#/properties/last" + }, + last: { + $ref: "./required-string.yaml" + }, + suffix: { + $ref: "#/properties/prefix", + type: "string", + maxLength: 3 + }, + first: { + $ref: "../definitions/definitions.json#/required string" + } }, + title: "name" +}; - requiredString: { - minLength: 1, - type: "string", - title: "required string" - } +export const requiredString = { + minLength: 1, + type: "string", + title: "required string" }; diff --git a/test/specs/circular-extended/circular-extended.spec.js b/test/specs/circular-extended/circular-extended.spec.js index 3ed16d4d..2d832344 100644 --- a/test/specs/circular-extended/circular-extended.spec.js +++ b/test/specs/circular-extended/circular-extended.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve, shouldNotGetCalled } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { self, thing, ancestor, personWithSpouse, pet, animals, indirect, parentWithChildren, childWithParents, indirectAncestor, parentWithChild, childWithChildren } from "./parsed"; diff --git a/test/specs/circular-external/circular-external.spec.js b/test/specs/circular-external/circular-external.spec.js index 9ed3f678..fb64fb45 100644 --- a/test/specs/circular-external/circular-external.spec.js +++ b/test/specs/circular-external/circular-external.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve, shouldNotGetCalled } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, pet, child, parent, person } from "./parsed"; diff --git a/test/specs/circular-multi/circular-multi.spec.js b/test/specs/circular-multi/circular-multi.spec.js index 4a23bd23..42ee0045 100644 --- a/test/specs/circular-multi/circular-multi.spec.js +++ b/test/specs/circular-multi/circular-multi.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { rel } from "../../utils/path"; import bundledSchema from "./bundled"; diff --git a/test/specs/date-strings/date-strings.spec.js b/test/specs/date-strings/date-strings.spec.js index 78d85be4..15f8a798 100644 --- a/test/specs/date-strings/date-strings.spec.js +++ b/test/specs/date-strings/date-strings.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema } from "./parsed"; diff --git a/test/specs/deep-circular/deep-circular.spec.js b/test/specs/deep-circular/deep-circular.spec.js index 687dc2f0..58a5b7e0 100644 --- a/test/specs/deep-circular/deep-circular.spec.js +++ b/test/specs/deep-circular/deep-circular.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve, shouldNotGetCalled } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, name, requiredString } from "./parsed"; diff --git a/test/specs/deep/deep.spec.js b/test/specs/deep/deep.spec.js index b23689d1..32af402a 100644 --- a/test/specs/deep/deep.spec.js +++ b/test/specs/deep/deep.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, name, requiredString } from "./parsed"; diff --git a/test/specs/dereference-callback/dereference-callback.spec.js b/test/specs/dereference-callback/dereference-callback.spec.js index 3c9b550e..d804eea6 100644 --- a/test/specs/dereference-callback/dereference-callback.spec.js +++ b/test/specs/dereference-callback/dereference-callback.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { rel } from "../../utils/path"; describe("Schema with a $ref", () => { diff --git a/test/specs/error-source/error-source.spec.js b/test/specs/error-source/error-source.spec.js index 5fac6fe6..0676819b 100644 --- a/test/specs/error-source/error-source.spec.js +++ b/test/specs/error-source/error-source.spec.js @@ -3,7 +3,7 @@ import chaiSubset from "chai-subset"; use(chaiSubset); const { expect } = chai; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { shouldNotGetCalled } from "../../utils/helper"; import { abs, unixify } from "../../utils/path"; import { InvalidPointerError, ResolverError, MissingPointerError } from "../../../lib/util/errors"; diff --git a/test/specs/external-from-internal/external-from-internal.spec.js b/test/specs/external-from-internal/external-from-internal.spec.js index 7f4eac95..e2410773 100644 --- a/test/specs/external-from-internal/external-from-internal.spec.js +++ b/test/specs/external-from-internal/external-from-internal.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve } from "../../utils/helper"; import { abs, rel, url } from "../../utils/path"; import { schema as _schema, definitions } from "./parsed"; diff --git a/test/specs/external-multiple/external-multiple.spec.js b/test/specs/external-multiple/external-multiple.spec.js index 377e9605..183e65a1 100644 --- a/test/specs/external-multiple/external-multiple.spec.js +++ b/test/specs/external-multiple/external-multiple.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve } from "../../utils/helper"; import { abs, rel } from "../../utils/path"; import { schema as _schema, definitions } from "./parsed"; diff --git a/test/specs/external-partial/external-partial.spec.js b/test/specs/external-partial/external-partial.spec.js index 82bf21e0..6a718d28 100644 --- a/test/specs/external-partial/external-partial.spec.js +++ b/test/specs/external-partial/external-partial.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, definitions, name, requiredString } from "./parsed"; diff --git a/test/specs/external/external.spec.js b/test/specs/external/external.spec.js index 514d7f1f..6d0a1abe 100644 --- a/test/specs/external/external.spec.js +++ b/test/specs/external/external.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve } from "../../utils/helper"; import { abs, rel, url } from "../../utils/path"; import { schema as _schema, definitions, name, requiredString } from "./parsed"; diff --git a/test/specs/internal/internal.spec.js b/test/specs/internal/internal.spec.js index 83190ef8..68d167ae 100644 --- a/test/specs/internal/internal.spec.js +++ b/test/specs/internal/internal.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import parsedSchema from "./parsed"; diff --git a/test/specs/no-refs/no-refs.spec.js b/test/specs/no-refs/no-refs.spec.js index 3d8aec93..37aa092a 100644 --- a/test/specs/no-refs/no-refs.spec.js +++ b/test/specs/no-refs/no-refs.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import parsedSchema from "./parsed"; diff --git a/test/specs/object-source-with-path/object-source-with-path.spec.js b/test/specs/object-source-with-path/object-source-with-path.spec.js index dbdc883e..c237d804 100644 --- a/test/specs/object-source-with-path/object-source-with-path.spec.js +++ b/test/specs/object-source-with-path/object-source-with-path.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { cloneDeep } from "../../utils/helper"; import { abs, rel } from "../../utils/path"; import internalRefsParsedSchema from "../internal/parsed"; diff --git a/test/specs/object-source/object-source.spec.js b/test/specs/object-source/object-source.spec.js index 395cb0aa..5d4e3b98 100644 --- a/test/specs/object-source/object-source.spec.js +++ b/test/specs/object-source/object-source.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { cloneDeep } from "../../utils/helper"; import { cwd, abs } from "../../utils/path"; import internalRefsParsedSchema from "../internal/parsed"; diff --git a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js index 9d6e8103..5b4ec925 100644 --- a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js +++ b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { rel } from "../../utils/path"; import dereferencedSchema from "./dereferenced"; diff --git a/test/specs/root/root.spec.js b/test/specs/root/root.spec.js index 1203146c..674953bc 100644 --- a/test/specs/root/root.spec.js +++ b/test/specs/root/root.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, root, extended, name } from "./parsed"; diff --git a/test/specs/substrings/substrings.spec.js b/test/specs/substrings/substrings.spec.js index 189d78d4..52e1a564 100644 --- a/test/specs/substrings/substrings.spec.js +++ b/test/specs/substrings/substrings.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../.."; +import $RefParser from "../../../lib/index.js"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, definitions, strings } from "./parsed"; diff --git a/test/utils/path.js b/test/utils/path.js index d9fdedce..bbe2a219 100644 --- a/test/utils/path.js +++ b/test/utils/path.js @@ -1,34 +1,37 @@ -const { host } = require("@jsdevtools/host-environment"); +import { host } from "@jsdevtools/host-environment"; const isWindows = /^win/.test(globalThis.process?.platform); const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath; -if (host.node) { - module.exports = filesystemPathHelpers(); -} -else { - module.exports = urlPathHelpers(); -} +const { rel, abs, unixify, url, cwd } = host.node ? filesystemPathHelpers() : urlPathHelpers(); + +export { rel, abs, unixify, url, cwd }; /** * Helper functions for getting local filesystem paths in various formats */ function filesystemPathHelpers () { - const nodePath = require("path"); - const nodeUrl = require("url"); + // TODO should be using this syntax but that has to go at top of the file? + // import { normalize, join, resolve, sep } from "path"; + // import { format } from "url"; - const testsDir = nodePath.resolve(__dirname, ".."); + // eslint-disable-next-line @typescript-eslint/no-require-imports + const { normalize, join, resolve, sep } = require("path"); + // eslint-disable-next-line @typescript-eslint/no-require-imports + const { format } = require("url"); + + const testsDir = resolve(__dirname, ".."); // Run all tests from the "test" directory process.chdir(testsDir); - const path = { + return { /** * Returns the relative path of a file in the "test" directory */ rel (file) { - const relativePath = nodePath.normalize(nodePath.join(file)); - const filePath = isWindows ? nodePath.resolve(relativePath) : relativePath; + const relativePath = normalize(join(file)); + const filePath = isWindows ? resolve(relativePath) : relativePath; return getPathFromOs(filePath); }, @@ -36,7 +39,7 @@ function filesystemPathHelpers () { * Returns the absolute path of a file in the "test" directory */ abs (file) { - const absolutePath = nodePath.resolve(nodePath.join(file || nodePath.sep)); + const absolutePath = resolve(join(file || sep)); return getPathFromOs(absolutePath); }, @@ -58,7 +61,7 @@ function filesystemPathHelpers () { pathname = pathname.replace(/\\/g, "/"); // Convert Windows separators to URL separators } - let url = nodeUrl.format({ + let url = format({ protocol: "file:", slashes: true, pathname @@ -71,11 +74,9 @@ function filesystemPathHelpers () { * Returns the absolute path of the current working directory. */ cwd () { - return getPathFromOs(nodePath.join(process.cwd(), nodePath.sep)); + return getPathFromOs(join(process.cwd(), sep)); } }; - - return path; } /** @@ -93,7 +94,7 @@ function urlPathHelpers () { return encodeURIComponent(file).split("%2F").join("/"); } - const path = { + return { /** * Returns the relative path of a file in the "test" directory * @@ -143,6 +144,4 @@ function urlPathHelpers () { return location.href; } }; - - return path; } From e56b675474edfa38fd3db5c905cc65acdbdf5822 Mon Sep 17 00:00:00 2001 From: Kevin Swiber Date: Mon, 7 Nov 2022 14:43:03 -0800 Subject: [PATCH 6/7] Adding ESM support. --- lib/index.mjs | 3 +++ package.json | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 lib/index.mjs diff --git a/lib/index.mjs b/lib/index.mjs new file mode 100644 index 00000000..69322538 --- /dev/null +++ b/lib/index.mjs @@ -0,0 +1,3 @@ +import _default from "./index.js"; + +export default _default; diff --git a/package.json b/package.json index 716e811a..5a3e7d68 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,10 @@ }, "license": "MIT", "main": "lib/index.js", + "exports": { + "import": "./lib/index.mjs", + "require": "./lib/index.js" + }, "typings": "lib/index.d.ts", "browser": { "fs": false From 2b00735e3b6448ecb8234908acb02134e5399b4d Mon Sep 17 00:00:00 2001 From: Phil Sturgeon <67381+philsturgeon@users.noreply.github.com> Date: Fri, 25 Nov 2022 14:12:09 +0000 Subject: [PATCH 7/7] remove .js from import again --- lib/bundle.js | 6 +++--- lib/dereference.js | 6 +++--- lib/index.js | 16 ++++++++-------- lib/index.mjs | 2 +- lib/normalize-args.js | 2 +- lib/options.js | 12 ++++++------ lib/parse.js | 6 +++--- lib/parsers/json.js | 2 +- lib/parsers/text.js | 2 +- lib/parsers/yaml.js | 2 +- lib/pointer.js | 6 +++--- lib/ref.js | 6 +++--- lib/refs.js | 4 ++-- lib/resolve-external.js | 10 +++++----- lib/resolvers/file.js | 4 ++-- lib/resolvers/http.js | 4 ++-- lib/util/errors.js | 2 +- test/specs/absolute-root/absolute-root.spec.js | 14 +++++++------- .../circular-extended/circular-extended.spec.js | 2 +- .../circular-external/circular-external.spec.js | 2 +- test/specs/circular-multi/circular-multi.spec.js | 2 +- test/specs/date-strings/date-strings.spec.js | 2 +- test/specs/deep-circular/deep-circular.spec.js | 2 +- test/specs/deep/deep.spec.js | 2 +- .../dereference-callback.spec.js | 2 +- test/specs/error-source/error-source.spec.js | 2 +- .../external-from-internal.spec.js | 2 +- .../external-multiple/external-multiple.spec.js | 2 +- .../external-partial/external-partial.spec.js | 2 +- test/specs/external/external.spec.js | 2 +- test/specs/internal/internal.spec.js | 2 +- test/specs/no-refs/no-refs.spec.js | 2 +- .../object-source-with-path.spec.js | 2 +- test/specs/object-source/object-source.spec.js | 2 +- .../ref-in-excluded-path.spec.js | 2 +- test/specs/root/root.spec.js | 2 +- test/specs/substrings/substrings.spec.js | 2 +- 37 files changed, 72 insertions(+), 72 deletions(-) diff --git a/lib/bundle.js b/lib/bundle.js index 6a2cfb67..2af15a85 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -1,6 +1,6 @@ -import $Ref from "./ref.js"; -import Pointer from "./pointer.js"; -import url from "./util/url.js"; +import $Ref from "./ref"; +import Pointer from "./pointer"; +import url from "./util/url"; export default bundle; diff --git a/lib/dereference.js b/lib/dereference.js index f116d29b..c81c5b2e 100644 --- a/lib/dereference.js +++ b/lib/dereference.js @@ -1,8 +1,8 @@ -import $Ref from "./ref.js"; -import Pointer from "./pointer.js"; +import $Ref from "./ref"; +import Pointer from "./pointer"; import { ono } from "@jsdevtools/ono"; -import url from "./util/url.js"; +import url from "./util/url"; export default dereference; diff --git a/lib/index.js b/lib/index.js index 25fd3225..ddc26194 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,11 +1,11 @@ /* eslint-disable no-unused-vars */ -import $Refs from "./refs.js"; -import _parse from "./parse.js"; -import normalizeArgs from "./normalize-args.js"; -import resolveExternal from "./resolve-external.js"; -import _bundle from "./bundle.js"; -import _dereference from "./dereference.js"; -import url from "./util/url.js"; +import $Refs from "./refs"; +import _parse from "./parse"; +import normalizeArgs from "./normalize-args"; +import resolveExternal from "./resolve-external"; +import _bundle from "./bundle"; +import _dereference from "./dereference"; +import url from "./util/url"; import { InvalidPointerError, isHandledError, @@ -16,7 +16,7 @@ import { ResolverError, UnmatchedParserError, UnmatchedResolverError -} from "./util/errors.js"; +} from "./util/errors"; import maybe from "call-me-maybe"; import { ono } from "@jsdevtools/ono"; diff --git a/lib/index.mjs b/lib/index.mjs index 69322538..793faafa 100644 --- a/lib/index.mjs +++ b/lib/index.mjs @@ -1,3 +1,3 @@ -import _default from "./index.js"; +import _default from "./index"; export default _default; diff --git a/lib/normalize-args.js b/lib/normalize-args.js index 4f5d0fcd..1fafcdbb 100644 --- a/lib/normalize-args.js +++ b/lib/normalize-args.js @@ -1,4 +1,4 @@ -import Options from "./options.js"; +import Options from "./options"; export default normalizeArgs; diff --git a/lib/options.js b/lib/options.js index af2227e6..a3c8dada 100644 --- a/lib/options.js +++ b/lib/options.js @@ -1,9 +1,9 @@ -import jsonParser from "./parsers/json.js"; -import yamlParser from "./parsers/yaml.js"; -import textParser from "./parsers/text.js"; -import binaryParser from "./parsers/binary.js"; -import fileResolver from "./resolvers/file.js"; -import httpResolver from "./resolvers/http.js"; +import jsonParser from "./parsers/json"; +import yamlParser from "./parsers/yaml"; +import textParser from "./parsers/text"; +import binaryParser from "./parsers/binary"; +import fileResolver from "./resolvers/file"; +import httpResolver from "./resolvers/http"; export default $RefParserOptions; diff --git a/lib/parse.js b/lib/parse.js index 121d0b61..b997b5ef 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1,8 +1,8 @@ import { ono } from "@jsdevtools/ono"; -import url from "./util/url.js"; -import plugins from "./util/plugins.js"; -import { isHandledError, ParserError, ResolverError, UnmatchedParserError, UnmatchedResolverError } from "./util/errors.js"; +import url from "./util/url"; +import plugins from "./util/plugins"; +import { isHandledError, ParserError, ResolverError, UnmatchedParserError, UnmatchedResolverError } from "./util/errors"; export default parse; diff --git a/lib/parsers/json.js b/lib/parsers/json.js index 116ece19..0ed9b76f 100644 --- a/lib/parsers/json.js +++ b/lib/parsers/json.js @@ -1,4 +1,4 @@ -import { ParserError } from "../util/errors.js"; +import { ParserError } from "../util/errors"; export default { /** diff --git a/lib/parsers/text.js b/lib/parsers/text.js index a4975eb8..f235c97a 100644 --- a/lib/parsers/text.js +++ b/lib/parsers/text.js @@ -1,4 +1,4 @@ -import { ParserError } from "../util/errors.js"; +import { ParserError } from "../util/errors"; let TEXT_REGEXP = /\.(txt|htm|html|md|xml|js|min|map|css|scss|less|svg)$/i; diff --git a/lib/parsers/yaml.js b/lib/parsers/yaml.js index ecc39287..f2307a86 100644 --- a/lib/parsers/yaml.js +++ b/lib/parsers/yaml.js @@ -1,4 +1,4 @@ -import { ParserError } from "../util/errors.js"; +import { ParserError } from "../util/errors"; import yaml, { JSON_SCHEMA } from "js-yaml"; diff --git a/lib/pointer.js b/lib/pointer.js index bfb47cd1..b8344ac7 100644 --- a/lib/pointer.js +++ b/lib/pointer.js @@ -1,6 +1,6 @@ -import $Ref from "./ref.js"; -import url from "./util/url.js"; -import { InvalidPointerError, isHandledError, JSONParserError, MissingPointerError } from "./util/errors.js"; +import $Ref from "./ref"; +import url from "./util/url"; +import { InvalidPointerError, isHandledError, JSONParserError, MissingPointerError } from "./util/errors"; export default Pointer; diff --git a/lib/ref.js b/lib/ref.js index 78ea16be..b8d6fec0 100644 --- a/lib/ref.js +++ b/lib/ref.js @@ -1,6 +1,6 @@ -import Pointer from "./pointer.js"; -import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors.js"; -import url from "./util/url.js"; +import Pointer from "./pointer"; +import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors"; +import url from "./util/url"; export default $Ref; diff --git a/lib/refs.js b/lib/refs.js index 7cf46a1a..2cff7e6a 100644 --- a/lib/refs.js +++ b/lib/refs.js @@ -1,7 +1,7 @@ import { ono } from "@jsdevtools/ono"; -import $Ref from "./ref.js"; -import url from "./util/url.js"; +import $Ref from "./ref"; +import url from "./util/url"; const isWindows = /^win/.test(globalThis.process?.platform); const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath; diff --git a/lib/resolve-external.js b/lib/resolve-external.js index 50f9fe16..af192fda 100644 --- a/lib/resolve-external.js +++ b/lib/resolve-external.js @@ -1,8 +1,8 @@ -import $Ref from "./ref.js"; -import Pointer from "./pointer.js"; -import parse from "./parse.js"; -import url from "./util/url.js"; -import { isHandledError } from "./util/errors.js"; +import $Ref from "./ref"; +import Pointer from "./pointer"; +import parse from "./parse"; +import url from "./util/url"; +import { isHandledError } from "./util/errors"; export default resolveExternal; diff --git a/lib/resolvers/file.js b/lib/resolvers/file.js index d198deec..8a38eb88 100644 --- a/lib/resolvers/file.js +++ b/lib/resolvers/file.js @@ -1,8 +1,8 @@ import fs from "fs"; import { ono } from "@jsdevtools/ono"; -import url from "../util/url.js"; -import { ResolverError } from "../util/errors.js"; +import url from "../util/url"; +import { ResolverError } from "../util/errors"; export default { /** diff --git a/lib/resolvers/http.js b/lib/resolvers/http.js index fdb8788d..2c4041a9 100644 --- a/lib/resolvers/http.js +++ b/lib/resolvers/http.js @@ -1,7 +1,7 @@ import { ono } from "@jsdevtools/ono"; -import url from "../util/url.js"; -import { ResolverError } from "../util/errors.js"; +import url from "../util/url"; +import { ResolverError } from "../util/errors"; export default { /** diff --git a/lib/util/errors.js b/lib/util/errors.js index 19b24de6..3201f33a 100644 --- a/lib/util/errors.js +++ b/lib/util/errors.js @@ -1,6 +1,6 @@ import { Ono } from "@jsdevtools/ono"; -import url from "./url.js"; +import url from "./url"; export class JSONParserError extends Error { constructor (message, source) { diff --git a/test/specs/absolute-root/absolute-root.spec.js b/test/specs/absolute-root/absolute-root.spec.js index fef9670b..012c5012 100644 --- a/test/specs/absolute-root/absolute-root.spec.js +++ b/test/specs/absolute-root/absolute-root.spec.js @@ -1,12 +1,12 @@ import { expect } from "chai"; import { resolve } from "path"; -import $RefParser from "../../../lib/index.js"; -import { abs, url as _url } from "../../utils/path.js"; -import { testResolve } from "../../utils/helper.js"; -import { cwd } from "../../../lib/util/url.js"; -import { schema as _schema, definitions, name, requiredString } from "./parsed.js"; -import dereferencedSchema from "./dereferenced.js"; -import bundledSchema from "./bundled.js"; +import $RefParser from "../../../lib/index.mjs"; +import { abs, url as _url } from "../../utils/path"; +import { testResolve } from "../../utils/helper"; +import { cwd } from "../../../lib/util/url"; +import { schema as _schema, definitions, name, requiredString } from "./parsed"; +import dereferencedSchema from "./dereferenced"; +import bundledSchema from "./bundled"; describe("When executed in the context of root directory", () => { // Store the OS root directory diff --git a/test/specs/circular-extended/circular-extended.spec.js b/test/specs/circular-extended/circular-extended.spec.js index 2d832344..485a896e 100644 --- a/test/specs/circular-extended/circular-extended.spec.js +++ b/test/specs/circular-extended/circular-extended.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve, shouldNotGetCalled } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { self, thing, ancestor, personWithSpouse, pet, animals, indirect, parentWithChildren, childWithParents, indirectAncestor, parentWithChild, childWithChildren } from "./parsed"; diff --git a/test/specs/circular-external/circular-external.spec.js b/test/specs/circular-external/circular-external.spec.js index fb64fb45..e9b6ea50 100644 --- a/test/specs/circular-external/circular-external.spec.js +++ b/test/specs/circular-external/circular-external.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve, shouldNotGetCalled } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, pet, child, parent, person } from "./parsed"; diff --git a/test/specs/circular-multi/circular-multi.spec.js b/test/specs/circular-multi/circular-multi.spec.js index 42ee0045..eb33b85c 100644 --- a/test/specs/circular-multi/circular-multi.spec.js +++ b/test/specs/circular-multi/circular-multi.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { rel } from "../../utils/path"; import bundledSchema from "./bundled"; diff --git a/test/specs/date-strings/date-strings.spec.js b/test/specs/date-strings/date-strings.spec.js index 15f8a798..ed72c517 100644 --- a/test/specs/date-strings/date-strings.spec.js +++ b/test/specs/date-strings/date-strings.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema } from "./parsed"; diff --git a/test/specs/deep-circular/deep-circular.spec.js b/test/specs/deep-circular/deep-circular.spec.js index 58a5b7e0..55319a07 100644 --- a/test/specs/deep-circular/deep-circular.spec.js +++ b/test/specs/deep-circular/deep-circular.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve, shouldNotGetCalled } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, name, requiredString } from "./parsed"; diff --git a/test/specs/deep/deep.spec.js b/test/specs/deep/deep.spec.js index 32af402a..ecd11fa9 100644 --- a/test/specs/deep/deep.spec.js +++ b/test/specs/deep/deep.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, name, requiredString } from "./parsed"; diff --git a/test/specs/dereference-callback/dereference-callback.spec.js b/test/specs/dereference-callback/dereference-callback.spec.js index d804eea6..60ffc164 100644 --- a/test/specs/dereference-callback/dereference-callback.spec.js +++ b/test/specs/dereference-callback/dereference-callback.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { rel } from "../../utils/path"; describe("Schema with a $ref", () => { diff --git a/test/specs/error-source/error-source.spec.js b/test/specs/error-source/error-source.spec.js index 0676819b..cdf51470 100644 --- a/test/specs/error-source/error-source.spec.js +++ b/test/specs/error-source/error-source.spec.js @@ -3,7 +3,7 @@ import chaiSubset from "chai-subset"; use(chaiSubset); const { expect } = chai; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { shouldNotGetCalled } from "../../utils/helper"; import { abs, unixify } from "../../utils/path"; import { InvalidPointerError, ResolverError, MissingPointerError } from "../../../lib/util/errors"; diff --git a/test/specs/external-from-internal/external-from-internal.spec.js b/test/specs/external-from-internal/external-from-internal.spec.js index e2410773..2df8a2ab 100644 --- a/test/specs/external-from-internal/external-from-internal.spec.js +++ b/test/specs/external-from-internal/external-from-internal.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve } from "../../utils/helper"; import { abs, rel, url } from "../../utils/path"; import { schema as _schema, definitions } from "./parsed"; diff --git a/test/specs/external-multiple/external-multiple.spec.js b/test/specs/external-multiple/external-multiple.spec.js index 183e65a1..eb4b5840 100644 --- a/test/specs/external-multiple/external-multiple.spec.js +++ b/test/specs/external-multiple/external-multiple.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve } from "../../utils/helper"; import { abs, rel } from "../../utils/path"; import { schema as _schema, definitions } from "./parsed"; diff --git a/test/specs/external-partial/external-partial.spec.js b/test/specs/external-partial/external-partial.spec.js index 6a718d28..c835ba79 100644 --- a/test/specs/external-partial/external-partial.spec.js +++ b/test/specs/external-partial/external-partial.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, definitions, name, requiredString } from "./parsed"; diff --git a/test/specs/external/external.spec.js b/test/specs/external/external.spec.js index 6d0a1abe..68356fb7 100644 --- a/test/specs/external/external.spec.js +++ b/test/specs/external/external.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve } from "../../utils/helper"; import { abs, rel, url } from "../../utils/path"; import { schema as _schema, definitions, name, requiredString } from "./parsed"; diff --git a/test/specs/internal/internal.spec.js b/test/specs/internal/internal.spec.js index 68d167ae..6cacac2f 100644 --- a/test/specs/internal/internal.spec.js +++ b/test/specs/internal/internal.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import parsedSchema from "./parsed"; diff --git a/test/specs/no-refs/no-refs.spec.js b/test/specs/no-refs/no-refs.spec.js index 37aa092a..29edd3d3 100644 --- a/test/specs/no-refs/no-refs.spec.js +++ b/test/specs/no-refs/no-refs.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import parsedSchema from "./parsed"; diff --git a/test/specs/object-source-with-path/object-source-with-path.spec.js b/test/specs/object-source-with-path/object-source-with-path.spec.js index c237d804..5c16284a 100644 --- a/test/specs/object-source-with-path/object-source-with-path.spec.js +++ b/test/specs/object-source-with-path/object-source-with-path.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { cloneDeep } from "../../utils/helper"; import { abs, rel } from "../../utils/path"; import internalRefsParsedSchema from "../internal/parsed"; diff --git a/test/specs/object-source/object-source.spec.js b/test/specs/object-source/object-source.spec.js index 5d4e3b98..090feba8 100644 --- a/test/specs/object-source/object-source.spec.js +++ b/test/specs/object-source/object-source.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { cloneDeep } from "../../utils/helper"; import { cwd, abs } from "../../utils/path"; import internalRefsParsedSchema from "../internal/parsed"; diff --git a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js index 5b4ec925..3b873e3c 100644 --- a/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js +++ b/test/specs/ref-in-excluded-path/ref-in-excluded-path.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { rel } from "../../utils/path"; import dereferencedSchema from "./dereferenced"; diff --git a/test/specs/root/root.spec.js b/test/specs/root/root.spec.js index 674953bc..b20cd0ad 100644 --- a/test/specs/root/root.spec.js +++ b/test/specs/root/root.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, root, extended, name } from "./parsed"; diff --git a/test/specs/substrings/substrings.spec.js b/test/specs/substrings/substrings.spec.js index 52e1a564..36c743b8 100644 --- a/test/specs/substrings/substrings.spec.js +++ b/test/specs/substrings/substrings.spec.js @@ -1,5 +1,5 @@ import { expect } from "chai"; -import $RefParser from "../../../lib/index.js"; +import $RefParser from "../../../lib/index"; import { testResolve } from "../../utils/helper"; import { rel, abs } from "../../utils/path"; import { schema as _schema, definitions, strings } from "./parsed";