Skip to content

Commit 7660052

Browse files
martin107paul-m
martin107
authored andcommitted
Issue #2771429 by martin107, Mile23: Add .eslintrc, lint JS errors
1 parent 88abab5 commit 7660052

File tree

10 files changed

+146
-12
lines changed

10 files changed

+146
-12
lines changed

.eslintrc

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"extends": "eslint:recommended",
3+
"env": {
4+
"browser": true
5+
},
6+
"globals": {
7+
"Drupal": true,
8+
"drupalSettings": true,
9+
"drupalTranslations": true,
10+
"domready": true,
11+
"jQuery": true,
12+
"_": true,
13+
"matchMedia": true,
14+
"Backbone": true,
15+
"Modernizr": true,
16+
"CKEDITOR": true
17+
},
18+
"rules": {
19+
// Errors.
20+
"array-bracket-spacing": [2, "never"],
21+
"block-scoped-var": 2,
22+
"brace-style": [2, "stroustrup", {"allowSingleLine": true}],
23+
"comma-dangle": [2, "never"],
24+
"comma-spacing": 2,
25+
"comma-style": [2, "last"],
26+
"computed-property-spacing": [2, "never"],
27+
"curly": [2, "all"],
28+
"eol-last": 2,
29+
"eqeqeq": [2, "smart"],
30+
"guard-for-in": 2,
31+
"indent": [2, 2, {"SwitchCase": 1}],
32+
"key-spacing": [2, {"beforeColon": false, "afterColon": true}],
33+
"keyword-spacing": [2, {"before": true, "after": true}],
34+
"linebreak-style": [2, "unix"],
35+
"lines-around-comment": [2, {"beforeBlockComment": true, "afterBlockComment": false}],
36+
"new-parens": 2,
37+
"no-array-constructor": 2,
38+
"no-caller": 2,
39+
"no-catch-shadow": 2,
40+
"no-eval": 2,
41+
"no-extend-native": 2,
42+
"no-extra-bind": 2,
43+
"no-extra-parens": [2, "functions"],
44+
"no-implied-eval": 2,
45+
"no-iterator": 2,
46+
"no-label-var": 2,
47+
"no-labels": 2,
48+
"no-lone-blocks": 2,
49+
"no-loop-func": 2,
50+
"no-multi-spaces": 2,
51+
"no-multi-str": 2,
52+
"no-native-reassign": 2,
53+
"no-nested-ternary": 2,
54+
"no-new-func": 2,
55+
"no-new-object": 2,
56+
"no-new-wrappers": 2,
57+
"no-octal-escape": 2,
58+
"no-process-exit": 2,
59+
"no-proto": 2,
60+
"no-return-assign": 2,
61+
"no-script-url": 2,
62+
"no-sequences": 2,
63+
"no-shadow-restricted-names": 2,
64+
"no-spaced-func": 2,
65+
"no-trailing-spaces": 2,
66+
"no-undef-init": 2,
67+
"no-undefined": 2,
68+
"no-unused-expressions": 2,
69+
"no-unused-vars": [2, {"vars": "all", "args": "none"}],
70+
"no-with": 2,
71+
"object-curly-spacing": [2, "never"],
72+
"one-var": [2, "never"],
73+
"quote-props": [2, "consistent-as-needed"],
74+
"quotes": [2, "single", "avoid-escape"],
75+
"semi": [2, "always"],
76+
"semi-spacing": [2, {"before": false, "after": true}],
77+
"space-before-blocks": [2, "always"],
78+
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
79+
"space-in-parens": [2, "never"],
80+
"space-infix-ops": 2,
81+
"space-unary-ops": [2, { "words": true, "nonwords": false }],
82+
"spaced-comment": [2, "always"],
83+
"strict": 2,
84+
"yoda": [2, "never"],
85+
// Warnings.
86+
"max-nested-callbacks": [1, 3],
87+
"valid-jsdoc": [1, {
88+
"prefer": {
89+
"returns": "return",
90+
"property": "prop"
91+
},
92+
"requireReturn": false
93+
}]
94+
}
95+
}

STANDARDS.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
HOWTO: Drupal Examples For Developers Coding Standards
22
=======================================================
33

4-
Examples uses the same coding standards as Drupal core.
4+
Examples uses mostly the same coding standards as Drupal core.
5+
6+
Examples uses the phpcs tool to allow for checking PHP coding standards. We also
7+
use eslint for JavaScript coding standards.
58

69
Examples has a `phpcs.xml.dist` file at the root of the project. This file
710
specifies the current coding standards 'sniffs' which code in the project must
@@ -59,3 +62,21 @@ Always look at the changes to see what `phpcbf` did.
5962

6063
And always re-run `phpcs` in order to discover whether `phpcbf` handled all the
6164
errors.
65+
66+
Installing eslint
67+
-----------------
68+
69+
`eslint` is a node.js tool. You can and probably should install it globally,
70+
since installing it locally would add files to the examples project.
71+
Instructions available here: https://www.npmjs.com/package/eslint
72+
73+
Examples has an `.eslintrc` file which defines the JavaScript coding standard.
74+
This file should be identical to the current Drupal core standard.
75+
76+
Running eslint
77+
--------------
78+
79+
You can run eslint this way:
80+
81+
$ cd /path/to/examples
82+
$ eslint .

field_example/field_example.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
* Provides a farbtastic colorpicker for the fancier widget.
88
*/
99
(function ($) {
10+
11+
'use strict';
12+
1013
Drupal.behaviors.field_example_colorpicker = {
1114
attach: function () {
12-
$(".edit-field-example-colorpicker").on("focus", function (event) {
15+
$('.edit-field-example-colorpicker').on('focus', function (event) {
1316
var edit_field = this;
14-
var picker = $(this).closest('div').parent().find(".field-example-colorpicker");
17+
var picker = $(this).closest('div').parent().find('.field-example-colorpicker');
1518
// Hide all color pickers except this one.
16-
$(".field-example-colorpicker").hide();
19+
$('.field-example-colorpicker').hide();
1720
$(picker).show();
1821
$.farbtastic(picker, function (color) {
1922
edit_field.value = color;

js_example/js/black.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55

66
(function ($, Drupal, drupalSettings) {
7-
"use strict";
7+
8+
'use strict';
9+
810
/**
911
* Attaches the JS test behavior to to weight div.
1012
*/

js_example/js/blue.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55

66
(function ($, Drupal, drupalSettings) {
7-
"use strict";
7+
8+
'use strict';
9+
810
/**
911
* Attaches the JS test behavior to weight div.
1012
*/

js_example/js/brown.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55

66
(function ($, Drupal, drupalSettings) {
7-
"use strict";
7+
8+
'use strict';
9+
810
/**
911
* Attaches the JS test behavior to weight div.
1012
*/

js_example/js/green.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55

66
(function ($, Drupal, drupalSettings) {
7-
"use strict";
7+
8+
'use strict';
9+
810
/**
911
* Attaches the JS test behavior to weight div.
1012
*/

js_example/js/js_example_accordion.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
*/
55

66
(function ($) {
7+
8+
'use strict';
9+
710
$(function () {
8-
$("#accordion").accordion();
9-
})
11+
$('#accordion').accordion();
12+
});
1013
})(jQuery);

js_example/js/purple.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55

66
(function ($, Drupal, drupalSettings) {
7-
"use strict";
7+
8+
'use strict';
9+
810
/**
911
* Attaches the JS test behavior to weight div.
1012
*/

js_example/js/red.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55

66
(function ($, Drupal, drupalSettings) {
7-
"use strict";
7+
8+
'use strict';
9+
810
/**
911
* Attaches the JS test behavior to weight div.
1012
*/

0 commit comments

Comments
 (0)