Skip to content

Commit 3b1abb1

Browse files
committed
fix: handle double quotes inside single-quoted GYP strings
parseConfigGypi() converted single quotes to double quotes with a bare `config.replace(/'/g, '"')`, which produced invalid JSON when a single-quoted string contained double-quote characters, such as the GYP condition strings used by native addons (e.g. `'OS=="win"'`). Convert each single-quoted string individually and escape the double quotes inside it instead. Fixes: #3333 Signed-off-by: Paul Bouchon <mail@bitpshr.net>
1 parent ef586e1 commit 3b1abb1

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎lib/create-config-gypi.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ function parseConfigGypi (config) {
1010
config = config.replace(/#.*/g, '')
1111
// 2. join multiline strings
1212
config = config.replace(/'$\s+'/mg, '')
13-
// 3. normalize string literals from ' into "
14-
config = config.replace(/'/g, '"')
13+
// 3. normalize string literals from ' into ", escaping any double-quote
14+
// characters that appear inside a single-quoted string (e.g. GYP condition
15+
// strings such as 'OS=="win"') so the result is still valid JSON
16+
config = config.replace(/'(?:[^'\\]|\\.)*'/g, (str) => {
17+
const inner = str.slice(1, -1).replace(/\\'/g, "'").replace(/"/g, '\\"')
18+
return `"${inner}"`
19+
})
1520
return JSON.parse(config)
1621
}
1722

‎test/test-create-config-gypi.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,10 @@ describe('create-config-gypi', function () {
7676
const config = parseConfigGypi(str)
7777
assert.deepStrictEqual(config, { variables: { multiline: 'AB' } })
7878
})
79+
80+
it('config.gypi parsing with double quotes inside single-quoted strings', function () {
81+
const str = '{\'variables\': {\'cond\': \'OS=="win"\', \'flags\': \'-DFOO="x"\'}}'
82+
const config = parseConfigGypi(str)
83+
assert.deepStrictEqual(config, { variables: { cond: 'OS=="win"', flags: '-DFOO="x"' } })
84+
})
7985
})

0 commit comments

Comments
 (0)