Skip to content

Commit

Permalink
Merge pull request #160 from phase2/feature/eslint-support
Browse files Browse the repository at this point in the history
Add ESLint support with grunt-eslint
  • Loading branch information
grayside committed Jun 12, 2015
2 parents e7bb7a9 + d795070 commit 20ce86d
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 9 deletions.
37 changes: 32 additions & 5 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@ This is an example of the settings for the validate tasks:

```
{
"eslint": {
"dir": [
'<%= config.srcPaths.drupal %>/**/*.js',
'!<%= config.srcPaths.drupal %>/**/bower/**/*.js',
'!<%= config.srcPaths.drupal %>/sites/**/files/**/*.js'
]
},
"phpcs": {
"path": "bin/phpcs",
"standard": "vendor/drupal/coder/coder_sniffer/Drupal"
Expand All @@ -376,6 +383,26 @@ This is an example of the settings for the validate tasks:
> If there is no `phpcs` key in the configuration, the system will assume you
are not using PHPCS and will suppress it from the system.

**eslint**: To enable eslint, set to `true` to use default options or an object
with the following optional settings.

**eslint.configFile**: The path to the eslint config file to use. If no value
is specified, then `.eslintrc` in the project root is used.

**eslint.dir**: An array of glob patterns to include/exclude files for
review by eslint. The following is used by default:

```
{
"eslint": {
"dir": [
'<%= config.srcPaths.drupal %>/**/*.js',
'!<%= config.srcPaths.drupal %>/sites/**/files/**/*.js'
]
}
}
```

**phpcs.path**: The path to the PHPCS executable.

**phpcs.standard**: The PHPCS coding standard to use. The example composer.json
Expand All @@ -385,8 +412,8 @@ installs the Drupal Coder's standard, the path of which is shown above.
This can be used to replace the defaults supplied by grunt-drupal-tasks.

This example placed in the Gruntconfig.json file ignores directories named
"pattern-lab" and a "bower_components" in addition to the defaults that come with
grunt-drupal-tasks:
"pattern-lab" and a "bower_components" in addition to the defaults that come
with grunt-drupal-tasks:

```
{
Expand All @@ -412,12 +439,12 @@ grunt-drupal-tasks:
}
```

**phpcs.ignoreExitCode**: Set to `false` if you want validate to fail on PHPCS
issues.

**phplint.dir**: An array of globbing patterns which phplint should include or
exclude from PHP code syntax validation.

**validate.ignoreError**: Set to `true` to prevent failing the build if code
quality validation fails (which also prevents other tasks from executing).

### Package Settings

The `grunt package` task allows you to assemble and independently exported
Expand Down
19 changes: 19 additions & 0 deletions example/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
##
# This file instructs eslint to skip certain directories in it's analysis.
#
# It is not needed by grunt-drupal-tasks but is intended to make direct use
# of eslint more effective.
#
# @see http://eslint.org/docs/user-guide/configuring.html
##

# node_modules ignored by default

# Ignore composer and bundler managed dependencies
vendor/**/*

# Ignore generated files and upstream dependencies via Drush Make
build/**/*

# Ignore JS in the Drupal files directory such as aggregations.
src/sites/**/files/**/*.js
29 changes: 29 additions & 0 deletions example/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"env": {
"browser": true
},
"globals": {
"Drupal": true,
"jQuery": true,
"tinyMCE": true
},
"rules": {
"eqeqeq": [2, "smart"],
"guard-for-in": 2,
"no-undef": 2,
//"no-unused-vars": [2, {"vars": "local", "args": "none"}],
"no-unused-vars": 0,
"strict": 0,
"new-cap": 0,
"quotes": 0,
"camelcase": 0,
"no-underscore-dangle": 0,
"no-new": 0,
"no-alert": 0,
"no-use-before-define": 0,
"consistent-return": 0,
"no-constant-condition": 0,
"no-comma-dangle" : 2,
"no-catch-shadow" : 2
}
}
3 changes: 2 additions & 1 deletion example/Gruntconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
},
"behat": {
"flags": "--tags ~@wip"
}
},
"eslint": true
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"grunt-contrib-symlink": "0.3.0",
"grunt-contrib-watch": "0.6.1",
"grunt-drush": "0.0.6",
"grunt-eslint": "13.0.0",
"grunt-force-task": "1.0.0",
"grunt-log-headers": "1.0.1",
"grunt-mkdir": "0.1.2",
"grunt-newer": "1.1.1",
Expand Down
34 changes: 32 additions & 2 deletions tasks/quality.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ module.exports = function(grunt) {
* Deeper inspection & analyze of codebase, not done on every build.
* Produces reports for Jenkins. May be a long-running task.
*/
grunt.loadNpmTasks('grunt-eslint');
grunt.loadNpmTasks('grunt-force-task');
grunt.loadNpmTasks('grunt-phplint');
grunt.loadNpmTasks('grunt-phpcs');
grunt.loadNpmTasks('grunt-phpmd');

var Help = require('../lib/help')(grunt);

// Task set aliases are registered at the end of the file based on these values.
Expand Down Expand Up @@ -49,8 +52,9 @@ module.exports = function(grunt) {
var phpStandard = grunt.config('config.phpcs.standard')
|| 'vendor/drupal/coder/coder_sniffer/Drupal';

var ignoreError = grunt.config('config.phpcs.ignoreExitCode');
ignoreError = ignoreError === undefined ? true : ignoreError;
// Support deprecated config.phpcs.ignoreExitCode value until 1.0.
var ignoreError = grunt.config('config.validate.ignoreError') || grunt.config('config.phpcs.ignoreExitCode');
ignoreError = ignoreError === undefined ? false : ignoreError;

grunt.config('phpcs', {
analyze: {
Expand Down Expand Up @@ -118,6 +122,32 @@ module.exports = function(grunt) {
analyze.push('phpmd:custom');
}

if (grunt.config.get('config.eslint') != undefined) {
var eslintConfig = grunt.config.get('config.eslint'),
eslintTarget = eslintConfig.dir || [
'<%= config.srcPaths.drupal %>/**/*.js',
'!<%= config.srcPaths.drupal %>/sites/**/files/**/*.js'
],
eslintConfigFile = eslintConfig.configFile || './.eslintrc',
eslintIgnoreError = grunt.config.get('config.validate.ignoreError') === undefined ? false : grunt.config.get('config.validate.ignoreError'),
eslintName = eslintIgnoreError ? 'force:eslint' : 'eslint';
grunt.config('eslint', {
options: {
configFile: eslintConfigFile
},
validate: eslintTarget,
analyze: {
options: {
format: 'checkstyle',
outputFile: '<%= config.buildPaths.reports %>/eslint.xml'
},
src: eslintTarget
}
});
validate.push(eslintName + ':validate');
analyze.push(eslintName + ':analyze');
}

// If any of the themes have code quality commands, attach them here.
var themes = grunt.config('config.themes');
for (var key in themes) {
Expand Down
2 changes: 1 addition & 1 deletion test/create_working_copy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mkdir test/working_copy
PATH_WORKING_COPY="`pwd`/test/working_copy"

# Copy the example skeleton into working_copy
cp -r example/* $PATH_WORKING_COPY
cp -r example/. $PATH_WORKING_COPY

# Copy the test assets into working_copy
PATH_TEST_ASSETS="test/test_assets"
Expand Down
4 changes: 4 additions & 0 deletions test/test_assets/Gruntconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"behat": {
"flags": "--tags ~@wip"
},
"validate": {
"ignoreError": true
},
"eslint": true,
"themes": {
"example_theme": {
"path": "<%= config.srcPaths.drupal %>/themes/example_theme",
Expand Down
6 changes: 6 additions & 0 deletions test/test_assets/src/modules/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

var test = "hi";

if (test == 'hello') {
}

4 changes: 4 additions & 0 deletions test/test_assets_d8/Gruntconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"behat": {
"flags": "--tags ~@wip"
},
"validate": {
"ignoreError": true
},
"eslint": true,
"themes": {
"example_theme": {
"path": "<%= config.srcPaths.drupal %>/themes/example_theme",
Expand Down
6 changes: 6 additions & 0 deletions test/test_assets_d8/src/modules/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

var test = "hi";

if (test == 'hello') {
}

0 comments on commit 20ce86d

Please sign in to comment.