Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ESLint] Handle optional member chains #19275

Merged
merged 3 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1384,17 +1384,16 @@ const tests = {
errors: [
{
message:
// TODO: Ideally this would suggest props.foo?.bar.baz instead.
"React Hook useCallback has a missing dependency: 'props.foo.bar.baz'. " +
"React Hook useCallback has a missing dependency: 'props.foo?.bar.baz'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [props.foo.bar.baz]',
desc: 'Update the dependencies array to be: [props.foo?.bar.baz]',
output: normalizeIndent`
function MyComponent(props) {
useCallback(() => {
console.log(props.foo?.bar.baz);
}, [props.foo.bar.baz]);
}, [props.foo?.bar.baz]);
}
`,
},
Expand All @@ -1413,17 +1412,17 @@ const tests = {
errors: [
{
message:
// TODO: Ideally this would suggest props.foo?.bar?.baz instead.
"React Hook useCallback has a missing dependency: 'props.foo.bar.baz'. " +
"React Hook useCallback has a missing dependency: 'props.foo?.bar?.baz'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [props.foo.bar.baz]',
desc:
'Update the dependencies array to be: [props.foo?.bar?.baz]',
output: normalizeIndent`
function MyComponent(props) {
useCallback(() => {
console.log(props.foo?.bar?.baz);
}, [props.foo.bar.baz]);
}, [props.foo?.bar?.baz]);
}
`,
},
Expand All @@ -1442,17 +1441,16 @@ const tests = {
errors: [
{
message:
// TODO: Ideally this would suggest props.foo?.bar instead.
"React Hook useCallback has a missing dependency: 'props.foo.bar'. " +
"React Hook useCallback has a missing dependency: 'props.foo?.bar'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [props.foo.bar]',
desc: 'Update the dependencies array to be: [props.foo?.bar]',
output: normalizeIndent`
function MyComponent(props) {
useCallback(() => {
console.log(props.foo?.bar.toString());
}, [props.foo.bar]);
}, [props.foo?.bar]);
}
`,
},
Expand Down Expand Up @@ -2045,18 +2043,18 @@ const tests = {
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'history.foo'. " +
"React Hook useEffect has a missing dependency: 'history?.foo'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [history.foo]',
desc: 'Update the dependencies array to be: [history?.foo]',
output: normalizeIndent`
function MyComponent({ history }) {
useEffect(() => {
return [
history?.foo
];
}, [history.foo]);
}, [history?.foo]);
}
`,
},
Expand Down Expand Up @@ -6986,7 +6984,6 @@ const testsTypescript = {
],
},
{
// https://github.com/facebook/react/issues/19243
code: normalizeIndent`
function MyComponent() {
const pizza = {};
Expand All @@ -7000,22 +6997,122 @@ const testsTypescript = {
errors: [
{
message:
"React Hook useEffect has missing dependencies: 'pizza.crust' and 'pizza.toppings'. " +
"React Hook useEffect has missing dependencies: 'pizza.crust' and 'pizza?.toppings'. " +
'Either include them or remove the dependency array.',
suggestions: [
{
// TODO the description and suggestions should probably also
// preserve the optional chaining.
desc:
'Update the dependencies array to be: [pizza.crust, pizza.toppings]',
'Update the dependencies array to be: [pizza.crust, pizza?.toppings]',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: technically this could be further improved to not add ? at all because another dep didn't use it. However, I don't think it matters because at least this doesn't cause a crash. Additionally, this points your attention to unnecessary ? inside the function — since presumably if data is immutable, you shouldn't need it anyway.

output: normalizeIndent`
function MyComponent() {
const pizza = {};

useEffect(() => ({
crust: pizza.crust,
toppings: pizza?.toppings,
}), [pizza.crust, pizza.toppings]);
}), [pizza.crust, pizza?.toppings]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const pizza = {};

useEffect(() => ({
crust: pizza?.crust,
density: pizza.crust.density,
}), []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'pizza.crust'. " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want this to explain why only one is included with something like:

React Hook useEffect has a missing dependencies: 'pizza?.crust' and 'pizza.crust' (which can be reduced to a dependency on 'pizza.crust'). Either include it or remove the dependency array.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh. I don't think this is useful enough to clutter the message.

'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [pizza.crust]',
output: normalizeIndent`
function MyComponent() {
const pizza = {};

useEffect(() => ({
crust: pizza?.crust,
density: pizza.crust.density,
}), [pizza.crust]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const pizza = {};

useEffect(() => ({
crust: pizza.crust,
density: pizza?.crust.density,
}), []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'pizza.crust'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [pizza.crust]',
output: normalizeIndent`
function MyComponent() {
const pizza = {};

useEffect(() => ({
crust: pizza.crust,
density: pizza?.crust.density,
}), [pizza.crust]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const pizza = {};

useEffect(() => ({
crust: pizza?.crust,
density: pizza?.crust.density,
}), []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'pizza?.crust'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [pizza?.crust]',
output: normalizeIndent`
function MyComponent() {
const pizza = {};

useEffect(() => ({
crust: pizza?.crust,
density: pizza?.crust.density,
}), [pizza?.crust]);
}
`,
},
Expand Down
Loading