Tooling JavaScript to ensure consistency in coding style

34
TOOLING JAVASCRIPT TO ENSURE CONSISTENCY IN CODING STYLE Dmitry Sheiko

description

These slides inllustrate what are coding style, conventions and standards, why they matter and how to use JS_CodeSniffer to check tat your code doesn't violates given coding standard

Transcript of Tooling JavaScript to ensure consistency in coding style

Page 1: Tooling JavaScript to ensure consistency in coding style

TOOLING JAVASCRIPT TO ENSURE

CONSISTENCY IN CODING STYLE

Dmitry Sheiko

Page 2: Tooling JavaScript to ensure consistency in coding style

CODING CONVENTIONS

Page 3: Tooling JavaScript to ensure consistency in coding style

Coding conventions mean guidelines for a specific programming language that recommend programming style

indentation

comments

white-spaces

naming conventions

etc.

Page 4: Tooling JavaScript to ensure consistency in coding style

CODING STANDARDS

Page 5: Tooling JavaScript to ensure consistency in coding style

When coding conventions are adopted they make a coding standard

Page 6: Tooling JavaScript to ensure consistency in coding style

JAVASCRIPT CODING STANDARDS

Page 7: Tooling JavaScript to ensure consistency in coding style

jQuery JavaScript

Style Guide

Idiomatic.js

Dojo Style Guide

Google JavaScript

Style Guide

Page 8: Tooling JavaScript to ensure consistency in coding style

DOES IT REALLY MATTER?

Page 9: Tooling JavaScript to ensure consistency in coding style

“No programmer is an island”

Page 10: Tooling JavaScript to ensure consistency in coding style

Code conventions improve the readability and maintainability of the software, enabling engineers to understand the code more quickly

Page 11: Tooling JavaScript to ensure consistency in coding style

I RUN JS*INT, SO I’M FINE…

Page 12: Tooling JavaScript to ensure consistency in coding style

Linter is to detect the code that may cause potential problems. To check the coding style one needs a code sniffer.

Page 13: Tooling JavaScript to ensure consistency in coding style

JS_CODESNIFFER

Page 14: Tooling JavaScript to ensure consistency in coding style

JSCodeSniffer is a tool that checks JavaScript code style consistency according to a provided coding style

Page 15: Tooling JavaScript to ensure consistency in coding style

It feels like PHP_CodeSniffer, but for JavaScript.

Page 16: Tooling JavaScript to ensure consistency in coding style

LET’S ROCK IT

Page 17: Tooling JavaScript to ensure consistency in coding style

Installation:$sudo npm install jscodesniffer -g

Page 18: Tooling JavaScript to ensure consistency in coding style

Get detailed report on a target (file or directory) coding style according to jQuery JavaScript Style Guide:$jscs ./js/source/main.js --standard=Jquery --report-full

Page 19: Tooling JavaScript to ensure consistency in coding style
Page 20: Tooling JavaScript to ensure consistency in coding style

Get summary report on a target:$jscs ./js/source --standard=Jquery --report-summary

Page 21: Tooling JavaScript to ensure consistency in coding style
Page 22: Tooling JavaScript to ensure consistency in coding style

Get a report suitable for Jenkins CI-server:$jscs ./js/source --standard=Jquery —report=checkstyle —report-file=./build/checkstyle.xml

Page 23: Tooling JavaScript to ensure consistency in coding style

Enforce a coding standard in the code: // Your source code /* jscs standard:Jquery */

Page 24: Tooling JavaScript to ensure consistency in coding style

SETTING UP CUSTOM RULE-SET

Page 25: Tooling JavaScript to ensure consistency in coding style

1. Take a rule-set file in ./standard directory 2. Copy it under the name of your custom

standard (MyConventions) 3. Modify the JSON to fulfil your requirements

Page 26: Tooling JavaScript to ensure consistency in coding style

Check your code: $jscs ./js/source --standard=MyConventions

Page 27: Tooling JavaScript to ensure consistency in coding style

I FOLLOW JQUERY CODING STYLE, BUT NOT STRICTLY…

Page 28: Tooling JavaScript to ensure consistency in coding style

JUST SET RELAXING OPTIONS IN RUN-TIME CONFIGURATION

Page 29: Tooling JavaScript to ensure consistency in coding style

Place in the root of the project file .jscsrc: { "Indentation": false, "QuoteConventions": false }

Page 30: Tooling JavaScript to ensure consistency in coding style

WHAT ABOUT CONTINUOUS INTEGRATION?

Page 31: Tooling JavaScript to ensure consistency in coding style

Apache Ant setup: <target name="jscs-ci" description="Find coding standard violations using JS_CodeSniffer and print human readable output."> <exec executable="jscs"> <arg value="--standard=Jquery" /> <arg value="--report=checkstyle" /> <arg value="--report-file=${basedir}/build/logs/checkstyle.xml" /> <arg path="${basedir}/src" /> </exec> </target>

Page 32: Tooling JavaScript to ensure consistency in coding style

Grunt setup:

grunt.loadNpmTasks('grunt-contrib-jscs'); grunt.initConfig({ // Validate against jQuery coding standard jscs: { options: { "standard": "Jquery" }, all: ["js-folder"] } });

Page 33: Tooling JavaScript to ensure consistency in coding style

JS CodeSniffer https://github.com/dsheiko/jscodesniffer JS CodeSniffer Grunt task https://github.com/dsheiko/grunt-contrib-jscs !

My credits to flaticon.com for these amazing icons

Thank you!

Page 34: Tooling JavaScript to ensure consistency in coding style

DMITRY SHEIKO dsheiko.com

@sheiko https://github.com/dsheiko