@@ -49,6 +49,23 @@ const domJsxTypes =
4949 ' interface IntrinsicElements { [elemName: string]: Record<string, unknown> }\n' +
5050 '}\n'
5151
52+ const styleImportTypes =
53+ "declare module '*.css' {\n" +
54+ ' const cssText: string\n' +
55+ ' export default cssText\n' +
56+ '}\n' +
57+ "declare module '*.less' {\n" +
58+ ' const lessText: string\n' +
59+ ' export default lessText\n' +
60+ '}\n' +
61+ "declare module '*.sass' {\n" +
62+ ' const sassText: string\n' +
63+ ' export default sassText\n' +
64+ '}\n' +
65+ "declare module '*.scss' {\n" +
66+ ' const scssText: string\n' +
67+ ' export default scssText\n' +
68+ '}\n'
5269const normalizeVirtualFileName = fileName =>
5370 typeof fileName === 'string' && fileName . startsWith ( '/' ) ? fileName . slice ( 1 ) : fileName
5471
@@ -234,6 +251,7 @@ export const createTypeDiagnosticsController = ({
234251 getTypeScriptLibUrls,
235252 getTypePackageFileUrls,
236253 getJsxSource,
254+ getTypecheckSourcePath = ( ) => '' ,
237255 getWorkspaceTabs = ( ) => [ ] ,
238256 getRenderMode = ( ) => 'dom' ,
239257 defaultTypeScriptLibFileName = 'lib.esnext.full.d.ts' ,
@@ -800,12 +818,21 @@ export const createTypeDiagnosticsController = ({
800818 )
801819 }
802820
803- const collectTypeDiagnostics = async ( compiler , sourceText ) => {
821+ const collectTypeDiagnostics = async (
822+ compiler ,
823+ { sourceText, sourcePathOverride = '' } ,
824+ ) => {
804825 const workspaceComponentTabs = toWorkspaceComponentTabs ( )
805826 const resolvedEntryTab = resolveWorkspaceEntryForTypecheck ( workspaceComponentTabs )
806- const sourceFileName = resolvedEntryTab ?. path || 'component.tsx'
827+ const normalizedSourcePathOverride =
828+ typeof sourcePathOverride === 'string'
829+ ? normalizeRelativePath ( sourcePathOverride )
830+ : ''
831+ const sourceFileName =
832+ normalizedSourcePathOverride || resolvedEntryTab ?. path || 'component.tsx'
807833 const typecheckSourceFileName = sourceFileName . replace ( / \. ( j s x ? | m j s | c j s ) $ / i, '.tsx' )
808834 const jsxTypesFileName = 'knighted-jsx-runtime.d.ts'
835+ const styleImportTypesFileName = 'knighted-style-imports.d.ts'
809836 const renderMode = getRenderMode ( )
810837 const isReactMode = renderMode === 'react'
811838 const libFiles = await ensureTypeScriptLibFiles ( )
@@ -824,6 +851,7 @@ export const createTypeDiagnosticsController = ({
824851 }
825852
826853 files . set ( typecheckSourceFileName , sourceText )
854+ files . set ( styleImportTypesFileName , styleImportTypes )
827855
828856 if ( ! isReactMode ) {
829857 files . set ( jsxTypesFileName , domJsxTypes )
@@ -978,7 +1006,7 @@ export const createTypeDiagnosticsController = ({
9781006 resolveModuleNames,
9791007 }
9801008
981- const rootNames = [ typecheckSourceFileName ]
1009+ const rootNames = [ typecheckSourceFileName , styleImportTypesFileName ]
9821010 if ( ! isReactMode ) {
9831011 rootNames . push ( jsxTypesFileName )
9841012 }
@@ -1005,7 +1033,11 @@ export const createTypeDiagnosticsController = ({
10051033
10061034 const runTypeDiagnostics = async (
10071035 runId ,
1008- { userInitiated = false , sourceOverride = undefined } = { } ,
1036+ {
1037+ userInitiated = false ,
1038+ sourceOverride = undefined ,
1039+ sourcePathOverride = undefined ,
1040+ } = { } ,
10091041 ) => {
10101042 incrementTypeDiagnosticsRuns ( )
10111043 setTypeDiagnosticsPending ( false )
@@ -1025,7 +1057,14 @@ export const createTypeDiagnosticsController = ({
10251057
10261058 const sourceForRun =
10271059 typeof sourceOverride === 'string' ? sourceOverride : getJsxSource ( )
1028- const diagnostics = await collectTypeDiagnostics ( compiler , sourceForRun )
1060+ const sourcePathForRun =
1061+ typeof sourcePathOverride === 'string' && sourcePathOverride . length > 0
1062+ ? sourcePathOverride
1063+ : getTypecheckSourcePath ( )
1064+ const diagnostics = await collectTypeDiagnostics ( compiler , {
1065+ sourceText : sourceForRun ,
1066+ sourcePathOverride : sourcePathForRun ,
1067+ } )
10291068 const errorCategory = compiler . DiagnosticCategory ?. Error
10301069 const errors = diagnostics . filter (
10311070 diagnostic => diagnostic . category === errorCategory ,
@@ -1087,11 +1126,16 @@ export const createTypeDiagnosticsController = ({
10871126 }
10881127 }
10891128
1090- const triggerTypeDiagnostics = ( { userInitiated = false , source = undefined } = { } ) => {
1129+ const triggerTypeDiagnostics = ( {
1130+ userInitiated = false ,
1131+ source = undefined ,
1132+ sourcePath = undefined ,
1133+ } = { } ) => {
10911134 typeCheckRunId += 1
10921135 void runTypeDiagnostics ( typeCheckRunId , {
10931136 userInitiated,
10941137 sourceOverride : source ,
1138+ sourcePathOverride : sourcePath ,
10951139 } )
10961140 }
10971141
0 commit comments