예제 #1
0
 protected void AddErrorFor(int line, int offset, string message)
 {
     JsLintData err = new JsLintData();
     err.Source = "yui";
     err.Line = Math.Max(0,line);
     err.Character = offset;
     using (StringReader reader = new StringReader(message))
     {
         string text;
         int lineNum = 0;
         while ((text = reader.ReadLine()) != null)
         {
             if (lineNum != 0)
             {
                 text = Environment.NewLine + "    " + text;
             }
             err.Reason += text;
             lineNum++;
         }
     }
     Errors.Add(err);
 }
예제 #2
0
        protected void AddErrorFor(int line, int offset, string message)
        {
            JsLintData err = new JsLintData();

            err.Source    = "yui";
            err.Line      = Math.Max(0, line);
            err.Character = offset;
            using (StringReader reader = new StringReader(message))
            {
                string text;
                int    lineNum = 0;
                while ((text = reader.ReadLine()) != null)
                {
                    if (lineNum != 0)
                    {
                        text = Environment.NewLine + "    " + text;
                    }
                    err.Reason += text;
                    lineNum++;
                }
            }
            Errors.Add(err);
        }
예제 #3
0
 private static int LintDataComparer(JsLintData x, JsLintData y) 
 {
     return x.Line.CompareTo(y.Line);
 }
예제 #4
0
        public void ProcessData(object data)
        {
            Dictionary<string, object> dataDict = data as Dictionary<string, object>;

            if (dataDict != null)
            {

                if (dataDict.ContainsKey("errors"))
                {
                    ProcessListOfObject(dataDict["errors"], (error) =>
                    {
                        JsLintData jsError = new JsLintData();
                        jsError.Source = "lint";
                        if (error.ContainsKey("line"))
                        {
                            jsError.Line = error["line"] != null ? (int)error["line"] : -1;
                        }

                        if (error.ContainsKey("character"))
                        {
                            jsError.Character = error["character"] != null ? (int)error["character"] : -1;
                        }

                        if (error.ContainsKey("reason"))
                        {
                            jsError.Reason = (string)error["reason"];
                        }

                        _errors.Add(jsError);
                    });
                }

                if (_processUnuseds && dataDict.ContainsKey("unused"))
                {
                    int lastLine = -1;
                    JsLintData jsError = null;
                    string unusedList = String.Empty;
                    int unusedCount = 0;
                    ProcessListOfObject(dataDict["unused"], (unused) =>
                    {

                        int line = 0;
                        if (unused.ContainsKey("line"))
                        {
                            line = (int)unused["line"];
                        }
                        if (line != lastLine)
                        {
                            if (jsError != null)
                            {
                                jsError.Reason = "Unused Variable" + (unusedCount > 1 ? "s " : " ") + unusedList;
                                _errors.Add(jsError);

                            }
                            jsError = new JsLintData();
                            jsError.Source = "lint";
                            jsError.Character = -1;
                            jsError.Line = line;
                            unusedCount = 0;
                            unusedList = String.Empty;
                        }

                        if (unused.ContainsKey("name"))
                        {
                            unusedList += (unusedCount == 0 ? String.Empty : ", ") + unused["name"];
                            unusedCount++;
                        }
                        lastLine = line;
                    });
                    jsError.Reason = "Unused Variable" + (unusedCount > 1 ? "s " : " ") + unusedList;
                    _errors.Add(jsError);
                }
            }
        }
예제 #5
0
        public JsLintResult Lint(string javascript)
        {
            StringBuilder finalJs = new StringBuilder();

            Configure();

            lock (_lock)
            {
                bool hasSkips  = false;
                bool hasUnused = false;
                if (Configuration.LinterType == LinterType.JSLint)
                {
                    hasUnused = Configuration.GetOption <bool>("unused");
                }
                else if (Configuration.LinterType == LinterType.JSLint)
                {
                    // we consider the "unused" option to be activated if the config value is either empty
                    // (since the default is "true") or anything other than "false"
                    string unusedConfig = Configuration.GetOption <string>("unused");
                    hasUnused = string.IsNullOrEmpty(unusedConfig) || unusedConfig != "false";
                }
                LintDataCollector dataCollector = new LintDataCollector(hasUnused);

                LineExclusion = new List <bool>();
                // lines are evaluated, but errors are ignored: we want to use this for blocks excluded
                // within a javascript file, because otherwise the parser will freak out if other parts of the
                // code wouldn't validate if that block were missing
                bool ignoreErrors = false;

                // lines are not evaluted by the parser at all - in HTML files we want to pretend non-JS lines
                // are not even there.
                bool ignoreLines = Configuration.InputType == InputType.Html;

                int startSkipLine = 0;

                using (StringReader reader = new StringReader(javascript))
                {
                    string text;
                    int    line = 0;

                    while ((text = reader.ReadLine()) != null)
                    {
                        line++;

                        if (!ignoreLines &&
                            Configuration.InputType == InputType.Html && isEndScript(text))
                        {
                            ignoreLines = true;
                        }

                        if (!ignoreErrors && isIgnoreStart(text))
                        {
                            startSkipLine = line;
                            ignoreErrors  = true;
                            hasSkips      = true;
                        }
                        // always check for end - if they both appear on a line, don't do anything. should
                        // always fall back to continuing to check.
                        if (ignoreErrors && isIgnoreEnd(text))
                        {
                            ignoreErrors = false;
                        }
                        LineExclusion.Add(ignoreErrors);

                        finalJs.AppendLine(ignoreLines ? "" : text);

                        if (ignoreLines &&
                            Configuration.InputType == InputType.Html &&
                            isStartScript(text))
                        {
                            ignoreLines = false;
                        }
                    }
                }
                if (ignoreErrors)
                {
                    // there was no ignore-end found, so cancel the results
                    JsLintData err = new JsLintData();
                    err.Line      = startSkipLine;
                    err.Character = 0;
                    err.Reason    = "An ignore-start marker was found, but there was no ignore-end. Nothing was ignored.";
                    dataCollector.Errors.Add(err);

                    hasSkips = false;
                }


                if (finalJs.Length == 0)
                {
                    JsLintData err = new JsLintData();
                    err.Line      = 0;
                    err.Character = 0;
                    err.Reason    = "The file was empty.";
                    dataCollector.Errors.Add(err);
                }
                else
                {
                    // Setting the externals parameters of the context
                    _context.SetParameter("dataCollector", dataCollector);
                    _context.SetParameter("javascript", finalJs.ToString());
                    _context.SetParameter("options", Configuration.ToJsOptionVar());


                    // Running the script
                    _context.Run("lintRunner(dataCollector, javascript, options);");
                }

                JsLintResult result = new JsLintResult();
                result.Errors = new List <JsLintData>();

                int index = 0;
                while (result.Errors.Count <= Configuration.MaxErrors &&
                       index < dataCollector.Errors.Count)
                {
                    var error = dataCollector.Errors[index++];
                    if (!hasSkips)
                    {
                        result.Errors.Add(error);
                    }
                    else
                    {
                        if (error.Line >= 0 && error.Line < LineExclusion.Count)
                        {
                            if (!LineExclusion[error.Line - 1])
                            {
                                result.Errors.Add(error);
                            }
                        }
                        else
                        {
                            result.Errors.Add(error);
                        }
                    }
                }
                // if we went over, mark that there were more errors and remove last one
                if (result.Errors.Count > Configuration.MaxErrors)
                {
                    result.Errors.RemoveAt(result.Errors.Count - 1);
                    result.Limited = true;
                }

                return(result);
            }
        }
예제 #6
0
        public JsLintResult Lint(string javascript)
        {
            StringBuilder finalJs = new StringBuilder();
            Configure();

            lock (_lock)
            {
                bool hasSkips = false;
                LintDataCollector dataCollector = new LintDataCollector(Configuration.GetOption<bool>("unused"));

                LineExclusion = new List<bool>();
                // lines are evaluated, but errors are ignored: we want to use this for blocks excluded
                // within a javascript file, because otherwise the parser will freak out if other parts of the
                // code wouldn't validate if that block were missing
                bool ignoreErrors=false;

                // lines are not evaluted by the parser at all - in HTML files we want to pretend non-JS lines
                // are not even there.
                bool ignoreLines = Configuration.InputType == InputType.Html;

                int startSkipLine = 0;

                using (StringReader reader = new StringReader(javascript))
                {
                    string text;
                    int line = 0;

                    while ((text = reader.ReadLine()) != null)
                    {
                        line++;

                        if (!ignoreLines
                            && Configuration.InputType == InputType.Html && isEndScript(text))
                        {
                            ignoreLines = true;
                        }

                        if (!ignoreErrors  && isIgnoreStart(text))
                        {
                            startSkipLine = line;
                            ignoreErrors = true;
                            hasSkips = true;
                        }
                        // always check for end - if they both appear on a line, don't do anything. should
                        // always fall back to continuing to check.
                        if (ignoreErrors && isIgnoreEnd(text))
                        {
                            ignoreErrors = false;
                        }
                        LineExclusion.Add(ignoreErrors);

                        finalJs.AppendLine(ignoreLines ? "" : text);

                        if (ignoreLines
                            && Configuration.InputType == InputType.Html
                            && isStartScript(text))
                        {
                            ignoreLines = false;
                        }

                    }
                }
                if (ignoreErrors)
                {
                    // there was no ignore-end found, so cancel the results
                    JsLintData err = new JsLintData();
                    err.Line = startSkipLine;
                    err.Character = 0;
                    err.Reason = "An ignore-start marker was found, but there was no ignore-end. Nothing was ignored.";
                    dataCollector.Errors.Add(err);

                    hasSkips = false;
                }

                if (finalJs.Length == 0)
                {
                    JsLintData err = new JsLintData();
                    err.Line = 0;
                    err.Character = 0;
                    err.Reason = "The file was empty.";
                    dataCollector.Errors.Add(err);
                }
                else
                {
                    // Setting the externals parameters of the context
                    _context.SetParameter("dataCollector", dataCollector);
                    _context.SetParameter("javascript", finalJs.ToString());
                    _context.SetParameter("options", Configuration.ToJsOptionVar());

                    // Running the script
                    _context.Run("lintRunner(dataCollector, javascript, options);");
                }

                JsLintResult result = new JsLintResult();
                result.Errors = new List<JsLintData>();

                int index = 0;
                while (result.Errors.Count <= Configuration.MaxErrors
                    && index<dataCollector.Errors.Count)
                {
                    var error = dataCollector.Errors[index++];
                    if (!hasSkips)
                    {
                        result.Errors.Add(error);
                    }
                    else
                    {
                        if (error.Line >= 0 && error.Line < LineExclusion.Count)
                        {
                            if (!LineExclusion[error.Line - 1])
                            {
                                result.Errors.Add(error);
                            }
                        }
                        else
                        {
                            result.Errors.Add(error);
                        }
                    }
                }
                // if we went over, mark that there were more errors and remove last one
                if (result.Errors.Count > Configuration.MaxErrors)
                {
                    result.Errors.RemoveAt(result.Errors.Count - 1);
                    result.Limited = true;
                }

                return result;
            }
        }
예제 #7
0
        public void ProcessData(object data)
        {
            Dictionary <string, object> dataDict = data as Dictionary <string, object>;

            if (dataDict != null)
            {
                if (dataDict.ContainsKey("errors"))
                {
                    ProcessListOfObject(dataDict["errors"], (error) =>
                    {
                        JsLintData jsError = new JsLintData();
                        jsError.Source     = "lint";
                        if (error.ContainsKey("line"))
                        {
                            jsError.Line = (int)error["line"];
                        }

                        if (error.ContainsKey("character"))
                        {
                            jsError.Character = (int)error["character"];
                        }

                        if (error.ContainsKey("reason"))
                        {
                            jsError.Reason = (string)error["reason"];
                        }

                        _errors.Add(jsError);
                    });
                }

                if (_processUnuseds && dataDict.ContainsKey("unused"))
                {
                    int        lastLine    = -1;
                    JsLintData jsError     = null;
                    string     unusedList  = String.Empty;
                    int        unusedCount = 0;
                    ProcessListOfObject(dataDict["unused"], (unused) =>
                    {
                        int line = 0;
                        if (unused.ContainsKey("line"))
                        {
                            line = (int)unused["line"];
                        }
                        if (line != lastLine)
                        {
                            if (jsError != null)
                            {
                                jsError.Reason = "Unused Variable" + (unusedCount > 1 ? "s " : " ") + unusedList;
                                _errors.Add(jsError);
                            }
                            jsError           = new JsLintData();
                            jsError.Source    = "lint";
                            jsError.Character = -1;
                            jsError.Line      = line;
                            unusedCount       = 0;
                            unusedList        = String.Empty;
                        }

                        if (unused.ContainsKey("name"))
                        {
                            unusedList += (unusedCount == 0 ? String.Empty : ", ") + unused["name"];
                            unusedCount++;
                        }
                        lastLine = line;
                    });
                    jsError.Reason = "Unused Variable" + (unusedCount > 1 ? "s " : " ") + unusedList;
                    _errors.Add(jsError);
                }
            }
        }
예제 #8
0
 private static int LintDataComparer(JsLintData x, JsLintData y)
 {
     return(x.Line.CompareTo(y.Line));
 }
예제 #9
0
        public JsLintResult Lint(string javascript)
        {
            lock (_lock)
            {
                bool hasSkips = false;
                LintDataCollector dataCollector = new LintDataCollector(Configuration.GetOption<bool>("unused"));

                if (!String.IsNullOrEmpty(Configuration.IgnoreStart) && !String.IsNullOrEmpty(Configuration.IgnoreEnd))
                {
                    LineExclusion = new List<bool>();
                    bool skipping=false;
                    int startSkipLine = 0;

                    using (StringReader reader = new StringReader(javascript))
                    {
                        string text;
                        int line = 0;

                        while ((text = reader.ReadLine()) != null)
                        {
                            line++;
                            if (text.IndexOf("/*" + (skipping ? Configuration.IgnoreEnd : Configuration.IgnoreStart) + "*/") >= 0)
                            {
                                if (!skipping)
                                {
                                    startSkipLine = line;
                                    skipping = true;
                                    hasSkips = true;
                                }
                                else
                                {

                                    skipping = false;
                                }
                            }
                            LineExclusion.Add(skipping);
                        }
                    }
                    if (skipping)
                    {
                        // there was no ignore-end found, so cancel the results
                        JsLintData err = new JsLintData();
                        err.Line = startSkipLine;
                        err.Character = 0;
                        err.Reason = "An ignore start marker was found, but there was no ignore-end. Nothing was ignored.";
                        dataCollector.Errors.Add(err);

                        hasSkips = false;
                    }
                }
                if (string.IsNullOrEmpty(javascript))
                {
                    JsLintData err = new JsLintData();
                    err.Line=0;
                    err.Character=0;
                    err.Reason="The file was empty.";
                    dataCollector.Errors.Add(err);
                }
                // Setting the externals parameters of the context
                _context.SetParameter("dataCollector", dataCollector);
                _context.SetParameter("javascript", javascript);
                _context.SetParameter("options", Configuration.ToJsOptionVar());

                // Running the script
                _context.Run("lintRunner(dataCollector, javascript, options);");

                JsLintResult result = new JsLintResult();
                if (!hasSkips) {
                    result.Errors = dataCollector.Errors;
                } else {
                    result.Errors = new List<JsLintData>();
                    foreach (var error in dataCollector.Errors) {
                        if (error.Line >= 0 && error.Line < LineExclusion.Count)
                        {
                            if (!LineExclusion[error.Line - 1])
                            {
                                result.Errors.Add(error);
                            }
                        }
                        else
                        {
                            result.Errors.Add(error);
                        }
                    }
                }

                return result;
            }
        }