Skip to content

Commit 7e3ac0b

Browse files
committed
fix: Do not modify input options in the tokenize method
These options can be reused for parse, tokenize or validator methods. Calling tokenize flips the output from the parsed object to token arraym, which would break the following usage of the same options.
1 parent fe3ac65 commit 7e3ac0b

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"search.exclude": {
1010
"**/benchmarks": true,
1111
"**/coverage": true,
12+
"**/lib/jsonlint.js": true,
1213
"**/test/fails": true,
1314
"**/test/passes": true,
1415
"**/test/recursive": true,

src/custom-parser.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,11 @@ function tokenize (input, options) { // eslint-disable-line no-unused-vars
651651
if (!options) {
652652
options = {}
653653
}
654+
// As long as this is the single modification, this is easier than cloning.
655+
// (Once Node.js 6 is dropped, this can be replaced by object destructuring.)
656+
var oldTokenize = options.tokenize
654657
options.tokenize = true
655-
return parseInternal(input, options)
658+
var tokens = parseInternal(input, options)
659+
options.tokenize = oldTokenize
660+
return tokens
656661
}

test/tokenize.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,10 @@ addTest('without raw input, location and path properties', function () {
127127
})
128128
})
129129

130+
addTest('does not enforce tokenization in the input options', function () {
131+
var options = {}
132+
tokenize('{}', options)
133+
assert.equal(options.tokenize, undefined)
134+
})
135+
130136
if (require.main === module) { require('test').run(exports) }

0 commit comments

Comments
 (0)