Exemplo n.º 1
0
        public static async Task <string> Run(string tsFilename)
        {
            var existingPath = TsLint.Cache.SingleOrDefault(
                p => tsFilename.Contains(p.Key)
                );

            if (existingPath.Equals(TsLint.DefKeyValuePair))
            {
                // First, check if the project for this file has local installation of tslint.
                var potentialPath = TsLint.TryGetProjectTsLint(tsFilename);

                if (potentialPath.Equals(TsLint.DefKeyValuePair))
                {
                    // Now, check if the solution has local installation of tslint.
                    potentialPath = TsLint.TryGetSolutionTsLint();
                }

                if (potentialPath.Equals(TsLint.DefKeyValuePair))
                {
                    // No project, no solution, check if we can find local installation of tslint "manually".
                    potentialPath = TsLint.TryGetTsLint(tsFilename);
                }

                if (!potentialPath.Equals(TsLint.DefKeyValuePair))
                {
                    existingPath = potentialPath;
                    TsLint.Cache.Add(existingPath.Key, existingPath.Value);
                }
            }

            if (existingPath.Equals(TsLint.DefKeyValuePair))
            {
                return(null);
            }

            var procInfo = new ProcessStartInfo()
            {
                FileName               = existingPath.Value,
                WorkingDirectory       = existingPath.Key,
                Arguments              = $"-t JSON {tsFilename}",
                RedirectStandardOutput = true,
                WindowStyle            = ProcessWindowStyle.Hidden,
                CreateNoWindow         = true,
                UseShellExecute        = false
            };

            var proc = Process.Start(procInfo);

            if (proc == null)
            {
                return(null);
            }

            var reader = proc.StandardOutput;

            await proc.WaitForExitAsync();

            return(await reader.ReadToEndAsync());
        }
Exemplo n.º 2
0
        protected override void Initialize()
        {
            // Get directory of the currently opened solution.
            var dte2 = (DTE2)Package.GetGlobalService(typeof(SDTE));

            if (dte2 != null)
            {
                // Init linter.
                TsLint.Init(dte2);

                // Init Error List helper.
                ErrorListHelper.Init(this);
            }

            base.Initialize();
        }
Exemplo n.º 3
0
        public static async Task <string> Run(string tsFilename)
        {
            var existingPath = TsLint.Cache.SingleOrDefault(
                p => tsFilename.Contains(p.Key)
                );

            if (existingPath.Equals(TsLint.DefKeyValuePair))
            {
                // First, check if the project for this file has local installation of tslint.
                var potentialPath = TsLint.TryGetProjectTsLint(tsFilename);

                if (potentialPath.Equals(TsLint.DefKeyValuePair))
                {
                    // Now, check if the solution has local installation of tslint.
                    potentialPath = TsLint.TryGetSolutionTsLint();
                }

                if (potentialPath.Equals(TsLint.DefKeyValuePair))
                {
                    // No project, no solution, check if we can find local installation of tslint "manually".
                    potentialPath = TsLint.TryGetTsLint(tsFilename);
                }

                if (!potentialPath.Equals(TsLint.DefKeyValuePair))
                {
                    existingPath = potentialPath;
                    TsLint.Cache.Add(existingPath.Key, existingPath.Value);
                }
            }

            if (existingPath.Equals(TsLint.DefKeyValuePair))
            {
                return(null);
            }

            var tscCmdPath    = $"{existingPath.Value}tsc.cmd";
            var tscConfigPath = $"{existingPath.Key}\\tsconfig.json";

            var useTsLintProjectFlag = false;

            if (File.Exists(tscCmdPath) && File.Exists(tscConfigPath))
            {
                useTsLintProjectFlag = true;
            }

            var procInfo = new ProcessStartInfo()
            {
                FileName               = $"{existingPath.Value}tslint.cmd",
                WorkingDirectory       = existingPath.Key,
                Arguments              = $"-t JSON {(useTsLintProjectFlag ? $"--project \"{tscConfigPath}\"" : "")} \"{tsFilename}\"",
                RedirectStandardOutput = true,
                WindowStyle            = ProcessWindowStyle.Hidden,
                CreateNoWindow         = true,
                UseShellExecute        = false
            };

            var proc = Process.Start(procInfo);

            if (proc == null)
            {
                return(null);
            }

            var standardOutput = proc.StandardOutput;
            var output         = await standardOutput.ReadToEndAsync();

            await proc.WaitForExitAsync();

            return(output);
        }
Exemplo n.º 4
0
        private async System.Threading.Tasks.Task CollectTags(string tsFilename)
        {
            this._collectedTags.Clear();

            var output = await TsLint.Run(tsFilename);

            if (string.IsNullOrEmpty(output))
            {
                return;
            }

            var jArray = JArray.Parse(output);
            var result = jArray.ToObject <List <TsLintResult> >();

            foreach (var entry in result)
            {
                // If the document has been closed
                if (this._document.TextBuffer == null)
                {
                    continue;
                }

                var startLine = this._document.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(entry.StartPosition.Line);
                var endLine   = this._document.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(entry.EndPosition.Line);

                var start = entry.StartPosition.Character;
                var end   = entry.EndPosition.Character;

                SnapshotSpan span;

                if (start == end)
                {
                    // Handle some special cases. If the number of special cases increases,
                    // I should refactor this.
                    if (entry.RuleName == "semicolon")
                    {
                        // "Move" left by one character so that we can pin-point the text with the missing semicolon
                        start -= 1;
                    }

                    var extent = this._textStructureNavigator.GetExtentOfWord(endLine.Start + start);
                    span = extent.Span;
                }
                else
                {
                    span = new SnapshotSpan(
                        this._view.TextSnapshot,
                        Span.FromBounds(startLine.Start + start, endLine.Start + end)
                        );
                }

                var trackingSpan =
                    this._view.TextSnapshot.CreateTrackingSpan(
                        span,
                        SpanTrackingMode.EdgeInclusive
                        );

                var type = PredefinedErrorTypeNames.Warning;

                if (entry.RuleSeverity != null && entry.RuleSeverity == "ERROR")
                {
                    type = PredefinedErrorTypeNames.SyntaxError;
                }

                this._collectedTags.Add(
                    new TsLintTag(
                        trackingSpan,
                        type,
                        $"[tslint] {entry.Failure} ({entry.RuleName})",
                        tsFilename,
                        entry.StartPosition.Line,
                        entry.StartPosition.Character
                        )
                    );
            }
        }