Skip to content

Instantly share code, notes, and snippets.

@sanemat
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanemat/5416e4f701922a47773a to your computer and use it in GitHub Desktop.
Save sanemat/5416e4f701922a47773a to your computer and use it in GitHub Desktop.
lintとか #tng15

Lint for JavaScript

  • eslint
  • jscs
  • jshint
  • jslint
  • Closure Linter
  • coffeelint
  • tslint

lintにかかわらず、こういう開発便利系何使ってる? 何知ってる?

pronto

で、pronto gemってのがある Ruby

これjsのutilも使いたい

pronto-jshint を参照しよう! -> jshintrb ??? イマココ

↓みたいな感じにしたい

pronto pick-patch | any-command | pronto github_pr

For Ruby

  • rubocop
  • brakeman
  • rails best practice

Web service

  • hound-ci
@sanemat
Copy link
Author

sanemat commented Feb 10, 2015

pronto.js っていうweb アプリケーション フレームワークがあるのが名前はアレ

@sanemat
Copy link
Author

sanemat commented Feb 10, 2015

node-inspector

@sanemat
Copy link
Author

sanemat commented Feb 10, 2015

Closure Linter はfixerがついてる

@sanemat
Copy link
Author

sanemat commented Feb 10, 2015

他はエラーが出るだけ

@sanemat
Copy link
Author

sanemat commented Feb 10, 2015

カバレッジ

@sanemat
Copy link
Author

sanemat commented Feb 10, 2015

差分lintをするならファイルを取って違反の行数をカウントして、diffの部分にそれが含まれるか 含まれたらコメントする

@sanemat
Copy link
Author

sanemat commented Feb 10, 2015

junit形式のxmlなりからpull requestのコメントを作るといいのでは

@sanemat
Copy link
Author

sanemat commented Feb 10, 2015

eslintのformat
https://github.com/eslint/eslint/tree/master/docs/command-line-interface#-f---format
"stylish" (the default), "compact", "checkstyle", "jslint-xml", "junit" and "tap".
eslint -f compact file.js
OR
custom formatter
eslint -f customformat.js file.js

@sanemat
Copy link
Author

sanemat commented Feb 10, 2015

jscsのreporter
http://jscs.info/overview.html#-reporter-
jscs itself provides six reporters: checkstyle, console, inline, junit and text.
jscs path[ path[...]] --reporter=console
OR
custom reporter
jscs path[ path[...]] --reporter=./some-dir/my-reporter.js

@sanemat
Copy link
Author

sanemat commented Feb 10, 2015

rubocop formatter
https://github.com/bbatsov/rubocop#formatters

Formatters
Progress Formatter (default)
Clang Style Formatter
Fuubar Style Formatter
Emacs Style Formatter
Simple Formatter
File List Formatter
JSON Formatter
Offense Count Formatter
HTML Formatter

File List Formatter and JSON Formatter is machine parsable, document says.

@sanemat
Copy link
Author

sanemat commented Feb 13, 2015

jscs machine readable format

$ ne jscs bin/browser-sync.js --reporter=checkstyle
<?xml version="1.0" encoding="utf-8"?>
<checkstyle version="4.3">
    <file name="bin/browser-sync.js">
        <error line="21" column="1" severity="error" message="Multiple line break" source="jscs" />
        <error line="143" column="1" severity="error" message="Missing line feed at file end" source="jscs" />
    </file>
</checkstyle>

$ ne jscs bin/browser-sync.js --reporter=inline
bin/browser-sync.js: line 21, col 0, Multiple line break
bin/browser-sync.js: line 143, col 0, Missing line feed at file end

$ ne jscs bin/browser-sync.js --reporter=text
Multiple line break at bin/browser-sync.js :
    19 |    help: getHelpText(path.resolve(__dirname, "../lib/cli/help.txt"))
    20 |});
    21 |
--------^
    22 |
    23 |/**

Missing line feed at file end at bin/browser-sync.js :
   141 |
   142 |module.exports = handleCli;
   143 |module.exports.getHelpText = getHelpText;
--------^


2 code style errors found.

$ ne jscs bin/browser-sync.js --reporter=inlinesingle
bin/browser-sync.js: line 21, col 0, Multiple line break
bin/browser-sync.js: line 143, col 0, Missing line feed at file end

$ ne jscs bin/browser-sync.js --reporter=console
Multiple line break at bin/browser-sync.js :
    19 |    help: getHelpText(path.resolve(__dirname, "../lib/cli/help.txt"))
    20 |});
    21 |
--------^
    22 |
    23 |/**

Missing line feed at file end at bin/browser-sync.js :
   141 |
   142 |module.exports = handleCli;
   143 |module.exports.getHelpText = getHelpText;
--------^


2 code style errors found.

$ ne jscs bin/browser-sync.js --reporter=junit
<?xml version="1.0"?>
<testsuite name="JSCS" tests="1" failures="2">
  <testcase name="bin/browser-sync.js" failures="2">
    <failure>Multiple line break at bin/browser-sync.js :
    19 |    help: getHelpText(path.resolve(__dirname, "../lib/cli/help.txt"))
    20 |});
    21 |
--------^
    22 |
    23 |/**</failure>
    <failure>Missing line feed at file end at bin/browser-sync.js :
   141 |
   142 |module.exports = handleCli;
   143 |module.exports.getHelpText = getHelpText;
--------^</failure>
  </testcase>
</testsuite>

@sanemat
Copy link
Author

sanemat commented Feb 13, 2015

checkstyle format, jscsの場合はこれ採用

@sanemat
Copy link
Author

sanemat commented Feb 13, 2015

https://github.com/jquery/esprima
有用な奴の宝庫っぽい

 "scripts": {
        "generate-regex": "node tools/generate-identifier-regex.js",

        "test": "node test/run.js && npm run lint && npm run coverage",

        "lint": "npm run check-version && npm run eslint && npm run jscs && npm run complexity",
        "check-version": "node tools/check-version.js",
        "jscs": "jscs esprima.js test/*test.js",
        "eslint": "node node_modules/eslint/bin/eslint.js esprima.js",
        "complexity": "node tools/list-complexity.js && cr -s -l -w --maxcyc 16 esprima.js",

        "coverage": "npm run analyze-coverage && npm run check-coverage",
        "analyze-coverage": "node node_modules/istanbul/lib/cli.js cover test/runner.js",
        "check-coverage": "node node_modules/istanbul/lib/cli.js check-coverage --statement 100 --branch 100 --function 100",

        "benchmark": "node test/benchmarks.js",
        "benchmark-quick": "node test/benchmarks.js quick"

@sanemat
Copy link
Author

sanemat commented Feb 13, 2015

eslint format(from esprima examples)

$ node node_modules/eslint/bin/eslint.js --format stylish examples/tokendist.js

examples/tokendist.js
  21:18  error  Unexpected sync method: 'readFileSync'  no-sync
  31:8   error  Unexpected console statement            no-console

✖ 2 problems

$ node node_modules/eslint/bin/eslint.js --format compact examples/tokendist.js
examples/tokendist.js: line 21, col 18, Error - Unexpected sync method: 'readFileSync'. (no-sync)
examples/tokendist.js: line 31, col 8, Error - Unexpected console statement. (no-console)

2 problems

$ node node_modules/eslint/bin/eslint.js --format checkstyle examples/tokendist.js
<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3"><file name="examples/tokendist.js"><error line="21" column="18" severity="error" message="Unexpected sync method: &apos;readFileSync&apos;. (no-sync)" /><error line="31" column="8" severity="error" message="Unexpected console statement. (no-console)" /></file></checkstyle>

$ node node_modules/eslint/bin/eslint.js --format jslint-xml examples/tokendist.js
<?xml version="1.0" encoding="utf-8"?><jslint><file name="examples/tokendist.js"><issue line="21" char="18" evidence="fs.readFileSync" reason="Unexpected sync method: &apos;readFileSync&apos;. (no-sync)" /><issue line="31" char="8" evidence="console.log" reason="Unexpected console statement. (no-console)" /></file></jslint>

$ node node_modules/eslint/bin/eslint.js --format junit examples/tokendist.js
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite package="org.eslint" time="0" tests="2" errors="2" name="examples/tokendist.js">
<testcase time="0" name="org.eslint.no-sync"><failure message="Unexpected sync method: &apos;readFileSync&apos;."><![CDATA[line 21, col 18, Error - Unexpected sync method: &apos;readFileSync&apos;. (no-sync)]]></failure></testcase>
<testcase time="0" name="org.eslint.no-console"><failure message="Unexpected console statement."><![CDATA[line 31, col 8, Error - Unexpected console statement. (no-console)]]></failure></testcase>
</testsuite>
</testsuites>

$ node node_modules/eslint/bin/eslint.js --format tap examples/tokendist.js
TAP version 13
1..1
not ok 1 - examples/tokendist.js
  ---
  message: "Unexpected sync method: 'readFileSync'."
  severity: error
  data:
    line: 21
    column: 18
    ruleId: no-sync
  messages:
    - message: Unexpected console statement.
      severity: error
      data:
        line: 31
        column: 8
        ruleId: no-console
  ...

@sanemat
Copy link
Author

sanemat commented Feb 13, 2015

eslintの場合 checkstyle, jslint-xml がparseしやすい

@sanemat
Copy link
Author

sanemat commented Feb 13, 2015

@sanemat
Copy link
Author

sanemat commented Feb 13, 2015

rubocop とrubocop-checkstyle_formatter, file nameがフルパスだ

$ rubocop --require rubocop/formatter/checkstyle_formatter --format RuboCop::Formatter::CheckstyleFormatter lib/github_api.rb
<?xml version='1.0'?>
<checkstyle>
  <file name='/Users/sane/work/tachiko/pigeon.tachikoma.io/lib/github_api.rb'>
    <error line='4' column='0' severity='info' message='Class definition is too long. [164/100]' source='com.puppycrawl.tools.checkstyle.Metrics/ClassLength'/>
    <error line='42' column='2' severity='info' message='Method has too many lines. [11/10]' source='com.puppycrawl.tools.checkstyle.Metrics/MethodLength'/>
    <error line='52' column='4' severity='info' message='Favor modifier `unless` usage when having a single-line body. Another good alternative is the usage of control flow `&amp;&amp;`/`||`.' source='com.puppycrawl.tools.checkstyle.Style/IfUnlessModifier'/>
    <error line='82' column='2' severity='info' message='Assignment Branch Condition size for fork_from_this is too high. [22.65/15]' source='com.puppycrawl.tools.checkstyle.Metrics/AbcSize'/>
    <error line='82' column='2' severity='info' message='Method has too many lines. [12/10]' source='com.puppycrawl.tools.checkstyle.Metrics/MethodLength'/>
    <error line='187' column='2' severity='info' message='Method has too many lines. [11/10]' source='com.puppycrawl.tools.checkstyle.Metrics/MethodLength'/>
  </file>
</checkstyle>

@sanemat
Copy link
Author

sanemat commented Feb 15, 2015

errorなしだとこんな出力になる

<?xml version='1.0'?>
<checkstyle>
  <file name='/Users/sane/work/tachiko/pigeon.tachikoma.io/Rakefile'/>
</checkstyle>

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