MCPcopy Index your code
hub / github.com/microsoft/typescript-go / GetSourceFileFromReference

Method GetSourceFileFromReference

internal/compiler/program.go:231–272  ·  view source on GitHub ↗

** This should have similar behavior to 'processSourceFile' without diagnostics or mutation. */

(origin *ast.SourceFile, ref *ast.FileReference)

Source from the content-addressed store, hash-verified

229
230/** This should have similar behavior to 'processSourceFile' without diagnostics or mutation. */
231func (p *Program) GetSourceFileFromReference(origin *ast.SourceFile, ref *ast.FileReference) *ast.SourceFile {
232 // TODO: The module loader in corsa is fairly different than strada, it should probably be able to expose this functionality at some point,
233 // rather than redoing the logic approximately here, since most of the related logic now lives in module.Resolver
234 // Still, without the failed lookup reporting that only the loader does, this isn't terribly complicated
235
236 fileName := tspath.ResolvePath(tspath.GetDirectoryPath(origin.FileName()), ref.FileName)
237 supportedExtensionsBase := tsoptions.GetSupportedExtensions(p.Options(), nil /*extraFileExtensions*/)
238 supportedExtensions := tsoptions.GetSupportedExtensionsWithJsonIfResolveJsonModule(p.Options(), supportedExtensionsBase)
239 allowNonTsExtensions := p.Options().AllowNonTsExtensions.IsTrue()
240 if tspath.HasExtension(fileName) {
241 if !allowNonTsExtensions {
242 canonicalFileName := tspath.GetCanonicalFileName(fileName, p.UseCaseSensitiveFileNames())
243 supported := false
244 for _, group := range supportedExtensions {
245 if tspath.FileExtensionIsOneOf(canonicalFileName, group) {
246 supported = true
247 break
248 }
249 }
250 if !supported {
251 return nil // unsupported extensions are forced to fail
252 }
253 }
254
255 return p.GetSourceFileForResolvedModule(fileName)
256 }
257 if allowNonTsExtensions {
258 extensionless := p.GetSourceFileForResolvedModule(fileName)
259 if extensionless != nil {
260 return extensionless
261 }
262 }
263
264 // Only try adding extensions from the first supported group (which should be .ts/.tsx/.d.ts)
265 for _, ext := range supportedExtensions[0] {
266 result := p.GetSourceFileForResolvedModule(fileName + ext)
267 if result != nil {
268 return result
269 }
270 }
271 return nil
272}
273
274func NewProgram(opts ProgramOptions) *Program {
275 p := &Program{opts: opts}

Callers

nothing calls this directly

Calls 12

OptionsMethod · 0.95
ResolvePathFunction · 0.92
GetDirectoryPathFunction · 0.92
GetSupportedExtensionsFunction · 0.92
HasExtensionFunction · 0.92
GetCanonicalFileNameFunction · 0.92
FileExtensionIsOneOfFunction · 0.92
IsTrueMethod · 0.80
FileNameMethod · 0.65

Tested by

no test coverage detected