Skip to content
Merged
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
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
23 changes: 23 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading