Skip to content

TSMethodSignature and TypeError: Cannot read property 'length' of undefined #323

Closed
@JustFly1984

Description

@JustFly1984

What code were you trying to parse?

import { connect } from 'react-redux'
import clsx from 'clsx'

import { sanitizeZip } from '../../utils/sanitize-zip'

import {
  changeInput,
  validateInput
} from '../../redux/actions/fields'

import InputCustom from '../../components/fields/input-custom'

import grid from '../../styles/grid.module.css'
import fields from '../../styles/fields.module.css'

import {
  ReduxState,
  Dispatch,
  OnOff,
  TImmutableInput,
  TImmutablePaths
} from '../../types/common'

interface StateProps {
  id: string,
  name: string,
  type: string
  pattern: string,
  inputMode: string,
  labelText: string,
  placeholder: string,
  autoComplete: OnOff,
  autoCapitalize: OnOff
  autoCorrect: OnOff
  noValidate: boolean,
  className: string,
  params: TImmutableInput
}

interface DispatchProps {
  onChange(value: string, path: TImmutablePaths): void
  onBlur(value: string, path: TImmutablePaths): void
}

const mapStateToProps = (state: ReduxState): StateProps => ({
  id: 'zip',
  name: 'postal-code',
  type: 'text',
  pattern: '[0-9]*',
  inputMode: 'numeric',
  labelText: 'ZIP code *',
  placeholder: '12345',
  autoComplete: 'on' as OnOff,
  autoCapitalize: 'off' as OnOff,
  autoCorrect: 'off' as OnOff,
  noValidate: true,
  className: clsx(
    fields.field,
    grid.colMd1
  ),
  params: state
    .getIn(['fields', 'userCart', 'zip'])
})

const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
  onChange: (value, path) => {
    dispatch(
      changeInput({
        path,
        value: sanitizeZip(value)
      })
    )
  },
  onBlur: (value, path) => {
    dispatch(
      validateInput({
        path,
        value: sanitizeZip(value)
      })
    )
  }
})

const InputZip = connect(
  mapStateToProps,
  mapDispatchToProps
)(InputCustom)

export default InputZip

What did you expect to happen?
it should not crash eslint
What actually happened?

Versions

package version
@typescript-eslint/parser 1.4.2
TypeScript 3.3.3333
ESLint 5.14.1
node 11.7.0
npm 6.8.0

eslint --debug output:

  eslint:linter An error occurred while traversing +186ms
  eslint:linter Filename: /Users/justfly/projects/agrarian-gatsby/src/account/cart-common/input-zip.ts +0ms
  eslint:linter Parser Options: { ecmaVersion: 6,
  ecmaFeatures: { globalReturn: false, jsx: true, modules: true },
  sourceType: 'module',
  useJSXTextNode: true,
  project: './tsconfig.json',
  tsconfigRootDir: './' } +0ms
  eslint:linter Parser Path: /Users/justfly/projects/agrarian-gatsby/node_modules/@typescript-eslint/parser/dist/parser.js +1ms
  eslint:linter Settings: { react: { version: '16.8.3' },
  'import/resolver': { typescript: { directory: './tsconfig.json' } } } +0ms
TypeError: Cannot read property 'length' of undefined
    at nthChild (/Users/justfly/projects/agrarian-gatsby/node_modules/esquery/esquery.js:259:34)
    at matches (/Users/justfly/projects/agrarian-gatsby/node_modules/esquery/esquery.js:158:25)
    at matches (/Users/justfly/projects/agrarian-gatsby/node_modules/esquery/esquery.js:76:30)
    at Function.matches (/Users/justfly/projects/agrarian-gatsby/node_modules/esquery/esquery.js:109:25)
    at NodeEventGenerator.applySelector (/Users/justfly/projects/agrarian-gatsby/node_modules/eslint/lib/util/node-event-generator.js:250:21)
    at NodeEventGenerator.applySelectors (/Users/justfly/projects/agrarian-gatsby/node_modules/eslint/lib/util/node-event-generator.js:278:22)
    at NodeEventGenerator.enterNode (/Users/justfly/projects/agrarian-gatsby/node_modules/eslint/lib/util/node-event-generator.js:294:14)
    at CodePathAnalyzer.enterNode (/Users/justfly/projects/agrarian-gatsby/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:632:23)
    at nodeQueue.forEach.traversalInfo (/Users/justfly/projects/agrarian-gatsby/node_modules/eslint/lib/linter.js:750:28)
    at Array.forEach (<anonymous>)

esquery.js:

function nthChild(node, ancestry, idxFn) {
            var parent = ancestry[0], listProp, keys, i, l, idx;
            if (!parent) { return false; }
            keys = estraverse.VisitorKeys[parent.type]; 
            console.log('keys: ', keys) // => undefined
            console.log('parent.type: ', parent.type) // => TSMethodSignature
            for (i = 0, l = keys.length; i < l; ++i) {
                listProp = parent[keys[i]];
                if (isArray(listProp)) {
                    idx = listProp.indexOf(node);
                    if (idx >= 0 && idx === idxFn(listProp.length)) { return true; }
                }
            }
            return false;
        }

Did I miss something with my TS setup?

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting responseIssues waiting for a reply from the OP or another partypackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginquestionQuestions! (i.e. not a bug / enhancment / documentation)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions