示例#1
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;
            }
        }
示例#2
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);
            }
        }
示例#3
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;
            }
        }