Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript import handle undefined

Is it possible to catch typos like in switch case example statement below?

Preferred way would be that eslinter report an warning/error. Currently adding toString() to const can be used to raise an TypeError in runtime if undefined.

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;
  }
}

UPDATE:

As @nikc.org answered eslint-plugin-import with namespace option can be used for linting such bugs.

Here is small repository with configuration and demo:

https://github.com/bmihelac/test-js-import-undefined/tree/eslint-plugin-import

Relevant part of eslint config is:

"plugins": ["import"],
"rules": {
  "import/namespace": [2],
like image 679
bmihelac Avatar asked Nov 10 '22 01:11

bmihelac


1 Answers

Disclaimer, I'm the autor of tern-lint.

I suggest you that you use tern-lint which is able to report errors like "Unknown property".

You can use this linter with command or with an editor which supports it (Emacs, Atom, CodeMirror or Eclipse). Here a screenshot with Eclipse tern.java

enter image description here

like image 60
Angelo Avatar answered Nov 15 '22 06:11

Angelo