Skip to content

Commit

Permalink
use replace instead of replaceAll to support node<=14
Browse files Browse the repository at this point in the history
  • Loading branch information
axelpale committed Jan 8, 2024
1 parent 2b543b1 commit 6fb5c9c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ const stringify = (value, options) => {
// - unescaped double or single quotes '
if (quote === '\'') {
// escape unescaped single quote
const escapedValue = value.replaceAll('\'', '\\\'')
const escapedValue = value.replace(/'/g, '\\\'')
return quote + escapedValue + quote
} else if (quote === '"') {
// escape unescaped double quote
const escapedValue = value.replaceAll('"', '\\"')
const escapedValue = value.replace(/"/g, '\\"')
return quote + escapedValue + quote
} else if (quote === '`') {
// escape unescaped backtick
const escapedValue = value.replaceAll('`', '\\`')
const escapedValue = value.replace(/`/g, '\\`')
return quote + escapedValue + quote
}
// else
Expand Down
60 changes: 60 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,66 @@ describe('genversion cli', () => {
})
})

it('should handle single quotes in properties', (done) => {
const clit = new CliTest()
const cmd = GENERATE_COMMAND + ' ' +
'--source ./test/fixture ' +
'--property author ' + P
clit.exec(cmd, (err, response) => {
if (err) {
return done(err)
}

readTemp().should.equal(SIGNATURE +
'module.exports = { ' +
'name: \'Fo\\\'o "Bar" l`Baz\'' +
' }\n'
)

return done()
})
})

it('should handle double quotes in properties', (done) => {
const clit = new CliTest()
const cmd = GENERATE_COMMAND + ' ' +
'--source ./test/fixture ' +
'--property author --double ' + P
clit.exec(cmd, (err, response) => {
if (err) {
return done(err)
}

readTemp().should.equal(SIGNATURE +
'module.exports = { ' +
'name: "Fo\'o \\"Bar\\" l`Baz"' +
' }\n'
)

return done()
})
})

it('should handle backticks in properties', (done) => {
const clit = new CliTest()
const cmd = GENERATE_COMMAND + ' ' +
'--source ./test/fixture ' +
'--property author -b ' + P
clit.exec(cmd, (err, response) => {
if (err) {
return done(err)
}

readTemp().should.equal(SIGNATURE +
'module.exports = { ' +
'name: `Fo\'o "Bar" l\\`Baz`' +
' }\n'
)

return done()
})
})

it('should not understand multiple property flags', (done) => {
const clit = new CliTest()
const cmd = GENERATE_COMMAND + ' --property name ' +
Expand Down
3 changes: 3 additions & 0 deletions test/fixture/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"keywords": ["foo", "bar"],
"main": "index.js",
"license": "MIT",
"author": {
"name": "Fo'o \"Bar\" l`Baz"
},
"dependencies": {},
"engines": {
"node": ">=10.0.0",
Expand Down

0 comments on commit 6fb5c9c

Please sign in to comment.