Repository: eslint/eslint (js)

Issue:
sort-keys: Cannot set property 'prevName' of null at SpreadElement
<!--
    ESLint adheres to the [JS Foundation Code of Conduct](https://js.foundation/community/code-of-conduct).

    This template is for bug reports. If you are here for another reason, please see below:

    1. To propose a new rule: https://eslint.org/docs/developer-guide/contributing/new-rules
    2. To request a rule change: https://eslint.org/docs/developer-guide/contributing/rule-changes
    3. To request a change that is not a bug fix, rule change, or new rule: https://eslint.org/docs/developer-guide/contributing/changes
    4. If you have any questions, please stop by our chatroom: https://gitter.im/eslint/eslint

    Note that leaving sections blank will make it difficult for us to troubleshoot and we may have to close the issue.
-->

**Tell us about your environment**

* **ESLint Version:** 5.14.0
* **Node Version:** 10
* **npm Version:** 

**What parser (default, Babel-ESLint, etc.) are you using?**

`babel-eslint`

**Please show your full configuration:**

<details>
<summary>Configuration</summary>

<!-- Paste your configuration below: -->
```js

```

</details>

**What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.**

<!-- Paste the source code below: -->
```ts
import UntypedJed from 'jed';
import { TranslatorConfig } from './types';

interface Jed {
  translate(input: string): Jed;
  ifPlural(value: number, plural: string): Jed;
  fetch(...args: any[]): string;
}

const DEFAULT_LANGUAGE_PACK = {
  domain: 'superset',
  locale_data: {
    superset: {
      '': {
        domain: 'superset',
        lang: 'en',
        plural_forms: 'nplurals=1; plural=0',
      },
    },
  },
};

export default class Translator {
  i18n: Jed;

  constructor(config: TranslatorConfig = {}) {
    const { languagePack = DEFAULT_LANGUAGE_PACK } = config;
    this.i18n = new UntypedJed(languagePack) as Jed;
  }

  translate(input: string, ...args: any[]): string {
    return this.i18n.translate(input).fetch(...args);
  }

  translateWithNumber(singular: string, plural: string, num: number = 0, ...args: any[]): string {
    return this.i18n
      .translate(singular)
      .ifPlural(num, plural)
      .fetch(...args);
  }
}
```

<!-- Paste the command you used to run ESLint: -->
```bash
beemo eslint "./packages/superset-ui-translation/{src,test,storybook}/**/Translator.{js,jsx,ts,tsx}"
```

**What did you expect to happen?**

Lint completes successfully. It works fine with `eslint@5.13.0`

**What actually happened? Please include the actual, raw output from ESLint.**

```
TypeError: Cannot set property 'prevName' of null
    at SpreadElement (/Users/krist_wongsuphasawat/workspace/superset-ui/node_modules/eslint/lib/rules/sort-keys.js:133:32)
    at listeners.(anonymous function).forEach.listener (/Users/krist_wongsuphasawat/workspace/superset-ui/node_modules/eslint/lib/util/safe-emitter.js:45:58)
    at Array.forEach (<anonymous>)
    at Object.emit (/Users/krist_wongsuphasawat/workspace/superset-ui/node_modules/eslint/lib/util/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/Users/krist_wongsuphasawat/workspace/superset-ui/node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors (/Users/krist_wongsuphasawat/workspace/superset-ui/node_modules/eslint/lib/util/node-event-generator.js:280:22)
    at NodeEventGenerator.enterNode (/Users/krist_wongsuphasawat/workspace/superset-ui/node_modules/eslint/lib/util/node-event-generator.js:294:14)
    at CodePathAnalyzer.enterNode (/Users/krist_wongsuphasawat/workspace/superset-ui/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:632:23)
    at nodeQueue.forEach.traversalInfo (/Users/krist_wongsuphasawat/workspace/superset-ui/node_modules/eslint/lib/linter.js:750:28)
    at Array.forEach (<anonymous>)
```

See more at Travis
https://travis-ci.com/apache-superset/superset-ui/builds/101145033

**Are you willing to submit a pull request to fix this bug?**

Just posted #11403 

Pull request:
Fix: sort-keys throws Error at SpreadElement (fixes #11402)
<!--
    ESLint adheres to the [JS Foundation Code of Conduct](https://js.foundation/community/code-of-conduct).
-->

**What is the purpose of this pull request? (put an "X" next to item)**

[ ] Documentation update
[x] Bug fix ([template](https://raw.githubusercontent.com/eslint/eslint/master/templates/bug-report.md))
[ ] New rule ([template](https://raw.githubusercontent.com/eslint/eslint/master/templates/rule-proposal.md))
[ ] Changes an existing rule ([template](https://raw.githubusercontent.com/eslint/eslint/master/templates/rule-change-proposal.md))
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

<!--
    If the item you've checked above has a template, please paste the template questions below and answer them. (If this pull request is addressing an issue, you can just paste a link to the issue here instead.)
-->

<!--
    Please ensure your pull request is ready:

    - Read the pull request guide (https://eslint.org/docs/developer-guide/contributing/pull-requests)
    - Include tests for this change
    - Update documentation for this change (if appropriate)
-->

<!--
    The following is required for all pull requests:
-->

**What changes did you make? (Give an overview)**

Fix #11402 

**Is there anything you'd like reviewers to focus on?**




Patch:
diff --git a/lib/rules/sort-keys.js b/lib/rules/sort-keys.js
index 08bfcdf3d..fd5dd8a13 100644
--- a/lib/rules/sort-keys.js
+++ b/lib/rules/sort-keys.js
@@ -129,8 +129,10 @@ module.exports = {
                 stack = stack.upper;
             },
 
-            SpreadElement() {
-                stack.prevName = null;
+            SpreadElement(node) {
+                if (node.parent.type === "ObjectExpression") {
+                    stack.prevName = null;
+                }
             },
 
             Property(node) {


Test patch:
diff --git a/tests/lib/rules/sort-keys.js b/tests/lib/rules/sort-keys.js
index 3106d3146..1ae091fad 100644
--- a/tests/lib/rules/sort-keys.js
+++ b/tests/lib/rules/sort-keys.js
@@ -46,6 +46,10 @@ ruleTester.run("sort-keys", rule, {
         { code: "var obj = {a:1, b:1, ...z}", options: [], parserOptions: { ecmaVersion: 2018 } },
         { code: "var obj = {...z, ...x, a:1, ...c, ...d, f:5, e:4}", options: ["desc"], parserOptions: { ecmaVersion: 2018 } },
 
+        // works when spread occurs somewhere other than an object literal
+        { code: "function fn(...args) { return [...args].length; }", options: [], parserOptions: { ecmaVersion: 2018 } },
+        { code: "function g() {}; function f(...args) { return g(...args); }", options: [], parserOptions: { ecmaVersion: 2018 } },
+
         // ignore destructuring patterns.
         { code: "let {a, b} = {}", options: [], parserOptions: { ecmaVersion: 6 } },