diff --git a/src/TreeNode.js b/src/TreeNode.js index 6f47f4b..df57fd7 100644 --- a/src/TreeNode.js +++ b/src/TreeNode.js @@ -4,6 +4,8 @@ export class TreeNode { constructor(name) { /** @type {string} */ this.name = name + /** @type {boolean} */ + this.is_anonymous = false /** @type {Map>} */ this.children = new Map() /** @type {T[]} */ @@ -11,7 +13,6 @@ export class TreeNode { } /** - * * @param {string[]} path * @param {string} name * @param {T} location @@ -33,6 +34,7 @@ export class TreeNode { // Otherwise, create the item and add the location const new_node = new TreeNode(name) new_node.locations.push(location) + new_node.is_anonymous = name.startsWith('__anonymous') current.children.set(name, new_node) } } @@ -40,6 +42,7 @@ export class TreeNode { /** * @typedef PlainObject * @property {string} name + * @property {boolean} is_anonymous * @property {T[]} locations * @property {PlainObject[]} children */ @@ -51,6 +54,7 @@ export class TreeNode { to_plain_object() { return { name: this.name, + is_anonymous: this.is_anonymous, locations: this.locations, children: Array .from(this.children.values(), (child) => child.to_plain_object()) diff --git a/test/global.spec.js b/test/global.spec.js index 668b9d3..4df610a 100644 --- a/test/global.spec.js +++ b/test/global.spec.js @@ -25,24 +25,29 @@ test('mixed imports and layers', () => { let expected = [ { name: '__anonymous-1__', + is_anonymous: true, locations: [{ line: 2, column: 3, start: 3, end: 33 }], children: [] }, { name: 'test', + is_anonymous: false, locations: [{ line: 3, column: 3, start: 36, end: 72 }], children: [] }, { name: 'anotherTest', + is_anonymous: false, locations: [{ line: 4, column: 3, start: 75, end: 148 }], children: [ { name: 'moreTest', + is_anonymous: false, locations: [{ line: 5, column: 4, start: 99, end: 144 }], children: [ { name: 'deepTest', + is_anonymous: false, locations: [{ line: 6, column: 5, start: 121, end: 139 }], children: [] } @@ -52,6 +57,7 @@ test('mixed imports and layers', () => { }, { name: '__anonymous-2__', + is_anonymous: true, locations: [{ line: 10, column: 3, start: 176, end: 185 }], children: [] } diff --git a/test/import.spec.js b/test/import.spec.js index a5785fd..5e22b83 100644 --- a/test/import.spec.js +++ b/test/import.spec.js @@ -6,6 +6,7 @@ test('@import url() layer', () => { let actual = layer_tree('@import url("foo.css") layer;') let expected = [{ name: '__anonymous-1__', + is_anonymous: true, locations: [{ line: 1, column: 1, start: 0, end: 29 }], children: [] }] @@ -16,6 +17,7 @@ test('@import url() LAYER', () => { let actual = layer_tree('@import url("foo.css") LAYER;') let expected = [{ "name": "__anonymous-1__", + is_anonymous: true, locations: [{ line: 1, column: 1, start: 0, end: 29 }], "children": [] }] @@ -26,6 +28,7 @@ test('@import url() layer(named)', () => { let actual = layer_tree('@import url("foo.css") layer(named);') let expected = [{ name: 'named', + is_anonymous: false, locations: [{ line: 1, column: 1, start: 0, end: 36 }], children: [] }] @@ -36,6 +39,7 @@ test('@import url() LAYER(named)', () => { let actual = layer_tree('@import url("foo.css") LAYER(named);') let expected = [{ name: 'named', + is_anonymous: false, locations: [{ line: 1, column: 1, start: 0, end: 36 }], children: [] }] @@ -46,9 +50,11 @@ test('@import url() layer(named.nested)', () => { let actual = layer_tree('@import url("foo.css") layer(named.nested);') let expected = [{ name: 'named', + is_anonymous: false, locations: [{ line: 1, column: 1, start: 0, end: 43 }], children: [{ name: 'nested', + is_anonymous: false, locations: [{ line: 1, column: 1, start: 0, end: 43 }], children: [] }] @@ -60,9 +66,11 @@ test('@import url() layer(named.nested )', () => { let actual = layer_tree('@import url("foo.css") layer(named.nested );') let expected = [{ name: 'named', + is_anonymous: false, locations: [{ line: 1, column: 1, start: 0, end: 48 }], children: [{ name: 'nested', + is_anonymous: false, locations: [{ line: 1, column: 1, start: 0, end: 48 }], children: [] }] @@ -74,9 +82,11 @@ test('@import url() layer(/* test */named.nested )', () => { let actual = layer_tree('@import url("foo.css") layer(/* test */named.nested );') let expected = [{ name: 'named', + is_anonymous: false, locations: [{ line: 1, column: 1, start: 0, end: 58 }], children: [{ name: 'nested', + is_anonymous: false, locations: [{ line: 1, column: 1, start: 0, end: 58 }], children: [] }] diff --git a/test/layer.spec.js b/test/layer.spec.js index 2df12ad..1930bab 100644 --- a/test/layer.spec.js +++ b/test/layer.spec.js @@ -7,6 +7,7 @@ test('single anonymous layer without body', () => { let expected = [ { name: '__anonymous-1__', + is_anonymous: true, children: [], locations: [{ line: 1, column: 1, start: 0, end: 7 }] }, @@ -19,6 +20,7 @@ test('single anonymous layer with body', () => { let expected = [ { name: '__anonymous-1__', + is_anonymous: true, children: [], locations: [{ line: 1, column: 1, start: 0, end: 9 }] }, @@ -31,6 +33,7 @@ test('single named layer without body', () => { let expected = [ { name: 'first', + is_anonymous: false, children: [], locations: [{ line: 1, column: 1, start: 0, end: 13 }] }, @@ -43,6 +46,7 @@ test('single named layer with body', () => { let expected = [ { name: 'first', + is_anonymous: false, children: [], locations: [{ line: 1, column: 1, start: 0, end: 15 }] }, @@ -55,11 +59,13 @@ test('multiple named layers in one line', () => { let expected = [ { name: 'first', + is_anonymous: false, children: [], locations: [{ line: 1, column: 1, start: 0, end: 21 }] }, { name: 'second', + is_anonymous: false, children: [], locations: [{ line: 1, column: 1, start: 0, end: 21 }] }, @@ -75,6 +81,7 @@ test('repeated use of the same layer name', () => { let expected = [ { name: 'first', + is_anonymous: false, children: [], locations: [ { line: 2, column: 3, start: 3, end: 18 }, @@ -98,19 +105,23 @@ test('nested layers', () => { let expected = [ { name: 'first', + is_anonymous: false, locations: [{ line: 2, column: 3, start: 3, end: 104 }], children: [ { name: 'second', + is_anonymous: false, locations: [{ line: 3, column: 4, start: 21, end: 100 }], children: [ { name: 'third', + is_anonymous: false, locations: [{ line: 4, column: 5, start: 41, end: 56 }], children: [], }, { name: 'fourth', + is_anonymous: false, locations: [{ line: 6, column: 5, start: 79, end: 95 }], children: [], }, @@ -131,10 +142,12 @@ test('nested layers with anonymous layers', () => { let expected = [ { name: '__anonymous-1__', + is_anonymous: true, locations: [{ line: 2, column: 3, start: 3, end: 28 }], children: [ { name: '__anonymous-2__', + is_anonymous: true, children: [], locations: [{ line: 3, column: 4, start: 15, end: 24 }], }, @@ -152,11 +165,13 @@ test('consecutive anonymous layers', () => { let expected = [ { name: '__anonymous-1__', + is_anonymous: true, locations: [{ line: 2, column: 3, start: 3, end: 12 }], children: [], }, { name: '__anonymous-2__', + is_anonymous: true, locations: [{ line: 3, column: 3, start: 15, end: 24 }], children: [], }, @@ -175,10 +190,12 @@ test('nested layers with anonymous layers and duplicate names', () => { let expected = [ { name: '__anonymous-1__', + is_anonymous: true, locations: [{ line: 2, column: 3, start: 3, end: 34 }], children: [ { name: 'first', + is_anonymous: false, children: [], locations: [{ line: 3, column: 4, start: 15, end: 30 }], }, @@ -186,6 +203,7 @@ test('nested layers with anonymous layers and duplicate names', () => { }, { name: 'first', + is_anonymous: false, locations: [{ line: 6, column: 3, start: 38, end: 53 }], children: [], },