File tree Expand file tree Collapse file tree 5 files changed +56
-4
lines changed
src/compiler/phases/3-transform/css
tests/css/samples/unused-selector-in-between Expand file tree Collapse file tree 5 files changed +56
-4
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ ' svelte ' : patch
3+ ---
4+
5+ fix: preserve the separator between selectors when an unused selector is in between
Original file line number Diff line number Diff line change @@ -162,10 +162,13 @@ const visitors = {
162162 // Only add comments if we're not inside a complex selector that itself is unused
163163 if ( ! path . find ( ( n ) => n . type === 'ComplexSelector' && ! n . metadata . used ) ) {
164164 let pruning = false ;
165- let last = node . children [ 0 ] . start ;
166165
167- for ( let i = 0 ; i < node . children . length ; i += 1 ) {
168- const selector = node . children [ i ] ;
166+ const children = node . children ;
167+
168+ let last = children . start ;
169+
170+ for ( let i = 0 ; i < children . length ; i += 1 ) {
171+ const selector = children [ i ] ;
169172
170173 if ( selector . metadata . used === pruning ) {
171174 if ( pruning ) {
@@ -177,7 +180,10 @@ const visitors = {
177180 if ( i === 0 ) {
178181 state . code . prependRight ( selector . start , '/* (unused) ' ) ;
179182 } else {
180- state . code . overwrite ( last , selector . start , ' /* (unused) ' ) ;
183+ // If this is not the last selector add a separator
184+ const separator = i !== children . length - 1 ? ',' : '' ;
185+
186+ state . code . overwrite ( last , selector . start , `${ separator } /* (unused) ` ) ;
181187 }
182188 }
183189
Original file line number Diff line number Diff line change 1+ import { test } from '../../test' ;
2+
3+ export default test ( {
4+ warnings : [
5+ {
6+ code : 'css_unused_selector' ,
7+ end : {
8+ character : 72 ,
9+ column : 3 ,
10+ line : 10
11+ } ,
12+ message : 'Unused CSS selector "h4"' ,
13+ start : {
14+ character : 70 ,
15+ column : 1 ,
16+ line : 10
17+ }
18+ }
19+ ]
20+ } ) ;
Original file line number Diff line number Diff line change 1+
2+ h1 .svelte-xyz ,
3+ h2 .svelte-xyz ,
4+ h3 .svelte-xyz , /* (unused) h4*/
5+ p .svelte-xyz {
6+ color : red;
7+ }
Original file line number Diff line number Diff line change 1+ <h1 >h1</h1 >
2+ <h2 >h2</h2 >
3+ <h3 >h3</h3 >
4+ <p >p</p >
5+
6+ <style >
7+ h1 ,
8+ h2 ,
9+ h3 ,
10+ h4 ,
11+ p {
12+ color : red ;
13+ }
14+ </style >
You can’t perform that action at this time.
0 commit comments