Skip to content

Commit 03e3ea5

Browse files
authored
Merge pull request #19 from mbland/check-string-first-avoid-escaped-whitespace-bug
Bump to 1.0.4, fix escaped whitespace before " bug
2 parents 9d3677a + b071ce6 commit 03e3ea5

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Running the wrapper will generate the local `file://` URL to the generated
5555
`index.html` file, e.g.:
5656

5757
```text
58-
file:///path/to/jsdoc/output/jsdoc-cli-wrapper/1.0.3/index.html
58+
file:///path/to/jsdoc/output/jsdoc-cli-wrapper/1.0.4/index.html
5959
```
6060

6161
You can click on or copy this link to open it in your browser. You can also open
@@ -99,10 +99,10 @@ This wrapper resolves both of these minor annoyances.
9999
```sh
100100
$ pnpm jsdoc
101101

102-
> [email protected].3 jsdoc /path/to/jsdoc-cli-wrapper
102+
> [email protected].4 jsdoc /path/to/jsdoc-cli-wrapper
103103
> node index.js -c jsdoc.json .
104104

105-
file:///path/to/jsdoc-cli-wrapper/jsdoc/jsdoc-cli-wrapper/1.0.3/index.html
105+
file:///path/to/jsdoc-cli-wrapper/jsdoc/jsdoc-cli-wrapper/1.0.4/index.html
106106
```
107107

108108
Of course, your own project would use `jsdoc-cli-wrapper` instead of `node

lib/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,19 @@ export function stripJsonComments(str) {
183183
for (let i = 0; i !== str.length; ++i) {
184184
let c = str[i]
185185

186-
if (c === '\n') {
187-
if (comment === 'line') comment = null
188-
} else if (c.trimStart() === '') { // preserve other existing whitespace
189-
} else if (inString) {
186+
if (inString) { // check first so illegally escaped whitespace won't hide "
190187
inString = c !== '"' || escaped
191188
escaped = c === '\\' && !escaped
189+
} else if (c === '\n') {
190+
if (comment === 'line') comment = null
191+
} else if (c.trimStart() === '') { // preserve all other existing whitespace
192192
} else if (comment) {
193193
if (c === '/' && comment === 'block' && str[i-1] === '*') comment = null
194194
c = ' '
195195
} else if (c === '/' || c === '*') { // maybe a comment, don't update comma
196196
if (str[i-1] === '/') { // definitely a comment, or else a syntax error
197197
comment = (c === '/') ? 'line' : 'block'
198-
c = result[i-1] = ' '
198+
result[i-1] = c = ' '
199199
}
200200
} else if (c === ',') {
201201
comma = i

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsdoc-cli-wrapper",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "JSDoc command line interface wrapper",
55
"main": "index.js",
66
"bin": "./index.js",

test/stripJsonComments.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,34 @@ describe('stripJsonComments', () => {
286286
` JSON at position ${src.indexOf(', // ...but this last comma')}`
287287
)
288288
})
289+
290+
test('a string contains an escaped space before the closing quote', () => {
291+
const src = [
292+
'{',
293+
' "opts": {',
294+
' // This comment should disappear regardless.',
295+
' "fubar": "If not handled, the comments below will remain.\\ "',
296+
' /* Escaped space is illegal JSON, but it shouldn\'t hide a',
297+
' * closing quote and prevent comment removal. */',
298+
' }',
299+
'}'].join('\n')
300+
301+
const result = stripJsonComments(src)
302+
303+
expect(result).toBe([
304+
'{',
305+
' "opts": {',
306+
' ',
307+
' "fubar": "If not handled, the comments below will remain.\\ "',
308+
' ',
309+
' ',
310+
' }',
311+
'}'].join('\n'))
312+
expect(() => JSON.parse(result)).toThrowError(
313+
// It will choke on the space, not the escape slash.
314+
errPrefix(' ', 'Bad escaped character in') +
315+
` JSON at position ${src.indexOf('\\ "') + 1}`
316+
)
317+
})
289318
})
290319
})

0 commit comments

Comments
 (0)