Skip to content

Commit bbc1013

Browse files
mizdraclaude
andauthored
refactor(core): tidy up @value/@import parser internals (#414)
* refactor(core): rename @value parser node types for consistency The internal node types in at-value-parser were verbose and inconsistent with the surrounding vocabulary (TokenImporter, entries). Rename to align: - ValueImportDeclaration -> ValueImporter (matches *TokenImporter) - values -> entries (matches NamedTokenImporter/ExternalTokenReference) - discriminant 'valueDeclaration'/'valueImportDeclaration' -> 'declaration'/'importer' Aligning `entries` also lets the consumer drop the rename step. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * refactor(core): clean up @value/@import parser internals - Drop the dead `value` field comment in ValueDeclaration - Rename isValueImport/parseValueImport to isValueImporter/parseValueImporter to match the ValueImporter node type - Compute the @import specifier location with positionBy instead of hand-rolling line/column/offset Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent a247518 commit bbc1013

4 files changed

Lines changed: 109 additions & 115 deletions

File tree

packages/core/src/parser/at-import-parser.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,13 @@ function convertParsedAtImport(
3030
): ParsedAtImport {
3131
// The length of the `@import ` part in `@import '...'`
3232
const baseLength = 7 + (atImport.raws.afterName?.length ?? 0);
33-
const start = {
34-
line: atImport.source!.start!.line,
35-
column: atImport.source!.start!.column + baseLength + node.sourceIndex + (node.type === 'string' ? 1 : 0),
36-
offset: atImport.source!.start!.offset + baseLength + node.sourceIndex + (node.type === 'string' ? 1 : 0),
37-
};
38-
const end = {
39-
line: start.line,
40-
column: start.column + node.value.length,
41-
offset: start.offset + node.value.length,
42-
};
33+
// For string nodes, skip the leading quote to point at the specifier itself.
34+
const startIndex = baseLength + node.sourceIndex + (node.type === 'string' ? 1 : 0);
4335
return {
4436
from: node.value,
45-
fromLoc: { start, end },
37+
fromLoc: {
38+
start: atImport.positionBy({ index: startIndex }),
39+
end: atImport.positionBy({ index: startIndex + node.value.length }),
40+
},
4641
};
4742
}

packages/core/src/parser/at-value-parser.test.ts

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('parseAtValue', () => {
4949
},
5050
},
5151
"name": "basic",
52-
"type": "valueDeclaration",
52+
"type": "declaration",
5353
},
5454
"diagnostics": [],
5555
},
@@ -80,7 +80,7 @@ describe('parseAtValue', () => {
8080
},
8181
},
8282
"name": "withoutColon",
83-
"type": "valueDeclaration",
83+
"type": "declaration",
8484
},
8585
"diagnostics": [],
8686
},
@@ -111,7 +111,7 @@ describe('parseAtValue', () => {
111111
},
112112
},
113113
"name": "empty",
114-
"type": "valueDeclaration",
114+
"type": "declaration",
115115
},
116116
"diagnostics": [],
117117
},
@@ -142,7 +142,7 @@ describe('parseAtValue', () => {
142142
},
143143
},
144144
"name": "comment",
145-
"type": "valueDeclaration",
145+
"type": "declaration",
146146
},
147147
"diagnostics": [],
148148
},
@@ -173,27 +173,13 @@ describe('parseAtValue', () => {
173173
},
174174
},
175175
"name": "complex",
176-
"type": "valueDeclaration",
176+
"type": "declaration",
177177
},
178178
"diagnostics": [],
179179
},
180180
{
181181
"atValue": {
182-
"from": "test.css",
183-
"fromLoc": {
184-
"end": {
185-
"column": 29,
186-
"line": 6,
187-
"offset": 155,
188-
},
189-
"start": {
190-
"column": 21,
191-
"line": 6,
192-
"offset": 147,
193-
},
194-
},
195-
"type": "valueImportDeclaration",
196-
"values": [
182+
"entries": [
197183
{
198184
"loc": {
199185
"end": {
@@ -210,26 +196,26 @@ describe('parseAtValue', () => {
210196
"name": "import",
211197
},
212198
],
213-
},
214-
"diagnostics": [],
215-
},
216-
{
217-
"atValue": {
218199
"from": "test.css",
219200
"fromLoc": {
220201
"end": {
221-
"column": 39,
222-
"line": 7,
223-
"offset": 196,
202+
"column": 29,
203+
"line": 6,
204+
"offset": 155,
224205
},
225206
"start": {
226-
"column": 31,
227-
"line": 7,
228-
"offset": 188,
207+
"column": 21,
208+
"line": 6,
209+
"offset": 147,
229210
},
230211
},
231-
"type": "valueImportDeclaration",
232-
"values": [
212+
"type": "importer",
213+
},
214+
"diagnostics": [],
215+
},
216+
{
217+
"atValue": {
218+
"entries": [
233219
{
234220
"loc": {
235221
"end": {
@@ -261,26 +247,26 @@ describe('parseAtValue', () => {
261247
"name": "import2",
262248
},
263249
],
264-
},
265-
"diagnostics": [],
266-
},
267-
{
268-
"atValue": {
269250
"from": "test.css",
270251
"fromLoc": {
271252
"end": {
272-
"column": 40,
273-
"line": 8,
274-
"offset": 238,
253+
"column": 39,
254+
"line": 7,
255+
"offset": 196,
275256
},
276257
"start": {
277-
"column": 32,
278-
"line": 8,
279-
"offset": 230,
258+
"column": 31,
259+
"line": 7,
260+
"offset": 188,
280261
},
281262
},
282-
"type": "valueImportDeclaration",
283-
"values": [
263+
"type": "importer",
264+
},
265+
"diagnostics": [],
266+
},
267+
{
268+
"atValue": {
269+
"entries": [
284270
{
285271
"loc": {
286272
"end": {
@@ -310,6 +296,20 @@ describe('parseAtValue', () => {
310296
"name": "import3",
311297
},
312298
],
299+
"from": "test.css",
300+
"fromLoc": {
301+
"end": {
302+
"column": 40,
303+
"line": 8,
304+
"offset": 238,
305+
},
306+
"start": {
307+
"column": 32,
308+
"line": 8,
309+
"offset": 230,
310+
},
311+
},
312+
"type": "importer",
313313
},
314314
"diagnostics": [],
315315
},
@@ -340,27 +340,13 @@ describe('parseAtValue', () => {
340340
},
341341
},
342342
"name": "withSpace1",
343-
"type": "valueDeclaration",
343+
"type": "declaration",
344344
},
345345
"diagnostics": [],
346346
},
347347
{
348348
"atValue": {
349-
"from": "test.css",
350-
"fromLoc": {
351-
"end": {
352-
"column": 63,
353-
"line": 10,
354-
"offset": 332,
355-
},
356-
"start": {
357-
"column": 55,
358-
"line": 10,
359-
"offset": 324,
360-
},
361-
},
362-
"type": "valueImportDeclaration",
363-
"values": [
349+
"entries": [
364350
{
365351
"loc": {
366352
"end": {
@@ -405,6 +391,20 @@ describe('parseAtValue', () => {
405391
"name": "withSpace3",
406392
},
407393
],
394+
"from": "test.css",
395+
"fromLoc": {
396+
"end": {
397+
"column": 63,
398+
"line": 10,
399+
"offset": 332,
400+
},
401+
"start": {
402+
"column": 55,
403+
"line": 10,
404+
"offset": 324,
405+
},
406+
},
407+
"type": "importer",
408408
},
409409
"diagnostics": [],
410410
},
@@ -446,21 +446,7 @@ describe('parseAtValue', () => {
446446
},
447447
{
448448
"atValue": {
449-
"from": "test.css",
450-
"fromLoc": {
451-
"end": {
452-
"column": 27,
453-
"line": 2,
454-
"offset": 34,
455-
},
456-
"start": {
457-
"column": 19,
458-
"line": 2,
459-
"offset": 26,
460-
},
461-
},
462-
"type": "valueImportDeclaration",
463-
"values": [
449+
"entries": [
464450
{
465451
"loc": {
466452
"end": {
@@ -492,6 +478,20 @@ describe('parseAtValue', () => {
492478
"name": "b",
493479
},
494480
],
481+
"from": "test.css",
482+
"fromLoc": {
483+
"end": {
484+
"column": 27,
485+
"line": 2,
486+
"offset": 34,
487+
},
488+
"start": {
489+
"column": 19,
490+
"line": 2,
491+
"offset": 26,
492+
},
493+
},
494+
"type": "importer",
495495
},
496496
"diagnostics": [
497497
{
@@ -532,7 +532,7 @@ describe('parseAtValue', () => {
532532
},
533533
},
534534
"name": "\\\\c",
535-
"type": "valueDeclaration",
535+
"type": "declaration",
536536
},
537537
"diagnostics": [],
538538
},
@@ -563,7 +563,7 @@ describe('parseAtValue', () => {
563563
},
564564
},
565565
"name": "\\'d",
566-
"type": "valueDeclaration",
566+
"type": "declaration",
567567
},
568568
"diagnostics": [],
569569
},
@@ -594,7 +594,7 @@ describe('parseAtValue', () => {
594594
},
595595
},
596596
"name": "\\31",
597-
"type": "valueDeclaration",
597+
"type": "declaration",
598598
},
599599
"diagnostics": [],
600600
},

0 commit comments

Comments
 (0)