Skip to content

Commit d74b0a5

Browse files
committed
feat: throw an error if key is not defined
1 parent c75b461 commit d74b0a5

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/process-value.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {METHOD} from './constant';
1+
import {METHOD, ERROR_PREFIX} from './constant';
22

33
import parseParenthesisContent from './parse-parenthesis-content';
44

@@ -60,6 +60,10 @@ function getKeyFromMapString(mapString, keyParameter) {
6060
}
6161
}
6262

63+
if (!map[keyValue]) {
64+
throw new Error(`${ERROR_PREFIX} – unable to find “${keyValue}“ key inside map “(${mapString})“`);
65+
}
66+
6367
return map[keyValue];
6468
}
6569

test/test.plugin.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,15 @@ test('it should resolve nested invocation', t => {
5353
const value = '.foo {color: map-get(map-get((corporate: (textColor: green), ea: (textColor: black)), corporate), textColor)}';
5454
t.is(processing(value), expected);
5555
});
56+
57+
test('it should throw an error when key is not defined', t => {
58+
const requestedKey = 'notfound';
59+
const map = '(main: #FF0000)';
60+
const value = `.foo { color: map-get(${map}, ${requestedKey}) }`;
61+
62+
const testError = t.throws(() => {
63+
processing(value);
64+
}, null);
65+
66+
t.is(testError.message, `postcss – map-get – unable to find “${requestedKey}“ key inside map “${map}“`);
67+
});

0 commit comments

Comments
 (0)