From 6fb5c9c2a9bc87f5b03c33e8b005fd71cee42dc4 Mon Sep 17 00:00:00 2001 From: Akseli Palen Date: Mon, 8 Jan 2024 23:58:20 +0200 Subject: [PATCH] use replace instead of replaceAll to support node<=14 --- lib/stringify.js | 6 ++-- test/cli.test.js | 60 +++++++++++++++++++++++++++++++++++++++ test/fixture/package.json | 3 ++ 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/lib/stringify.js b/lib/stringify.js index 15b4ded..d9e9214 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -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 diff --git a/test/cli.test.js b/test/cli.test.js index 9a73354..c048576 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -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 ' + diff --git a/test/fixture/package.json b/test/fixture/package.json index b857efd..42314cf 100644 --- a/test/fixture/package.json +++ b/test/fixture/package.json @@ -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",