Skip to content

Commit 88bef76

Browse files
authored
#1435 Add note to docs about Symbol() usage (#1436)
1 parent d1ce09b commit 88bef76

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

docs/writing-a-plugin.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,37 @@ 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+
194225
There two types or listeners: enter and exit. `Once`, `Root`, `AtRule`,
195226
and `Rule` will be called before processing children. `OnceExit`, `RootExit`,
196227
`AtRuleExit`, and `RuleExit` after processing all children inside node.

0 commit comments

Comments
 (0)