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
5 changes: 5 additions & 0 deletions .changeset/twenty-windows-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@css-modules-kit/core': patch
---

fix: disallow non-JavaScript identifier `@value`
69 changes: 68 additions & 1 deletion packages/core/src/parser/at-value-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,12 @@ describe('parseAtValue', () => {
`);
});
test('invalid', () => {
const [atValue1, atValue2] = fakeAtValues(
const [atValue1, atValue2, atValue3, atValue4] = fakeAtValues(
fakeRoot(dedent`
@value;
@value a,,b from "test.css";
@value non-js-ident-1: #000;
@value non-js-ident-1, non-js-ident-2 as alias_1, a as non-js-ident-3 from "test.css";
`),
);
expect(parseAtValue(atValue1!)).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -496,5 +498,70 @@ describe('parseAtValue', () => {
],
}
`);
expect(parseAtValue(atValue3!)).toMatchInlineSnapshot(`
{
"diagnostics": [
{
"category": "error",
"length": 14,
"start": {
"column": 8,
"line": 3,
},
"text": "css-modules-kit does not support non-JavaScript identifier as value names.",
},
],
}
`);
expect(parseAtValue(atValue4!)).toMatchInlineSnapshot(`
{
"atValue": {
"from": "test.css",
"fromLoc": {
"end": {
"column": 85,
"line": 4,
"offset": 150,
},
"start": {
"column": 77,
"line": 4,
"offset": 142,
},
},
"type": "valueImportDeclaration",
"values": [],
},
"diagnostics": [
{
"category": "error",
"length": 14,
"start": {
"column": 8,
"line": 4,
},
"text": "css-modules-kit does not support non-JavaScript identifier as value names.",
},
{
"category": "error",
"length": 14,
"start": {
"column": 24,
"line": 4,
},
"text": "css-modules-kit does not support non-JavaScript identifier as value names.",
},
{
"category": "error",
"length": 14,
"start": {
"column": 56,
"line": 4,
},
"text": "css-modules-kit does not support non-JavaScript identifier as value names.",
},
],
}
`);
});
});
34 changes: 34 additions & 0 deletions packages/core/src/parser/at-value-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AtRule } from 'postcss';
import type { DiagnosticPosition, DiagnosticWithDetachedLocation, Location } from '../type.js';
import { JS_IDENTIFIER_PATTERN } from '../util.js';

interface ValueDeclaration {
type: 'valueDeclaration';
Expand Down Expand Up @@ -83,6 +84,17 @@ export function parseAtValue(atValue: AtRule): ParseAtValueResult {
column: start.column + name.length,
offset: start.offset + name.length,
};

if (!JS_IDENTIFIER_PATTERN.test(name)) {
diagnostics.push({
start: { line: start.line, column: start.column },
length: name.length,
text: `css-modules-kit does not support non-JavaScript identifier as value names.`,
category: 'error',
});
continue;
}

const result = { name, loc: { start, end } };
if (localName === undefined) {
values.push(result);
Expand All @@ -98,6 +110,17 @@ export function parseAtValue(atValue: AtRule): ParseAtValueResult {
column: start.column + localName.length,
offset: start.offset + localName.length,
};

if (!JS_IDENTIFIER_PATTERN.test(localName)) {
diagnostics.push({
start: { line: start.line, column: start.column },
length: localName.length,
text: `css-modules-kit does not support non-JavaScript identifier as value names.`,
category: 'error',
});
continue;
}

values.push({ ...result, localName, localLoc: { start, end } });
}
} else {
Expand Down Expand Up @@ -154,6 +177,17 @@ export function parseAtValue(atValue: AtRule): ParseAtValueResult {
column: start.column + name.length,
offset: start.offset + name.length,
};

if (!JS_IDENTIFIER_PATTERN.test(name)) {
diagnostics.push({
start: { line: start.line, column: start.column },
length: name.length,
text: `css-modules-kit does not support non-JavaScript identifier as value names.`,
category: 'error',
});
return { diagnostics };
}

const parsedAtValue: ValueDeclaration = {
type: 'valueDeclaration',
name,
Expand Down