Пример #1
0
        public string resolveModuleMain(string name, TSFileAdditionalInfo parentInfo)
        {
            var moduleInfo = TSProject.FindInfoForModule(_owner.Owner, _owner.DiskCache, name, out var diskName);

            if (moduleInfo == null)
            {
                return(null);
            }
            if (name != diskName)
            {
                parentInfo.ReportDiag(false, 2, "Module import has wrong casing '" + name + "' on disk '" + diskName + "'", 0, 0, 0, 0);
            }
            moduleInfo.LoadProjectJson(true);
            parentInfo.ImportingModule(moduleInfo);
            var mainFile = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.MainFile);
            var item     = _owner.DiskCache.TryGetItem(mainFile) as IFileCache;

            if (item == null || item.IsInvalid)
            {
                return(null);
            }
            var itemInfo = TSFileAdditionalInfo.Get(item, _owner.DiskCache);

            itemInfo.ImportedAsModule = name;
            AddSource(itemInfo);
            if (!IsTsOrTsx(mainFile))
            {
                itemInfo.Type = FileCompilationType.JavaScript;
                CheckAdd(mainFile);
                if (moduleInfo.TypesMainFile != null)
                {
                    var dtsPath = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.TypesMainFile);
                    item             = _owner.DiskCache.TryGetItem(dtsPath) as IFileCache;
                    itemInfo.DtsLink = TSFileAdditionalInfo.Get(item, _owner.DiskCache);
                    if (item != null && !item.IsInvalid)
                    {
                        return(dtsPath);
                    }
                }
                return(null);
            }
            itemInfo.Type = FileCompilationType.TypeScript;
            CheckAdd(item.FullPath);
            TryToResolveFromBuildCache(itemInfo);
            Crawl();
            if (itemInfo.DtsLink != null && !ToCompile.Contains(item.FullPath))
            {
                return(itemInfo.DtsLink.Owner.FullPath);
            }
            return(item.FullPath);
        }
Пример #2
0
        public string resolveModuleMain(string name, TSFileAdditionalInfo parentInfo)
        {
            var moduleInfo =
                TSProject.FindInfoForModule(_owner.Owner, _owner.DiskCache, _owner.Logger, name, out var diskName);

            if (moduleInfo == null)
            {
                return(null);
            }
            if (name != diskName)
            {
                parentInfo.ReportDiag(false, -2,
                                      "Module import has wrong casing '" + name + "' on disk '" + diskName + "'", 0, 0, 0, 0);
            }

            moduleInfo.LoadProjectJson(true);
            parentInfo.ImportingModule(moduleInfo);
            var mainFile = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.MainFile);
            var item     = _owner.DiskCache.TryGetItem(mainFile) as IFileCache;

            if (item == null || item.IsInvalid)
            {
                return(null);
            }

            var itemInfo = TSFileAdditionalInfo.Get(item, _owner.DiskCache);

            moduleInfo.MainFileInfo   = itemInfo;
            itemInfo.ImportedAsModule = name;
            itemInfo.MyProject        = moduleInfo;
            var parentProject = parentInfo.MyProject;

            if (parentProject.IsRootProject &&
                ((parentProject.Dependencies == null || !parentProject.Dependencies.Contains(name)) &&
                 (parentProject.DevDependencies == null || !parentProject.DevDependencies.Contains(name))))
            {
                parentInfo.ReportDiag(false, -12,
                                      "Importing module " + name + " without being in package.json as dependency", 0, 0, 0, 0);
            }

            if (moduleInfo.ProjectOptions?.ObsoleteMessage != null)
            {
                if (!PragmaParser.ParseIgnoreImportingObsolete(parentInfo.Owner.Utf8Content).Contains(name))
                {
                    parentInfo.ReportDiag(false, -14,
                                          "Importing obsolete module: " + moduleInfo.ProjectOptions?.ObsoleteMessage, 0, 0, 0, 0);
                }
            }

            AddSource(itemInfo);
            if (!IsTsOrTsx(mainFile))
            {
                itemInfo.Type = FileCompilationType.JavaScript;
                CheckAdd(mainFile);
                if (moduleInfo.TypesMainFile != null)
                {
                    var dtsPath = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.TypesMainFile);
                    item             = _owner.DiskCache.TryGetItem(dtsPath) as IFileCache;
                    itemInfo.DtsLink = TSFileAdditionalInfo.Get(item, _owner.DiskCache);
                    if (item != null && !item.IsInvalid)
                    {
                        return(dtsPath);
                    }
                }

                return(null);
            }

            itemInfo.Type = FileCompilationType.TypeScript;
            CheckAdd(item.FullPath);
            TryToResolveFromBuildCache(itemInfo);
            if (itemInfo.DtsLink != null && !ToCompile.Contains(item.FullPath) && !itemInfo.NeedsCompilation())
            {
                return(itemInfo.DtsLink.Owner.FullPath);
            }

            return(item.FullPath);
        }
Пример #3
0
        public string resolveModuleMain(string name, TSFileAdditionalInfo parentInfo)
        {
            if (!_owner.ProjectOptions.AllowModuleDeepImport)
            {
                if (!parentInfo.Owner.Name.EndsWith(".d.ts") && (name.Contains('/') || name.Contains('\\')))
                {
                    parentInfo.ReportDiag(true, -10, "Absolute import '" + name + "' must be just simple module name",
                                          0, 0, 0, 0);
                    return(null);
                }
            }

            var mname      = PathUtils.EnumParts(name).First().name;
            var moduleInfo =
                TSProject.FindInfoForModule(_owner.Owner, parentInfo.Owner.Parent, _owner.DiskCache, _owner.Logger,
                                            mname, out var diskName);

            if (moduleInfo == null)
            {
                return(null);
            }
            if (mname != diskName)
            {
                parentInfo.ReportDiag(false, -2,
                                      "Module import has wrong casing '" + mname + "' on disk '" + diskName + "'", 0, 0, 0, 0);
            }

            moduleInfo.LoadProjectJson(true);
            if (mname.Length != name.Length)
            {
                return(ResolveLocalImport(PathUtils.Join(moduleInfo.Owner.FullPath, name.Substring(mname.Length + 1)),
                                          parentInfo, moduleInfo, name));
            }

            parentInfo.ImportingModule(moduleInfo);
            var mainFile = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.MainFile);
            var item     = _owner.DiskCache.TryGetItemPreferReal(mainFile) as IFileCache;

            if (item == null || item.IsInvalid)
            {
                return(null);
            }

            var itemInfo = TSFileAdditionalInfo.Get(item, _owner.DiskCache);

            moduleInfo.MainFileInfo   = itemInfo;
            itemInfo.ImportedAsModule = name;
            itemInfo.MyProject        = moduleInfo;
            var parentProject = parentInfo.MyProject;

            if (parentProject != null && parentProject.IsRootProject && !parentInfo.Owner.FullPath.Contains("/node_modules/") &&
                ((parentProject.Dependencies == null || !parentProject.Dependencies.Contains(name)) &&
                 (parentProject.DevDependencies == null || !parentProject.DevDependencies.Contains(name))))
            {
                parentInfo.ReportDiag(false, -12,
                                      "Importing module " + name + " without being in package.json as dependency", 0, 0, 0, 0);
            }

            if (moduleInfo.ProjectOptions?.ObsoleteMessage != null)
            {
                if (!PragmaParser.ParseIgnoreImportingObsolete(parentInfo.Owner.Utf8Content).Contains(name))
                {
                    parentInfo.ReportDiag(false, -14,
                                          "Importing obsolete module: " + moduleInfo.ProjectOptions?.ObsoleteMessage, 0, 0, 0, 0);
                }
            }

            AddSource(itemInfo);
            if (!IsTsOrTsx(mainFile))
            {
                if (moduleInfo.MainFileNeedsToBeCompiled)
                {
                    itemInfo.Type = FileCompilationType.EsmJavaScript;
                    CheckAdd(mainFile);
                    TryToResolveFromBuildCache(itemInfo);
                }
                else
                {
                    itemInfo.Type = FileCompilationType.JavaScript;
                    CheckAdd(mainFile);
                }

                if (moduleInfo.TypesMainFile != null)
                {
                    var dtsPath = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.TypesMainFile);
                    item             = _owner.DiskCache.TryGetItem(dtsPath) as IFileCache;
                    itemInfo.DtsLink = TSFileAdditionalInfo.Get(item, _owner.DiskCache);
                    if (item != null && !item.IsInvalid)
                    {
                        return(dtsPath);
                    }
                }

                return(null);
            }

            itemInfo.Type = FileCompilationType.TypeScript;
            CheckAdd(item.FullPath);
            TryToResolveFromBuildCache(itemInfo);
            if (itemInfo.DtsLink != null && !ToCompile.Contains(item.FullPath) && !itemInfo.NeedsCompilation())
            {
                var fp = itemInfo.DtsLink.Owner.FullPath;
                _readFileMap[fp] = itemInfo.DtsLink.Owner;
                return(fp);
            }

            return(item.FullPath);
        }