diff --git a/index.js b/index.js index 046a397..5f66ddf 100644 --- a/index.js +++ b/index.js @@ -13,14 +13,20 @@ function variable (variables, node, str, name, opts, result) { if (isIgnore(node, name)) return str if (opts.only) { - if (typeof opts.only[name] !== 'undefined') { + if ( + Object.prototype.hasOwnProperty.call(opts.only, name) && + typeof opts.only[name] !== 'undefined' + ) { return opts.only[name] } return str } - if (typeof variables[name] !== 'undefined') { + if ( + Object.prototype.hasOwnProperty.call(variables, name) && + typeof variables[name] !== 'undefined' + ) { return variables[name] } diff --git a/index.test.js b/index.test.js index 56a4e53..4822b17 100644 --- a/index.test.js +++ b/index.test.js @@ -257,4 +257,27 @@ test('works inside function-like declarations', () => { ) }) +test('rejects inherited object members as undefined variables', () => { + throws(() => { + postcss([plugin()]).process('a { color: $constructor }', { + from: '/test.css' + }).css + }, /Undefined variable/) +}) + +test('keeps inherited names unchanged in silent mode', () => { + run('a { color: $toString }', 'a { color: $toString }', { silent: true }) +}) + +test('ignores inherited members of only', () => { + run('a { color: $constructor }', 'a { color: $constructor }', { only: {} }) +}) + +test('allows explicitly defined object member names', () => { + run('$constructor: red; a { color: $constructor }', 'a { color: red }') + run('a { color: $toString }', 'a { color: blue }', { + only: { toString: 'blue' } + }) +}) + test.run()