11import type { Mapping } from '@volar/language-core' ;
22import { camelize , capitalize } from '@vue/shared' ;
3- import { computed , unstable } from 'alien-signals' ;
3+ import { computed } from 'alien-signals' ;
44import * as path from 'path-browserify' ;
55import { generateScript } from '../codegen/script' ;
66import { generateTemplate } from '../codegen/template' ;
77import { parseScriptRanges } from '../parsers/scriptRanges' ;
88import { parseScriptSetupRanges } from '../parsers/scriptSetupRanges' ;
99import { parseVueCompilerOptions } from '../parsers/vueCompilerOptions' ;
1010import type { Code , Sfc , VueLanguagePlugin } from '../types' ;
11+ import { computedSet } from '../utils/signals' ;
1112import { CompilerOptionsResolver } from '../utils/ts' ;
1213
1314export const tsCodegen = new WeakMap < Sfc , ReturnType < typeof createTsx > > ( ) ;
@@ -28,23 +29,21 @@ const plugin: VueLanguagePlugin = ctx => {
2829 ] ,
2930
3031 getEmbeddedCodes ( fileName , sfc ) {
31- const tsx = useTsx ( fileName , sfc ) ;
32+ const codegen = useCodegen ( fileName , sfc ) ;
3233 const files : {
3334 id : string ;
3435 lang : string ;
3536 } [ ] = [ ] ;
36- if ( [ 'js' , 'ts' , 'jsx' , 'tsx' ] . includes ( tsx . lang . get ( ) ) ) {
37- files . push ( { id : 'script_' + tsx . lang . get ( ) , lang : tsx . lang . get ( ) } ) ;
37+ if ( [ 'js' , 'ts' , 'jsx' , 'tsx' ] . includes ( codegen . getLang ( ) ) ) {
38+ files . push ( { id : 'script_' + codegen . getLang ( ) , lang : codegen . getLang ( ) } ) ;
3839 }
3940 return files ;
4041 } ,
4142
4243 resolveEmbeddedCode ( fileName , sfc , embeddedFile ) {
43-
44- const _tsx = useTsx ( fileName , sfc ) ;
45-
4644 if ( / s c r i p t _ ( j s | j s x | t s | t s x ) / . test ( embeddedFile . id ) ) {
47- const tsx = _tsx . generatedScript . get ( ) ;
45+ const codegen = useCodegen ( fileName , sfc ) ;
46+ const tsx = codegen . getGeneratedScript ( ) ;
4847 if ( tsx ) {
4948 embeddedFile . content = [ ...tsx . codes ] ;
5049 embeddedFile . linkedCodeMappings = [ ...tsx . linkedCodeMappings ] ;
@@ -53,7 +52,7 @@ const plugin: VueLanguagePlugin = ctx => {
5352 } ,
5453 } ;
5554
56- function useTsx ( fileName : string , sfc : Sfc ) {
55+ function useCodegen ( fileName : string , sfc : Sfc ) {
5756 if ( ! tsCodegen . has ( sfc ) ) {
5857 let appendGlobalTypes = false ;
5958 if ( ! ctx . vueCompilerOptions . __setupedGlobalTypes && ! appendedGlobalTypes ) {
@@ -75,13 +74,13 @@ function createTsx(
7574 appendGlobalTypes : boolean
7675) {
7776 const ts = ctx . modules . typescript ;
78- const lang = computed ( ( ) => {
77+ const getLang = computed ( ( ) => {
7978 return ! _sfc . script && ! _sfc . scriptSetup ? 'ts'
8079 : _sfc . scriptSetup && _sfc . scriptSetup . lang !== 'js' ? _sfc . scriptSetup . lang
8180 : _sfc . script && _sfc . script . lang !== 'js' ? _sfc . script . lang
8281 : 'js' ;
8382 } ) ;
84- const vueCompilerOptions = computed ( ( ) => {
83+ const getResolvedOptions = computed ( ( ) => {
8584 const options = parseVueCompilerOptions ( _sfc . comments ) ;
8685 if ( options ) {
8786 const resolver = new CompilerOptionsResolver ( ) ;
@@ -90,20 +89,20 @@ function createTsx(
9089 }
9190 return ctx . vueCompilerOptions ;
9291 } ) ;
93- const scriptRanges = computed ( ( ) =>
92+ const getScriptRanges = computed ( ( ) =>
9493 _sfc . script
9594 ? parseScriptRanges ( ts , _sfc . script . ast , ! ! _sfc . scriptSetup , false )
9695 : undefined
9796 ) ;
98- const scriptSetupRanges = computed ( ( ) =>
97+ const getScriptSetupRanges = computed ( ( ) =>
9998 _sfc . scriptSetup
100- ? parseScriptSetupRanges ( ts , _sfc . scriptSetup . ast , vueCompilerOptions . get ( ) )
99+ ? parseScriptSetupRanges ( ts , _sfc . scriptSetup . ast , getResolvedOptions ( ) )
101100 : undefined
102101 ) ;
103- const scriptSetupBindingNames = unstable . computedSet (
102+ const getSetupBindingNames = computedSet (
104103 computed ( ( ) => {
105104 const newNames = new Set < string > ( ) ;
106- const bindings = scriptSetupRanges . get ( ) ?. bindings ;
105+ const bindings = getScriptSetupRanges ( ) ?. bindings ;
107106 if ( _sfc . scriptSetup && bindings ) {
108107 for ( const { range } of bindings ) {
109108 newNames . add ( _sfc . scriptSetup . content . slice ( range . start , range . end ) ) ;
@@ -112,10 +111,10 @@ function createTsx(
112111 return newNames ;
113112 } )
114113 ) ;
115- const scriptSetupImportComponentNames = unstable . computedSet (
114+ const getSetupImportComponentNames = computedSet (
116115 computed ( ( ) => {
117116 const newNames = new Set < string > ( ) ;
118- const bindings = scriptSetupRanges . get ( ) ?. bindings ;
117+ const bindings = getScriptSetupRanges ( ) ?. bindings ;
119118 if ( _sfc . scriptSetup && bindings ) {
120119 for ( const { range, moduleName, isDefaultImport, isNamespace } of bindings ) {
121120 if (
@@ -131,68 +130,68 @@ function createTsx(
131130 return newNames ;
132131 } )
133132 ) ;
134- const destructuredPropNames = unstable . computedSet (
133+ const getSetupDestructuredPropNames = computedSet (
135134 computed ( ( ) => {
136- const newNames = new Set ( scriptSetupRanges . get ( ) ?. defineProps ?. destructured ?. keys ( ) ) ;
137- const rest = scriptSetupRanges . get ( ) ?. defineProps ?. destructuredRest ;
135+ const newNames = new Set ( getScriptSetupRanges ( ) ?. defineProps ?. destructured ?. keys ( ) ) ;
136+ const rest = getScriptSetupRanges ( ) ?. defineProps ?. destructuredRest ;
138137 if ( rest ) {
139138 newNames . add ( rest ) ;
140139 }
141140 return newNames ;
142141 } )
143142 ) ;
144- const templateRefNames = unstable . computedSet (
143+ const getSetupTemplateRefNames = computedSet (
145144 computed ( ( ) => {
146145 const newNames = new Set (
147- scriptSetupRanges . get ( ) ?. useTemplateRef
146+ getScriptSetupRanges ( ) ?. useTemplateRef
148147 . map ( ( { name } ) => name )
149148 . filter ( name => name !== undefined )
150149 ) ;
151150 return newNames ;
152151 } )
153152 ) ;
154- const hasDefineSlots = computed ( ( ) => ! ! scriptSetupRanges . get ( ) ?. defineSlots ) ;
155- const slotsAssignName = computed ( ( ) => scriptSetupRanges . get ( ) ?. defineSlots ?. name ) ;
156- const propsAssignName = computed ( ( ) => scriptSetupRanges . get ( ) ?. defineProps ?. name ) ;
157- const inheritAttrs = computed ( ( ) => {
158- const value = scriptSetupRanges . get ( ) ?. defineOptions ?. inheritAttrs ?? scriptRanges . get ( ) ?. exportDefault ?. inheritAttrsOption ;
153+ const setupHasDefineSlots = computed ( ( ) => ! ! getScriptSetupRanges ( ) ?. defineSlots ) ;
154+ const getSetupSlotsAssignName = computed ( ( ) => getScriptSetupRanges ( ) ?. defineSlots ?. name ) ;
155+ const getSetupPropsAssignName = computed ( ( ) => getScriptSetupRanges ( ) ?. defineProps ?. name ) ;
156+ const getSetupInheritAttrs = computed ( ( ) => {
157+ const value = getScriptSetupRanges ( ) ?. defineOptions ?. inheritAttrs ?? getScriptRanges ( ) ?. exportDefault ?. inheritAttrsOption ;
159158 return value !== 'false' ;
160159 } ) ;
161- const selfComponentName = computed ( ( ) => {
162- const { exportDefault } = scriptRanges . get ( ) ?? { } ;
160+ const getComponentSelfName = computed ( ( ) => {
161+ const { exportDefault } = getScriptRanges ( ) ?? { } ;
163162 if ( _sfc . script && exportDefault ?. nameOption ) {
164163 const { nameOption } = exportDefault ;
165164 return _sfc . script . content . slice ( nameOption . start + 1 , nameOption . end - 1 ) ;
166165 }
167- const { defineOptions } = scriptSetupRanges . get ( ) ?? { } ;
166+ const { defineOptions } = getScriptSetupRanges ( ) ?? { } ;
168167 if ( _sfc . scriptSetup && defineOptions ?. name ) {
169168 return defineOptions . name ;
170169 }
171170 const baseName = path . basename ( fileName ) ;
172171 return capitalize ( camelize ( baseName . slice ( 0 , baseName . lastIndexOf ( '.' ) ) ) ) ;
173172 } ) ;
174- const generatedTemplate = computed ( ( ) => {
173+ const getGeneratedTemplate = computed ( ( ) => {
175174
176- if ( vueCompilerOptions . get ( ) . skipTemplateCodegen || ! _sfc . template ) {
175+ if ( getResolvedOptions ( ) . skipTemplateCodegen || ! _sfc . template ) {
177176 return ;
178177 }
179178
180179 const codes : Code [ ] = [ ] ;
181180 const codegen = generateTemplate ( {
182181 ts,
183182 compilerOptions : ctx . compilerOptions ,
184- vueCompilerOptions : vueCompilerOptions . get ( ) ,
183+ vueCompilerOptions : getResolvedOptions ( ) ,
185184 template : _sfc . template ,
186- edited : vueCompilerOptions . get ( ) . __test || ( fileEditTimes . get ( fileName ) ?? 0 ) >= 2 ,
187- scriptSetupBindingNames : scriptSetupBindingNames . get ( ) ,
188- scriptSetupImportComponentNames : scriptSetupImportComponentNames . get ( ) ,
189- destructuredPropNames : destructuredPropNames . get ( ) ,
190- templateRefNames : templateRefNames . get ( ) ,
191- hasDefineSlots : hasDefineSlots . get ( ) ,
192- slotsAssignName : slotsAssignName . get ( ) ,
193- propsAssignName : propsAssignName . get ( ) ,
194- inheritAttrs : inheritAttrs . get ( ) ,
195- selfComponentName : selfComponentName . get ( ) ,
185+ edited : getResolvedOptions ( ) . __test || ( fileEditTimes . get ( fileName ) ?? 0 ) >= 2 ,
186+ scriptSetupBindingNames : getSetupBindingNames ( ) ,
187+ scriptSetupImportComponentNames : getSetupImportComponentNames ( ) ,
188+ destructuredPropNames : getSetupDestructuredPropNames ( ) ,
189+ templateRefNames : getSetupTemplateRefNames ( ) ,
190+ hasDefineSlots : setupHasDefineSlots ( ) ,
191+ slotsAssignName : getSetupSlotsAssignName ( ) ,
192+ propsAssignName : getSetupPropsAssignName ( ) ,
193+ inheritAttrs : getSetupInheritAttrs ( ) ,
194+ selfComponentName : getComponentSelfName ( ) ,
196195 } ) ;
197196
198197 let current = codegen . next ( ) ;
@@ -208,23 +207,23 @@ function createTsx(
208207 codes,
209208 } ;
210209 } ) ;
211- const generatedScript = computed ( ( ) => {
210+ const getGeneratedScript = computed ( ( ) => {
212211 const codes : Code [ ] = [ ] ;
213212 const linkedCodeMappings : Mapping [ ] = [ ] ;
214213 let generatedLength = 0 ;
215214 const codegen = generateScript ( {
216215 ts,
217216 compilerOptions : ctx . compilerOptions ,
218- vueCompilerOptions : vueCompilerOptions . get ( ) ,
217+ vueCompilerOptions : getResolvedOptions ( ) ,
219218 sfc : _sfc ,
220- edited : vueCompilerOptions . get ( ) . __test || ( fileEditTimes . get ( fileName ) ?? 0 ) >= 2 ,
219+ edited : getResolvedOptions ( ) . __test || ( fileEditTimes . get ( fileName ) ?? 0 ) >= 2 ,
221220 fileName,
222- lang : lang . get ( ) ,
223- scriptRanges : scriptRanges . get ( ) ,
224- scriptSetupRanges : scriptSetupRanges . get ( ) ,
225- templateCodegen : generatedTemplate . get ( ) ,
226- destructuredPropNames : destructuredPropNames . get ( ) ,
227- templateRefNames : templateRefNames . get ( ) ,
221+ lang : getLang ( ) ,
222+ scriptRanges : getScriptRanges ( ) ,
223+ scriptSetupRanges : getScriptSetupRanges ( ) ,
224+ templateCodegen : getGeneratedTemplate ( ) ,
225+ destructuredPropNames : getSetupDestructuredPropNames ( ) ,
226+ templateRefNames : getSetupTemplateRefNames ( ) ,
228227 getGeneratedLength : ( ) => generatedLength ,
229228 linkedCodeMappings,
230229 appendGlobalTypes,
@@ -250,10 +249,10 @@ function createTsx(
250249 } ) ;
251250
252251 return {
253- scriptRanges ,
254- scriptSetupRanges ,
255- lang ,
256- generatedScript ,
257- generatedTemplate ,
252+ getScriptRanges ,
253+ getScriptSetupRanges ,
254+ getLang ,
255+ getGeneratedScript ,
256+ getGeneratedTemplate ,
258257 } ;
259258}
0 commit comments