1
- import type { Plugin } from 'postcss'
1
+ import type { Plugin , PluginCreator } from 'postcss'
2
2
import parseValue from 'postcss-value-parser'
3
3
import { inGamut } from 'culori'
4
4
import { formatColor , getColorFromValue } from './color'
@@ -16,51 +16,55 @@ export function getEquivalentColor(value: string): string {
16
16
return formatColor ( color )
17
17
}
18
18
19
- export function equivalentColorValues ( { comments } : { comments : Comment [ ] } ) : Plugin {
20
- return {
21
- postcssPlugin : 'plugin' ,
22
- Declaration ( decl ) {
23
- if ( ! allowedFunctions . some ( ( fn ) => decl . value . includes ( fn ) ) ) {
24
- return
25
- }
26
-
27
- parseValue ( decl . value ) . walk ( ( node ) => {
28
- if ( node . type !== 'function' ) {
29
- return true
19
+ export const equivalentColorValues : PluginCreator < any > = Object . assign (
20
+ ( { comments } : { comments : Comment [ ] } ) : Plugin => {
21
+ return {
22
+ postcssPlugin : 'plugin' ,
23
+ Declaration ( decl ) {
24
+ if ( ! allowedFunctions . some ( ( fn ) => decl . value . includes ( fn ) ) ) {
25
+ return
30
26
}
31
27
32
- if ( node . value === 'var' ) {
33
- return true
34
- }
28
+ parseValue ( decl . value ) . walk ( ( node ) => {
29
+ if ( node . type !== 'function' ) {
30
+ return true
31
+ }
35
32
36
- if ( ! allowedFunctions . includes ( node . value ) ) {
37
- return false
38
- }
33
+ if ( node . value === 'var' ) {
34
+ return true
35
+ }
39
36
40
- const values = node . nodes . filter ( ( n ) => n . type === 'word' ) . map ( ( n ) => n . value )
41
- if ( values . length < 3 ) {
42
- return false
43
- }
37
+ if ( ! allowedFunctions . includes ( node . value ) ) {
38
+ return false
39
+ }
44
40
45
- let color = `${ node . value } (${ values . join ( ' ' ) } )`
41
+ const values = node . nodes . filter ( ( n ) => n . type === 'word' ) . map ( ( n ) => n . value )
42
+ if ( values . length < 3 ) {
43
+ return false
44
+ }
46
45
47
- let equivalent = getEquivalentColor ( color )
46
+ let color = ` ${ node . value } ( ${ values . join ( ' ' ) } )`
48
47
49
- if ( equivalent === color ) {
50
- return false
51
- }
48
+ let equivalent = getEquivalentColor ( color )
52
49
53
- comments . push ( {
54
- index :
55
- decl . source . start . offset +
56
- `${ decl . prop } ${ decl . raws . between } ` . length +
57
- node . sourceEndIndex ,
58
- value : equivalent ,
59
- } )
50
+ if ( equivalent === color ) {
51
+ return false
52
+ }
60
53
61
- return false
62
- } )
63
- } ,
64
- }
65
- }
66
- equivalentColorValues . postcss = true
54
+ comments . push ( {
55
+ index :
56
+ decl . source . start . offset +
57
+ `${ decl . prop } ${ decl . raws . between } ` . length +
58
+ node . sourceEndIndex ,
59
+ value : equivalent ,
60
+ } )
61
+
62
+ return false
63
+ } )
64
+ } ,
65
+ }
66
+ } ,
67
+ {
68
+ postcss : true as const ,
69
+ } ,
70
+ )
0 commit comments