Skip to content

Commit

Permalink
Merge pull request #166 from stoplightio/fix/no-stoplight-packages
Browse files Browse the repository at this point in the history
Do not use Stoplight packages
  • Loading branch information
philsturgeon committed Apr 20, 2020
2 parents a4eec2b + 8dd2b59 commit 0980b0a
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 172 deletions.
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ declare namespace $RefParser {
export class JSONParserError extends Error {
readonly name: string;
readonly message: string;
readonly source: string;
readonly path: Array<string | number>;
readonly errors: string;
readonly code: JSONParserErrorType;
Expand Down
4 changes: 2 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { ono } = require("@jsdevtools/ono");
const url = require("./util/url");
const plugins = require("./util/plugins");
const { StoplightParserError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } = require("./util/errors");
const { ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } = require("./util/errors");

module.exports = parse;

Expand Down Expand Up @@ -140,7 +140,7 @@ function parseFile (file, options, $refs) {
else if (!err || !("error" in err)) {
reject(ono.syntax(`Unable to parse ${file.url}`));
}
else if (err.error instanceof ParserError || err.error instanceof StoplightParserError) {
else if (err.error instanceof ParserError) {
reject(err.error);
}
else {
Expand Down
19 changes: 7 additions & 12 deletions lib/parsers/json.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";

const { parseWithPointers } = require("@stoplight/json");
const { StoplightParserError } = require("../util/errors");

const { ParserError } = require("../util/errors");

module.exports = {
/**
Expand Down Expand Up @@ -46,18 +44,15 @@ module.exports = {

if (typeof data === "string") {
if (data.trim().length === 0) {
return;
return; // This mirrors the YAML behavior
}
else {
let result = parseWithPointers(data, {
ignoreDuplicateKeys: false,
});

if (StoplightParserError.hasErrors(result.diagnostics)) {
throw new StoplightParserError(result.diagnostics, file.url);
try {
return JSON.parse(data);
}
catch (e) {
throw new ParserError(e.message, file.url);
}

return result.data;
}
}
else {
Expand Down
19 changes: 7 additions & 12 deletions lib/parsers/yaml.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const { parseWithPointers } = require("@stoplight/yaml");
const { StoplightParserError } = require("../util/errors");
const YAML = require("../util/yaml");
const { ParserError } = require("../util/errors");

module.exports = {
/**
Expand Down Expand Up @@ -44,17 +44,12 @@ module.exports = {
}

if (typeof data === "string") {
let result = parseWithPointers(data, {
json: true,
mergeKeys: true,
ignoreDuplicateKeys: false,
});

if (StoplightParserError.hasErrors(result.diagnostics)) {
throw new StoplightParserError(result.diagnostics, file.url);
try {
return YAML.parse(data);
}
catch (e) {
throw new ParserError(e.message, file.url);
}

return result.data;
}
else {
// data is already a JavaScript value (object, array, number, null, NaN, etc.)
Expand Down
52 changes: 0 additions & 52 deletions lib/util/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,58 +48,6 @@ const JSONParserErrorGroup = exports.JSONParserErrorGroup = class JSONParserErro

setErrorName(JSONParserErrorGroup);

exports.StoplightParserError = class StoplightParserError extends JSONParserError {
constructor (diagnostics, source) {
super(`Error parsing ${source}`, source);

this.code = "ESTOPLIGHTPARSER";

this._source = source;
this._path = [];
this.errors = diagnostics.filter(StoplightParserError.pickError).map(error => {
let parserError = new ParserError(error.message, source);
parserError.message = error.message;
return parserError;
});
}

static pickError (diagnostic) {
return diagnostic.severity === 0;
}

static hasErrors (diagnostics) {
return diagnostics.some(StoplightParserError.pickError);
}

get source () {
return this._source;
}

set source (source) {
this._source = source;

if (this.errors) {
for (let error of this.errors) {
error.source = source;
}
}
}

get path () {
return this._path;
}

set path (path) {
this._path = path;

if (this.errors) {
for (let error of this.errors) {
error.path = path;
}
}
}
};

const ParserError = exports.ParserError = class ParserError extends JSONParserError {
constructor (message, source) {
super(`Error parsing ${source}: ${message}`, source);
Expand Down
21 changes: 13 additions & 8 deletions lib/util/url.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use strict";

const { pointerToPath } = require("@stoplight/json");

let isWindows = /^win/.test(process.platform),
forwardSlashPattern = /\//g,
protocolPattern = /^(\w{2,}):\/\//i,
url = module.exports;
url = module.exports,
jsonPointerSlash = /~1/g,
jsonPointerTilde = /~0/g;

// RegExp patterns to URL-encode special characters in local filesystem paths
let urlEncodePatterns = [
Expand Down Expand Up @@ -237,16 +237,21 @@ exports.toFileSystemPath = function toFileSystemPath (path, keepFileProtocol) {

/**
* Converts a $ref pointer to a valid JSON Path.
* It _does not_ throw.
*
* @param {string} pointer
* @returns {Array<number | string>}
*/
exports.safePointerToPath = function safePointerToPath (pointer) {
try {
return pointerToPath(pointer);
}
catch (ex) {
if (pointer.length <= 1 || pointer[0] !== "#" || pointer[1] !== "/") {
return [];
}

return pointer
.slice(2)
.split("/")
.map((value) => {
return decodeURIComponent(value)
.replace(jsonPointerSlash, "/")
.replace(jsonPointerTilde, "~");
});
};
34 changes: 34 additions & 0 deletions lib/util/yaml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint lines-around-comment: [2, {beforeBlockComment: false}] */
"use strict";

const yaml = require("js-yaml");
const { ono } = require("@jsdevtools/ono");

/**
* Simple YAML parsing functions, similar to {@link JSON.parse} and {@link JSON.stringify}
*/
module.exports = {
/**
* Parses a YAML string and returns the value.
*
* @param {string} text - The YAML string to be parsed
* @param {function} [reviver] - Not currently supported. Provided for consistency with {@link JSON.parse}
* @returns {*}
*/
parse (text, reviver) {
return yaml.safeLoad(text);
},

/**
* Converts a JavaScript value to a YAML string.
*
* @param {*} value - The value to convert to YAML
* @param {function|array} replacer - Not currently supported. Provided for consistency with {@link JSON.stringify}
* @param {string|number} space - The number of spaces to use for indentation, or a string containing the number of spaces.
* @returns {string}
*/
stringify (value, replacer, space) {
let indent = (typeof space === "string" ? space.length : space) || 2;
return yaml.safeDump(value, { indent });
}
};
71 changes: 6 additions & 65 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@
},
"dependencies": {
"@jsdevtools/ono": "^7.1.2",
"@stoplight/json": "^3.7.0",
"@stoplight/yaml": "^3.8.0",
"call-me-maybe": "^1.0.1"
"call-me-maybe": "^1.0.1",
"js-yaml": "^3.13.1"
}
}

0 comments on commit 0980b0a

Please sign in to comment.