Skip to content

add is_anonymous flag to tree nodes #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
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
6 changes: 5 additions & 1 deletion src/TreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ export class TreeNode {
constructor(name) {
/** @type {string} */
this.name = name
/** @type {boolean} */
this.is_anonymous = false
/** @type {Map<string, TreeNode<T>>} */
this.children = new Map()
/** @type {T[]} */
this.locations = []
}

/**
*
* @param {string[]} path
* @param {string} name
* @param {T} location
Expand All @@ -33,13 +34,15 @@ 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)
}
}

/**
* @typedef PlainObject
* @property {string} name
* @property {boolean} is_anonymous
* @property {T[]} locations
* @property {PlainObject[]} children
*/
Expand All @@ -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())
Expand Down
6 changes: 6 additions & 0 deletions test/global.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
}
Expand All @@ -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: []
}
Expand Down
10 changes: 10 additions & 0 deletions test/import.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
}]
Expand All @@ -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": []
}]
Expand All @@ -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: []
}]
Expand All @@ -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: []
}]
Expand All @@ -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: []
}]
Expand All @@ -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: []
}]
Expand All @@ -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: []
}]
Expand Down
18 changes: 18 additions & 0 deletions test/layer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]
},
Expand All @@ -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 }]
},
Expand All @@ -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 }]
},
Expand All @@ -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 }]
},
Expand All @@ -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 }]
},
Expand All @@ -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 },
Expand All @@ -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: [],
},
Expand All @@ -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 }],
},
Expand All @@ -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: [],
},
Expand All @@ -175,17 +190,20 @@ 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 }],
},
]
},
{
name: 'first',
is_anonymous: false,
locations: [{ line: 6, column: 3, start: 38, end: 53 }],
children: [],
},
Expand Down