1
1
/* eslint-disable @typescript-eslint/no-require-imports */
2
+
3
+ import type { Configuration } from './types'
4
+ import { compact } from '@zardoy/utils'
5
+ import { get } from 'lodash'
6
+
2
7
// will be required from ./node_modules/typescript-essential-plugins/index.js
3
8
const originalPluginFactory = require ( 'typescript-essential-plugins' )
4
9
5
10
const plugin = ( ( context , { typescript : tsModule } = { } ) => {
6
11
if ( ! context ) throw new Error ( 'Not recieve context' )
7
12
const { typescript } = context
8
13
let configurationHost = context . env
9
- if ( context [ 'configurationHost' ] ! ) configurationHost = context [ 'configurationHost' ] !
10
- const patchConfig = config => {
14
+ if ( context [ 'configurationHost' ] ) configurationHost = context [ 'configurationHost' ]
15
+ configurationHost = configurationHost [ 'configurationHost' ] ?? configurationHost
16
+ const mergeAndPatchConfig = ( generalConfig , vueConfig ) => {
17
+ const mergedConfig = {
18
+ ...generalConfig ,
19
+ ...Object . fromEntries (
20
+ Object . entries ( vueConfig ) . map ( ( [ key , value ] ) => {
21
+ const getType = obj => {
22
+ return Array . isArray ( obj ) ? 'array' : typeof obj === 'object' && obj !== null ? 'object' : undefined
23
+ }
24
+ const type = getType ( value )
25
+ if ( ! type || vueConfig [ 'resetSettings' ] ?. includes ( key ) ) return [ key , value ]
26
+ const generalConfigValue = get ( generalConfig , key )
27
+ const generalValueType = getType ( generalConfigValue )
28
+ if ( type !== generalValueType ) return [ key , value ]
29
+ return [ key , generalValueType === 'object' ? { ...generalConfigValue , ...value } : [ ...generalConfigValue , ...value ] ]
30
+ } ) ,
31
+ ) ,
32
+ }
33
+
11
34
return {
12
- ...config ,
35
+ ...mergedConfig ,
13
36
_additionalPluginOptions : {
14
37
pluginSpecificSyntaxServerConfigCheck : false ,
15
38
} ,
16
- enablePlugin : config . enableVueSupport ,
39
+ enablePlugin : generalConfig . enableVueSupport ,
17
40
}
18
41
}
19
42
@@ -22,12 +45,24 @@ const plugin = ((context, { typescript: tsModule } = {}) => {
22
45
const plugin = originalPluginFactory ( {
23
46
typescript : ts ,
24
47
} )
25
- // todo support vue-specific settings
26
48
const originalLsMethods = { ...typescript . languageService }
27
49
28
- void configurationHost . getConfiguration ! < any > ( 'tsEssentialPlugins' ) . then ( _configuration => {
50
+ const getResolvedUserConfig = async ( ) => {
51
+ const regularConfig = await configurationHost . getConfiguration ! < any > ( 'tsEssentialPlugins' )
52
+ const _vueSpecificConfig = await configurationHost . getConfiguration ! < any > ( '[vue]' )
53
+ const vueSpecificConfig = Object . fromEntries (
54
+ compact (
55
+ Object . entries ( _vueSpecificConfig ) . map ( ( [ key , value ] ) =>
56
+ key . startsWith ( 'tsEssentialPlugins' ) ? [ key . slice ( 'tsEssentialPlugins.' . length ) , value ] : undefined ,
57
+ ) ,
58
+ ) ,
59
+ )
60
+ const config : Configuration = mergeAndPatchConfig ( regularConfig , vueSpecificConfig )
61
+ return config
62
+ }
63
+
64
+ void getResolvedUserConfig ( ) . then ( async config => {
29
65
// if (typescript.languageService[thisPluginMarker]) return
30
- const config = patchConfig ( _configuration )
31
66
if ( ! config . enablePlugin ) return
32
67
const proxy = plugin . create ( {
33
68
...typescript ,
@@ -42,8 +77,7 @@ const plugin = ((context, { typescript: tsModule } = {}) => {
42
77
} )
43
78
44
79
configurationHost . onDidChangeConfiguration ! ( ( ) => {
45
- void configurationHost . getConfiguration ! < any > ( 'tsEssentialPlugins' ) . then ( config => {
46
- config = patchConfig ( config )
80
+ void getResolvedUserConfig ( ) . then ( config => {
47
81
plugin . onConfigurationChanged ?.( config )
48
82
// temporary workaround
49
83
if ( ! config . enablePlugin ) {
0 commit comments