question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-nested-ternary: Add option to traverse deeper than one level

See original GitHub issue

Tell us about your environment

  • ESLint Version: 4.19.1
  • Node Version: v9.6.1
  • npm Version: 5.6.0

What parser (default, Babel-ESLint, etc.) are you using? babel-eslint Please show your full configuration:

Configuration
{
  "root": true,

  "plugins": [
    "react"
  ],

  "extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "prettier",
    "prettier/react"
  ],

  "env": {
    "es6": true
  },

  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 6,
    "ecmaFeatures": {
      "jsx": true
    },
    "sourceType": "script"
  },

  "rules": {
    // Disabled, May conflict with Prettier
    "quotes": [0, "single", { "allowTemplateLiterals": true }],
    "max-len": [0, { "code": 100, "ignoreUrls": true }],
    "arrow-parens": [0, "as-needed"],
    "arrow-spacing": 0,
    "brace-style": [0, "1tbs", { "allowSingleLine": false }],
    "eol-last": 0,
    "indent": [0, 2, { "SwitchCase": 1 }],
    "key-spacing": [0, { "mode": "strict" }],
    "keyword-spacing": 0,
    "no-extra-semi": 0,
    "no-mixed-spaces-and-tabs": 0,
    "no-multi-spaces": [0, {
      "exceptions": {
        "VariableDeclarator": true,
        "ImportDeclaration": true,
        "ImportSpecifier": true,
        "AssignmentExpression": true,
        "ClassProperty": true
      }
    }],
    "no-multiple-empty-lines": [0, { "max": 1 }],
    "no-trailing-spaces": 0,
    "object-curly-spacing": [0, "always"],
    "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
    "semi": [0, "always"],
    "semi-spacing": [0, {"before": false, "after": true}],
    "semi-style": [0, "last"],
    "space-before-blocks": [0, "always"],
    "switch-colon-spacing": [0, {"before": false, "after": true}],
    "func-call-spacing": [0, "never"],
    "function-paren-newline": [0, "multiline"],

    "react/jsx-closing-bracket-location": [0, "line-aligned"],
    "react/jsx-closing-tag-location": 0,
    "react/jsx-equals-spacing": [0, "never"],
    "react/jsx-indent": [0, 2],
    "react/jsx-tag-spacing": [0, {
      "closingSlash": "never",
      "beforeSelfClosing": "always",
      "afterOpening": "never"
    }],
    "react/jsx-wrap-multilines": [0, {
      "declaration": false,
      "assignment": false,
      "return": true
    }],

    // Warning
    "no-console": 1,
    "no-debugger": 1,
    "no-undef": 1,
    "no-unreachable": 1,
    "no-unused-vars": [1, { "vars": "all", "args": "after-used" }],
    "newline-after-var": [1, "always"],
    "newline-before-return": 1,
    "prefer-arrow-callback": 1,
    "prefer-const": 1,
    "no-multi-assign": 1,
    "no-warning-comments": [1, { "terms": ["fixme", "todo", "hack", "review", "xxx"] }],
    "no-useless-constructor": 1,

    "linebreak-style": [1, "unix"],
    "no-else-return": 1,
    "curly": [1, "all"],
    "no-inline-comments": 1,
    "spaced-comment": [1, "always", { "exceptions": ["-"] }],
    "no-nested-ternary": 1,
    "no-unneeded-ternary": [1, { "defaultAssignment": false }],
    "no-var": 1,
    "eqeqeq": [1, "always"],
    "no-return-assign": 1,
    "no-multi-str": 1,
    "padding-line-between-statements": [
      1,
      { "blankLine": "always", "prev": "*", "next": "return" },
      { "blankLine": "always", "prev": ["const", "let"], "next": "*"},
      { "blankLine": "any",    "prev": ["const", "let"], "next": ["const", "let"]}
    ],

    // React

    // Disabled - Should be fixed and set as warning
    "react/no-deprecated": 0,
    "react/display-name": 0,
    "react/react-in-jsx-scope": 0,
    "react/forbid-component-props": [0, { "forbid": ["className", "style"] }],
    "react/no-is-mounted": 0,

    // Disabled
    "react/forbid-elements": 0,
    "react/forbid-prop-types": [0, { "forbid": ["any", "array", "object"] }],
    "react/no-array-index-key": 0,
    "react/no-danger": 0,
    "react/no-set-state": 0,
    "react/prop-types": 0,
    "react/require-default-props": 0,
    "react/sort-prop-types": 0,
    "react/require-optimization": 0,
    "react/style-prop-object": 0,

    "react/jsx-curly-spacing": [0, "always", { "allowMultiline": true }],
    "react/jsx-first-prop-new-line": 0,
    "react/jsx-handler-names": 0,
    "react/jsx-indent-props": [0, 2],
    "react/jsx-key": 0,
    "react/jsx-max-props-per-line": 0,
    "react/jsx-no-comment-textnodes": 0,
    "react/jsx-no-literals": 0,
    "react/no-unescaped-entities": 0,
    "react/jsx-sort-props": 0,

    // Warning
    "react/default-props-match-prop-types": 1,
    "react/forbid-foreign-prop-types": 1,
    "react/no-children-prop": 1,
    "react/no-danger-with-children": 1,
    "react/no-did-mount-set-state": 1,
    "react/no-did-update-set-state": 1,
    "react/no-will-update-set-state": 1,
    "react/no-multi-comp": [1, { "ignoreStateless": true }],
    "react/no-redundant-should-component-update": 1,
    "react/no-string-refs": 1,
    "react/no-unused-prop-types": 1,
    "react/prefer-es6-class": [1, "always"],
    "react/prefer-stateless-function": [1, { "ignorePureComponents": true }],
    "react/self-closing-comp": 1,
    "react/sort-comp": [1, {
      "order": [
        "static-methods",
        "lifecycle",
        "/^on.+$/",
        "/^(get|set)(?!(InitialState$|DefaultProps$|ChildContext$)).+$/",
        "everything-else",
        "/^render.+$/",
        "render"
      ]
    }],
    "react/void-dom-elements-no-children": 1,

    "react/jsx-boolean-value": [1, "never"],
    "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
    "react/jsx-no-bind": [1, {
      "ignoreRefs": true,
      "allowArrowFunctions": true
    }],
    "react/jsx-no-target-blank": 1,
    "react/jsx-pascal-case": 1
  },

  // Override config (only applied to files that match the given globs)
  "overrides": [
    // Server only (all JS files except Client)
    {
      "files": ["**/*.js"],
      "excludedFiles": ["assets/**/*.js"],
      "env": {
        "node": true
      },
      "rules": {
        "strict": [1, "global"]
      }
    },

    // Client only
    {
      "files": ["assets/js/**/*.{js,jsx}"],
      "env": {
        "browser": true,
        "amd": true
      },
      "rules": {
        "strict": [1, "never"]
      }
    },

    // Client only test files
    {
      "files": ["assets/js/**/_*.{js,jsx}", "**/*.test.js"],
      "env": {
        "mocha": true,
        "jasmine": true,
        "qunit": true
      }
    }
  ]
}

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

const nestedTernary =
  global.foo === 0
    ? 'foo is 0, and '
    : 'foo is not 0, and ' + (global.bar === 0 ? 'bar is 0' : 'bar is not 0');
eslint temp.js

What did you expect to happen? I expected ESLint to warn me about the nested ternary What actually happened? Please include the actual, raw output from ESLint. Did not warn, it didn’t catch the nested ternary when it’s surrounded by braces and part of a statement.

Proposal

Based on the comments, maybe it’s best to add an option to traverse the tree deeper than only one level.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
eslint-deprecated[bot]commented, Dec 11, 2018

Unfortunately, it looks like there wasn’t enough interest from the team or community to implement this change. While we wish we’d be able to accommodate everyone’s requests, we do need to prioritize. We’ve found that issues failing to reach accepted status after 21 days tend to never be accepted, and as such, we close those issues. This doesn’t mean the idea isn’t interesting or useful, just that it’s not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

1reaction
kaicataldocommented, Jul 24, 2018

It feels off to me that it would look arbitrarily deep - @not-an-aardvark’s example is a good one. Disallowing the code in that example also doesn’t feel like it’s staying true to the spirit of the rule, since the ternary in the function doesn’t create any confusion with the outer ternary.

Could it stop looking when a new scope is created?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to traverse in a tree? - AfterAcademy
You will go from index 0 to index N-1 and thereby, traverse all elements of the array. But what if you're given the...
Read more >
Diff - 402e1b6e55e9041dfd1a93580e45e5c5dba1db55^!
diff --git a/LICENSE b/LICENSE new file mode 100644 index ... Docs: Default option from `operator-linebreak` is `after`and not always ...
Read more >
vis-network.min.js.map - UNPKG
node_modules/core-js-pure/internals/create-iterator-constructor.js",". ... we find a visible item, the code that uses\r\n * this function will then iterate ...
Read more >
eslint | Yarn - Package Manager
... 075ef2c feat: add suggestion for no-return-await (#16637) (Daniel Bartholomae); ba74253 chore: standardize npm script names per #14827 (#16315) (Patrick ...
Read more >
Binary tree: Level Order Traversal - YouTube
See complete series on data structures here:http://www.youtube.com/playlist?list=PL2_aWCzGMAwI3W_JlcBbtYTwiQSsOTa6PIn this lesson, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found