-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fix #3908: mobx/missing-observer rule false positive with forwardRef #3909
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"eslint-plugin-mobx": patch | ||
--- | ||
|
||
mobx/missing-observer rule false positive with forwardRef #3908 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,40 @@ | ||
/* eslint mobx/missing-observer: "error" */ | ||
|
||
function Named() { } | ||
const foo = function Named() { } | ||
const Anonym = function () { }; | ||
const Arrow = () => { }; | ||
function Named() {} | ||
const named = function Named() {} | ||
const namedRef = forwardRef(function Named() {}) | ||
const Anonym = function () {} | ||
const AnonymRef = forwardRef(function () {}) | ||
const Arrow = () => {} | ||
const ArrowRef = forwardRef(() => {}) | ||
|
||
observer(function Named() { }); | ||
const foo = observer(function Named() { }) | ||
const Anonym = observer(function () { }); | ||
const Arrow = observer(() => { }); | ||
observer(function Named() {}) | ||
observer(forwardRef(function Named() {})) | ||
const namedObs = observer(function Named() {}) | ||
const namedRefObs = observer(forwardRef(function Named() {})) | ||
const AnonymObs = observer(function () {}) | ||
const AnonymRefObs = observer(forwardRef(function () {})) | ||
const ArrowObs = observer(() => {}) | ||
const ArrowRefObs = observer(forwardRef(() => {})) | ||
|
||
function notCmp() { } | ||
const notCmp = function notCmp() { } | ||
const notCmp = function () { } | ||
const notCmp = () => { } | ||
function notCmp() {} | ||
const notCmp = function notCmp() {} | ||
const notCmp = function () {} | ||
const notCmp = () => {} | ||
const notCmp = forwardRef(() => {}) | ||
|
||
class Cmp extends React.Component { } | ||
class Cmp extends Component { } | ||
const Cmp = class extends React.Component { } | ||
const Cmp = class extends Component { } | ||
class extends Component { } | ||
class extends React.Component { } | ||
class Cmp extends React.Component {} | ||
class Cmp extends Component {} | ||
const Cmp = class extends React.Component {} | ||
const Cmp = class extends Component {} | ||
;(class extends Component {}) | ||
;(class extends React.Component {}) | ||
|
||
class NotCmp { } | ||
class NotCmp extends Foo { } | ||
class NotCmp extends React.Foo { } | ||
class NotCmp {} | ||
class NotCmp extends Foo {} | ||
class NotCmp extends React.Foo {} | ||
|
||
const Cmp = observer(class Cmp extends React.Component { }) | ||
const Cmp = observer(class Cmp extends Component { }) | ||
const Cmp = observer(class extends React.Component { }) | ||
const Cmp = observer(class extends Component { }) | ||
const Cmp = observer(class Cmp extends React.Component {}) | ||
const Cmp = observer(class Cmp extends Component {}) | ||
const Cmp = observer(class extends React.Component {}) | ||
const Cmp = observer(class extends Component {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,94 @@ | ||
'use strict'; | ||
"use strict" | ||
|
||
function create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
const sourceCode = context.getSourceCode() | ||
|
||
return { | ||
'FunctionDeclaration,FunctionExpression,ArrowFunctionExpression,ClassDeclaration,ClassExpression': cmp => { | ||
// Already has observer | ||
if (cmp.parent && cmp.parent.type === 'CallExpression' && cmp.parent.callee.name === 'observer') return; | ||
let name = cmp.id?.name; | ||
// If anonymous try to infer name from variable declaration | ||
if (!name && cmp.parent?.type === 'VariableDeclarator') { | ||
name = cmp.parent.id.name; | ||
} | ||
if (cmp.type.startsWith('Class')) { | ||
// Must extend Component or React.Component | ||
const { superClass } = cmp; | ||
if (!superClass) return; | ||
const superClassText = sourceCode.getText(superClass); | ||
if (superClassText !== 'Component' && superClassText !== 'React.Component') return; | ||
} else { | ||
// Name must start with uppercase letter | ||
if (!name?.charAt(0).match(/^[A-Z]$/)) return; | ||
} | ||
return { | ||
"FunctionDeclaration,FunctionExpression,ArrowFunctionExpression,ClassDeclaration,ClassExpression": | ||
cmp => { | ||
if ( | ||
cmp.parent && | ||
cmp.parent.type === "CallExpression" && | ||
cmp.parent.callee.name === "observer" | ||
) { | ||
// observer(...) | ||
return | ||
} | ||
let forwardRef = | ||
cmp.parent && | ||
cmp.parent.type === "CallExpression" && | ||
cmp.parent.callee.name === "forwardRef" | ||
? cmp.parent | ||
: undefined | ||
if ( | ||
forwardRef && | ||
forwardRef.parent && | ||
forwardRef.parent.type === "CallExpression" && | ||
forwardRef.parent.callee.name === "observer" | ||
) { | ||
// forwardRef(observer(...)) | ||
return | ||
} | ||
|
||
const fix = fixer => { | ||
return [ | ||
fixer.insertTextBefore( | ||
sourceCode.getFirstToken(cmp), | ||
(name && cmp.type.endsWith('Declaration') ? `const ${name} = ` : '') + 'observer(', | ||
), | ||
fixer.insertTextAfter( | ||
sourceCode.getLastToken(cmp), | ||
')', | ||
), | ||
] | ||
} | ||
context.report({ | ||
node: cmp, | ||
messageId: 'missingObserver', | ||
data: { | ||
name: name || '<anonymous>', | ||
}, | ||
fix, | ||
}) | ||
}, | ||
}; | ||
const cmpOrForwardRef = forwardRef || cmp | ||
let name = cmp.id?.name | ||
// If anonymous try to infer name from variable declaration | ||
if (!name && cmpOrForwardRef.parent?.type === "VariableDeclarator") { | ||
name = cmpOrForwardRef.parent.id.name | ||
} | ||
if (cmp.type.startsWith("Class")) { | ||
// Must extend Component or React.Component | ||
const { superClass } = cmp | ||
if (!superClass) { | ||
// not a component | ||
return | ||
} | ||
const superClassText = sourceCode.getText(superClass) | ||
if (superClassText !== "Component" && superClassText !== "React.Component") { | ||
// not a component | ||
return | ||
} | ||
} else { | ||
// Name must start with uppercase letter | ||
if (!name?.charAt(0).match(/^[A-Z]$/)) { | ||
// not a component | ||
return | ||
} | ||
} | ||
|
||
const fix = fixer => { | ||
return [ | ||
fixer.insertTextBefore( | ||
sourceCode.getFirstToken(cmpOrForwardRef), | ||
(name && cmp.type.endsWith("Declaration") ? `const ${name} = ` : "") + | ||
"observer(" | ||
), | ||
fixer.insertTextAfter(sourceCode.getLastToken(cmpOrForwardRef), ")") | ||
] | ||
} | ||
context.report({ | ||
node: cmp, | ||
messageId: "missingObserver", | ||
data: { | ||
name: name || "<anonymous>" | ||
}, | ||
fix | ||
}) | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
fixable: 'code', | ||
docs: { | ||
description: 'prevents missing `observer` on react component', | ||
recommended: true, | ||
}, | ||
messages: { | ||
missingObserver: "Component `{{ name }}` is missing `observer`.", | ||
meta: { | ||
type: "problem", | ||
fixable: "code", | ||
docs: { | ||
description: "prevents missing `observer` on react component", | ||
recommended: true | ||
}, | ||
messages: { | ||
missingObserver: "Component `{{ name }}` is missing `observer`." | ||
} | ||
}, | ||
}, | ||
create, | ||
}; | ||
create | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why add this configuration? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
corepack
can't live without it unless you set env variableCOREPACK_ENABLE_AUTO_PIN
to0
, which is quite inconvinient to do. But now when thinking about it,yarn
maybe also checks this field, so it may cause issues to non-corepack users, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, there is a
SKIP_YARN_COREPACK_CHECK
env var foryarn
, but this is equally bad as the need to setCOREPACK_ENABLE_AUTO_PIN
. Feel free to revert the change if it's causing problems, unless you know of a better solution of course.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yarnpkg/yarn#9015