Skip to content
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

Merged
merged 41 commits into from Feb 3, 2018
Merged

Setup linter for HTML #5195

merged 41 commits into from Feb 3, 2018

Conversation

loicbourgois
Copy link
Contributor

@loicbourgois loicbourgois commented Jan 28, 2018

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 option lint-script-elements to lint (or not) the HTML files in lintTask().
Enabling this option gives 4166 errors.

$ gulp lint --lint-script-elements
✖ 4230 problems (4166 errors, 64 warnings)
  3481 errors, 0 warnings potentially fixable with the `--fix` option.

What would be the best way to update the examples : file by file, folder by folder, all at once ?

Copy link
Member

@simonbrunel simonbrunel left a 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})
Copy link
Member

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 = [
Copy link
Member

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);
Copy link
Member

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'
   ];

@loicbourgois
Copy link
Contributor Author

loicbourgois commented Jan 28, 2018

Some more details about the errors :

  • 2606 Expected indentation of x tab(s)
  • 603 Strings must use singlequote
  • 598 is not defined
    • 499 'randomScalingFactor' is not defined
    • 80 'Chart' is not defined
    • 19 other
  • 359 other

@loicbourgois
Copy link
Contributor Author

I think HTML files should at least be checked for indentation. It will be a lot easier to have consistency between the content of <script> elements and the rest of the DOM.

@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.".

@simonbrunel
Copy link
Member

I think HTML files should at least be checked for indentation

Let's talk about that on Slack / another PR since I doubt ESLint can handle that.

https://chart-js-automation.herokuapp.com/

Right, it's broken but I don't have access to fix it (@etimberg ?)

@simonbrunel
Copy link
Member

@tannerlinsley actually, that might be you that setup the heroku app?
@loicbourgois I sent you an invitation to join Slack

}
}
};
<div style="width: 75%;">
Copy link
Contributor Author

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

// 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
Copy link
Contributor Author

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

Copy link
Contributor

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

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
Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Contributor Author

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}'`

Copy link
Member

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems broken

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #5197

Copy link
Contributor

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

@simonbrunel
Copy link
Member

simonbrunel commented Jan 29, 2018

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.

Copy link
Member

@simonbrunel simonbrunel left a 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 ?
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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',
]
Copy link
Member

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,
Copy link
Member

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 = {
Copy link
Member

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'*/],
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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']);
Copy link
Member

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'
Copy link
Member

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",
Copy link
Member

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)

-ms-user-select: none;
}
</style>
<title>Labelling Data Points</title>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All files are now indented with 2 tabs instead of 1 for the HTML part

image

@@ -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>
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Contributor Author

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 ignore no-undef-var for /samples
  • use addEventListener

Copy link
Member

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 :)

Copy link
Member

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() {
//...

@loicbourgois
Copy link
Contributor Author

@simonbrunel you can add ?w=1 to the url to ignore whitespace in diffs.
see camsong/chrome-github-mate#5
Not sure how well it works.
I'm happy to proceed with multiple PR if it's easier.

@simonbrunel
Copy link
Member

simonbrunel commented Jan 29, 2018

That's much better with ?w=1, thanks for the tip!

@simonbrunel
Copy link
Member

simonbrunel commented Jan 30, 2018

The only con with ?w=1: you can't add review comments

@@ -152,6 +152,18 @@
});
chart.update();
}

document.getElementById('togglePropagate').onclick = function() {
Copy link
Contributor

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'?

Copy link
Member

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
Copy link
Contributor

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?

@loicbourgois
Copy link
Contributor Author

Fixed indentation for most files (2 tabs -> 1 tab).
However, I did not find any option to check for this with htmllint. So I added the functionality locally + submitted a PR.
This means that this kind of indentation error will not get caught for now.

Copy link
Member

@simonbrunel simonbrunel left a 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']);
Copy link
Member

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",
Copy link
Member

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': {
Copy link
Member

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

@loicbourgois
Copy link
Contributor Author

@simonbrunel what about no-unused-vars for inline events ?
Should we use comments to disable it for now and revert in another PR once BenoitZugmeyer/eslint-plugin-html#87 is merged ?

@simonbrunel
Copy link
Member

You right, it's not no-undef-var but no-unused-vars. I'm fine with disabling locally in comment (// eslint-disable-next-line no-unused-var) or globally at the samples/.eslintrc.yml.

@loicbourgois
Copy link
Contributor Author

Should be ready for merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants