Skip to content

Commit cb85048

Browse files
authored
add is_anonymous flag to tree nodes (#6)
1 parent d134631 commit cb85048

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

src/TreeNode.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ export class TreeNode {
44
constructor(name) {
55
/** @type {string} */
66
this.name = name
7+
/** @type {boolean} */
8+
this.is_anonymous = false
79
/** @type {Map<string, TreeNode<T>>} */
810
this.children = new Map()
911
/** @type {T[]} */
1012
this.locations = []
1113
}
1214

1315
/**
14-
*
1516
* @param {string[]} path
1617
* @param {string} name
1718
* @param {T} location
@@ -33,13 +34,15 @@ export class TreeNode {
3334
// Otherwise, create the item and add the location
3435
const new_node = new TreeNode(name)
3536
new_node.locations.push(location)
37+
new_node.is_anonymous = name.startsWith('__anonymous')
3638
current.children.set(name, new_node)
3739
}
3840
}
3941

4042
/**
4143
* @typedef PlainObject
4244
* @property {string} name
45+
* @property {boolean} is_anonymous
4346
* @property {T[]} locations
4447
* @property {PlainObject[]} children
4548
*/
@@ -51,6 +54,7 @@ export class TreeNode {
5154
to_plain_object() {
5255
return {
5356
name: this.name,
57+
is_anonymous: this.is_anonymous,
5458
locations: this.locations,
5559
children: Array
5660
.from(this.children.values(), (child) => child.to_plain_object())

test/global.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,29 @@ test('mixed imports and layers', () => {
2525
let expected = [
2626
{
2727
name: '__anonymous-1__',
28+
is_anonymous: true,
2829
locations: [{ line: 2, column: 3, start: 3, end: 33 }],
2930
children: []
3031
},
3132
{
3233
name: 'test',
34+
is_anonymous: false,
3335
locations: [{ line: 3, column: 3, start: 36, end: 72 }],
3436
children: []
3537
},
3638
{
3739
name: 'anotherTest',
40+
is_anonymous: false,
3841
locations: [{ line: 4, column: 3, start: 75, end: 148 }],
3942
children: [
4043
{
4144
name: 'moreTest',
45+
is_anonymous: false,
4246
locations: [{ line: 5, column: 4, start: 99, end: 144 }],
4347
children: [
4448
{
4549
name: 'deepTest',
50+
is_anonymous: false,
4651
locations: [{ line: 6, column: 5, start: 121, end: 139 }],
4752
children: []
4853
}
@@ -52,6 +57,7 @@ test('mixed imports and layers', () => {
5257
},
5358
{
5459
name: '__anonymous-2__',
60+
is_anonymous: true,
5561
locations: [{ line: 10, column: 3, start: 176, end: 185 }],
5662
children: []
5763
}

test/import.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ test('@import url() layer', () => {
66
let actual = layer_tree('@import url("foo.css") layer;')
77
let expected = [{
88
name: '__anonymous-1__',
9+
is_anonymous: true,
910
locations: [{ line: 1, column: 1, start: 0, end: 29 }],
1011
children: []
1112
}]
@@ -16,6 +17,7 @@ test('@import url() LAYER', () => {
1617
let actual = layer_tree('@import url("foo.css") LAYER;')
1718
let expected = [{
1819
"name": "__anonymous-1__",
20+
is_anonymous: true,
1921
locations: [{ line: 1, column: 1, start: 0, end: 29 }],
2022
"children": []
2123
}]
@@ -26,6 +28,7 @@ test('@import url() layer(named)', () => {
2628
let actual = layer_tree('@import url("foo.css") layer(named);')
2729
let expected = [{
2830
name: 'named',
31+
is_anonymous: false,
2932
locations: [{ line: 1, column: 1, start: 0, end: 36 }],
3033
children: []
3134
}]
@@ -36,6 +39,7 @@ test('@import url() LAYER(named)', () => {
3639
let actual = layer_tree('@import url("foo.css") LAYER(named);')
3740
let expected = [{
3841
name: 'named',
42+
is_anonymous: false,
3943
locations: [{ line: 1, column: 1, start: 0, end: 36 }],
4044
children: []
4145
}]
@@ -46,9 +50,11 @@ test('@import url() layer(named.nested)', () => {
4650
let actual = layer_tree('@import url("foo.css") layer(named.nested);')
4751
let expected = [{
4852
name: 'named',
53+
is_anonymous: false,
4954
locations: [{ line: 1, column: 1, start: 0, end: 43 }],
5055
children: [{
5156
name: 'nested',
57+
is_anonymous: false,
5258
locations: [{ line: 1, column: 1, start: 0, end: 43 }],
5359
children: []
5460
}]
@@ -60,9 +66,11 @@ test('@import url() layer(named.nested )', () => {
6066
let actual = layer_tree('@import url("foo.css") layer(named.nested );')
6167
let expected = [{
6268
name: 'named',
69+
is_anonymous: false,
6370
locations: [{ line: 1, column: 1, start: 0, end: 48 }],
6471
children: [{
6572
name: 'nested',
73+
is_anonymous: false,
6674
locations: [{ line: 1, column: 1, start: 0, end: 48 }],
6775
children: []
6876
}]
@@ -74,9 +82,11 @@ test('@import url() layer(/* test */named.nested )', () => {
7482
let actual = layer_tree('@import url("foo.css") layer(/* test */named.nested );')
7583
let expected = [{
7684
name: 'named',
85+
is_anonymous: false,
7786
locations: [{ line: 1, column: 1, start: 0, end: 58 }],
7887
children: [{
7988
name: 'nested',
89+
is_anonymous: false,
8090
locations: [{ line: 1, column: 1, start: 0, end: 58 }],
8191
children: []
8292
}]

test/layer.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test('single anonymous layer without body', () => {
77
let expected = [
88
{
99
name: '__anonymous-1__',
10+
is_anonymous: true,
1011
children: [],
1112
locations: [{ line: 1, column: 1, start: 0, end: 7 }]
1213
},
@@ -19,6 +20,7 @@ test('single anonymous layer with body', () => {
1920
let expected = [
2021
{
2122
name: '__anonymous-1__',
23+
is_anonymous: true,
2224
children: [],
2325
locations: [{ line: 1, column: 1, start: 0, end: 9 }]
2426
},
@@ -31,6 +33,7 @@ test('single named layer without body', () => {
3133
let expected = [
3234
{
3335
name: 'first',
36+
is_anonymous: false,
3437
children: [],
3538
locations: [{ line: 1, column: 1, start: 0, end: 13 }]
3639
},
@@ -43,6 +46,7 @@ test('single named layer with body', () => {
4346
let expected = [
4447
{
4548
name: 'first',
49+
is_anonymous: false,
4650
children: [],
4751
locations: [{ line: 1, column: 1, start: 0, end: 15 }]
4852
},
@@ -55,11 +59,13 @@ test('multiple named layers in one line', () => {
5559
let expected = [
5660
{
5761
name: 'first',
62+
is_anonymous: false,
5863
children: [],
5964
locations: [{ line: 1, column: 1, start: 0, end: 21 }]
6065
},
6166
{
6267
name: 'second',
68+
is_anonymous: false,
6369
children: [],
6470
locations: [{ line: 1, column: 1, start: 0, end: 21 }]
6571
},
@@ -75,6 +81,7 @@ test('repeated use of the same layer name', () => {
7581
let expected = [
7682
{
7783
name: 'first',
84+
is_anonymous: false,
7885
children: [],
7986
locations: [
8087
{ line: 2, column: 3, start: 3, end: 18 },
@@ -98,19 +105,23 @@ test('nested layers', () => {
98105
let expected = [
99106
{
100107
name: 'first',
108+
is_anonymous: false,
101109
locations: [{ line: 2, column: 3, start: 3, end: 104 }],
102110
children: [
103111
{
104112
name: 'second',
113+
is_anonymous: false,
105114
locations: [{ line: 3, column: 4, start: 21, end: 100 }],
106115
children: [
107116
{
108117
name: 'third',
118+
is_anonymous: false,
109119
locations: [{ line: 4, column: 5, start: 41, end: 56 }],
110120
children: [],
111121
},
112122
{
113123
name: 'fourth',
124+
is_anonymous: false,
114125
locations: [{ line: 6, column: 5, start: 79, end: 95 }],
115126
children: [],
116127
},
@@ -131,10 +142,12 @@ test('nested layers with anonymous layers', () => {
131142
let expected = [
132143
{
133144
name: '__anonymous-1__',
145+
is_anonymous: true,
134146
locations: [{ line: 2, column: 3, start: 3, end: 28 }],
135147
children: [
136148
{
137149
name: '__anonymous-2__',
150+
is_anonymous: true,
138151
children: [],
139152
locations: [{ line: 3, column: 4, start: 15, end: 24 }],
140153
},
@@ -152,11 +165,13 @@ test('consecutive anonymous layers', () => {
152165
let expected = [
153166
{
154167
name: '__anonymous-1__',
168+
is_anonymous: true,
155169
locations: [{ line: 2, column: 3, start: 3, end: 12 }],
156170
children: [],
157171
},
158172
{
159173
name: '__anonymous-2__',
174+
is_anonymous: true,
160175
locations: [{ line: 3, column: 3, start: 15, end: 24 }],
161176
children: [],
162177
},
@@ -175,17 +190,20 @@ test('nested layers with anonymous layers and duplicate names', () => {
175190
let expected = [
176191
{
177192
name: '__anonymous-1__',
193+
is_anonymous: true,
178194
locations: [{ line: 2, column: 3, start: 3, end: 34 }],
179195
children: [
180196
{
181197
name: 'first',
198+
is_anonymous: false,
182199
children: [],
183200
locations: [{ line: 3, column: 4, start: 15, end: 30 }],
184201
},
185202
]
186203
},
187204
{
188205
name: 'first',
206+
is_anonymous: false,
189207
locations: [{ line: 6, column: 3, start: 38, end: 53 }],
190208
children: [],
191209
},

0 commit comments

Comments
 (0)