Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine JsHint and JSCS rule, hack to stop errors #637

Merged
merged 6 commits into from
Feb 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public void VsTextViewCreated(IVsTextView textViewAdapter)
ITextDocument document;
if (TextDocumentFactoryService.TryGetTextDocument(textView.TextDataModel.DocumentBuffer, out document))
{
var jsHintLintInvoker = new LintFileInvoker(f => new JsHintReporter(f), document);
var jsHintLintInvoker = new LintFileInvoker(f => new JavaScriptLintReporter(new JsHintCompiler(), f), document);
textView.Closed += (s, e) => jsHintLintInvoker.Dispose();

textView.TextBuffer.Properties.GetOrCreateSingletonProperty(() => jsHintLintInvoker);

var jsCodeStyleLintInvoker = new LintFileInvoker(f => new LintReporter(new JsCodeStyleCompiler(), WESettings.Instance.JavaScript, f), document);
var jsCodeStyleLintInvoker = new LintFileInvoker(f => new JavaScriptLintReporter(new JsCodeStyleCompiler(), f), document);
textView.Closed += (s, e) => jsCodeStyleLintInvoker.Dispose();

textView.TextBuffer.Properties.GetOrCreateSingletonProperty(() => jsCodeStyleLintInvoker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

namespace MadsKristensen.EditorExtensions
{
internal class JsHintReporter : LintReporter
internal class JavaScriptLintReporter : LintReporter
{
public JsHintReporter(string fileName) : base(new JsHintCompiler(), WESettings.Instance.JavaScript, fileName) { }
public JavaScriptLintReporter(ILintCompiler lintCompiler, string fileName) :
base(lintCompiler, WESettings.Instance.JavaScript, fileName) { }

public override Task RunLinterAsync()
{
Expand All @@ -21,7 +22,7 @@ public override Task RunLinterAsync()
return base.RunLinterAsync();
}

public static bool NotJsOrIsMinifiedOrNotExists(string file)
public static bool NotJsOrMinifiedOrDocumentOrNotExists(string file)
{
return !Path.GetExtension(file).Equals(".js", StringComparison.OrdinalIgnoreCase) ||
file.EndsWith(".min.js", StringComparison.OrdinalIgnoreCase) ||
Expand All @@ -33,20 +34,20 @@ public static bool NotJsOrIsMinifiedOrNotExists(string file)

public static bool ShouldIgnore(string file)
{
if (NotJsOrIsMinifiedOrNotExists(file))
if (NotJsOrMinifiedOrDocumentOrNotExists(file))
{
return true;
}

ProjectItem item = ProjectHelpers.GetProjectItem(file);

if (item == null)
return true;

// Ignore files nested under other files such as bundle or TypeScript output
ProjectItem parent = item.Collection.Parent as ProjectItem;
if (parent != null && !Directory.Exists(parent.FileNames[1]) || File.Exists(item.FileNames[1] + ".bundle"))
return true;
if (item != null)
{
// Ignore files nested under other files such as bundle or TypeScript output
ProjectItem parent = item.Collection.Parent as ProjectItem;
if (parent != null && !Directory.Exists(parent.FileNames[1]) || File.Exists(item.FileNames[1] + ".bundle"))
return true;
}

string name = Path.GetFileName(file);
return _builtInIgnoreRegex.IsMatch(name);
Expand All @@ -61,19 +62,19 @@ public static bool ShouldIgnore(string file)
@"dojo\.js",
@"ember\.js",
@"ext-core\.js",
@"handlebars\.*",
@"handlebars.*",
@"highlight\.js",
@"history\.js",
@"jquery-([0-9\.]+)\.js",
@"jquery\.blockui\.*",
@"jquery\.validate\.*",
@"jquery\.unobtrusive\.*",
@"jquery.blockui.*",
@"jquery.validate.*",
@"jquery.unobtrusive.*",
@"jquery-ui-([0-9\.]+)\.js",
@"json2\.js",
@"knockout-([0-9\.]+)\.js",
@"MicrosoftAjax([a-z]+)\.js",
@"modernizr-([0-9\.]+)\.js",
@"mustache\.*",
@"mustache.*",
@"prototype\.js ",
@"qunit-([0-9a-z\.]+)\.js",
@"require\.js",
Expand Down
24 changes: 21 additions & 3 deletions EditorExtensions/Commands/LintReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,27 @@ private void ReadResult(IEnumerable<CompilerError> results)
_provider.Tasks.Remove(task);
}

// HACK: stop JSCS (and any other) from output more than 500 items, hope JSCS can do this
int errorLimit = 500;
int resultCount = results.Count();
int count = 0;
foreach (CompilerError error in results.Where(r => r != null))
{
ErrorTask task = CreateTask(error);
ErrorTask task;

// JSHint has build in limit up to 500
// the 501 one is "too many" one, no need to replace it with this hack
if (++count > errorLimit &&
!(_compiler is JsHintCompiler && resultCount == 501))
{
task = CreateTask(error,
string.Format("{0}: Too many errors. Only shows {1} errors from {2}.",
_compiler.ServiceName, errorLimit, results.Count()));
_provider.Tasks.Add(task);
break;
}

task = CreateTask(error);
_provider.Tasks.Add(task);
}
}
Expand All @@ -116,7 +134,7 @@ private void ReadResult(IEnumerable<CompilerError> results)
}
}

private ErrorTask CreateTask(CompilerError error)
private ErrorTask CreateTask(CompilerError error, string message = null)
{
ErrorTask task = new ErrorTask()
{
Expand All @@ -126,7 +144,7 @@ private ErrorTask CreateTask(CompilerError error)
Category = TaskCategory.Html,
Document = error.FileName,
Priority = TaskPriority.Low,
Text = error.Message,
Text = message ?? error.Message,
};

task.AddHierarchyItem();
Expand Down
9 changes: 6 additions & 3 deletions EditorExtensions/Compilers/NodeExecutorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,19 @@ private CompilerResult ProcessResult(Process process, string errorText, string s
private void ValidateResult(Process process, string outputFile, string errorText, CompilerResult result)
{
try
{ /* Temporary hack see; //github.com/mdevils/node-jscs/issues/212 */
if (process.ExitCode == 0 && !errorText.StartsWith("<?xml version=", StringComparison.CurrentCulture))
{
if (process.ExitCode == 0 &&
/* Temporary hack see; //github.com/mdevils/node-jscs/issues/212 */
(!errorText.StartsWith("<?xml version=", StringComparison.CurrentCulture) ||
errorText == "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<checkstyle version=\"4.3\">\n</checkstyle>"))
{
if (!string.IsNullOrEmpty(outputFile))
result.Result = File.ReadAllText(outputFile);
result.IsSuccess = true;
}
else
{
result.Errors = ParseErrors(errorText.Replace("\r", ""));
result.Errors = ParseErrors(errorText);
}
}
catch (FileNotFoundException missingFileException)
Expand Down
4 changes: 2 additions & 2 deletions EditorExtensions/EditorExtensionsPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ private void BuildEvents_OnBuildDone(vsBuildScope Scope, vsBuildAction Action)

if (WESettings.Instance.JavaScript.LintOnBuild)
{
LintFileInvoker.RunOnAllFilesInProjectAsync(".js", f => new JsHintReporter(f))
LintFileInvoker.RunOnAllFilesInProjectAsync(".js", f => new JavaScriptLintReporter(new JsHintCompiler(), f))
.DontWait("running solution-wide JSHint");
LintFileInvoker.RunOnAllFilesInProjectAsync(".js", f => new LintReporter(new JsCodeStyleCompiler(), WESettings.Instance.JavaScript, f))
LintFileInvoker.RunOnAllFilesInProjectAsync(".js", f => new JavaScriptLintReporter(new JsCodeStyleCompiler(), f))
.DontWait("running solution-wide JSCS");
}

Expand Down
2 changes: 1 addition & 1 deletion EditorExtensions/MenuItems/JsCodeStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void menuCommand_BeforeQueryStatus(object sender, System.EventArgs e)
OleMenuCommand menuCommand = sender as OleMenuCommand;

files = ProjectHelpers.GetSelectedFilePaths()
.Where(f => !JsHintReporter.NotJsOrIsMinifiedOrNotExists(f)).ToList();
.Where(f => !JavaScriptLintReporter.NotJsOrMinifiedOrDocumentOrNotExists(f)).ToList();

menuCommand.Enabled = files.Count > 0;
}
Expand Down
4 changes: 2 additions & 2 deletions EditorExtensions/MenuItems/JsHint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void menuCommand_BeforeQueryStatus(object sender, System.EventArgs e)
OleMenuCommand menuCommand = sender as OleMenuCommand;

var raw = ProjectHelpers.GetSelectedFilePaths();
files = raw.Where(f => !JsHintReporter.NotJsOrIsMinifiedOrNotExists(f)).ToList();
files = raw.Where(f => !JavaScriptLintReporter.NotJsOrMinifiedOrDocumentOrNotExists(f)).ToList();

menuCommand.Enabled = files.Count > 0;
}
Expand All @@ -56,7 +56,7 @@ private void RunJsHint()

foreach (string file in files)
{
JsHintReporter runner = new JsHintReporter(file);
JavaScriptLintReporter runner = new JavaScriptLintReporter(new JsHintCompiler(), file);
runner.RunLinterAsync().DontWait("linting " + file);
}
}
Expand Down
2 changes: 1 addition & 1 deletion EditorExtensions/Resources/settings-defaults/.jscs.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"disallowKeywords": ["with"],
"disallowMultipleLineBreaks": true,
"disallowKeywordsOnNewLine": ["else"],
"excludeFiles": ["**/*.min.js", "**/*.debug.js", "**/*.intellisense.js", "**/*-vsdoc.js"],
"excludeFiles": [],
"validateJSDoc": {
"checkParamNames": true,
"requireParamTypes": true
Expand Down
2 changes: 1 addition & 1 deletion EditorExtensions/WebEssentials2013.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@
<Compile Include="Commands\JavaScript\JavaScriptFindReferencesCommandTarget.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Commands\JavaScript\JsHintReporter.cs">
<Compile Include="Commands\JavaScript\JavaScriptLintReporter.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Commands\LESS\LessExtractMixinCommandTarget.cs" />
Expand Down