Ejemplo n.º 1
0
        public void Config()
        {
            JsLintConfiguration config = new JsLintConfiguration();

            Assert.AreEqual("maxerr: 50", config.OptionsToString(), "Default has no options set.");
            Assert.AreEqual(String.Empty, config.GlobalsToString(), "Default has no globals set.");
            Assert.AreEqual(50, config.MaxErrors, "Maxerrors=99999");
            Assert.AreEqual(true, config.ErrorOnUnused, "Unused default");
            Assert.AreEqual(config.MaxErrors, config.GetOption <int>("maxerr"), config.MaxErrors, "Maxerrors = options(maxerr)");

            config.SetOption("unused");
            config.SetOption("evil");
            config.SetOption("maxerr", 50);
            config.SetOption("immed");

            Assert.AreEqual("maxerr: 50, unused: true, evil: true, immed: true", config.OptionsToString(), "Basic option setting worked.");

            JsLintConfiguration config2 = new JsLintConfiguration();

            config2.SetOption("eqeqeq", true);
            config2.SetOption("maxerr", 25);
            config2.SetOption("immed", false);

            config.MergeOptions(config2);

            Assert.AreEqual("maxerr: 25, unused: true, evil: true, immed: false, eqeqeq: true", config.OptionsToString(), "Basic option setting worked.");
        }
Ejemplo n.º 2
0
		public void Config()
		{

            JsLintConfiguration config = new JsLintConfiguration();

            Assert.AreEqual("maxerr: 50", config.OptionsToString(), "Default has no options set.");
            Assert.AreEqual(String.Empty, config.GlobalsToString(), "Default has no globals set.");
            Assert.AreEqual(50, config.MaxErrors, "Maxerrors=99999");
            Assert.AreEqual(true, config.ErrorOnUnused, "Unused default");
            Assert.AreEqual(config.MaxErrors,config.GetOption<int>("maxerr") , config.MaxErrors, "Maxerrors = options(maxerr)");
            
            config.SetOption("unused");
            config.SetOption("evil");
            config.SetOption("maxerr", 50);
            config.SetOption("immed");

            Assert.AreEqual("maxerr: 50, unused: true, evil: true, immed: true", config.OptionsToString(), "Basic option setting worked.");

            JsLintConfiguration config2 = new JsLintConfiguration();
            config2.SetOption("eqeqeq", true);
            config2.SetOption("maxerr", 25);
            config2.SetOption("immed", false);

            config.MergeOptions(config2);

            Assert.AreEqual("maxerr: 25, unused: true, evil: true, immed: false, eqeqeq: true", config.OptionsToString(), "Basic option setting worked.");

        }
Ejemplo n.º 3
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);
            }
        }