standard / standard

🌟 JavaScript Style Guide, with linter & automatic code fixer
https://standardjs.com
MIT License
29.13k stars 2.33k forks source link

Cannot read property 'name' of null #1055

Closed adyshimony closed 6 years ago

adyshimony commented 6 years ago

I am getting this error only on one file. Removing the try block solves the issue.

ERROR:

> standard "./rest-api/src/routes/api/accountTx.ts"

standard: Unexpected linter output:

TypeError: Cannot read property 'name' of null
    at checkForViolation (/home/adys/Sources/supernode/node_modules/eslint/lib/rules/no-shadow-restricted-names.js:33:39)
    at EventEmitter.CatchClause (/home/adys/Sources/supernode/node_modules/eslint/lib/rules/no-shadow-restricted-names.js:64:17)
    at EventEmitter.emit (events.js:165:20)
    at NodeEventGenerator.applySelector (/home/adys/Sources/supernode/node_modules/eslint/lib/util/node-event-generator.js:265:26)
    at NodeEventGenerator.applySelectors (/home/adys/Sources/supernode/node_modules/eslint/lib/util/node-event-generator.js:294:22)
    at NodeEventGenerator.enterNode (/home/adys/Sources/supernode/node_modules/eslint/lib/util/node-event-generator.js:308:14)
    at CodePathAnalyzer.enterNode (/home/adys/Sources/supernode/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:602:23)
    at CommentEventGenerator.enterNode (/home/adys/Sources/supernode/node_modules/eslint/lib/util/comment-event-generator.js:98:23)
    at Traverser.enter (/home/adys/Sources/supernode/node_modules/eslint/lib/eslint.js:929:36)
    at Traverser.__execute (/home/adys/Sources/supernode/node_modules/estraverse/estraverse.js:397:31)

CODE that cause the issue:

import * as express from 'express'
import * as logger from 'winston'
import * as dbUtils from '../../utils/dbUtils'

const router = express.Router()
// return all transactions history for account address
router.get('/:address', async (req, res, next) => {
  try {
    let result = await dbUtils.getAccountAsync(req.params.address)
    return res.json(
    {
      'status': '1',
      'message': 'OK',
      'result': result
    })
  } catch {
      return res.json(
      {
        'status': '0',
        'message': 'ERROR',
        'result': 'not found'
      })
  }
})

module.exports = router

Remark the block:

import * as express from 'express'
import * as logger from 'winston'
import * as dbUtils from '../../utils/dbUtils'

const router = express.Router()
// return all transactions history for account address
router.get('/:address', async (req, res, next) => {
  //try {  <-------------------- here
    let result = await dbUtils.getAccountAsync(req.params.address)
    return res.json(
    {
      'status': '1',
      'message': 'OK',
      'result': result
    })
  //} catch {  <-------------------- here
      return res.json(
      {
        'status': '0',
        'message': 'ERROR',
        'result': 'not found'
      })
  //}  <-------------------- and here
})

module.exports = router

And everything is working now.

> supernode-rest-api@1.0.0 standard-rest-api /home/adys/Sources/supernode
> standard "./rest-api/src/routes/api/accountTx.ts"

standard: Use JavaScript Standard Style (https://standardjs.com)
standard: Run `standard --fix` to automatically fix some problems.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:2:13: 'logger' is defined but never used.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:9:5: Expected indentation of 2 spaces but found 4.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:10:5: Expected indentation of 2 spaces but found 4.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:11:5: Expected indentation of 6 spaces but found 4.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:12:7: Expected indentation of 8 spaces but found 6.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:13:7: Expected indentation of 8 spaces but found 6.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:14:7: Expected indentation of 8 spaces but found 6.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:15:5: Expected indentation of 6 spaces but found 4.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:17:7: Expected indentation of 2 spaces but found 6.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:17:7: Unreachable code.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:18:7: Expected indentation of 8 spaces but found 6.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:19:9: Expected indentation of 10 spaces but found 8.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:20:9: Expected indentation of 10 spaces but found 8.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:21:9: Expected indentation of 10 spaces but found 8.
  /home/adys/Sources/supernode/rest-api/src/routes/api/accountTx.ts:22:7: Expected indentation of 8 spaces but found 6.

standard config in package.json:

"standard": { "parser": "typescript-eslint-parser", "plugins": [ "typescript" ] }

Not sure if its elint or standard, opening here too: https://github.com/eslint/typescript-eslint-parser/issues/439

stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

feross commented 6 years ago

Copying from the linked issue, this is the workaround:

ESLint doesn’t support catch clauses without a parameter, as they’re not yet part of JavaScript. Using catch (_) or similar instead should fix the issue.

Mentioned in this comment: https://github.com/eslint/typescript-eslint-parser/issues/439#issuecomment-361409152