HAKASHUN / manabi

manabi
14 stars 1 forks source link

AngularJSプロジェクトにおけるjscsスタイル設定 #66

Closed HAKASHUN closed 9 years ago

HAKASHUN commented 9 years ago

.jscs.json

{
  "disallowKeywords": ["with"],
  "disallowMixedSpacesAndTabs": true,
  "disallowMultipleLineStrings": true,
  "disallowNewlineBeforeBlockStatements": true,
  "disallowSpaceAfterObjectKeys": true,
  "disallowSpaceAfterPrefixUnaryOperators": ["!"],
  "disallowSpaceBeforeBinaryOperators": [","],
  "disallowSpacesInAnonymousFunctionExpression": {
    "beforeOpeningRoundBrace": true
  },
  "disallowSpacesInFunctionDeclaration": {
    "beforeOpeningRoundBrace": true
  },
  "disallowSpacesInNamedFunctionExpression": {
    "beforeOpeningRoundBrace": true
  },
  "disallowSpacesInsideParentheses": true,
  "disallowTrailingComma": true,
  "disallowTrailingWhitespace": true,
  "requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return", "try", "catch"],
  "requireSpacesInFunction": {
    "beforeOpeningCurlyBrace": true
  },
  "validateParameterSeparator": ", "
}

.jscs.json.todo

{
  "requireCurlyBraces": ["if", "else", "for", "while", "do", "try", "catch"],
  "requireSpaceAfterBinaryOperators": ["?", ":", "+", "-", "/", "*", "%", "==", "===", "!=", "!==", ">", ">=", "<", "<=", "&&", "||"],
  "requireSpaceBeforeBinaryOperators": ["?", ":", "+", "-", "/", "*", "%", "==", "===", "!=", "!==", ">", ">=", "<", "<=", "&&", "||"],
  "disallowImplicitTypeConversion": ["string"],
  "disallowMultipleLineBreaks": true,
  "disallowKeywordsOnNewLine": ["else"],
  "requireLineFeedAtFileEnd": true,
  "validateJSDoc": {
    "checkParamNames": true,
    "requireParamTypes": true
  }
}
HAKASHUN commented 9 years ago

"disallowKeywords": ["with"]

// ダメ
with (x) {
   prop++;
}
HAKASHUN commented 9 years ago

"disallowMixedSpacesAndTabs": true

HAKASHUN commented 9 years ago

"disallowMultipleLineStrings": true

// ダメ
var x = "multi \
       line";

// オッケー
var x = "multi" +
       "line";
var y = "single line";
HAKASHUN commented 9 years ago

"disallowNewlineBeforeBlockStatements": true

ダメ

function bad()
{
    var obj =
    {
        val: true
    };

    return {
        data: obj
    };
}

if (cond)
{
    foo();
}

for (var e in elements)
{
    bar(e);
}

while (cond)
{
    foo();
}

オッケー

function good(){
    var obj = {
        val: true
    };

    return {
        data: obj
    };
}

if (cond){
    foo();
}

for (var e in elements){
    bar(e);
}

while (cond){
    foo();
}
HAKASHUN commented 9 years ago

"disallowSpaceAfterObjectKeys": true

// ダメ
var x = {a : 1};

// オッケー
var x = {a: 1};
HAKASHUN commented 9 years ago

"disallowSpaceAfterPrefixUnaryOperators": ["!"]

// ダメ
x = ! y;

//オッケー
x = !y;
HAKASHUN commented 9 years ago

"disallowSpaceBeforeBinaryOperators": [","]

// ダメ
var hoge = [1 , 2 , 3];
// オッケー
var hoge = [1, 2, 3];
HAKASHUN commented 9 years ago

"disallowSpacesInAnonymousFunctionExpression": { "beforeOpeningRoundBrace": true }

// ダメ
function () {}

// オッケー
function() {}
HAKASHUN commented 9 years ago

"disallowSpacesInFunctionDeclaration": { "beforeOpeningRoundBrace": true }

// ダメ
function () {}

// オッケー
function() {}
HAKASHUN commented 9 years ago

"disallowSpacesInNamedFunctionExpression": { "beforeOpeningRoundBrace": true }

// ダメ
function a () {}

// オッケー
function a() {}
HAKASHUN commented 9 years ago

"disallowSpacesInsideParentheses": true

// ダメ
var x = ( 1 + 2 ) * 3;

// オッケー
var x = (1 + 2) * 3;
HAKASHUN commented 9 years ago

"disallowTrailingComma": true

// ダメ
var foo = [1, 2, 3, ];
var bar = {a: "a", b: "b", }

// オッケー
var foo = [1, 2, 3];
var bar = {a: "a", b: "b"}
HAKASHUN commented 9 years ago

"disallowTrailingWhitespace": true

// ダメ
var foo = "blah blah"; //<-- whitespace character here

// オッケー
var foo = "blah blah";
HAKASHUN commented 9 years ago

"requireSpaceAfterKeywords"

"if", "else", "for", "while", "do", "switch", "return", "try", "catch"

// ダメ
if(x) {
  x++;
}

//オッケー
if (x) {
  x++;
}

// ダメ
return0;

// オッケー
return 0;
HAKASHUN commented 9 years ago

"requireSpacesInFunction": { "beforeOpeningCurlyBrace": true }

// ダメ
function a(){}

//オッケー
function a() {}
HAKASHUN commented 9 years ago

"validateParameterSeparator": ", "

// ダメ
function a(b , c) {}
function a(b,c) {}

// オッケー
function a(b, c) {}
HAKASHUN commented 9 years ago

"requireCurlyBraces": ["if", "else", "for", "while", "do", "try", "catch"]

"if", "else", "for", "while", "do", "try", "catch"
// ダメ
if (x) x++;

// オッケー
if (x) {
   x++;
}
HAKASHUN commented 9 years ago

requireSpaceAfterBinaryOperators, requireSpaceBeforeBinaryOperators

"?", ":", "+", "-", "/", "*", "%", "==", "===", "!=", "!==", ">", ">=", "<", "<=", "&&", "||"
// ダメ
x +y;
x+y;

// オッケー
x + y;
HAKASHUN commented 9 years ago

"disallowImplicitTypeConversion": ["string"]

// ダメ
x = '' + y;

// オッケー
x = String(y);
HAKASHUN commented 9 years ago

"disallowMultipleLineBreaks": true

ダメ

var x = 1;

x++;

オッケー

var x = 1;

x++;
HAKASHUN commented 9 years ago

"disallowKeywordsOnNewLine": ["else"]

// ダメ
if (x < 0) {
    x++;
}
else {
    x--;
}

// オッケー
if (x < 0) {
    x++;
} else {
    x--;
}
HAKASHUN commented 9 years ago

"requireLineFeedAtFileEnd": true

HAKASHUN commented 9 years ago

"validateJSDoc": { "checkParamNames": true, "requireParamTypes": true }

checkParamNames

requireParamTypes

HAKASHUN commented 9 years ago

11月に更新されていました

https://github.com/angular/angular.js/commits/master/.jscs.json

jscs定義が更新されていたので差分をまとめます。

HAKASHUN commented 9 years ago

追加されたルール

"disallowSpaceAfterPrefixUnaryOperators"

後ろにスペースを入れてはならないオペレータが増えました

Before

"disallowSpaceAfterPrefixUnaryOperators": ["!"]

After

"disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"]

"disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],

新しく追加された定義です。

++--を変数の後ろに定義する際は、変数との間にスペースを入れては行けません。

// ダメ
x = y ++; y = z --;

// オッケー
x = y++; y = z--;

"disallowSpacesInsideArrayBrackets": true

// ダメ
var x = [ [ 1 ] ];

// オッケー
var x = [[1]];

"requireCommaBeforeLineBreak": true

オブジェクトを定義する際は、プロパティ定義の末尾にカンマをつけなくてはいけません。

// ダメ
var x = {
    one: 1
    , two: 2
};

// オッケー
var x = {
    one: 1,
    two: 2
};
var y = { three: 3, four: 4};

"requireSpaceBeforeBlockStatements": true

制御フローのためのブロック文定義の際には、{の目に半角スペースをいれなければなりません

// ダメ
if (cond){
    foo();
}

for (var e in elements){
    bar(e);
}

while (cond){
    foo();
}

// オッケー
if (cond) {
    foo();
}

for (var e in elements) {
    bar(e);
}

while (cond) {
    foo();
}

"requireSpacesInConditionalExpression"

三項演算子を定義する際に用いる、?:の残後には必ず半角スペースをいれなければなりません。

// ダメ
var a = b? c : d;
var a = b ?c : d;
var a = b ? c: d;
var a = b ? c :d;

// オッケー
var a = b ? c : d;
var a= b ? c : d;

"validateLineBreaks": "LF"

改行を意味するコードはLFでなくてはなりません。

// ダメ
var x = 1;<CRLF>
x++;

// オッケー
var x = 1;<LF>
x++;
HAKASHUN commented 9 years ago

削除されたルール