1- import { prepareSourceForParsing , getBabelParserPluginsForFile } from '../src/utils' ;
1+ import { extractScriptFromSFC , getBabelParserPluginsForFile } from '../src/utils' ;
22
3- describe ( 'prepareSourceForParsing ' , ( ) => {
4- it ( 'returns default result when sourcePath is not provided' , ( ) => {
3+ describe ( 'extractScriptFromSFC ' , ( ) => {
4+ it ( 'returns passed source code and line with hasTypeScriptLang false when sourcePath is not provided' , ( ) => {
55 const sourceCode = `line1\nline2\nline3` ;
66
7- expect ( prepareSourceForParsing ( sourceCode , 2 ) ) . toEqual ( {
7+ expect ( extractScriptFromSFC ( sourceCode , 2 ) ) . toEqual ( {
88 code : sourceCode ,
99 targetLine : 2 ,
1010 hasTypeScriptLang : false ,
@@ -14,7 +14,7 @@ describe('prepareSourceForParsing', () => {
1414 it ( 'returns default result when sourcePath is not a framework file (.vue/.svelte)' , ( ) => {
1515 const sourceCode = `<script>console.log(1)</script>` ;
1616
17- expect ( prepareSourceForParsing ( sourceCode , 1 , 'src/app.ts' ) ) . toEqual ( {
17+ expect ( extractScriptFromSFC ( sourceCode , 1 , 'src/app.ts' ) ) . toEqual ( {
1818 code : sourceCode ,
1919 targetLine : 1 ,
2020 hasTypeScriptLang : false ,
@@ -35,7 +35,7 @@ describe('prepareSourceForParsing', () => {
3535 `const b = 2;\n` +
3636 `</script>\n` ;
3737
38- const res = prepareSourceForParsing ( sourceCode , 4 , 'src/App.vue' ) ;
38+ const res = extractScriptFromSFC ( sourceCode , 4 , 'src/App.vue' ) ;
3939
4040 // startLine = 2 (line with <script>)
4141 // originalLine = 4 => relativeLine = 4 - 2 + 1 = 3
@@ -51,7 +51,7 @@ describe('prepareSourceForParsing', () => {
5151 `let x: number = 1;\n` +
5252 `</script>\n` ;
5353
54- const res = prepareSourceForParsing ( sourceCode , 3 , 'src/App.vue' ) ;
54+ const res = extractScriptFromSFC ( sourceCode , 3 , 'src/App.vue' ) ;
5555
5656 expect ( res . hasTypeScriptLang ) . toBe ( true ) ;
5757 } ) ;
@@ -62,12 +62,12 @@ describe('prepareSourceForParsing', () => {
6262 `let y: number = 2;\n` +
6363 `</script>\n` ;
6464
65- const res = prepareSourceForParsing ( sourceCode , 2 , 'src/App.svelte' ) ;
65+ const res = extractScriptFromSFC ( sourceCode , 2 , 'src/App.svelte' ) ;
6666
6767 expect ( res . hasTypeScriptLang ) . toBe ( true ) ;
6868 } ) ;
6969
70- it ( 'picks the correct script block when there are multiple <script> tags' , ( ) => {
70+ it ( 'picks the script block related to the target line when there are multiple <script> tags' , ( ) => {
7171 const targetLine = 5 ;
7272 const startLine = 4 ;
7373
@@ -86,7 +86,7 @@ describe('prepareSourceForParsing', () => {
8686 `let z: number = 3;\n` +
8787 `</script>\n` ;
8888
89- const res = prepareSourceForParsing ( sourceCode , targetLine , 'src/App.vue' ) ;
89+ const res = extractScriptFromSFC ( sourceCode , targetLine , 'src/App.vue' ) ;
9090
9191 expect ( res . code ) . toBe ( `\nlet z: number = 3;\n` ) ;
9292 expect ( res . targetLine ) . toBe ( targetLine - startLine + 1 ) ;
@@ -103,7 +103,7 @@ describe('prepareSourceForParsing', () => {
103103 `</script>\n` ;
104104
105105 // line 2 is inside template, not script
106- const res = prepareSourceForParsing ( sourceCode , 2 , 'src/App.vue' ) ;
106+ const res = extractScriptFromSFC ( sourceCode , 2 , 'src/App.vue' ) ;
107107
108108 expect ( res ) . toEqual ( {
109109 code : sourceCode ,
@@ -115,7 +115,7 @@ describe('prepareSourceForParsing', () => {
115115 it ( 'handles script tag with attributes and inline content' , ( ) => {
116116 const sourceCode = `<script setup lang="ts">const a: number = 1;</script>\n` ;
117117 // originalLine 1 is the line containing <script ...> and content is inline
118- const res = prepareSourceForParsing ( sourceCode , 1 , 'src/App.vue' ) ;
118+ const res = extractScriptFromSFC ( sourceCode , 1 , 'src/App.vue' ) ;
119119
120120 expect ( res . code ) . toBe ( `const a: number = 1;` ) ;
121121 expect ( res . targetLine ) . toBe ( 1 ) ;
@@ -164,30 +164,24 @@ describe('getBabelParserPluginsForFile', () => {
164164 expect ( plugins ) . toEqual ( [ ...base , 'jsx' ] ) ;
165165 } ) ;
166166
167- it ( 'for .vue enables JSX (and TypeScript only if hasTypeScriptLang=true) ' , ( ) => {
167+ it ( 'for .vue enables one of JSX or TypeScript based on hasTypeScriptLang value ' , ( ) => {
168168 const pluginsNoTs = getBabelParserPluginsForFile ( 'src/App.vue' , false ) ;
169169
170170 expect ( pluginsNoTs ) . toEqual ( [ ...base , 'jsx' ] ) ;
171171
172172 const pluginsWithTs = getBabelParserPluginsForFile ( 'src/App.vue' , true ) ;
173173
174- expect ( pluginsWithTs ) . toEqual ( [ ...base , 'typescript' , 'jsx' ] ) ;
174+ expect ( pluginsWithTs ) . toEqual ( [ ...base , 'typescript' ] ) ;
175175 } ) ;
176176
177- it ( 'for .svelte enables JSX (and TypeScript only if hasTypeScriptLang=true) ' , ( ) => {
177+ it ( 'for .svelte enables one of JSX or TypeScript based on hasTypeScriptLang value ' , ( ) => {
178178 const pluginsNoTs = getBabelParserPluginsForFile ( 'src/App.svelte' , false ) ;
179179
180180 expect ( pluginsNoTs ) . toEqual ( [ ...base , 'jsx' ] ) ;
181181
182182 const pluginsWithTs = getBabelParserPluginsForFile ( 'src/App.svelte' , true ) ;
183183
184- expect ( pluginsWithTs ) . toEqual ( [ ...base , 'typescript' , 'jsx' ] ) ;
185- } ) ;
186-
187- it ( 'for .js enables JSX and respects hasTypeScriptLang' , ( ) => {
188- const plugins = getBabelParserPluginsForFile ( 'src/App.js' , true ) ;
189-
190- expect ( plugins ) . toEqual ( [ ...base , 'typescript' , 'jsx' ] ) ;
184+ expect ( pluginsWithTs ) . toEqual ( [ ...base , 'typescript' ] ) ;
191185 } ) ;
192186
193187 it ( 'for .js enables JSX without TypeScript when hasTypeScriptLang is false' , ( ) => {
0 commit comments