Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/create-config-gypi.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ function parseConfigGypi (config) {
config = config.replace(/#.*/g, '')
// 2. join multiline strings
config = config.replace(/'$\s+'/mg, '')
// 3. normalize string literals from ' into "
config = config.replace(/'/g, '"')
// 3. normalize string literals from ' into ", escaping any double-quote
// characters that appear inside a single-quoted string (e.g. GYP condition
// strings such as 'OS=="win"') so the result is still valid JSON
config = config.replace(/'(?:[^'\\]|\\.)*'/g, (str) => {
const inner = str.slice(1, -1).replace(/\\'/g, "'").replace(/"/g, '\\"')
return `"${inner}"`
})
return JSON.parse(config)
}

Expand Down
6 changes: 6 additions & 0 deletions test/test-create-config-gypi.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,10 @@ describe('create-config-gypi', function () {
const config = parseConfigGypi(str)
assert.deepStrictEqual(config, { variables: { multiline: 'AB' } })
})

it('config.gypi parsing with double quotes inside single-quoted strings', function () {
const str = '{\'variables\': {\'cond\': \'OS=="win"\', \'flags\': \'-DFOO="x"\'}}'
const config = parseConfigGypi(str)
assert.deepStrictEqual(config, { variables: { cond: 'OS=="win"', flags: '-DFOO="x"' } })
})
})
Loading