Ejemplo n.º 1
0
        private static ModuleTree ResolveModule(ModuleTree parentTree, string relativeName, AnalysisUnit unit)
        {
            ModuleTree curTree    = parentTree;
            var        components = ModuleTable.GetPathComponents(relativeName);

            for (int i = 0; i < components.Length; i++)
            {
                var comp = components[i];

                if (curTree == null)
                {
                    return(null);
                }

                if (comp == "." || comp == "")
                {
                    continue;
                }
                else if (comp == "..")
                {
                    curTree = curTree.Parent;
                    continue;
                }

                ModuleTree nextTree;
                if (i == components.Length - 1)
                {
                    nextTree = curTree.GetChild(comp + ".js", unit);

                    if (nextTree.ProjectEntry != null)
                    {
                        return(nextTree);
                    }
                }

                nextTree = curTree.GetChild(comp, unit);

                if (nextTree.Children.Count > 0 || nextTree.ProjectEntry != null)
                {
                    curTree = nextTree;
                    continue;
                }

                return(null);
            }

            return(curTree);
        }
Ejemplo n.º 2
0
        private IAnalysisSet RequireModule(Node node, AnalysisUnit unit, string moduleName, ModuleTree relativeTo)
        {
            if (moduleName.StartsWith("./") || moduleName.StartsWith("../"))
            {
                // search relative to our declaring module.
                return(GetExports(
                           node,
                           unit,
                           ResolveModule(relativeTo, moduleName, unit)
                           ));
            }
            else
            {
                // must be in node_modules, search in the current directory
                // and up through our parents
                do
                {
                    var nodeModules = relativeTo.GetChild(AnalysisConstants.NodeModulesFolder, unit);
                    var curTree     = ResolveModule(nodeModules, moduleName, unit);

                    if (curTree != null)
                    {
                        return(GetExports(node, unit, curTree));
                    }

                    relativeTo = relativeTo.Parent;
                } while (relativeTo != null);
            }
            return(AnalysisSet.Empty);
        }
Ejemplo n.º 3
0
        internal ModuleTree RequireModule(AnalysisUnit unit, string moduleName, ModuleTree relativeTo)
        {
            ModuleTree curTree = null;

            if (moduleName.StartsWith("./") || moduleName.StartsWith("../"))
            {
                // search relative to our declaring module.
                curTree = ResolveModule(relativeTo, moduleName, unit);
            }
            else
            {
                // must be in node_modules, search in the current directory
                // and up through our parents
                do
                {
                    var nodeModules = relativeTo.GetChild(AnalysisConstants.NodeModulesFolder, unit);
                    curTree = ResolveModule(nodeModules, moduleName, unit);

                    relativeTo = relativeTo.Parent;
                } while (relativeTo != null && curTree == null);
            }
            return(curTree);
        }