以下の switch case の例のステートメントのようにタイプミスを見つけることは可能ですか?
推奨される方法は、eslinter が警告/エラーを報告することです。現在、未定義の場合、実行時に を発生させるために追加toString()
を使用できます。const
TypeError
actionTypes.js
export const UPDATE_REQUEST = 'UPDATE_REQUEST';
reducer.js
import * as types from '../constants/actionTypes';
export default function pouchdbReducer(state = {}, action) {
switch (action.type) {
case types.UPDDATE_REQUEST:
// there is a typo above and it evaluates to `undefined`
// this code would never be reached - how to make it an error
return Object.assign({}, state, {updated: true});
default:
return state;
}
}
アップデート:
@nikc.org が答えたように、名前空間オプションを指定したeslint-plugin-importは、そのようなバグをリンティングするために使用できます。
構成とデモを含む小さなリポジトリは次のとおりです。
https://github.com/bmihelac/test-js-import-undefined/tree/eslint-plugin-import
eslint config の関連部分は次のとおりです。
"plugins": ["import"],
"rules": {
"import/namespace": [2],