From d126bea7c204fe34f72295c97ece18df49bfef08 Mon Sep 17 00:00:00 2001 From: Ludovico Fischer Date: Tue, 24 Nov 2020 18:22:32 +0100 Subject: [PATCH] refactor: switch to Once() API. --- src/index.js | 29 +++++++++++++++++------------ src/lib/transform.js | 5 ----- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/index.js b/src/index.js index 02c9dd5..905f999 100644 --- a/src/index.js +++ b/src/index.js @@ -8,20 +8,25 @@ function pluginCreator(opts) { mediaQueries: false, selectors: false }, opts); + return { postcssPlugin: 'postcss-calc', - Declaration(node, {result}) { - transform(node, "value", options, result); - }, - AtRule(node, {result}) { - if (options.mediaQueries) { - transform(node, "params", options, result); - } - }, - Rule(node, {result}) { - if (options.selectors) { - transform(node, "selector", options, result); - } + Once(css, { result }) { + css.walk(node => { + const { type } = node; + + if (type === 'decl') { + transform(node, "value", options, result); + } + + if (type === 'atrule' && options.mediaQueries) { + transform(node, "params", options, result); + } + + if (type === 'rule' && options.selectors) { + transform(node, "selector", options, result); + } + }); } } } diff --git a/src/lib/transform.js b/src/lib/transform.js index f97a1e1..2436ed4 100644 --- a/src/lib/transform.js +++ b/src/lib/transform.js @@ -8,7 +8,6 @@ import reducer from './reducer'; import stringifier from './stringifier'; const MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i; -const processed = Symbol('postcss-calc-processed'); function transformValue(value, options, result, item) { return valueParser(value).walk(node => { @@ -60,9 +59,6 @@ function transformSelector(value, options, result, item) { } export default (node, property, options, result) => { - if (node[processed]) { - return; - } const value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node); @@ -78,5 +74,4 @@ export default (node, property, options, result) => { } else { node[property] = value; } - node[processed] = true; };