Skip to content

Commit

Permalink
Fix: do not crash on @layer {} or other atrules without prelude (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman authored Nov 2, 2023
1 parent 417665f commit c2a8d1d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 44 deletions.
28 changes: 28 additions & 0 deletions src/atrules/atrules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ AtRules('finds @layer', () => {
@layer components {
@layer defaults, structures, themes, utilities;
}
/* Anonymous layer (no prelude/layer name) */
@layer {
test {
color: green;
}
}
`
const actual = analyze(fixture).atrules.layer
const expected = {
Expand Down Expand Up @@ -241,6 +248,9 @@ AtRules('finds @imports', () => {
@import url('../example.css') layer;
@import url('remedy.css') layer(reset.remedy);
/* @import without prelude */
@import;
`
const actual = analyze(fixture).atrules.import
const expected = {
Expand All @@ -265,6 +275,9 @@ AtRules('finds @charsets', () => {
const fixture = `
@charset "UTF-8";
@charset "UTF-16";
/* No prelude */
@charset;
`
const actual = analyze(fixture).atrules.charset
const expected = {
Expand All @@ -289,6 +302,9 @@ AtRules('finds @supports', () => {
@media (min-width: 0) {
@supports (-webkit-appearance: none) {}
}
/* No prelude */
@supports {}
`
const actual = analyze(fixture).atrules.supports

Expand Down Expand Up @@ -353,6 +369,9 @@ AtRules('finds @media', () => {
@supports (-webkit-appearance: none) {
@media (min-width: 0) {}
}
/* No prelude */
@media {}
`
const actual = analyze(fixture).atrules.media

Expand Down Expand Up @@ -427,6 +446,9 @@ AtRules('analyzes @keyframes', () => {
@keyframes one {}
@keyframes TWO {}
/* No prelude */
@keyframes {}
/* Prefixes */
@-webkit-keyframes animation {}
@-moz-keyframes animation {}
Expand Down Expand Up @@ -523,6 +545,9 @@ AtRules('analyzes container queries', () => {
/* only applies when an inline-size container is available */
h2 { font-size: calc(1.2em + 1cqi); }
}
/* No prelude */
@container {}
`
const result = analyze(fixture)
const actual = result.atrules.container
Expand Down Expand Up @@ -590,6 +615,9 @@ AtRules('analyzes @property', () => {
}
}
}
/* No prelude */
@property {}
`
const actual = analyze(fixture).atrules.property
const expected = {
Expand Down
91 changes: 47 additions & 44 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,53 +178,56 @@ export function analyze(css, options = {}) {
break
}

if (atRuleName === 'media') {
let prelude = stringifyNode(node.prelude)
medias.push(prelude, node.prelude.loc)
if (isMediaBrowserhack(node.prelude)) {
mediaBrowserhacks.push(prelude, node.prelude.loc)
// All the AtRules in here MUST have a prelude, we we can count their names
if (node.prelude !== null) {
if (atRuleName === 'media') {
let prelude = stringifyNode(node.prelude)
medias.push(prelude, node.prelude.loc)
if (isMediaBrowserhack(node.prelude)) {
mediaBrowserhacks.push(prelude, node.prelude.loc)
}
break
}
break
}
if (atRuleName === 'supports') {
let prelude = stringifyNode(node.prelude)
supports.push(prelude, node.prelude.loc)
if (isSupportsBrowserhack(node.prelude)) {
supportsBrowserhacks.push(prelude, node.prelude.loc)
if (atRuleName === 'supports') {
let prelude = stringifyNode(node.prelude)
supports.push(prelude, node.prelude.loc)
if (isSupportsBrowserhack(node.prelude)) {
supportsBrowserhacks.push(prelude, node.prelude.loc)
}
break
}
break
}
if (endsWith('keyframes', atRuleName)) {
let name = '@' + atRuleName + ' ' + stringifyNode(node.prelude)
if (hasVendorPrefix(atRuleName)) {
prefixedKeyframes.push(name, node.prelude.loc)
if (endsWith('keyframes', atRuleName)) {
let name = '@' + atRuleName + ' ' + stringifyNode(node.prelude)
if (hasVendorPrefix(atRuleName)) {
prefixedKeyframes.push(name, node.prelude.loc)
}
keyframes.push(name, node.prelude.loc)
break
}
if (atRuleName === 'import') {
imports.push(stringifyNode(node.prelude), node.prelude.loc)
break
}
if (atRuleName === 'charset') {
charsets.push(stringifyNode(node.prelude), node.prelude.loc)
break
}
if (atRuleName === 'container') {
containers.push(stringifyNode(node.prelude), node.prelude.loc)
break
}
if (atRuleName === 'layer') {
let prelude = stringifyNode(node.prelude)
prelude
.split(',')
.forEach(name => layers.push(name.trim(), node.prelude.loc))
break
}
if (atRuleName === 'property') {
let prelude = stringifyNode(node.prelude)
registeredProperties.push(prelude, node.prelude.loc)
break
}
keyframes.push(name, node.prelude.loc)
break
}
if (atRuleName === 'import') {
imports.push(stringifyNode(node.prelude), node.prelude.loc)
break
}
if (atRuleName === 'charset') {
charsets.push(stringifyNode(node.prelude), node.prelude.loc)
break
}
if (atRuleName === 'container') {
containers.push(stringifyNode(node.prelude), node.prelude.loc)
break
}
if (atRuleName === 'layer') {
let prelude = stringifyNode(node.prelude)
prelude
.split(',')
.forEach(name => layers.push(name.trim(), node.prelude.loc))
break
}
if (atRuleName === 'property') {
let prelude = stringifyNode(node.prelude)
registeredProperties.push(prelude, node.prelude.loc)
break
}
break
}
Expand Down

0 comments on commit c2a8d1d

Please sign in to comment.