Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Just to keep track of the idea and point to the standards:
It seems that there are just 2 basic rules.
Comments
Comment #1
stella CreditAttribution: stella commentedI've spent the last while trying to get consensus on the coding standards for JS files. I published the final version of the new standards yesterday, so we'll now be adding them to the Coder module in the near future.
Cheers,
Stella
Comment #2
douggreen CreditAttribution: douggreen commentedWe now read js files and can write rules for them. All rules are excluded from js by default, but you can add js only rules using
'#filename' => '\.js$'
and you can add existing rules to js files with'#filename-not' => ''
. This second empty rule is needed to override the default'#filename-not' => '\.js$'
.Comment #3
douggreen CreditAttribution: douggreen commentedThe #filename and #filename-not directives have recently changed slightly, but hopefully for the better. The default rules are only valid for php files. And js files are only read if a rule in one of the reviews being run, accesses it. It should be pretty easy to add the #filename to existing style rules that should also apply to js, such the many spacing, parenthesis, and bracket rules. Note that the directive now takes an array of extensions instead of a regex, so, to add js you'd now write:
'#filename' => array('js'),
Comment #4
stella CreditAttribution: stella commentedSo
'#filename' => array('js'),
will add js files to the list of file types that the review is being run on? What if you want a js only rule?Comment #5
douggreen CreditAttribution: douggreen commentedhmm, that is a js only review. The problem in my logic here is that unlike what my comments say, there's not a way to add a rule to js without explicitly naming array('js', 'php', 'module', 'inc', 'test'), and that's not good....
Comment #6
Manuel Garcia CreditAttribution: Manuel Garcia commented+1 for this feature request -- can help testing also :)
Comment #7
gagarine CreditAttribution: gagarine commentedWhat the statue on this?
Comment #8
gagarine CreditAttribution: gagarine commentedSo what I really want to review is the proper use of attr to avoid problem when we upgrade to jquery 1.5 to 1.7. This is documented in this issue #1498858: [meta] attr and prop.
Any guidance how I should do this in coder?
EDIT:
I think of course of http://php.net/manual/en/book.v8js.php and as you can see in http://css.dzone.com/articles/running-javascript-inside-php you can do some code introspection.
Comment #9
gagarine CreditAttribution: gagarine commentedMmmh how I get it is _coder_review_read_and_parse_file is PHP centric.
Comment #10
nod_This is relevant too #1664940: [Policy, patch] Decide on JSHint configuration
Comment #11
gagarine CreditAttribution: gagarine commentedI create a sandbox https://drupal.org/sandbox/gagarine/1674242 it's now working but need some more work and a configuration page...
Comment #12
nod_Using eslint for everything JS now :)
Comment #13
klausiNote that our phpcs standard can also check and fix coding standard errors in JS files, but it is by far not complete and perfect :-)
Comment #14
klausiHm, I think we can even call this fixed.
Check your JS coding standards like this:
Or if you want to check your whole module/theme folder anyway:
And if you want to automatically fix stuff replace "phpcs" with "phpcbf" in the above commands.
Please open separate issues for additional checks that we could add for javascript or bug reports or whatever you find :-)
Maybe nod_ can add some pointers here how people can install/run eslint?
Comment #15
nod_Let's not lie about what it's actually doing :D
Comment #16
nod_Comment #17
nod_About eslint we have some docs https://www.drupal.org/node/172169 and https://www.drupal.org/node/1955232
Comment #18
klausiExcellent, I'm currently working on a patch for pareview.sh to include eslint checking.
Comment #19
nod_To note that some things checked and raised as errors by phpcs are not errors. Fixing them would make eslint complain. So would be good to add a warning about this when checking js files or something.
Comment #20
klausiYeah, we should probably run phpcs on the D8 core javascript and add issues for false positives.
So the basic eslint prototype in pareview.sh works now, example: #2448867-24: [D7] Custom Cookie Compliance.