Skip to content

Commit 8bda5b1

Browse files
committed
feat: Remove deprecated exports Parser and parser
BREAKING CHANGE: The `Parser` class and `parser` instance did not bring any benefit. They were generated by Jison. After abandoning the Jison parser they were kept for compatibility only. The only method on the `Parser` prototype was the `parse`. It remains unchanged as a direct export. Drop the class interface and just call the `parse` method directly.
1 parent ea5a8a2 commit 8bda5b1

File tree

6 files changed

+64
-164
lines changed

6 files changed

+64
-164
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ This is a fork of the original package with the following enhancements:
2121
* Reports errors with rich additional information. From the schema validation too.
2222
* Implements JavaScript modules using [UMD] to work everywhere.
2323
* Depends on up-to-date npm modules with no installation warnings.
24-
* Small size - 19.4 kB minified, 6.7 kB gzipped.
24+
* Small size - 18.2 kB minified, 6.3 kB gzipped.
2525

26-
Integration to the favourite task loaders is provided by the following NPM modules:
26+
**Note:** In comparison with the original project, this package exports only the `parse` method; not the `Parser` object.
27+
28+
Integration to the favourite task loaders for JSON file validation is provided by the following NPM modules:
2729

2830
* [`Grunt`] - see [`@prantlf/grunt-jsonlint`]
2931
* [`Gulp`] - see [`@prantlf/gulp-jsonlint`]

scripts/bundle-jsonlint.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@ var prefix = `(function (global, factory) {
88
99
`
1010
var suffix = `
11-
exports.parser = jsonlint
12-
exports.Parser = jsonlint.Parser
13-
exports.parse = function (input, options) {
14-
return jsonlint.parse(input, options, false)
15-
}
16-
exports.parseNative = parseNative
17-
exports.parseCustom = parseCustom
11+
exports.parse = parse
1812
exports.tokenize = tokenize
19-
exports.getErrorTexts = getTexts
2013
exports.pathToPointer = pathToPointer
2114
exports.pointerToPath = pointerToPath
15+
16+
exports.parseNative = parseNative
17+
exports.parseCustom = parseCustom
18+
exports.getErrorTexts = getTexts
19+
2220
Object.defineProperty(exports, '__esModule', { value: true })
2321
}));
2422
`
2523
var unicodeFile = path.join(__dirname, '../src/unicode.js')
2624
var unicodeSource = fs.readFileSync(unicodeFile, 'utf8')
2725
var customFile = path.join(__dirname, '../src/custom-parser.js')
2826
var customSource = fs.readFileSync(customFile, 'utf8')
27+
var pointerFile = path.join(__dirname, '../src/pointer.js')
28+
var pointerSource = fs.readFileSync(pointerFile, 'utf8')
2929
var nativeFile = path.join(__dirname, '../src/native-parser.js')
3030
var nativeSource = fs.readFileSync(nativeFile, 'utf8')
3131
var configurableFile = path.join(__dirname, '../src/configurable-parser.js')
3232
var configurableSource = fs.readFileSync(configurableFile, 'utf8')
3333
var jsonlintFile = path.join(__dirname, '../lib/jsonlint.js')
3434
var jsonlintSource = prefix + unicodeSource + '\n' + customSource + '\n' +
35-
nativeSource + '\n' + configurableSource + suffix
35+
pointerSource + '\n' + nativeSource + '\n' + configurableSource + suffix
3636
fs.writeFileSync(jsonlintFile, jsonlintSource)

src/configurable-parser.js

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,12 @@
11
/* globals navigator, process, parseCustom, parseNative */
22

3-
function Parser (options, deprecate) {
4-
if (deprecate !== false) {
5-
console.warn('DEPRECATED: Do not instantiate an object of the class Parser. Call the method `parse` directly exported from the main module (`lib/jsonlint`).')
6-
}
7-
processOptions.call(this, options)
8-
}
9-
10-
function processOptions (options) {
11-
if (options) {
12-
var changed = {}
13-
if (options.ignoreComments !== undefined) {
14-
changed.ignoreComments = this.ignoreComments
15-
this.ignoreComments = options.ignoreComments
16-
}
17-
if (options.ignoreTrailingCommas !== undefined) {
18-
changed.ignoreTrailingCommas = this.ignoreTrailingCommas
19-
this.ignoreTrailingCommas = options.ignoreTrailingCommas
20-
}
21-
if (options.allowSingleQuotedStrings !== undefined) {
22-
changed.allowSingleQuotedStrings = this.allowSingleQuotedStrings
23-
this.allowSingleQuotedStrings = options.allowSingleQuotedStrings
24-
}
25-
if (options.allowDuplicateObjectKeys !== undefined) {
26-
changed.allowDuplicateObjectKeys = this.allowDuplicateObjectKeys
27-
this.allowDuplicateObjectKeys = options.allowDuplicateObjectKeys
28-
}
29-
if (options.mode !== undefined) {
30-
changed.mode = this.mode
31-
this.mode = options.mode
32-
}
33-
return changed
34-
}
35-
}
36-
37-
function restoreContext (changed) {
38-
if (changed) {
39-
for (var option in changed) {
40-
var value = changed[option]
41-
if (value === undefined) {
42-
delete this[option]
43-
} else {
44-
this[option] = value
45-
}
46-
}
47-
}
48-
}
49-
503
var isSafari = typeof navigator !== 'undefined' && /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor)
514
var oldNode = typeof process !== 'undefined' && process.version.startsWith('v4.')
525

53-
function needsCustomParser () {
54-
return this.ignoreComments || this.ignoreTrailingCommas ||
55-
this.allowSingleQuotedStrings || this.allowDuplicateObjectKeys === false ||
56-
this.mode === 'cjson' || this.mode === 'json5' || isSafari || oldNode
6+
function needsCustomParser (options) {
7+
return options.ignoreComments || options.ignoreTrailingCommas ||
8+
options.allowSingleQuotedStrings || options.allowDuplicateObjectKeys === false ||
9+
options.mode === 'cjson' || options.mode === 'json5' || isSafari || oldNode
5710
}
5811

5912
function getReviver (options) {
@@ -64,21 +17,9 @@ function getReviver (options) {
6417
}
6518
}
6619

67-
Parser.prototype.constructor = Parser
68-
Parser.prototype.Parser = Parser
69-
70-
Parser.prototype.parse = function (input, options, deprecate) {
71-
if (deprecate !== false) {
72-
console.warn('DEPRECATED: Do not call the instance method `parse`. method Call the `parse` directly exported from the main module (`lib/jsonlint`).')
73-
}
74-
var changed = processOptions.call(this, options)
75-
try {
76-
return needsCustomParser.call(this)
77-
? parseCustom.call(this, input, options)
78-
: parseNative(input, getReviver(options))
79-
} finally {
80-
restoreContext.call(this, changed)
81-
}
20+
function parse (input, options) { // eslint-disable-line no-unused-vars
21+
options || (options = {})
22+
return needsCustomParser(options)
23+
? parseCustom(input, options)
24+
: parseNative(input, getReviver(options))
8225
}
83-
84-
var jsonlint = new Parser(undefined, false) // eslint-disable-line no-unused-vars

src/custom-parser.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -654,38 +654,3 @@ function tokenize (input, options) { // eslint-disable-line no-unused-vars
654654
options.tokenize = true
655655
return parseInternal(input, options)
656656
}
657-
658-
function escapePointerToken (token) {
659-
return token
660-
.toString()
661-
.replace(/~/g, '~0')
662-
.replace(/\//g, '~1')
663-
}
664-
665-
function pathToPointer (tokens) { // eslint-disable-line no-unused-vars
666-
if (tokens.length === 0) {
667-
return ''
668-
}
669-
return '/' + tokens
670-
.map(escapePointerToken)
671-
.join('/')
672-
}
673-
674-
function unescapePointerToken (token) {
675-
return token
676-
.replace(/~1/g, '/')
677-
.replace(/~0/g, '~')
678-
}
679-
680-
function pointerToPath (pointer) { // eslint-disable-line no-unused-vars
681-
if (pointer === '') {
682-
return []
683-
}
684-
if (pointer[0] !== '/') {
685-
throw new Error('Missing initial "/" in the reference')
686-
}
687-
return pointer
688-
.substr(1)
689-
.split('/')
690-
.map(unescapePointerToken)
691-
}

src/pointer.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function escapePointerToken (token) {
2+
return token
3+
.toString()
4+
.replace(/~/g, '~0')
5+
.replace(/\//g, '~1')
6+
}
7+
8+
function pathToPointer (tokens) { // eslint-disable-line no-unused-vars
9+
if (tokens.length === 0) {
10+
return ''
11+
}
12+
return '/' + tokens
13+
.map(escapePointerToken)
14+
.join('/')
15+
}
16+
17+
function unescapePointerToken (token) {
18+
return token
19+
.replace(/~1/g, '/')
20+
.replace(/~0/g, '~')
21+
}
22+
23+
function pointerToPath (pointer) { // eslint-disable-line no-unused-vars
24+
if (pointer === '') {
25+
return []
26+
}
27+
if (pointer[0] !== '/') {
28+
throw new Error('Missing initial "/" in the reference')
29+
}
30+
return pointer
31+
.substr(1)
32+
.split('/')
33+
.map(unescapePointerToken)
34+
}

test/parse1.js

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ var path = require('path')
66
var assert = require('assert')
77

88
var exported = require('../lib/jsonlint')
9-
var Parser = exported.Parser
10-
var parser = exported.parser
119
var parse = exported.parse
1210
var validator = require('../lib/validator')
1311

@@ -32,61 +30,21 @@ function checkErrorInformation (error) {
3230
if (!nativeParser) {
3331
exports['test exported interface'] = function () {
3432
assert.equal(typeof parse, 'function')
35-
assert.equal(typeof parser, 'object')
36-
assert.equal(typeof Parser, 'function')
37-
assert.equal(typeof Parser.prototype, 'object')
38-
assert.equal(Object.getPrototypeOf(parser), Parser.prototype)
39-
assert.equal(Parser.prototype.constructor, Parser)
4033
}
4134

4235
exports['test function'] = function () {
4336
var json = '{"foo": "bar"}'
4437
assert.deepEqual(parse(json), { 'foo': 'bar' })
4538
}
39+
}
4640

47-
exports['test function object'] = function () {
48-
var json = '{"foo": "bar"}'
49-
var parser = new Parser()
50-
assert.deepEqual(parser.parse(json), { 'foo': 'bar' })
51-
}
52-
53-
exports['test context cleanup'] = function () {
54-
var json = '{"foo": "bar"}'
55-
assert.equal(Object.keys(parser).length, 0)
56-
assert.deepEqual(parse(json, { ignoreComments: true }), { 'foo': 'bar' })
57-
assert.equal(Object.keys(parser).length, 0)
58-
}
59-
60-
exports['test context restoration'] = function () {
61-
var json = '{"foo": "bar"}'
62-
var parser = new Parser({
63-
ignoreComments: false
64-
})
65-
assert.deepEqual(parse(json, { ignoreComments: true }), { 'foo': 'bar' })
66-
assert.equal(parser.ignoreComments, false)
67-
}
68-
69-
exports['test limited error information'] = function () {
70-
var json = '{"foo": ?}'
71-
var parser = new Parser({
72-
ignoreComments: true
73-
})
74-
try {
75-
parser.parse(json)
76-
assert.fail()
77-
} catch (error) {
78-
checkErrorInformation(error)
79-
}
80-
}
81-
} else {
82-
exports['test limited error information'] = function () {
83-
var json = '{"foo": ?}'
84-
try {
85-
parse(json)
86-
assert.fail()
87-
} catch (error) {
88-
checkErrorInformation(error)
89-
}
41+
exports['test limited error information'] = function () {
42+
var json = '{"foo": ?}'
43+
try {
44+
parse(json)
45+
assert.fail()
46+
} catch (error) {
47+
checkErrorInformation(error)
9048
}
9149
}
9250

0 commit comments

Comments
 (0)