Skip to content

[camelcase] False positive with optional chaining #1196

@thomasgazzoni

Description

@thomasgazzoni

Hi guys,

I updated my project to latest typescript and eslint-plugin (2.7.0) version to use the new optional chaining operator and I encounter a problem when enforcing the CamelCase rule. Below a I am posting an example:

interface IData {
  snake_id?: number;
  user_snake?: {
    first_name: string;
    last_name: string;
  };
  userCamel?: {
    firstName: string;
    lastName: string;
    snake_name: string;
  };
}

const data: IData = {};

// RIGHT: No problem when accessing CamelCase fields
console.log('CamelFirstName:', data.userCamel?.firstName);

// RIGHT: No problem when accessing snake_case's fields methods
console.log('CamelFirstName:', data.snake_id?.toString());

// WRONG: Error when accessing snake_case fields after optional chaining operator
console.log('snake_first_name:', data.user_snake?.first_name);
console.log('snake_first_name:', data.userCamel?.snake_name);

Expected Result

Accessing snake_case fields of an object with the rule "@typescript-eslint/camelcase":2 should not throw an linting error.

Actual Result

Lint error when accessing an object's fields if they are in snake_case

Repro

{
  "extends": [
    "airbnb-typescript",
    "plugin:@typescript-eslint/recommended",
    "prettier",
    "prettier/react",
    "prettier/@typescript-eslint",
    "prettier/babel",
    "prettier/standard",
  ],
  "rules": {
    "@typescript-eslint/camelcase": 2,
  }
}

Versions

package version
@typescript-eslint/eslint-plugin 2.7.0
@typescript-eslint/parser 2.7.0
TypeScript 3.7.2
ESLint 6.6.0
node 10.16.0
npm 6.9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomershas prthere is a PR raised to close thispackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy