Integrate JsLint/JsHint into the MSBuild – the easy way

I had the task to integrate static code analysis for javascript into our build. I’ve been struggling around by the solutions which I’ve found on the net, but I didn’t like any of them:

  1. Jorrit Saverda’s solution with windows scripting host: http://blog.jorritsalverda.com/2012/05/msbuild-by-convention.htmlt
  2. Drainbrain also with the windows scripting host: http://drainbrain.wordpress.com/2012/04/12/jslint-with-msbuild/

I recognized that the JSLint for Visual Studio 2010 plugin also has an MSBuild task among its libraries. I’ve found it interesting, that nobody described it before me – at least I didn’t find any articles on the net.
So I’ve downloaded it and integrated it also into our build process on the TFS build server. The steps which I’ve done:

  1. Download the MSBuild Task from here: http://jslint4vs2010.codeplex.com/releases/view/95840
  2. Create a JsLint.Targets file as follows and place it besides your downloaded and extracted binaries:
    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <UsingTask AssemblyFile="JSLint.MSBuild.dll" TaskName="JSLintTask"/>
      <PropertyGroup>
        <JsLintErrorsAsWarnings>true</JsLintErrorsAsWarnings>
      </PropertyGroup>
      <ItemGroup>
        <JsFiles Include="$(JsLintedFiles)"></JsFiles>
      </ItemGroup>
      <Target Name="JSLintTarget" Condition="'' != 'false'">
        <Message Text="Running JSLintTask" Importance="Normal" />
    
        <JSLintTask
          SourceFiles="@(JsFiles)"
    	  OptionsFile="$(SolutionDir)Import\JsLintMsBuild\StandardRuleset.xml"
    	  TreatViolationsAsWarnings="$(JsLintErrorsAsWarnings)"
                />
      </Target>
    </Project>
    
  3. Configure javascript files which you want to lint and call this target from the web project:
      <PropertyGroup>
        <!-- ... -->
        <JsLintedFiles>$(SolutionDir)WebProject\Scripts\*.*</JsLintedFiles>
      </PropertyGroup>
      
      <Target Name="BeforeBuild">
        <CallTarget Targets="JSLintTarget" />
      </Target>
    
      <Import Project="..\Import\JsLintMsBuild\JsLint.Targets" />
    
  4. When running it on the build server I had the following error:
    The "JSLintTask" task could not be loaded from the assembly JSLint.MSBuild.dll. 
    Could not load file or assembly 'JSLint.MSBuild.dll' or one of its dependencies. 
    Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
    

    The solution was to Edit the build definition and change the platform under Process/3. Advanced/MSBuild Platform from Auto to x86.
    We figured out what the problem was by checking the fusion log… :)

Advertisements

About Tamas Nemeth

Husband and proud father of two daughters in Nürnberg. I'm working as a Senior Software Developer and an enthusiastic Clean-Coder. I spend most of my free time with my family (playing, hiking, etc...). I also play table-tennis and badminton sometimes...
This entry was posted in Technical Interest and tagged , , , , , . Bookmark the permalink.

3 Responses to Integrate JsLint/JsHint into the MSBuild – the easy way

  1. Great post! do you know how to configure rules per solution rather than per machine?

  2. Hi I would like to know if there is anything similar for EsLint

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s