** This should have similar behavior to 'processSourceFile' without diagnostics or mutation. */
(origin *ast.SourceFile, ref *ast.FileReference)
| 229 | |
| 230 | /** This should have similar behavior to 'processSourceFile' without diagnostics or mutation. */ |
| 231 | func (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 | |
| 274 | func NewProgram(opts ProgramOptions) *Program { |
| 275 | p := &Program{opts: opts} |
nothing calls this directly
no test coverage detected