Skip to content

Commit

Permalink
add test that checks if references are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
waspeer committed Oct 14, 2023
1 parent 8d19ab8 commit 7437793
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions test/basic.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,55 @@ test('Should produce optional mask props', async t => {
test('Should produce typings files', async t => {
t.assert(fs.existsSync('./dist/open-props.module.d.ts'))
t.assert(fs.existsSync('./src/props.sizes.d.ts'))
})

test('References should be valid', async t => {
const formatter = new Intl.ListFormat();
const defined = new Set();
const referenced = new Set();
const referencedBy = new Map();

for (const [prop, value] of Object.entries(OpenProps)) {
// Add all defined variables to the defined set
defined.add(prop)

if (typeof value !== 'string') {
continue;
}

// Find all references to other variables: var(...)
const matches = value.matchAll(/var\(([^)]+)\)/g);

if (!matches) {
continue;
}

// Add all references to the referenced set
// Map all references to the prop that references them
for (const matchArray of matches) {
const reference = matchArray[1];

referenced.add(reference);

if (!prop.startsWith('--')) {
continue;
}

if (!referencedBy.has(reference)) {
referencedBy.set(reference, new Set());
}

referencedBy.get(reference).add(prop);
}
}

// Check that all referenced variables are defined
for (const reference of referenced) {
const referencing = formatter.format(Array.from(referencedBy.get(reference)));

t.assert(
defined.has(reference),
`Variable with name ${reference} was referenced by variable ${referencing}, but is not defined`
);
}
})

0 comments on commit 7437793

Please sign in to comment.