Skip to content

Commit

Permalink
chore: fix some type issues in the advanced presets plugins (#1338)
Browse files Browse the repository at this point in the history
* fix(stylehacks): check if match index exists

* chore(postcss-merge-idents): turn parameter into constant

* chore(postcss-reduce-idents): don't access node properties before narrowing node type

* chore(postcs-reduce-idents): replace array with Set
  • Loading branch information
ludofischer committed Feb 27, 2022
1 parent a79df07 commit 31c0d67
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 42 deletions.
45 changes: 27 additions & 18 deletions packages/postcss-merge-idents/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,32 @@ function canonical(obj) {
return key;
};
}

function mergeAtRules(css, pairs) {
pairs.forEach((pair) => {
pair.cache = [];
pair.replacements = [];
pair.decls = [];
pair.removals = [];
});
/**
* @param {import('postcss').Root} css
* @return {void}
*/
function mergeAtRules(css) {
const pairs = [
{
atrule: /keyframes/i,
decl: /animation/i,
/** @type {import('postcss').AtRule[]} */
cache: [],
replacements: {},
/** @type {import('postcss').Declaration[]} */
decls: [],
/** @type {import('postcss').AtRule[]} */
removals: [],
},
{
atrule: /counter-style/i,
decl: /(list-style|system)/i,
cache: [],
replacements: {},
decls: [],
removals: [],
},
];

let relevant;

Expand Down Expand Up @@ -100,16 +118,7 @@ function pluginCreator() {
postcssPlugin: 'postcss-merge-idents',

OnceExit(css) {
mergeAtRules(css, [
{
atrule: /keyframes/i,
decl: /animation/i,
},
{
atrule: /counter-style/i,
decl: /(list-style|system)/i,
},
]);
mergeAtRules(css);
},
};
}
Expand Down
12 changes: 6 additions & 6 deletions packages/postcss-reduce-idents/src/lib/counter-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const valueParser = require('postcss-value-parser');
const addToCache = require('./cache');

const RESERVED_KEYWORDS = [
const RESERVED_KEYWORDS = new Set([
'unset',
'initial',
'inherit',
Expand Down Expand Up @@ -64,7 +64,7 @@ const RESERVED_KEYWORDS = [
'upper-armenian',
'disclosure-open',
'disclosure-close',
];
]);

module.exports = function () {
let cache = {};
Expand All @@ -73,19 +73,19 @@ module.exports = function () {

return {
collect(node, encoder) {
const { name, prop, type } = node;
const { type } = node;

if (
type === 'atrule' &&
/counter-style/i.test(name) &&
RESERVED_KEYWORDS.indexOf(node.params.toLowerCase()) === -1
/counter-style/i.test(node.name) &&
!RESERVED_KEYWORDS.has(node.params.toLowerCase())
) {
addToCache(node.params, encoder, cache);

atRules.push(node);
}

if (type === 'decl' && /(list-style|system)/i.test(prop)) {
if (type === 'decl' && /(list-style|system)/i.test(node.prop)) {
decls.push(node);
}
},
Expand Down
7 changes: 4 additions & 3 deletions packages/postcss-reduce-idents/src/lib/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const valueParser = require('postcss-value-parser');
const addToCache = require('./cache');
const isNum = require('./isNum');

const RESERVED_KEYWORDS = ['unset', 'initial', 'inherit', 'none'];
const RESERVED_KEYWORDS = new Set(['unset', 'initial', 'inherit', 'none']);

module.exports = function () {
let cache = {};
Expand All @@ -12,18 +12,19 @@ module.exports = function () {

return {
collect(node, encoder) {
const { prop, type } = node;
const { type } = node;

if (type !== 'decl') {
return;
}
const { prop } = node;

if (/counter-(reset|increment)/i.test(prop)) {
node.value = valueParser(node.value).walk((child) => {
if (
child.type === 'word' &&
!isNum(child) &&
RESERVED_KEYWORDS.indexOf(child.value.toLowerCase()) === -1
!RESERVED_KEYWORDS.has(child.value.toLowerCase())
) {
addToCache(child.value, encoder, cache);

Expand Down
18 changes: 9 additions & 9 deletions packages/postcss-reduce-idents/src/lib/grid-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ const valueParser = require('postcss-value-parser');
const addToCache = require('./cache');
const isNum = require('./isNum');

const RESERVED_KEYWORDS = ['auto', 'span', 'inherit', 'initial', 'unset'];
const RESERVED_KEYWORDS = new Set([
'auto',
'span',
'inherit',
'initial',
'unset',
]);

module.exports = function () {
let cache = {};
Expand All @@ -22,10 +28,7 @@ module.exports = function () {
if (/\.+/.test(word)) {
// reduce empty zones to a single `.`
node.value = node.value.replace(word, '.');
} else if (
word &&
RESERVED_KEYWORDS.indexOf(word.toLowerCase()) === -1
) {
} else if (word && !RESERVED_KEYWORDS.has(word.toLowerCase())) {
addToCache(word, encoder, cache);
}
});
Expand All @@ -35,10 +38,7 @@ module.exports = function () {
declCache.push(node);
} else if (node.prop.toLowerCase() === 'grid-area') {
valueParser(node.value).walk((child) => {
if (
child.type === 'word' &&
RESERVED_KEYWORDS.indexOf(child.value) === -1
) {
if (child.type === 'word' && !RESERVED_KEYWORDS.has(child.value)) {
addToCache(child.value, encoder, cache);
}
});
Expand Down
10 changes: 5 additions & 5 deletions packages/postcss-reduce-idents/src/lib/keyframes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const valueParser = require('postcss-value-parser');
const addToCache = require('./cache');

const RESERVED_KEYWORDS = ['none', 'inherit', 'initial', 'unset'];
const RESERVED_KEYWORDS = new Set(['none', 'inherit', 'initial', 'unset']);

module.exports = function () {
let cache = {};
Expand All @@ -11,18 +11,18 @@ module.exports = function () {

return {
collect(node, encoder) {
const { name, prop, type } = node;
const { type } = node;

if (
type === 'atrule' &&
/keyframes/i.test(name) &&
RESERVED_KEYWORDS.indexOf(node.params.toLowerCase()) === -1
/keyframes/i.test(node.name) &&
!RESERVED_KEYWORDS.has(node.params.toLowerCase())
) {
addToCache(node.params, encoder, cache);
atRules.push(node);
}

if (type === 'decl' && /animation/i.test(prop)) {
if (type === 'decl' && /animation/i.test(node.prop)) {
decls.push(node);
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/stylehacks/src/plugins/important.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = class Important extends BasePlugin {

detect(decl) {
const match = decl.value.match(/!\w/);
if (match) {
if (match && match.index) {
const hack = decl.value.substr(match.index, decl.value.length - 1);
this.push(decl, {
identifier: '!important',
Expand Down

0 comments on commit 31c0d67

Please sign in to comment.