Example #1
0
		public void TestMultipleDifferentOptions()
		{
            JsLintConfiguration config = new JsLintConfiguration();
			SharpLinter lint = new SharpLinter(config);
            
            config.SetOption("eqeqeq",true);
            config.SetOption("plusplus",true);
            
            JsLintResult result = lint.Lint(
							@"function annon() { var i, number, x; for (i = 0; i == 5; i++) { number += ++i; } }"							
                            );

			Assert.AreEqual(3, result.Errors.Count);

            config = new JsLintConfiguration();
            config.SetOption("unused", false);

            // should fail on ++ since "plusplus=true" -- note that we are testing with JSHINT so the
            // behavior is opposite of JSLINT for this options

			JsLintResult result2 = lint.Lint(
							@"function annon() { var i, number; for (i = 0; i === 5; i++) { number += i; } }");

			Assert.AreEqual(1, result2.Errors.Count);
		}
Example #2
0
    public override PolicyFailure[] Evaluate()
    {
        JsLintConfiguration finalConfig = new JsLintConfiguration();
        SharpLinter lint = new SharpLinter(finalConfig);
        List<JsLintData> allErrors = new List<JsLintData>();

        StringCollection policyFailureMessages = new StringCollection();

        PendingChange[] changes = PendingCheckin.PendingChanges.CheckedPendingChanges;
        foreach (PendingChange pendingChange in changes)
        {
            if ((pendingChange.ChangeType.HasFlag(ChangeType.Add) ||
                  pendingChange.ChangeType.HasFlag(ChangeType.Edit)) &&
                  pendingChange.FileName.EndsWith(".js") && !ShouldIgnore(pendingChange.LocalItem))
            {
                allErrors.AddRange(LintFile(pendingChange.LocalItem, lint));
            }
        }

        if (allErrors.Count > 0)
        {
            List<PolicyFailure> policyFailures = new List<PolicyFailure>();

            foreach (JsLintData error in allErrors)
            {
                string character = error.Character.ToString();
                character = error.Character >= 0 ? "at character " + error.Character : String.Empty;
                policyFailures.Add(new PolicyFailure(string.Format(OutputFormat, error.FilePath, error.Line, error.Source, error.Reason, character)));
                //Console.WriteLine(string.Format(OutputFormat, error.FilePath, error.Line, error.Source, error.Reason, character));
            }

            //List<PolicyFailure> policyFailures = new List<PolicyFailure>();

            //foreach (string message in policyFailureMessages)
            //{
            //    policyFailures.Add(new PolicyFailure(message, this));
            //}

            return policyFailures.ToArray();
        }
        else
        {
            return new PolicyFailure[0];
        }
    }
Example #3
0
        public void ErrorLimit()
        {
            var config = TestResources.DefaultConfig;
            config.SetOption("maxerr", 10);

            SharpLinter lint = new SharpLinter(config);

            string file = TestResources.LoadAppRootedFile("resources\\errors.js") ;
            var result = lint.Lint(file);

            Assert.AreEqual(10, result.Errors.Count);
            Assert.AreEqual(true, result.Limited);

            config.SetOption("maxerr", 10000);
            result = lint.Lint(file);

            Assert.IsTrue(result.Errors.Count>100);
            Assert.AreEqual(false, result.Limited);
        }
        public void TestMultipleCalls()
        {
            var config = DefaultConfig;

            SharpLinter lint = new SharpLinter(config);

            JsLintResult result = lint.Lint(
                            @"var i, y; for (i = 0; i < 5; i++) console.Print(message + ' (' + i + ')'); number += i;");

            // original test was 4 errors - jshint defaults?
            Assert.AreEqual(4, result.Errors.Count);

            JsLintResult result2 = lint.Lint(
                            @"function annon() { var i, number; for (i = 0; i === 5; i++) { number += i; } }");

            Assert.AreEqual(1, result2.Errors.Count);

            JsLintResult result3 = lint.Lint(
                            @"function annon() { var i, number, x; for (i = 0; i == 5; i++) { number += i; } }");

            Assert.AreEqual(2, result3.Errors.Count);
        }
Example #5
0
    public List<JsLintData> LintFile(string file, SharpLinter lint)
    {
        List<JsLintData> fileErrors = new List<JsLintData>();
        string javascript = File.ReadAllText(file);
        JsLintResult result = lint.Lint(javascript);
        bool hasErrors = result.Errors.Count > 0;

        if (hasErrors)
        {
            foreach (JsLintData error in result.Errors)
            {
                error.FilePath = file;
                fileErrors.Add(error);
            }
        }

        SharpCompressor compressor = new SharpCompressor();

        // We always check for YUI errors when there were no lint errors and
        // Otherwise it might not compress.
        if (!hasErrors)
        {
            compressor.Clear();
            compressor.AllowEval = true;
            //compressor.KeepHeader = Configuration.MinimizeKeepHeader;
            //compressor.CompressorType = Configuration.CompressorType;

            hasErrors = !compressor.YUITest(javascript);

            if (hasErrors)
            {
                foreach (var error in compressor.Errors)
                {
                    fileErrors.Add(error);
                }
            }
        }

        fileErrors.Sort(LintDataComparer);
        return fileErrors;
    }