Skip to content

Commit 5050e8d

Browse files
committed
Introduce eslint code style checks
Ruleset copied from the one used in the Chartist project.
1 parent 2d1bc71 commit 5050e8d

10 files changed

Lines changed: 974 additions & 212 deletions

File tree

.github/workflows/nodejs.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ jobs:
2323
yarn run build --if-present
2424
env:
2525
CI: true
26+
27+
- name: lint
28+
run: yarn lint
29+
env:
30+
CI: true

.prettierrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"singleQuote": true,
3+
"jsxSingleQuote": true,
4+
"semi": true,
5+
"tabWidth": 2,
6+
"bracketSpacing": true,
7+
"arrowParens": "avoid",
8+
"trailingComma": "none"
9+
}

eslint.config.mjs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import js from "@eslint/js";
2+
import prettier from "eslint-config-prettier/flat";
3+
import globals from "globals";
4+
5+
export default [
6+
js.configs.recommended,
7+
prettier,
8+
{
9+
files: ["src/scripts/*.js"],
10+
languageOptions: {
11+
ecmaVersion: 2015,
12+
sourceType: "script",
13+
globals: {
14+
...globals.browser
15+
}
16+
},
17+
rules: {
18+
"no-console": 2,
19+
"curly": 2,
20+
"dot-notation": 1,
21+
"eqeqeq": 2,
22+
"no-alert": 2,
23+
"no-caller": 2,
24+
"no-eval": 2,
25+
"no-extra-bind": 2,
26+
"no-implied-eval": 2,
27+
"no-multi-spaces": 2,
28+
"no-with": 2,
29+
"no-shadow": 2,
30+
"no-shadow-restricted-names": 2,
31+
"brace-style": ["error", "1tbs"],
32+
"camelcase": 2,
33+
"comma-style": ["error", "last"],
34+
"eol-last": 2,
35+
"key-spacing": 2,
36+
"new-cap": 1,
37+
"no-array-constructor": 2,
38+
"no-mixed-spaces-and-tabs": 2,
39+
"no-multiple-empty-lines": 2,
40+
"semi": ["error", "always"],
41+
"semi-spacing": 2,
42+
"no-spaced-func": 2,
43+
"no-trailing-spaces": 2,
44+
"space-before-blocks": 2,
45+
"spaced-comment": 1,
46+
"no-var": 2
47+
}
48+
}
49+
];

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,28 @@
3434
"chartist": "^1.0.0"
3535
},
3636
"devDependencies": {
37+
"eslint": "^10.1.0",
38+
"eslint-config-prettier": "^10.1.8",
39+
"eslint-plugin-prettier": "^5.5.5",
3740
"grunt": "^1.6.1",
3841
"grunt-contrib-clean": "^2.0.1",
3942
"grunt-contrib-copy": "^1.0.0",
4043
"grunt-contrib-jshint": "^3.2.0",
4144
"grunt-contrib-uglify": "^5.2.2",
45+
"grunt-eslint": "^26.0.0",
4246
"grunt-sass": "^4.0.0",
4347
"grunt-umd": "^3.0.0",
4448
"jasmine-fixture": "^2.0.0",
4549
"jshint-stylish": "^2.2.1",
4650
"load-grunt-config": "^4.0.1",
51+
"prettier": "^3.8.1",
4752
"sass": "^1.79.4",
4853
"time-grunt": "^2.0.0"
4954
},
5055
"scripts": {
5156
"build": "grunt build",
57+
"format": "prettier --write 'src/**/*.js'",
58+
"lint": "grunt lint",
5259
"prepare": "grunt build",
5360
"version": "grunt build && git add -A ."
5461
},

src/css/chartist-plugin-tooltip.css

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/css/chartist-plugin-tooltip.css.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scripts/chartist-plugin-tooltip.js

Lines changed: 67 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
(function (window, document, Chartist) {
77
'use strict';
88

9-
var defaultOptions = {
9+
let defaultOptions = {
1010
currency: undefined,
1111
currencyFormatCallback: undefined,
1212
tooltipOffset: {
1313
x: 0,
14-
y: -20,
14+
y: -20
1515
},
1616
anchorToPoint: false,
1717
appendToBody: true,
1818
class: undefined,
19-
pointClass: 'ct-point',
19+
pointClass: 'ct-point'
2020
};
2121

2222
Chartist.plugins = Chartist.plugins || {};
@@ -25,23 +25,25 @@
2525

2626
return function tooltip(chart) {
2727
// Warning: If you are using npm link or yarn link, these instanceof checks will fail and you won't any tooltips
28-
var tooltipSelector = options.pointClass;
28+
let tooltipSelector = options.pointClass;
2929
if (chart instanceof Chartist.BarChart) {
3030
tooltipSelector = 'ct-bar';
3131
} else if (chart instanceof Chartist.PieChart) {
3232
// Added support for donut graph
3333
if (chart.options.donut) {
3434
// Added support for the solid donut graph
35-
tooltipSelector = chart.options.donutSolid ? 'ct-slice-donut-solid' : 'ct-slice-donut';
35+
tooltipSelector = chart.options.donutSolid
36+
? 'ct-slice-donut-solid'
37+
: 'ct-slice-donut';
3638
} else {
3739
tooltipSelector = 'ct-slice-pie';
3840
}
3941
}
4042

41-
var $chart = chart.container;
42-
var $toolTipIsShown = false;
43-
var $tooltipOffsetParent = offsetParent($chart);
44-
var $toolTip;
43+
let $chart = chart.container;
44+
let $toolTipIsShown = false;
45+
let $tooltipOffsetParent = offsetParent($chart);
46+
let $toolTip;
4547

4648
if (!options.appendToBody) {
4749
// searching for existing tooltip in the chart, because appendToBody is disabled
@@ -52,44 +54,54 @@
5254
}
5355
if (!$toolTip) {
5456
$toolTip = document.createElement('div');
55-
$toolTip.className = (!options.class) ? 'chartist-tooltip' : 'chartist-tooltip ' + options.class;
57+
$toolTip.className = !options.class
58+
? 'chartist-tooltip'
59+
: 'chartist-tooltip ' + options.class;
5660
if (!options.appendToBody) {
5761
$chart.appendChild($toolTip);
5862
} else {
5963
document.body.appendChild($toolTip);
6064
}
6165
}
62-
var height = $toolTip.offsetHeight;
63-
var width = $toolTip.offsetWidth;
66+
let height = $toolTip.offsetHeight;
67+
let width = $toolTip.offsetWidth;
6468

6569
hide($toolTip);
6670

6771
function on(event, selector, callback) {
6872
$chart.addEventListener(event, function (e) {
69-
if (!selector || hasClass(e.target, selector))
73+
if (!selector || hasClass(e.target, selector)) {
7074
callback(e);
75+
}
7176
});
7277
}
7378

7479
on('mouseover', tooltipSelector, function (event) {
75-
var $point = event.target;
76-
var tooltipText = '';
77-
78-
var isPieChart = (chart instanceof Chartist.PieChart) ? $point : $point.parentNode;
79-
var seriesName = (isPieChart) ? $point.parentNode.getAttribute('ct:meta') || $point.parentNode.getAttribute('ct:series-name') : '';
80-
var meta = $point.getAttribute('ct:meta') || seriesName || '';
81-
var hasMeta = !!meta;
82-
var value = $point.getAttribute('ct:value');
83-
84-
if (options.transformTooltipTextFnc && typeof options.transformTooltipTextFnc === 'function') {
80+
let $point = event.target;
81+
let tooltipText = '';
82+
83+
let isPieChart =
84+
chart instanceof Chartist.PieChart ? $point : $point.parentNode;
85+
let seriesName = isPieChart
86+
? $point.parentNode.getAttribute('ct:meta') ||
87+
$point.parentNode.getAttribute('ct:series-name')
88+
: '';
89+
let meta = $point.getAttribute('ct:meta') || seriesName || '';
90+
let hasMeta = !!meta;
91+
let value = $point.getAttribute('ct:value');
92+
93+
if (
94+
options.transformTooltipTextFnc &&
95+
typeof options.transformTooltipTextFnc === 'function'
96+
) {
8597
value = options.transformTooltipTextFnc(value);
8698
}
8799

88100
if (options.tooltipFnc && typeof options.tooltipFnc === 'function') {
89101
tooltipText = options.tooltipFnc(meta, value);
90102
} else {
91103
if (options.metaIsHTML) {
92-
var txt = document.createElement('textarea');
104+
let txt = document.createElement('textarea');
93105
txt.innerHTML = meta;
94106
meta = txt.value;
95107
}
@@ -102,7 +114,7 @@
102114
// For Pie Charts also take the labels into account
103115
// Could add support for more charts here as well!
104116
if (chart instanceof Chartist.PieChart) {
105-
var label = next($point, 'ct-label');
117+
let label = next($point, 'ct-label');
106118
if (label) {
107119
tooltipText += text(label) + '<br>';
108120
}
@@ -111,10 +123,12 @@
111123

112124
if (value) {
113125
if (options.currency) {
114-
if (options.currencyFormatCallback != undefined) {
126+
if (options.currencyFormatCallback !== undefined) {
115127
value = options.currencyFormatCallback(value, options);
116128
} else {
117-
value = options.currency + value.replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, '$1,');
129+
value =
130+
options.currency +
131+
value.replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, '$1,');
118132
}
119133
}
120134
value = '<span class="chartist-tooltip-value">' + value + '</span>';
@@ -157,16 +171,19 @@
157171
function setPosition(event) {
158172
height = height || $toolTip.offsetHeight;
159173
width = width || $toolTip.offsetWidth;
160-
var offsetX = -width / 2 + options.tooltipOffset.x;
161-
var offsetY = -height + options.tooltipOffset.y;
174+
let offsetX = -width / 2 + options.tooltipOffset.x;
175+
let offsetY = -height + options.tooltipOffset.y;
162176

163-
var anchor = options.anchorToPoint === true && event.target.x2 && event.target.y2;
177+
let anchor =
178+
options.anchorToPoint === true && event.target.x2 && event.target.y2;
164179

165180
if (options.appendToBody === true) {
166181
if (anchor) {
167-
var box = $chart.getBoundingClientRect();
168-
var left = event.target.x2.baseVal.value + box.left + window.pageXOffset;
169-
var top = event.target.y2.baseVal.value + box.top + window.pageYOffset;
182+
const box = $chart.getBoundingClientRect();
183+
const left =
184+
event.target.x2.baseVal.value + box.left + window.pageXOffset;
185+
const top =
186+
event.target.y2.baseVal.value + box.top + window.pageYOffset;
170187

171188
$toolTip.style.left = left + offsetX + 'px';
172189
$toolTip.style.top = top + offsetY + 'px';
@@ -175,14 +192,16 @@
175192
$toolTip.style.top = event.pageY + offsetY + 'px';
176193
}
177194
} else {
178-
var offsetBox = $tooltipOffsetParent.getBoundingClientRect();
179-
var allOffsetLeft = -offsetBox.left - window.pageXOffset + offsetX;
180-
var allOffsetTop = -offsetBox.top - window.pageYOffset + offsetY;
195+
let offsetBox = $tooltipOffsetParent.getBoundingClientRect();
196+
let allOffsetLeft = -offsetBox.left - window.pageXOffset + offsetX;
197+
let allOffsetTop = -offsetBox.top - window.pageYOffset + offsetY;
181198

182199
if (anchor) {
183-
var box = $chart.getBoundingClientRect();
184-
var left = event.target.x2.baseVal.value + box.left + window.pageXOffset;
185-
var top = event.target.y2.baseVal.value + box.top + window.pageYOffset;
200+
const box = $chart.getBoundingClientRect();
201+
const left =
202+
event.target.x2.baseVal.value + box.left + window.pageXOffset;
203+
const top =
204+
event.target.y2.baseVal.value + box.top + window.pageYOffset;
186205

187206
$toolTip.style.left = left + allOffsetLeft + 'px';
188207
$toolTip.style.top = top + allOffsetTop + 'px';
@@ -210,10 +229,9 @@
210229
*/
211230
function hide(element) {
212231
$toolTipIsShown = false;
213-
var regex = new RegExp('tooltip-show' + '\\s*', 'gi');
232+
let regex = new RegExp('tooltip-show' + '\\s*', 'gi');
214233
element.className = element.className.replace(regex, '').trim();
215234
}
216-
217235
};
218236
};
219237

@@ -224,7 +242,11 @@
224242
* @return {boolean}
225243
*/
226244
function hasClass(element, className) {
227-
return (' ' + element.getAttribute('class') + ' ').indexOf(' ' + className + ' ') > -1;
245+
return (
246+
(' ' + element.getAttribute('class') + ' ').indexOf(
247+
' ' + className + ' '
248+
) > -1
249+
);
228250
}
229251

230252
function next(element, className) {
@@ -250,7 +272,7 @@
250272
function offsetParent(elem) {
251273
if (offsetParent in elem) {
252274
// Using the native property if possible
253-
var parent = elem.offsetParent;
275+
let parent = elem.offsetParent;
254276

255277
if (!parent) {
256278
parent = document.body.parentElement;
@@ -259,7 +281,7 @@
259281
return parent;
260282
}
261283

262-
var parent = elem.parentNode;
284+
const parent = elem.parentNode;
263285
if (!parent) {
264286
return document.body.parentElement;
265287
}
@@ -272,5 +294,4 @@
272294
return offsetParent(parent);
273295
}
274296
}
275-
276-
}(window, document, Chartist));
297+
})(window, document, Chartist);

tasks/aliases.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ module.exports = {
1818
'uglify:dist'
1919
],
2020

21-
//tests
21+
// tests
2222
'test': [
2323
'clean:tmp',
2424
'jasmine'
25+
],
26+
27+
// lint
28+
'lint': [
29+
'eslint'
2530
]
2631
};

tasks/eslint.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* lint
3+
* =====
4+
*
5+
* Lint source files.
6+
*/
7+
8+
'use strict';
9+
10+
module.exports = function (grunt) {
11+
return {
12+
target: ['src/**/*.js', 'test/**/*.js']
13+
};
14+
};

0 commit comments

Comments
 (0)