Javascript import handle undefined

Is it possible to catch typos, as in the example case switch example?

The preferred way is that eslinter reports a warning / error. Currently, adding toString() to const can be used to raise a TypeError at 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 can be used to sort through such errors.

Here is a small repository with configuration and demo:

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

The relevant part of eslint configuration:

 "plugins": ["import"], "rules": { "import/namespace": [2], 
+5
source share
3 answers

Disclaimer, I am the author of tern-lint .

I suggest you use tern-lint , which can report errors like "Unknown property".

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

enter image description here

+1
source

The eslint-plugin-import plugin for ESLint performs static analysis on import and export and can be configured to raise errors or warnings by various criteria, including the case in your question where you mistakenly wrote a link to an imported symbol.

To completely fix the problem, you could take a look at Tern , which gives you an idea of ​​codes and code completion in several editors, avoiding spelling errors in general.

0
source

I believe that depending on the number of types of actions (and if you are ready to break them down), you can slightly change the way they are imported.

Maybe something like:

 import {TYPE_ONE, TYPE_TWO} from '../constants/firstActionTypes'; import {TYPE_THREE} from '../constants/secondActionTypes'; export default function pouchdbReducer(state = {}, action) { switch (action.type) { case TTYPE_ONE: // Linting should identify this easily now return Object.assign({}, state, {updated: true}); default: return state; } } 

Typical lints can easily pick this up, and a side benefit is that you can break down your actions into your problems.

0
source

Source: https://habr.com/ru/post/1236353/


All Articles