1- // @flow
21/* eslint-disable no-param-reassign */
32
43import balanced from 'balanced-match' ;
@@ -7,54 +6,17 @@ import { kebabify, isPlainObject } from './utils';
76
87const RE_PROP_SET = / ^ ( - - ) ( [ \w - ] + ) ( \s * ) ( [: ] ? ) $ / ;
98
10- export type Options = {
11- preserve ?: boolean ,
12- sets ?: {
13- [ key : string ] : {
14- [ key : string ] : string ,
15- } ,
16- } ,
17- } ;
18-
19- type Node = Object ;
20- type Decl = Object ;
21- type Result = Object ;
22-
23- type Rule = {
24- type : string ,
25- selector : string ,
26- selectors : Array < string > ,
27- nodes : Array < Node > ,
28- parent : Rule ,
29- raws : Object ,
30- last : boolean ,
31- warn : ( result : Result , text : string , opts ?: Object ) => void ,
32- clone : ( ) => Rule ,
33- remove : ( ) => void ,
34- append : ( ) => void ,
35- prepend : ( children : Node | Array < Node > | Object | string ) => void ,
36- insertBefore : ( node : Node , add : Node | Array < Node > | Object | string ) => void ,
37- replaceWith : ( nodes : Array < Node > ) => void ,
38- walkDecls : ( rule : Decl ) => void ,
39- } ;
40-
41- type AtRule = {
42- params : string ,
43- } & Rule ;
44-
459export default class Visitor {
4610 cache = { } ;
47-
4811 result = { } ;
12+ options = { } ;
4913
50- options : Options = { } ;
51-
52- defaults : Options = {
14+ defaults = {
5315 preserve : false ,
5416 sets : { } ,
5517 } ;
5618
57- constructor ( options : Options ) {
19+ constructor ( options ) {
5820 this . options = {
5921 ...this . defaults ,
6022 ...options ,
@@ -68,11 +30,9 @@ export default class Visitor {
6830 prepend = ( ) => {
6931 const { sets } = this . options ;
7032
71- // $FlowFixMe
72- Object . keys ( sets ) . forEach ( ( setName : string ) => {
73- const newRule : Rule = postcss . rule ( { selector : `--${ setName } ` } ) ;
33+ Object . keys ( sets ) . forEach ( setName => {
34+ const newRule = postcss . rule ( { selector : `--${ setName } ` } ) ;
7435
75- // $FlowFixMe
7636 const set = sets [ setName ] ;
7737
7838 if ( typeof set === 'string' ) {
@@ -94,15 +54,15 @@ export default class Visitor {
9454 /**
9555 * Collect all `:root` declared property sets and save them.
9656 */
97- collect = ( rule : Rule ) => {
57+ collect = rule => {
9858 const matches = RE_PROP_SET . exec ( rule . selector ) ;
9959
10060 if ( ! matches ) {
10161 return ;
10262 }
10363
104- const setName : string = matches [ 2 ] ;
105- const { parent } : Rule = rule ;
64+ const setName = matches [ 2 ] ;
65+ const { parent } = rule ;
10666
10767 if ( parent . selector !== ':root' ) {
10868 rule . warn (
@@ -122,7 +82,7 @@ export default class Visitor {
12282 // Custom property sets override each other wholly,
12383 // rather than cascading together like colliding style rules do.
12484 // @see : https://tabatkins.github.io/specs/css-apply-rule/#defining
125- const newRule : Rule = rule . clone ( ) ;
85+ const newRule = rule . clone ( ) ;
12686 this . cache [ setName ] = newRule ;
12787
12888 if ( ! this . options . preserve ) {
@@ -139,8 +99,8 @@ export default class Visitor {
13999 * Replace nested `@apply` at-rules declarations.
140100 */
141101 resolveNested = ( ) => {
142- Object . keys ( this . cache ) . forEach ( ( rule : string ) => {
143- this . cache [ rule ] . walkAtRules ( 'apply' , ( atRule : AtRule ) => {
102+ Object . keys ( this . cache ) . forEach ( rule => {
103+ this . cache [ rule ] . walkAtRules ( 'apply' , atRule => {
144104 this . resolve ( atRule ) ;
145105
146106 // @TODO honor `preserve` option.
@@ -152,8 +112,8 @@ export default class Visitor {
152112 /**
153113 * Replace `@apply` at-rules declarations with provided custom property set.
154114 */
155- resolve = ( atRule : AtRule ) => {
156- let ancestor : Rule = atRule . parent ;
115+ resolve = atRule => {
116+ let ancestor = atRule . parent ;
157117
158118 while ( ancestor && ancestor . type !== 'rule' ) {
159119 ancestor = ancestor . parent ;
@@ -173,15 +133,15 @@ export default class Visitor {
173133 return ;
174134 }
175135
176- const param : string = getParamValue ( atRule . params ) ;
136+ const param = getParamValue ( atRule . params ) ;
177137 const matches = RE_PROP_SET . exec ( param ) ;
178138
179139 if ( ! matches ) {
180140 return ;
181141 }
182142
183- const setName : string = matches [ 2 ] ;
184- const { parent } : Rule = atRule ;
143+ const setName = matches [ 2 ] ;
144+ const { parent } = atRule ;
185145
186146 if ( ! ( setName in this . cache ) ) {
187147 atRule . warn (
@@ -208,7 +168,7 @@ export default class Visitor {
208168/**
209169 * Helper: return whether the rule is a custom property set definition.
210170 */
211- function isDefinition ( rule : Rule ) : boolean {
171+ function isDefinition ( rule ) {
212172 return (
213173 ! ! rule . selector &&
214174 ! ! RE_PROP_SET . exec ( rule . selector ) &&
@@ -220,15 +180,15 @@ function isDefinition(rule: Rule): boolean {
220180 * Helper: allow parens usage in `@apply` AtRule declaration.
221181 * This is for Polymer integration.
222182 */
223- function getParamValue ( param : string ) : string {
183+ function getParamValue ( param ) {
224184 return / ^ \( / . test ( param ) ? balanced ( '(' , ')' , param ) . body : param ;
225185}
226186
227187/**
228188 * Helper: remove excessive declarations indentation.
229189 */
230- function cleanIndent ( rule : Rule ) {
231- rule . walkDecls ( ( decl : Decl ) => {
190+ function cleanIndent ( rule ) {
191+ rule . walkDecls ( decl => {
232192 if ( typeof decl . raws . before === 'string' ) {
233193 decl . raws . before = decl . raws . before . replace ( / [ ^ \S \n \r ] { 2 , } / , ' ' ) ;
234194 }
@@ -239,7 +199,7 @@ function cleanIndent(rule: Rule) {
239199 * Helper: correctly handle property sets removal and semi-colons.
240200 * @See : postcss/postcss#1014
241201 */
242- function safeRemoveRule ( rule : Rule ) {
202+ function safeRemoveRule ( rule ) {
243203 if ( rule === rule . parent . last && rule . raws . ownSemicolon ) {
244204 rule . parent . raws . semicolon = true ;
245205 }
@@ -250,7 +210,7 @@ function safeRemoveRule(rule: Rule) {
250210/**
251211 * Helper: remove immediate preceding comments.
252212 */
253- function removeCommentBefore ( node : Node ) {
213+ function removeCommentBefore ( node ) {
254214 const previousNode = node . prev ( ) ;
255215
256216 if ( previousNode && previousNode . type === 'comment' ) {
0 commit comments