From 5959b5fb21107cb63daef6fa5c95e921ebf9cd1f Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Sun, 1 Mar 2020 21:55:12 +0800 Subject: [PATCH] [fix] `prefer-stateless-function`: avoid crash on ts empty constructor Fixes #2187 --- lib/rules/prefer-stateless-function.js | 1 + tests/lib/rules/prefer-stateless-function.js | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/rules/prefer-stateless-function.js b/lib/rules/prefer-stateless-function.js index ee7e6a9897..e128b2d6bf 100644 --- a/lib/rules/prefer-stateless-function.js +++ b/lib/rules/prefer-stateless-function.js @@ -186,6 +186,7 @@ module.exports = { const contextTypes = name === 'contextTypes'; const defaultProps = name === 'defaultProps'; const isUselessConstructor = property.kind === 'constructor' && + !!property.value.body && isRedundantSuperCall(property.value.body.body, property.value.params); const isRender = name === 'render'; return !isDisplayName && !isPropTypes && !contextTypes && !defaultProps && !isUselessConstructor && !isRender; diff --git a/tests/lib/rules/prefer-stateless-function.js b/tests/lib/rules/prefer-stateless-function.js index e6a151f57b..7f7e41bb06 100644 --- a/tests/lib/rules/prefer-stateless-function.js +++ b/tests/lib/rules/prefer-stateless-function.js @@ -157,6 +157,18 @@ ruleTester.run('prefer-stateless-function', rule, { } } ` + }, { + // Issue 2187 + code: ` + class Foo extends React.Component { + constructor(props) + + render() { + return
{this.props.foo}
; + } + } + `, + parser: parsers.TYPESCRIPT_ESLINT }, { // Use this.bar code: `