【发布时间】:2015-11-19 16:52:14
【问题描述】:
是否可以在下面的 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 和namespace 选项可用于检查此类错误。
这是带有配置和演示的小型存储库:
https://github.com/bmihelac/test-js-import-undefined/tree/eslint-plugin-import
eslint 配置的相关部分是:
"plugins": ["import"],
"rules": {
"import/namespace": [2],
【问题讨论】:
-
好吧,试图捕捉这样的错误的问题在于它们不一定是错误。换句话说,考虑到 JavaScript 的规则,代码没有任何错误。
-
@Pointy 因为 es6 模块是静态的,我希望一些静态分析器可以找到这样的错别字。
-
对,聪明的静态分析可能至少会注意到没有拼写错误的常量。
标签: javascript node.js es6-module-loader