Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

TypeError: Cannot read property 'toLowerCase' of undefined in utils.isLowerCase via variableNameRule when object destructuring syntax is invalid #3288

Closed
sompylasar opened this issue Oct 5, 2017 · 1 comment · Fixed by #3292

Comments

@sompylasar
Copy link

Bug Report

  • TSLint version: 5.7.0
  • TypeScript version: 2.3.4
  • Running TSLint via: Atom linter-tslint

TypeScript code being linted

const foo = { bar: 1 }
const { bar: } = foo;

with tslint.json configuration:

{
  "rules": {
    "semicolon": [
      true,
      "always"
    ],
    "class-name": true,
    "array-type": [
      true,
      "array"
    ],
    "curly": true,
    "cyclomatic-complexity": [
      true,
      20
    ],
    "only-arrow-functions": [
      true,
      "allow-named-functions"
    ],
    // "no-console": [
    //   true,
    //   "log",
    //   "error"
    // ],
    "no-var-keyword": true,
    // "no-require-imports": true,
    "triple-equals": [
      true,
      "allow-null-check"
    ],
    "indent": [
      true,
      "spaces"
    ],
    "eofline": true,
    "prefer-const": true,
    // "quotemark": [
    //   true,
    //   "double"
    // ],
    "whitespace": [
      true,
      "check-branch",
      "check-decl",
      "check-operator",
      "check-module",
      "check-separator",
      "check-type",
      "check-typecast"
    ],
    "variable-name": [
      true,
      "check-format",
      "allow-leading-underscore"
    ],
    "space-before-function-paren": [
      true,
      {
        "anonymous": "always",
        "named": "never",
        "asyncArrow": "always"
      }
    ],
    "no-trailing-whitespace": true,
    "no-consecutive-blank-lines": [
      true,
      2
    ],
    "max-line-length": [
      true,
      200
    ],
    "no-use-before-declare": true,
    "no-invalid-this": true,
    // "prefer-for-of": true,
    // "no-magic-numbers": true,
    "no-any": true,
    "member-access": true,
    "typedef": [
      true,
      // "variable-declaration",
      "property-declaration",
      "call-signature",
      "parameter",
      "member-variable-declaration"
    ],
    "no-unused-variable": [
      true,
      "check-parameters"
    ],
    "member-ordering": [
      true,
      {
        "order": [
          "public-instance-field",
          "protected-instance-field",
          "private-instance-field",
          "public-static-field",
          "protected-static-field",
          "private-static-field",
          "public-constructor",
          "protected-constructor",
          "private-constructor",
          "public-instance-method",
          "protected-instance-method",
          "private-instance-method",
          "public-static-method",
          "protected-static-method",
          "private-static-method"
        ]
      }
    ]
  }
}

Actual behavior

TypeScript says "identifier expected".
Atom console says:

TypeError: Cannot read property 'toLowerCase' of undefined
    at Object.isLowerCase (/Users/__PROJECTDIR__/node_modules/tslint/lib/utils.js:64:23)
    at isCamelCase (/Users/__PROJECTDIR__/node_modules/tslint/lib/rules/variableNameRule.js:169:46)
    at handleVariableNameFormat (/Users/__PROJECTDIR__/node_modules/tslint/lib/rules/variableNameRule.js:129:14)
    at cb (/Users/__PROJECTDIR__/node_modules/tslint/lib/rules/variableNameRule.js:111:21)
    at visitEachNode (/Users/__PROJECTDIR__/node_modules/typescript/lib/typescript.js:14785:30)
    at Object.forEachChild (/Users/__PROJECTDIR__/node_modules/typescript/lib/typescript.js:14861:21)
    at cb (/Users/__PROJECTDIR__/node_modules/tslint/lib/rules/variableNameRule.js:119:19)
    at visitEachNode (/Users/__PROJECTDIR__/node_modules/typescript/lib/typescript.js:14785:30)
    at Object.forEachChild (/Users/__PROJECTDIR__/node_modules/typescript/lib/typescript.js:14959:24)
    at walk (/Users/__PROJECTDIR__/node_modules/tslint/lib/rules/variableNameRule.js:86:15)
    at Rule.AbstractRule.applyWithFunction (/Users/__PROJECTDIR__/node_modules/tslint/lib/language/rule/abstractRule.js:39:9)
    at Rule.apply (/Users/__PROJECTDIR__/node_modules/tslint/lib/rules/variableNameRule.js:39:21)
    at Linter.applyRule (/Users/__PROJECTDIR__/node_modules/tslint/lib/linter.js:180:29)
    at /Users/__PROJECTDIR__/node_modules/tslint/lib/linter.js:122:85
    at Object.flatMap (/Users/__PROJECTDIR__/node_modules/tslint/lib/utils.js:151:29)
    at Linter.getAllFailures (/Users/__PROJECTDIR__/node_modules/tslint/lib/linter.js:122:32)
    at Linter.lint (/Users/__PROJECTDIR__/node_modules/tslint/lib/linter.js:76:33)
    at /Users/__USERDIR__/.atom/packages/linter-tslint/lib/worker.js:219:12
    at Generator.next (<anonymous>)
    at step (/Users/__USERDIR__/.atom/packages/linter-tslint/lib/worker.js:251:2)

Expected behavior

No error from the linter because the TypeScript syntax is invalid.

@sompylasar
Copy link
Author

name[0] is undefined for an empty string.

function isCamelCase(name, options) {
    var firstCharacter = name[0];
// ...
    if (!options.allowPascalCase && !utils_1.isLowerCase(firstCharacter)) {
        return false;
    }
// ...
}

https://github.com/palantir/tslint/blob/master/src/rules/variableNameRule.ts#L188-L199

For some reason the error only appears once per Atom load (reproduces again after "View > Developer > Reload Window").

adidahiya pushed a commit that referenced this issue Oct 19, 2017
[bugfix] `variable-name` fixed crash on empty variable name
Fixes: #3288
HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this issue Apr 9, 2018
[bugfix] `variable-name` fixed crash on empty variable name
Fixes: palantir#3288
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant