Skip to content

Commit 0980b0a

Browse files
authored
Merge pull request #166 from stoplightio/fix/no-stoplight-packages
Do not use Stoplight packages
2 parents a4eec2b + 8dd2b59 commit 0980b0a

File tree

12 files changed

+96
-172
lines changed

12 files changed

+96
-172
lines changed

lib/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ declare namespace $RefParser {
410410
export class JSONParserError extends Error {
411411
readonly name: string;
412412
readonly message: string;
413+
readonly source: string;
413414
readonly path: Array<string | number>;
414415
readonly errors: string;
415416
readonly code: JSONParserErrorType;

lib/parse.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { ono } = require("@jsdevtools/ono");
44
const url = require("./util/url");
55
const plugins = require("./util/plugins");
6-
const { StoplightParserError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } = require("./util/errors");
6+
const { ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } = require("./util/errors");
77

88
module.exports = parse;
99

@@ -140,7 +140,7 @@ function parseFile (file, options, $refs) {
140140
else if (!err || !("error" in err)) {
141141
reject(ono.syntax(`Unable to parse ${file.url}`));
142142
}
143-
else if (err.error instanceof ParserError || err.error instanceof StoplightParserError) {
143+
else if (err.error instanceof ParserError) {
144144
reject(err.error);
145145
}
146146
else {

lib/parsers/json.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"use strict";
22

3-
const { parseWithPointers } = require("@stoplight/json");
4-
const { StoplightParserError } = require("../util/errors");
5-
3+
const { ParserError } = require("../util/errors");
64

75
module.exports = {
86
/**
@@ -46,18 +44,15 @@ module.exports = {
4644

4745
if (typeof data === "string") {
4846
if (data.trim().length === 0) {
49-
return;
47+
return; // This mirrors the YAML behavior
5048
}
5149
else {
52-
let result = parseWithPointers(data, {
53-
ignoreDuplicateKeys: false,
54-
});
55-
56-
if (StoplightParserError.hasErrors(result.diagnostics)) {
57-
throw new StoplightParserError(result.diagnostics, file.url);
50+
try {
51+
return JSON.parse(data);
52+
}
53+
catch (e) {
54+
throw new ParserError(e.message, file.url);
5855
}
59-
60-
return result.data;
6156
}
6257
}
6358
else {

lib/parsers/yaml.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

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

66
module.exports = {
77
/**
@@ -44,17 +44,12 @@ module.exports = {
4444
}
4545

4646
if (typeof data === "string") {
47-
let result = parseWithPointers(data, {
48-
json: true,
49-
mergeKeys: true,
50-
ignoreDuplicateKeys: false,
51-
});
52-
53-
if (StoplightParserError.hasErrors(result.diagnostics)) {
54-
throw new StoplightParserError(result.diagnostics, file.url);
47+
try {
48+
return YAML.parse(data);
49+
}
50+
catch (e) {
51+
throw new ParserError(e.message, file.url);
5552
}
56-
57-
return result.data;
5853
}
5954
else {
6055
// data is already a JavaScript value (object, array, number, null, NaN, etc.)

lib/util/errors.js

-52
Original file line numberDiff line numberDiff line change
@@ -48,58 +48,6 @@ const JSONParserErrorGroup = exports.JSONParserErrorGroup = class JSONParserErro
4848

4949
setErrorName(JSONParserErrorGroup);
5050

51-
exports.StoplightParserError = class StoplightParserError extends JSONParserError {
52-
constructor (diagnostics, source) {
53-
super(`Error parsing ${source}`, source);
54-
55-
this.code = "ESTOPLIGHTPARSER";
56-
57-
this._source = source;
58-
this._path = [];
59-
this.errors = diagnostics.filter(StoplightParserError.pickError).map(error => {
60-
let parserError = new ParserError(error.message, source);
61-
parserError.message = error.message;
62-
return parserError;
63-
});
64-
}
65-
66-
static pickError (diagnostic) {
67-
return diagnostic.severity === 0;
68-
}
69-
70-
static hasErrors (diagnostics) {
71-
return diagnostics.some(StoplightParserError.pickError);
72-
}
73-
74-
get source () {
75-
return this._source;
76-
}
77-
78-
set source (source) {
79-
this._source = source;
80-
81-
if (this.errors) {
82-
for (let error of this.errors) {
83-
error.source = source;
84-
}
85-
}
86-
}
87-
88-
get path () {
89-
return this._path;
90-
}
91-
92-
set path (path) {
93-
this._path = path;
94-
95-
if (this.errors) {
96-
for (let error of this.errors) {
97-
error.path = path;
98-
}
99-
}
100-
}
101-
};
102-
10351
const ParserError = exports.ParserError = class ParserError extends JSONParserError {
10452
constructor (message, source) {
10553
super(`Error parsing ${source}: ${message}`, source);

lib/util/url.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use strict";
22

3-
const { pointerToPath } = require("@stoplight/json");
4-
53
let isWindows = /^win/.test(process.platform),
64
forwardSlashPattern = /\//g,
75
protocolPattern = /^(\w{2,}):\/\//i,
8-
url = module.exports;
6+
url = module.exports,
7+
jsonPointerSlash = /~1/g,
8+
jsonPointerTilde = /~0/g;
99

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

238238
/**
239239
* Converts a $ref pointer to a valid JSON Path.
240-
* It _does not_ throw.
241240
*
242241
* @param {string} pointer
243242
* @returns {Array<number | string>}
244243
*/
245244
exports.safePointerToPath = function safePointerToPath (pointer) {
246-
try {
247-
return pointerToPath(pointer);
248-
}
249-
catch (ex) {
245+
if (pointer.length <= 1 || pointer[0] !== "#" || pointer[1] !== "/") {
250246
return [];
251247
}
248+
249+
return pointer
250+
.slice(2)
251+
.split("/")
252+
.map((value) => {
253+
return decodeURIComponent(value)
254+
.replace(jsonPointerSlash, "/")
255+
.replace(jsonPointerTilde, "~");
256+
});
252257
};

lib/util/yaml.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint lines-around-comment: [2, {beforeBlockComment: false}] */
2+
"use strict";
3+
4+
const yaml = require("js-yaml");
5+
const { ono } = require("@jsdevtools/ono");
6+
7+
/**
8+
* Simple YAML parsing functions, similar to {@link JSON.parse} and {@link JSON.stringify}
9+
*/
10+
module.exports = {
11+
/**
12+
* Parses a YAML string and returns the value.
13+
*
14+
* @param {string} text - The YAML string to be parsed
15+
* @param {function} [reviver] - Not currently supported. Provided for consistency with {@link JSON.parse}
16+
* @returns {*}
17+
*/
18+
parse (text, reviver) {
19+
return yaml.safeLoad(text);
20+
},
21+
22+
/**
23+
* Converts a JavaScript value to a YAML string.
24+
*
25+
* @param {*} value - The value to convert to YAML
26+
* @param {function|array} replacer - Not currently supported. Provided for consistency with {@link JSON.stringify}
27+
* @param {string|number} space - The number of spaces to use for indentation, or a string containing the number of spaces.
28+
* @returns {string}
29+
*/
30+
stringify (value, replacer, space) {
31+
let indent = (typeof space === "string" ? space.length : space) || 2;
32+
return yaml.safeDump(value, { indent });
33+
}
34+
};

package-lock.json

+6-65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@
7171
},
7272
"dependencies": {
7373
"@jsdevtools/ono": "^7.1.2",
74-
"@stoplight/json": "^3.7.0",
75-
"@stoplight/yaml": "^3.8.0",
76-
"call-me-maybe": "^1.0.1"
74+
"call-me-maybe": "^1.0.1",
75+
"js-yaml": "^3.13.1"
7776
}
7877
}

0 commit comments

Comments
 (0)