Skip to content

Commit cac20c0

Browse files
authored
Warn on invalid @value definitions (princed#57)
1 parent 64c7795 commit cac20c0

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ const replaceValueSymbols = (valueString, replacements) => {
3838
};
3939

4040
const getDefinition = (atRule, existingDefinitions, requiredDefinitions) => {
41-
const [/* match */, name, middle, value, end] = matchValueDefinition.exec(atRule.params);
41+
const matches = matchValueDefinition.exec(atRule.params);
42+
if (!matches) {
43+
return null;
44+
}
45+
46+
const [/* match */, name, middle, value, end] = matches;
4247
const valueWithReplacements = replaceValueSymbols(value, existingDefinitions);
4348

4449
if (!requiredDefinitions) {
@@ -110,6 +115,9 @@ const walk = async (requiredDefinitions, walkFile, root, result) => {
110115
}
111116

112117
const newDefinitions = getDefinition(atRule, existingDefinitions, requiredDefinitions);
118+
if (!newDefinitions) {
119+
result.warn(`Invalid value definition: ${atRule.params}`);
120+
}
113121
return Object.assign(existingDefinitions, newDefinitions);
114122
};
115123

index.test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ test('should remove exports if noEmitExports is true', async (t) => {
5151
await run(t, '@value red blue;', '', { noEmitExports: true });
5252
});
5353

54-
test('gives an error when there is no semicolon between lines', async (t) => {
54+
test('gives a warning when there is no semicolon between lines', async (t) => {
5555
const input = '@value red blue\n@value green yellow';
5656
const processor = postcss([plugin]);
5757
const result = await processor.process(input, { from: undefined });
@@ -61,13 +61,23 @@ test('gives an error when there is no semicolon between lines', async (t) => {
6161
t.expect(warnings[0].text).toBe('Invalid value definition: red blue\n@value green yellow');
6262
});
6363

64+
test('gives a warning when @value definition is invalid', async (t) => {
65+
const input = '@value oops:;';
66+
const processor = postcss([plugin]);
67+
const result = await processor.process(input, { from: undefined });
68+
const warnings = result.warnings();
69+
70+
t.expect(warnings.length).toBe(1);
71+
t.expect(warnings[0].text).toBe('Invalid value definition: oops:');
72+
});
73+
6474
test('gives an error when path to imported file is wrong', async (t) => {
6575
const input = '@value red from "./non-existent-file.css"';
6676
const processor = postcss([plugin]);
6777
await t.expect(processor.process(input, parserOpts)).rejects.toThrow("Can't resolve './non-existent-file.css'");
6878
});
6979

70-
test('gives an error when @value statement is invalid', async (t) => {
80+
test('gives an error when @value import statement is invalid', async (t) => {
7181
const input = '@value , from "./colors.css"';
7282
const processor = postcss([plugin]);
7383
await t.expect(processor.process(input, parserOpts)).rejects.toThrow('@value statement "" is invalid!');

0 commit comments

Comments
 (0)