-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor:
console.log(式); //=> 評価結果
に表記を統一 (#212)
* refactor: `console.log(式); //=> 評価結果` に表記を統一 fix #195 * refactor(operator): console.logを使うように * Revert "refactor(operator): console.logを使うように" This reverts commit e222f1d. * fix(test): Identifierを含んでいるかを判定に追加 * refactor(data-type): リテラルからはconsole.logを削除 * docs(CONTRIBUTING): どちらを優先するかを記述 * test(comment): 1行以下のもおんはチェックしないように
- Loading branch information
Showing
9 changed files
with
117 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// MIT © 2017 azu | ||
"use strict"; | ||
const esprima = require("esprima"); | ||
const esquery = require("esquery"); | ||
const path = require("path"); | ||
const ignoreFileList = [ | ||
// 演算子はいいかな | ||
"source/basic/operator", | ||
// これもリテラルの話なので… | ||
"source/basic/implicit-coercion" | ||
]; | ||
/** | ||
* 変数を含んでいるか | ||
* @param {Object} AST | ||
* @returns {boolean} | ||
*/ | ||
const isIncludeVariableInExpression = (AST) => { | ||
// 例外 | ||
// call({ x : 1}) | ||
const Identifiers = esquery(AST, "*:not(Property) Identifier"); | ||
if (Identifiers.length > 0) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
/** | ||
* コードで `評価式; // => 評価値` を利用している箇所で | ||
* `console.log`を付けるかどうかを判定する | ||
* https://github.com/asciidwango/js-primer/issues/195 | ||
* | ||
* - 基本的にはconsole.logを利用する | ||
* - リテラルや変数が登場しないコードでは`console.log`を省いても良い | ||
* | ||
* lineが問題ある行ならばErrorオブジェクトを返す | ||
* @param {string} text | ||
* @param {string} filePath ファイルパスは無視したい対象の指定に使う | ||
* @returns {Error|undefined} | ||
*/ | ||
module.exports = function shouldConsoleWithComment(text, filePath) { | ||
const lines = text.split("\n"); | ||
// 1行以下なら無視する | ||
if (lines.length <= 1) { | ||
return; | ||
} | ||
lines.forEach(line => { | ||
const error = checkLineThatShouldHaveComment(line, filePath); | ||
if (error instanceof Error) { | ||
throw error; | ||
} | ||
}); | ||
}; | ||
/** | ||
* @param {string} line | ||
* @param {string} filePath | ||
* @returns {Error|undefined} | ||
*/ | ||
function checkLineThatShouldHaveComment(line, filePath) { | ||
if (!/\/\/\s*=>\s*/.test(text)) { | ||
return; | ||
} | ||
if (text.includes("console.")) { | ||
return; | ||
} | ||
// エラーの場合は無視 | ||
if (/=>.*Error/.test(text)) { | ||
return; | ||
} | ||
// template literalっぽいのは無視 | ||
if (text.includes("`")) { | ||
return; | ||
} | ||
|
||
const AST = esprima.parse(text); | ||
// 変数を含まないリテラルのみであるならパスする | ||
if (!isIncludeVariableInExpression(AST)) { | ||
return; | ||
} | ||
// 無視リストに含まれているなら無視 | ||
const isIgnored = ignoreFileList.some(ignoreFilePath => { | ||
return filePath.includes(path.normalize(ignoreFilePath)); | ||
}); | ||
if (isIgnored) { | ||
return; | ||
} | ||
return new Error(`console.log(式); // => 評価結果 にそろえてください | ||
該当コード: ${text}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters