Esempio n. 1
0
        public void MultipleInput()
        {
            var result = LinterFactory.Lint(Settings.Instance, "../../artifacts/eslint/a.js", "../../artifacts/eslint/b.js");

            Assert.IsTrue(result.First().HasErrors);
            Assert.AreEqual(6, result.First().Errors.Count);
        }
Esempio n. 2
0
        public static async Task Lint(bool showErrorList, params string[] fileNames)
        {
            try
            {
                WebLinterPackage.Dte.StatusBar.Text = "Analyzing...";
                WebLinterPackage.Dte.StatusBar.Animate(true, vsStatusAnimation.vsStatusAnimationGeneral);

                EnsureDefaults();

                var result = await LinterFactory.Lint(WebLinterPackage.Settings, fileNames);

                if (result != null)
                {
                    ErrorListService.ProcessLintingResults(result, showErrorList);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
            finally
            {
                WebLinterPackage.Dte.StatusBar.Clear();
                WebLinterPackage.Dte.StatusBar.Animate(false, vsStatusAnimation.vsStatusAnimationGeneral);
            }
        }
Esempio n. 3
0
        public void Multiple()
        {
            var result = LinterFactory.Lint(Settings.Instance, "../../artifacts/coffeelint/a.coffee", "../../artifacts/coffeelint/b.coffee");

            Assert.IsTrue(result.First().HasErrors);
            Assert.AreEqual(3, result.First().Errors.Count);
        }
        public async Task StandardTsFixErrors()
        {
            try
            {
                File.Copy("../../artifacts/tslint/a.ts", "../../artifacts/tslint/aTest.ts", true);
                var result = await LinterFactory.Lint(MockSettings.Instance, true, false, "../../artifacts/tslint/aTest.ts");

                // Now, bizarrely, we have to fix twice to fix var -> const with the recommended TSLint rules
                // See TSLint issues #2835, #2843, #2625
                result = await LinterFactory.Lint(MockSettings.Instance, true, false, "../../artifacts/tslint/aTest.ts");

                Assert.IsTrue(result.First().HasErrors);
                Assert.IsFalse(string.IsNullOrEmpty(result.First().Errors.First().FileName), "File name is empty");
                // 2017-10-30: tslint 5.8.0 curly has a fixer #3262, reduces 4 -> 2 below
                Assert.AreEqual(2, result.First().Errors.Count);
                Assert.AreEqual("Calls to 'console.log' are not allowed.", result.First().Errors.First().Message);
                string actual   = File.ReadAllText("../../artifacts/tslint/aTest.ts");
                string expected = File.ReadAllText("../../artifacts/tslint/aFixed.ts");
                Assert.AreEqual(expected, actual);
            }
            finally
            {
                File.Delete("../../artifacts/tslint/aTest.ts");
            }
        }
        public static async Task <bool> Lint(bool showErrorList, bool fixErrors, bool callSync,
                                             string[] fileNames, string[] filterFileNames = null)
        {
            bool hasVSErrors = false;

            try
            {
                WebLinterPackage.Dte.StatusBar.Text = "Analyzing...";
                WebLinterPackage.Dte.StatusBar.Animate(true, vsStatusAnimation.vsStatusAnimationGeneral);

                await CopyResourceFilesToUserProfile(false, callSync);

                LintingResult[] result = await LinterFactory.Lint(WebLinterPackage.Settings, fixErrors, callSync,
                                                                  Logger.LogAndWarn, fileNames);

                if (result != null)
                {
                    ErrorListService.ProcessLintingResults(result, fileNames, filterFileNames, showErrorList);
                    hasVSErrors = result.Any(r => r.HasVsErrors);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
            finally
            {
                WebLinterPackage.Dte.StatusBar.Clear();
                WebLinterPackage.Dte.StatusBar.Animate(false, vsStatusAnimation.vsStatusAnimationGeneral);
            }
            return(hasVSErrors);
        }
Esempio n. 6
0
        public async Task MultipleInput()
        {
            var result = await LinterFactory.LintAsync(Settings.Instance, "../../artifacts/eslint/a.js", "../../artifacts/eslint/b.js");

            Assert.IsTrue(result.First().HasErrors);
            Assert.AreEqual(8, result.First().Errors.Count);
        }
Esempio n. 7
0
        public async Task LintWithDuplicateErrors()
        {
            // Arrange
            string topTsconfig     = Path.GetFullPath(@"../../artifacts/tsconfig/multiple/tsconfig.json");
            string folderbTsconfig = Path.GetFullPath(@"../../artifacts/tsconfig/multiple/b/tsconfig.json");

            string[] fileNames = new string[] { topTsconfig, folderbTsconfig };

            // Act
            var result = await LinterFactory.Lint(new MockSettings()
            {
                UseTsConfig = true
            }, false, false, fileNames);

            // Assert
            // file2 is in both tsconfigs.  It has 4 errors.  With the old code we got 8 in the Error List, and here.
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Length);
            Assert.AreEqual(33, result[0].Errors.Count);

            IList <LintingError> file2Errors = GetErrorsForFile("file2.ts", result[0].Errors);

            Assert.IsTrue(file2Errors.Count == 4);
            LintingError completedDocsError = file2Errors.First(le => le.ErrorCode == "completed-docs");

            Assert.IsNotNull(completedDocsError);
            Assert.AreEqual(2, completedDocsError.LineNumber);
            Assert.AreEqual(0, completedDocsError.ColumnNumber);
        }
Esempio n. 8
0
        public async Task Multiple()
        {
            var result = await LinterFactory.LintAsync(Settings.Instance, "../../artifacts/csslint/a.css", "../../artifacts/csslint/b.css");

            Assert.IsTrue(result.First().HasErrors);
            Assert.IsFalse(string.IsNullOrEmpty(result.First().Errors.First().FileName));
            Assert.AreEqual(3, result.First().Errors.Count);
        }
Esempio n. 9
0
        public async Task FileNotExist()
        {
            var result = await LinterFactory.LintAsync(Settings.Instance, "../../artifacts/csslint/doesntexist.css");

            //Assert.IsTrue(result.First().HasErrors);
            // Running on css file should have same result as any other non-TS file
            Assert.IsTrue(result.Length == 0);
        }
        public async Task StandardTsxNoErrors()
        {
            var result = await LinterFactory.Lint(MockSettings.Instance, "../../artifacts/tslint/d.tsx");

            Assert.IsFalse(result.First().HasErrors);
            Assert.AreEqual(0, result.First().Errors.Count);
            Assert.IsFalse(string.IsNullOrEmpty(result.First().FileNames.First()), "File name is empty");
        }
Esempio n. 11
0
        public async Task JSX()
        {
            var result = await LinterFactory.LintAsync(Settings.Instance, "../../artifacts/eslint/a.jsx");

            Assert.IsTrue(result.First().HasErrors);
            Assert.AreEqual("react/display-name", result.First().Errors.First().ErrorCode, "Unexpected error message");
            Assert.AreEqual(1, result.First().Errors.Count);
        }
Esempio n. 12
0
        public async Task Standard()
        {
            var result = await LinterFactory.Lint(Settings.Instance, "../../artifacts/tslint/a.ts");

            Assert.IsTrue(result.First().HasErrors);
            Assert.IsFalse(string.IsNullOrEmpty(result.First().Errors.First().FileName), "File name is empty");
            Assert.AreEqual(7, result.First().Errors.Count);
            Assert.AreEqual("if statements must be braced", result.First().Errors.First().Message);
        }
        public async Task StandardTsx()
        {
            var result = await LinterFactory.Lint(MockSettings.Instance, "../../artifacts/tslint/c.tsx");

            Assert.IsTrue(result.First().HasErrors);
            Assert.IsFalse(string.IsNullOrEmpty(result.First().Errors.First().FileName), "File name is empty");
            Assert.AreEqual(6, result.First().Errors.Count);
            Assert.AreEqual("The class method 'sayHello' must be marked either 'private', 'public', or 'protected'", result.First().Errors.First().Message);
        }
Esempio n. 14
0
        public async Task Standard()
        {
            var result = await LinterFactory.Lint(Settings.Instance, "../../artifacts/coffeelint/a.coffee");

            Assert.IsTrue(result.First().HasErrors);
            Assert.IsTrue(result.First().Errors.First().IsError, "Severity is not 'error'");
            Assert.IsFalse(string.IsNullOrEmpty(result.First().Errors.First().FileName), "File name is empty");
            Assert.AreEqual(2, result.First().Errors.Count);
        }
Esempio n. 15
0
        public async Task StandardTsx()
        {
            var result = await LinterFactory.LintAsync(Settings.Instance, "../../artifacts/tslint/c.tsx");

            Assert.IsTrue(result.First().HasErrors);
            Assert.IsFalse(string.IsNullOrEmpty(result.First().Errors.First().FileName), "File name is empty");
            Assert.AreEqual(5, result.First().Errors.Count);
            Assert.AreEqual("comment must start with lowercase letter", result.First().Errors.First().Message);
        }
Esempio n. 16
0
        public void Standard()
        {
            var result = LinterFactory.Lint(Settings.Instance, "../../artifacts/eslint/a.js");
            var first  = result.First();

            Assert.IsTrue(first.HasErrors);
            Assert.IsTrue(first.Errors.First().IsError, "Severity is not 'error'");
            Assert.IsFalse(string.IsNullOrEmpty(first.Errors.First().FileName), "File name is empty");
            Assert.AreEqual(3, first.Errors.Count);
        }
Esempio n. 17
0
        public async Task Standard()
        {
            var result = await LinterFactory.LintAsync(Settings.Instance, "../../artifacts/csslint/a.css");

            var first = result.First();

            Assert.IsTrue(first.HasErrors);
            Assert.IsFalse(result.First().Errors.First().IsError, result.First().Errors.First().ErrorCode + " is not 'warning'");
            Assert.IsFalse(string.IsNullOrEmpty(first.Errors.First().FileName));
            Assert.AreEqual(1, first.Errors.Count);
        }
Esempio n. 18
0
        public async Task Standard()
        {
            var result = await LinterFactory.LintAsync(Settings.Instance, "../../artifacts/eslint/a.js");

            Assert.IsTrue(result.Length == 0);
            //var first = result.First();
            //Assert.IsTrue(first.HasErrors);
            //Assert.IsFalse(first.Errors.First().IsError, "Severity is not 'warning'");
            //Assert.IsFalse(string.IsNullOrEmpty(first.Errors.First().FileName), "File name is empty");
            //Assert.AreEqual(4, first.Errors.Count);
        }
 public static bool IsLintableTsOrTsxFile(string fileName, bool checkIgnoreOptions = true)
 {
     // Check if filename is absolute because when debugging, script files are sometimes dynamically created.
     if (string.IsNullOrEmpty(fileName) || !Path.IsPathRooted(fileName) || !File.Exists(fileName))
     {
         return(false);
     }
     if (!LinterFactory.IsExtensionTsOrTsx(fileName))
     {
         return(false);
     }
     return(IsLintableFile(fileName, checkIgnoreOptions));
 }
Esempio n. 20
0
        public static bool IsFileSupported(string fileName)
        {
            // Check if filename is absolute because when debugging, script files are sometimes dynamically created.
            if (string.IsNullOrEmpty(fileName) || !Path.IsPathRooted(fileName))
            {
                return(false);
            }

            if (!LinterFactory.IsFileSupported(fileName))
            {
                return(false);
            }

            string extension = Path.GetExtension(fileName);

            var patterns = WebLinterPackage.Settings.GetIgnorePatterns();

            if (patterns.Any(p => fileName.Contains(p)))
            {
                return(false);
            }

            // Ignore nested files
            if (WebLinterPackage.Settings.IgnoreNestedFiles && WebLinterPackage.Dte.Solution != null)
            {
                var item = WebLinterPackage.Dte.Solution.FindProjectItem(fileName);

                if (item == null)
                {
                    return(false);
                }

                // item.Collection is not supported in Node.js projects
                if (item.ContainingProject.Kind.Equals("{9092aa53-fb77-4645-b42d-1ccca6bd08bd}", StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }

                if (item.Collection != null && item.Collection.Parent != null)
                {
                    var parent = item.Collection.Parent as ProjectItem;

                    if (parent != null && parent.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
        public async Task StandardTsxFixErrors()
        {
            try
            {
                File.Copy("../../artifacts/tslint/c.tsx", "../../artifacts/tslint/cTest.tsx", true);
                var result = await LinterFactory.Lint(MockSettings.Instance, true, false, "../../artifacts/tslint/cTest.tsx");

                Assert.IsFalse(result.First().HasErrors);
                Assert.AreEqual(0, result.First().Errors.Count);
                string actual   = File.ReadAllText("../../artifacts/tslint/cTest.tsx");
                string expected = File.ReadAllText("../../artifacts/tslint/cFixed.tsx");
                Assert.AreEqual(expected, actual);
            }
            finally
            {
                File.Delete("../../artifacts/tslint/cTest.tsx");
            }
        }
Esempio n. 22
0
        public static bool IsFileSupported(string fileName)
        {
            // Check if filename is absolute because when debugging, script files are sometimes dynamically created.
            if (string.IsNullOrEmpty(fileName) || !Path.IsPathRooted(fileName))
            {
                return(false);
            }

            if (!LinterFactory.IsFileSupported(fileName))
            {
                return(false);
            }

            string extension = Path.GetExtension(fileName);

            // Minified files should be ignored
            if (fileName.EndsWith(".min" + extension, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // Ignore nested files
            if (WebLinterPackage.Dte.Solution != null)
            {
                var item = WebLinterPackage.Dte.Solution.FindProjectItem(fileName);

                if (item == null)
                {
                    return(false);
                }

                if (item.Collection != null && item.Collection.Parent != null)
                {
                    var parent = item.Collection.Parent as ProjectItem;

                    if (parent != null && parent.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Esempio n. 23
0
 protected override void Initialize()
 {
     // Delay execution until VS is idle.
     Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
     {
         // Then execute in a background thread.
         System.Threading.ThreadPool.QueueUserWorkItem((o) =>
         {
             try
             {
                 LinterFactory.WarmUp();
             }
             catch (Exception ex)
             {
                 Logger.Log(ex);
             }
         });
     }), DispatcherPriority.ApplicationIdle, null);
 }
Esempio n. 24
0
 protected override void Initialize()
 {
     // Delay execution until VS is idle.
     Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
     {
         // Then execute in a background thread.
         System.Threading.Tasks.Task.Run(async() =>
         {
             try
             {
                 await LinterFactory.InitializeAsync();
             }
             catch (Exception ex)
             {
                 Logger.Log(ex);
             }
         });
     }), DispatcherPriority.ApplicationIdle, null);
 }
Esempio n. 25
0
 private void FindProjectItems(ProjectItem projectItem, string projectName, Dictionary <string, string> fileNameToProjectNameMap)
 {
     // We can't use ignore paths here as they don't apply for tsconfig results
     if (projectItem.GetFullPath() is string fileName)
     {
         // It's possible for the same file to be in two VS projects, linked in to either, issue #20
         if (LinterFactory.IsExtensionTsOrTsx(fileName) && !fileNameToProjectNameMap.ContainsKey(fileName))
         {
             fileNameToProjectNameMap.Add(fileName, projectName);
         }
         if (projectItem.ProjectItems == null)
         {
             return;
         }
         foreach (ProjectItem subProjectItem in projectItem.ProjectItems)
         {
             FindProjectItems(subProjectItem, projectName, fileNameToProjectNameMap);
         }
     }
 }
Esempio n. 26
0
        public static void Lint(bool showErrorList, params string[] fileNames)
        {
            ThreadPool.QueueUserWorkItem((o) =>
            {
                try
                {
                    EnsureDefaults();

                    var result = LinterFactory.Lint(WebLinterPackage.Settings, fileNames);

                    if (result != null)
                    {
                        ErrorListService.ProcessLintingResults(result, showErrorList);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
            });
        }
Esempio n. 27
0
        public async Task BasicLint()
        {
            // tslint.json is the usual file but has had completed-docs added to allow us to test the type checking

            // Arrange
            string mainProjectTsconfig = Path.GetFullPath(@"../../artifacts/tsconfig/multiple/tsconfig.json");

            string[] fileNames = new string[] { mainProjectTsconfig };

            // Act
            var result = await LinterFactory.Lint(new MockSettings()
            {
                UseTsConfig = true
            }, false, false, fileNames);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Length);
            Assert.AreEqual(30, result[0].Errors.Count);

            // file3.ts is excluded from this tsconfig.json, in spite of being in the VS project.
            // It has errors but they shouldn't appear here
            IList <LintingError> file3Errors = GetErrorsForFile("file3.ts", result[0].Errors);

            Assert.IsTrue(file3Errors.Count == 0);

            // file2.ts is the reverse of the above: it's included in the tsconfig.json file, but is not in the VS project
            // It should have 4 errors, one of them our completed-docs
            IList <LintingError> file2Errors = GetErrorsForFile("file2.ts", result[0].Errors);

            Assert.IsTrue(file2Errors.Count == 4);
            LintingError completedDocsError = file2Errors.First(le => le.ErrorCode == "completed-docs");

            Assert.IsNotNull(completedDocsError);
            Assert.AreEqual(2, completedDocsError.LineNumber);
            Assert.AreEqual(0, completedDocsError.ColumnNumber);
        }
Esempio n. 28
0
        public void FileNotExist()
        {
            var result = LinterFactory.Lint(Settings.Instance, "../../artifacts/eslint/doesntexist.js");

            Assert.IsTrue(result.First().HasErrors);
        }
Esempio n. 29
0
        public async Task FileNotExist()
        {
            var result = await LinterFactory.LintAsync(Settings.Instance, "../../artifacts/eslint/doesntexist.js");

            Assert.IsTrue(result.First().HasErrors);
        }
Esempio n. 30
0
        public async Task LintAll()
        {
            // Arrange
            string topTsconfig     = Path.GetFullPath(@"../../artifacts/tsconfig/multiple/tsconfig.json");
            string folderaTsconfig = Path.GetFullPath(@"../../artifacts/tsconfig/multiple/a/tsconfig.json");
            string folderbTsconfig = Path.GetFullPath(@"../../artifacts/tsconfig/multiple/b/tsconfig.json");
            // The tsconfig.json below isn't in a VS project, so couldn't be linted from the UI - can be here
            string folderbTsconfigEmptyProject = Path.GetFullPath(@"../../artifacts/tsconfig/none/b/tsconfig.json");

            string[] fileNames = new string[] { topTsconfig, folderaTsconfig, folderbTsconfig, folderbTsconfigEmptyProject };

            // Act
            var result = await LinterFactory.Lint(new MockSettings()
            {
                UseTsConfig = true
            }, false, false, fileNames);

            // Assert
            // file2 is in both tsconfigs.  It has 4 errors.  With the old code we got 8 in the Error List, and here.
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Length);
            Assert.AreEqual(37, result[0].Errors.Count);

            IList <LintingError> file1Errors = GetErrorsForFile("file1.ts", result[0].Errors);

            Assert.IsTrue(file1Errors.Count == 3);

            IList <LintingError> file2Errors = GetErrorsForFile("file2.ts", result[0].Errors);

            Assert.IsTrue(file2Errors.Count == 4);
            LintingError completedDocsError = file2Errors.First(le => le.ErrorCode == "completed-docs");

            Assert.IsNotNull(completedDocsError);
            Assert.AreEqual(2, completedDocsError.LineNumber);
            Assert.AreEqual(0, completedDocsError.ColumnNumber);

            IList <LintingError> file3Errors = GetErrorsForFile("file3.ts", result[0].Errors);

            Assert.IsTrue(file3Errors.Count == 3);

            IList <LintingError> file4Errors = GetErrorsForFile("file4.ts", result[0].Errors);

            Assert.IsTrue(file4Errors.Count == 3);

            IList <LintingError> file5Errors = GetErrorsForFile("file5.ts", result[0].Errors);

            Assert.IsTrue(file5Errors.Count == 4);

            IList <LintingError> file6TsxErrors = GetErrorsForFile("file6.tsx", result[0].Errors);

            Assert.IsTrue(file6TsxErrors.Count == 11);
            LintingError memberAccessError = file6TsxErrors.First(le => le.ErrorCode == "member-access");

            Assert.IsNotNull(memberAccessError);
            Assert.AreEqual(11, memberAccessError.LineNumber);
            Assert.AreEqual(4, memberAccessError.ColumnNumber);
            Assert.IsTrue(memberAccessError.Message.Contains("sayHello"));

            // IgnoreNested has no effect when we're using tsconfig.jsons (although it's false here)
            IList <LintingError> file7NestedErrors = GetErrorsForFile("file7.ts", result[0].Errors);

            Assert.IsTrue(file7NestedErrors.Count == 4);

            IList <LintingError> testErrors = GetErrorsForFile("test.ts", result[0].Errors);

            Assert.IsTrue(testErrors.Count == 5);
        }