Skip to content

Commit c00ad09

Browse files
committed
Clean up docs
1 parent 33ea15e commit c00ad09

1 file changed

Lines changed: 33 additions & 31 deletions

File tree

docs/writing-a-plugin.md

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -191,37 +191,6 @@ if (decl.value.includes('gradient(')) {
191191
}
192192
```
193193

194-
Also you can use `Symbol()` to store some meta information, to make plugin faster, more efficient and well tested. For example, mark rule as `skipped`, to prevent unnecessary tasks and add counter for test `skipped` is working correctly:
195-
196-
```js
197-
module.exports = (opts = {}) => {
198-
let rule = decl.parent;
199-
200-
let skipped = Symbol('isSkipped')
201-
let counter = Symbol('skippedCounter')
202-
203-
function makeRuleOverflowTouch(decl) {
204-
rule[counter] = Number.isInteger(rule[counter]) ? rule[counter] : 0;
205-
206-
if (!rule[skipped]) {
207-
// do some work
208-
rule[skipped] = true
209-
rule[counter]++
210-
}
211-
}
212-
213-
return {
214-
postcssPlugin: 'postcss-momentum-scrolling',
215-
Declaration: {
216-
'overflow': decl => makeRuleOverflowTouch(decl),
217-
'overflow-x': decl => makeRuleOverflowTouch(decl),
218-
'overflow-y': decl => makeRuleOverflowTouch(decl),
219-
}
220-
}
221-
}
222-
```
223-
Full [plugin](https://github.com/yunusga/postcss-momentum-scrolling/blob/master/index.js) and [test](https://github.com/yunusga/postcss-momentum-scrolling/blob/master/index.test.js#L15) example with `Symbol()` usage.
224-
225194
There two types or listeners: enter and exit. `Once`, `Root`, `AtRule`,
226195
and `Rule` will be called before processing children. `OnceExit`, `RootExit`,
227196
`AtRuleExit`, and `RuleExit` after processing all children inside node.
@@ -305,6 +274,39 @@ await postcss([plugin]).process('a { color: black }', { from })
305274
// => color: red
306275
```
307276

277+
Since visitors will re-visit node on any changes, just adding children will
278+
cause an infinite loop. To prevent it, you need to check
279+
that you already processed this node:
280+
281+
```js
282+
Declaration: {
283+
'will-change': decl => {
284+
if (decl.parent.some(decl => decl.prop === 'transform')) {
285+
decl.cloneBefore({ prop: 'transform', value: 'translate3d(0, 0, 0)' })
286+
}
287+
}
288+
}
289+
```
290+
291+
You can also use `Symbol` to mark processed nodes:
292+
293+
```js
294+
const processed = Symbol('processed')
295+
296+
const plugin = () => {
297+
return {
298+
postcssPlugin: 'example',
299+
Rule (rule) {
300+
if (!rule[processed]) {
301+
process(rule)
302+
rule[processed] = true
303+
}
304+
}
305+
}
306+
}
307+
plugin.postcss = true
308+
```
309+
308310
Second argument also have `result` object to add warnings:
309311

310312
```js

0 commit comments

Comments
 (0)