-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Setup linter for HTML #5195
Setup linter for HTML #5195
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Loic for tackling these tasks
[ ... edited ... ]
I didn't review the "htmllint" related changes.
What would be the best way to update the examples : file by file, folder by folder, all at once ?
All at once, in this PR
4230 problems (4166 errors, 64 warnings)
That's a lot, what the main reason for so many errors? space vs tabs?
Edit(SB): removed suggestion to split that PR after our Slack talk
gulpfile.js
Outdated
|
||
var argv = yargs | ||
.option('force-output', {default: false}) | ||
.option('lint-script-elements', {default: false}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we decide to lint our samples, then it should be part of the regular linting process. That why I would not add this --lint-script-element
argument and keep script simpler by always parsing those HTML files.
gulpfile.js
Outdated
.option('silent-errors', {default: false}) | ||
.option('verbose', {default: false}) | ||
.argv | ||
|
||
var srcDir = './src/'; | ||
var outDir = './dist/'; | ||
|
||
var htmlFiles = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is equivalent to './samples/**/*.html'
. I would put that one line directly where it's used (as we do for the rest of the gulpfile) (Note: I'm removing srcDir
/outDir
in the upcoming gulp rewrite).
gulpfile.js
Outdated
@@ -159,6 +169,9 @@ function lintTask() { | |||
'src/**/*.js', | |||
'test/**/*.js' | |||
]; | |||
if (argv.lintScriptElements) { | |||
files = files.concat(htmlFiles); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per my previous comment:
var files = [
'samples/**/*.html',
'samples/**/*.js',
'src/**/*.js',
'test/**/*.js'
];
Some more details about the errors :
|
I think HTML files should at least be checked for indentation. It will be a lot easier to have consistency between the content of @simonbrunel I'm not sure how to discuss this on slack : I tried the badge on the readme, and was redirected to https://chart-js-automation.herokuapp.com/ with a message "There's nothing here, yet.". |
Let's talk about that on Slack / another PR since I doubt ESLint can handle that. Right, it's broken but I don't have access to fix it (@etimberg ?) |
@tannerlinsley actually, that might be you that setup the heroku app? |
samples/advanced/progress-bar.html
Outdated
} | ||
} | ||
}; | ||
<div style="width: 75%;"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to find a rule to not allow 2 tabs
samples/advanced/data-labelling.html
Outdated
// Define a plugin to provide data labels | ||
Chart.plugins.register({ | ||
afterDatasetsDraw: function(chart/* , easing */) { // TODO : remove easing ? | ||
// To only draw at the end of animation, check for easing === 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment and code would be out of sync
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove both the variable and the comment
samples/charts/line/stepped.html
Outdated
function createConfig(details, data) { | ||
return { | ||
type: 'line', | ||
data: { | ||
labels: ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5', 'Day 6'], | ||
datasets: [{ | ||
label: 'steppedLine: ' + ((typeof(details.steppedLine) === 'boolean') ? details.steppedLine : `'${details.steppedLine}'`), | ||
label: 'steppedLine: ' + ((typeof (details.steppedLine) === 'boolean') ? details.steppedLine : details.steppedLine), // TODO : check this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure what's going on here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typeof
as operator without parenthesis?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, i mean why do we have :
? details.steppedLine : `'${details.steppedLine}'`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think typeof(foo)
is invalid because it's an operator so should not be used with parenthesis. We should write typeof foo
, not typeof (foo)
(with the extra space).
Anyway, you right, this line is weird, could be: label: 'steppedLine: ' + details.steppedLine
@@ -122,10 +122,13 @@ | |||
window.myLine.update(); | |||
}); | |||
|
|||
document.getElementById('addData').addEventListener('click', function() { | |||
document.getElementById('addData').addEventListener('click', function() { // TODO : fix issue with addData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems broken
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #5197
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would update the comment to link to the issue. It's not clear from the TODO right now what is supposed to be fixed
It's very hard to review this PR, many modifications that are obfuscated by the indentation change. It might have been better to submit a PR that only fixes the indentation without touching the rest of the code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few feedback but can't review the whole PR for the reasons explained in my previous comment (we may need to move the indentation changes in another PR). Also, the indentation doesn't seem correct for the HTML part.
gulpfile.js
Outdated
'max-statements': [1, 30] | ||
} | ||
'max-statements': [1, 30], | ||
'no-new': 0, // TODO : is this allowed ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not allow to call new
without storing the result. We should fix these cases instead of disabling this rule.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried var chart = new Chart(...)
but then it triggers an error because chart
is not used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, let's disable no-new
then
gulpfile.js
Outdated
'Chart', | ||
'moment', | ||
'randomScalingFactor', | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These globals should not be enabled for the whole repository but scoped to the samples directory in its own samples/.eslintrc.yml
.
gulpfile.js
Outdated
// 125:5 error Identifier 'samples_filler_analyser' is not in camel case camelcase | ||
// /Chart.js/samples/charts/area/radar.html | ||
// 103:5 error Identifier 'samples_filler_analyser' is not in camel case camelcase | ||
'camelcase': 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it pass with 'samples-filler-analyser'
? else this rule should be moved in samples/.eslintrc.yml
.htmllintrc.js
Outdated
@@ -0,0 +1,10 @@ | |||
module.exports = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does htmllint support YAML config? It's usually more readable and easy to manipulate but also consistent with our other config files.
.htmllintrc.js
Outdated
'indent-style': 'tabs', | ||
'attr-quote-style': 'double', | ||
'spec-char-escape': false, | ||
'attr-bans': ['align', 'background', 'bgcolor', 'border', 'frameborder', 'longdesc', 'marginwidth', 'marginheight', 'scrolling', 'syle'/*, 'width'*/], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is a typo to syle
but do we really need these explicit bans?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the default option and commented the attributes triggering errors.
See https://github.com/htmllint/htmllint/wiki/Options#attr-bans
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, then you can remove the 2 last values ('syle'
which was initially 'style'
and 'width'
)
gulpfile.js
Outdated
gulp.task('docs', docsTask); | ||
gulp.task('test', ['lint', 'validHTML', 'unittest']); | ||
gulp.task('test', ['lint', 'lint-html', 'validHTML', 'unittest']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think htmllint do the same job as gulp-html-validator
(unmaintained?), so we should remove the 'validHTML'
task and associated node dependencies in package.json.
gulpfile.js
Outdated
@@ -157,7 +160,8 @@ function lintTask() { | |||
var files = [ | |||
'samples/**/*.js', | |||
'src/**/*.js', | |||
'test/**/*.js' | |||
'test/**/*.js', | |||
'./samples/**/*.html' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency:
var files = [
'samples/**/*.html'
'samples/**/*.js',
'src/**/*.js',
'test/**/*.js'
]
package.json
Outdated
@@ -17,13 +17,15 @@ | |||
"coveralls": "^3.0.0", | |||
"eslint": "^4.9.0", | |||
"eslint-config-chartjs": "^0.1.0", | |||
"eslint-plugin-html": "^4.0.2", | |||
"gitbook-cli": "^2.3.2", | |||
"gulp": "3.9.x", | |||
"gulp-concat": "~2.6.x", | |||
"gulp-connect": "~5.0.0", | |||
"gulp-eslint": "^4.0.0", | |||
"gulp-file": "^0.3.0", | |||
"gulp-html-validator": "^0.0.5", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be removed (see previous comment)
samples/advanced/data-labelling.html
Outdated
-ms-user-select: none; | ||
} | ||
</style> | ||
<title>Labelling Data Points</title> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -18,14 +18,14 @@ | |||
<div class="wrapper col-2"><canvas id="chart-3"></canvas></div> | |||
|
|||
<div class="toolbar"> | |||
<button onclick="toggleSmooth(this)">Smooth</button> | |||
<button onclick="randomize(this)">Randomize</button> | |||
<button id="toggleSmooth">Smooth</button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What the reason of this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linting JS in HTML attributes is not supported yet, so I had to move event handling in a <script>
element.
I submitted a PR to add attributes linting support : BenoitZugmeyer/eslint-plugin-html#87
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really prefer the other syntax and was about to update other samples to remove these addEventListener
. Can we revert these changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the PR is accepted, yes.
If not, we have 2 choices :
- use
onclick()
and add a rule to ignoreno-undef-var
for /samples - use
addEventListener
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no-undef-var: 0
in samples/.eslintrc.yml
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively (and probably better), we can disable this rule inline before each method:
// eslint-disable-next-line no-undef-var
function randomize() {
//...
@simonbrunel you can add |
That's much better with |
The only con with |
@@ -152,6 +152,18 @@ | |||
}); | |||
chart.update(); | |||
} | |||
|
|||
document.getElementById('togglePropagate').onclick = function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe better to use addEventListener('click'
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to keep the onclick event handler inline, as DOM attribute (previous code)
if (config.data.datasets.length > 0) { | ||
var numTicks = myLine.scales['x-axis-0'].ticksAsTimestamps.length; | ||
var lastTime = numTicks ? moment(myLine.scales['x-axis-0'].ticksAsTimestamps[numTicks - 1]) : moment(); | ||
// var numTicks = window.myLine.scales['x-axis-0'].ticksAsTimestamps.length; // TODO : check |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are these commented out because of the issue in referenced in the TODO?
This reverts commit 3746bad.
Fixed indentation for most files (2 tabs -> 1 tab). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks really good, one extra fix (plugin id) and a few minor changes.
I think it's fine if we don't detect extra indentation until htmllint/htmllint#227 is merged.
gulp.task('docs', docsTask); | ||
gulp.task('test', ['lint', 'validHTML', 'unittest']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also remove the validHtml
associated function
package.json
Outdated
"gitbook-cli": "^2.3.2", | ||
"gulp": "3.9.x", | ||
"gulp-concat": "~2.6.x", | ||
"gulp-connect": "~5.0.0", | ||
"gulp-eslint": "^4.0.0", | ||
"gulp-file": "^0.3.0", | ||
"gulp-html-validator": "^0.0.5", | ||
"gulp-htmllint": "0.0.15", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"^0.0.15"
@@ -122,7 +122,7 @@ | |||
filler: { | |||
propagate: false | |||
}, | |||
samples_filler_analyser: { | |||
'samples-filler-analyser': { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You also need to change the plugin id
@simonbrunel what about |
You right, it's not |
Should be ready for merge |
A comment from @benmccann made me realise the Javascript linter was not checking the files in /samples. Comment is here.
This is a PR to setup a linter for HTML files using html plugin for eslint and htmllint.
The rules for
htmllint
can be modified in.htmllintrc.js
. List of options is available here.In
gulpfile.js
I added the optionlint-script-elements
to lint (or not) the HTML files inlintTask()
.Enabling this option gives 4166 errors.
What would be the best way to update the examples : file by file, folder by folder, all at once ?