File tree Expand file tree Collapse file tree 4 files changed +94
-22
lines changed Expand file tree Collapse file tree 4 files changed +94
-22
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ export class Generator {
88 walk ( node : SimpleNode ) : string | void {
99 switch ( node . type ) {
1010 case 'Program' :
11- return node . body . map ( this . walk . bind ( this ) ) . filter ( ( n : any ) => ! ! n ) . join ( '\n ' )
11+ return node . body . map ( this . walk . bind ( this ) ) . filter ( ( n : any ) => ! ! n && n !== '\r\n' ) . join ( '' )
1212 case 'CodeStatement' :
1313 return node . value
1414 }
Original file line number Diff line number Diff line change @@ -13,11 +13,37 @@ export class Lexer {
1313 scanner:
1414 while ( this . current < code . length ) {
1515 const startIndex = this . current
16- let endIndex = code . indexOf ( '\n' , startIndex + 1 )
17- if ( endIndex === - 1 )
16+ // 查找最近的换行符(\r\n, \n, \r)
17+ const nextCR = code . indexOf ( '\r' , startIndex )
18+ const nextLF = code . indexOf ( '\n' , startIndex )
19+ let endIndex : number
20+
21+ if ( nextCR === - 1 && nextLF === - 1 ) {
22+ // 没有找到换行符,说明是最后一行
1823 endIndex = code . length
24+ }
25+ else if ( nextCR === - 1 ) {
26+ // 只有 \n
27+ endIndex = nextLF
28+ }
29+ else if ( nextLF === - 1 ) {
30+ // 只有 \r
31+ endIndex = nextCR
32+ }
33+ else if ( nextCR < nextLF ) {
34+ // 如果是 \r\n,跳过 \n
35+ endIndex = nextCR
36+ if ( nextLF === nextCR + 1 ) {
37+ endIndex += 2
38+ }
39+ }
40+ else {
41+ // \n
42+ endIndex = nextLF
43+ }
1944
20- const line = code . slice ( startIndex , endIndex ) . trim ( )
45+ const rawLine = code . slice ( startIndex , endIndex )
46+ const line = rawLine . trim ( )
2147 if ( isComment ( line ) ) {
2248 for ( const lex of this . lexers ) {
2349 const comment = parseComment ( line )
@@ -32,7 +58,7 @@ export class Lexer {
3258 }
3359 this . tokens . push ( {
3460 type : 'code' ,
35- value : line ,
61+ value : rawLine ,
3662 } as CodeToken )
3763 this . current = endIndex
3864 }
Original file line number Diff line number Diff line change @@ -4,64 +4,95 @@ exports[`if > should parse if.css, dev = false 1`] = `
44"body {
55}
66body {
7- content : " !DEV" ;
7+ content : " !DEV" ;
88}
99body {
10- content : " !DEV" ;
10+ content : " !DEV" ;
1111}
1212body {
13- } "
13+ }
14+ "
1415` ;
1516
1617exports [` if > should parse if.css, dev = true 1` ] = `
1718"body {
18- content : " DEV" ;
19+ content : " DEV" ;
1920}
2021body {
21- content : " !DEV else" ;
22+ content : " !DEV else" ;
2223}
2324body {
24- content : " TEST" ;
25+ content : " TEST" ;
2526}
2627body {
27- content : " else" ;
28- } "
28+ content : " else" ;
29+ }
30+ "
2931` ;
3032
3133exports [` if > should parse if.html, dev = false 1` ] = `
3234"<div >!DEV</div >
33- <div >!DEV</div >"
35+ <div >!DEV</div >
36+ "
3437` ;
3538
3639exports [` if > should parse if.html, dev = true 1` ] = `
3740"<div >DEV</div >
3841<div >!DEV else</div >
3942<div >TEST</div >
4043<div >
41- <div >else</div >
42- </div >"
44+ <div >else</div >
45+ </div >
46+ "
4347` ;
4448
4549exports [` if > should parse if.js, dev = false 1` ] = `
4650"console.log('!DEV')
47- console.log('!DEV')"
51+ console.log('!DEV')
52+ "
4853` ;
4954
5055exports [` if > should parse if.js, dev = true 1` ] = `
5156"console.log('DEV')
5257console.log('!DEV else')
5358console.log('TEST')
54- console.log('else')"
59+ console.log('else')
60+ "
5561` ;
5662
5763exports [` if > should parse if.jsx, dev = false 1` ] = `
5864"const Component = () => <div >
59- </div >;"
65+ </div >;
66+ "
6067` ;
6168
6269exports [` if > should parse if.jsx, dev = true 1` ] = `
6370"const Component = () => <div >
64- DEV
65- DEV=true
66- </div >;"
71+ DEV
72+ DEV=true
73+ </div >;
74+ "
75+ ` ;
76+
77+ exports [` if > should parse if.vue, dev = false 1` ] = `
78+ "<script setup lang = " ts" >
79+ </script >
80+ <template >
81+ <pre >{
82+ } </pre >
83+ </template >
84+ <style >
85+ </style >"
86+ ` ;
87+
88+ exports [` if > should parse if.vue, dev = true 1` ] = `
89+ "<script setup lang = " ts" >
90+ </script >
91+ <template >
92+ <pre >{
93+ " data" : ' value' // comment
94+ } </pre >
95+ </template >
96+ <style >
97+ </style >"
6798` ;
Original file line number Diff line number Diff line change 1+ <script setup lang="ts">
2+
3+ </script >
4+
5+ <template >
6+ <pre >{
7+ // #if DEV
8+ "data": 'value' // comment
9+ // #endif
10+ }</pre >
11+ </template >
12+
13+ <style >
14+
15+ </style >
You can’t perform that action at this time.
0 commit comments