-
Notifications
You must be signed in to change notification settings - Fork 7
React19: Add a validator to check plugin compatibility #552
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
Open
leventebalogh
wants to merge
1
commit into
main
Choose a base branch
from
leventebalogh/react19-compat-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+448
−0
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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,179 @@ | ||
| package reactcompat | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "regexp" | ||
|
|
||
| "github.com/grafana/plugin-validator/pkg/analysis" | ||
| "github.com/grafana/plugin-validator/pkg/analysis/passes/modulejs" | ||
| ) | ||
|
|
||
| const react19UpgradeGuide = "https://react.dev/blog/2024/04/25/react-19-upgrade-guide" | ||
|
|
||
| var ( | ||
| react19PropTypes = &analysis.Rule{Name: "react-19-prop-types", Severity: analysis.Warning} | ||
| react19LegacyContext = &analysis.Rule{Name: "react-19-legacy-context", Severity: analysis.Warning} | ||
| react19StringRefs = &analysis.Rule{Name: "react-19-string-refs", Severity: analysis.Warning} | ||
| react19CreateFactory = &analysis.Rule{Name: "react-19-create-factory", Severity: analysis.Warning} | ||
| react19FindDOMNode = &analysis.Rule{Name: "react-19-find-dom-node", Severity: analysis.Warning} | ||
| react19LegacyRender = &analysis.Rule{Name: "react-19-legacy-render", Severity: analysis.Warning} | ||
| react19SecretInternals = &analysis.Rule{Name: "react-19-secret-internals", Severity: analysis.Warning} | ||
| react19Compatible = &analysis.Rule{Name: "react-19-compatible", Severity: analysis.OK} | ||
| ) | ||
|
|
||
| var Analyzer = &analysis.Analyzer{ | ||
| Name: "reactcompat", | ||
| Requires: []*analysis.Analyzer{modulejs.Analyzer}, | ||
| Run: run, | ||
| Rules: []*analysis.Rule{ | ||
| react19PropTypes, | ||
| react19LegacyContext, | ||
| react19StringRefs, | ||
| react19CreateFactory, | ||
| react19FindDOMNode, | ||
| react19LegacyRender, | ||
| react19SecretInternals, | ||
| react19Compatible, | ||
| }, | ||
| ReadmeInfo: analysis.ReadmeInfo{ | ||
| Name: "React 19 Compatibility", | ||
| Description: "Detects usage of React APIs removed or deprecated in React 19.", | ||
| }, | ||
| } | ||
|
|
||
| // detector checks a single module.js file for a specific pattern. | ||
| type detector interface { | ||
| Detect(moduleJs []byte) bool | ||
| Pattern() string | ||
| } | ||
|
|
||
| type containsBytesDetector struct { | ||
| pattern []byte | ||
| } | ||
|
|
||
| func (d *containsBytesDetector) Detect(moduleJs []byte) bool { | ||
| return bytes.Contains(moduleJs, d.pattern) | ||
| } | ||
|
|
||
| func (d *containsBytesDetector) Pattern() string { | ||
| return string(d.pattern) | ||
| } | ||
|
|
||
| type regexDetector struct { | ||
| regex *regexp.Regexp | ||
| } | ||
|
|
||
| func (d *regexDetector) Detect(moduleJs []byte) bool { | ||
| return d.regex.Match(moduleJs) | ||
| } | ||
|
|
||
| func (d *regexDetector) Pattern() string { | ||
| return d.regex.String() | ||
| } | ||
|
|
||
| // reactPattern groups a rule, a human-readable description, and the detectors that trigger it. | ||
| type reactPattern struct { | ||
| rule *analysis.Rule | ||
| title string | ||
| description string | ||
| detectors []detector | ||
| } | ||
|
|
||
| var reactPatterns = []reactPattern{ | ||
| { | ||
| rule: react19PropTypes, | ||
| title: "module.js: Uses removed React API propTypes or defaultProps", | ||
| description: "Detected usage of '%s'. propTypes and defaultProps on function components were removed in React 19.", | ||
| detectors: []detector{ | ||
| &containsBytesDetector{pattern: []byte(".propTypes=")}, | ||
| &containsBytesDetector{pattern: []byte(".defaultProps=")}, | ||
| }, | ||
| }, | ||
| { | ||
| rule: react19LegacyContext, | ||
| title: "module.js: Uses removed React legacy context API", | ||
| description: "Detected usage of '%s'. contextTypes, childContextTypes, and getChildContext were removed in React 19.", | ||
| detectors: []detector{ | ||
| &containsBytesDetector{pattern: []byte(".contextTypes=")}, | ||
| &containsBytesDetector{pattern: []byte(".childContextTypes=")}, | ||
| &containsBytesDetector{pattern: []byte("getChildContext")}, | ||
| }, | ||
| }, | ||
| { | ||
| rule: react19StringRefs, | ||
| title: "module.js: Uses removed React string refs", | ||
| description: "Detected usage of '%s'. String refs were removed in React 19. Use callback refs or React.createRef() instead.", | ||
| detectors: []detector{ | ||
| ®exDetector{regex: regexp.MustCompile(`ref:"[^"]+?"`)}, | ||
| ®exDetector{regex: regexp.MustCompile(`ref:'[^']+'`)}, | ||
| }, | ||
| }, | ||
| { | ||
| rule: react19CreateFactory, | ||
| title: "module.js: Uses removed React.createFactory", | ||
| description: "Detected usage of '%s'. React.createFactory was removed in React 19. Use JSX instead.", | ||
| detectors: []detector{ | ||
| &containsBytesDetector{pattern: []byte("createFactory(")}, | ||
| }, | ||
| }, | ||
| { | ||
| rule: react19FindDOMNode, | ||
| title: "module.js: Uses removed ReactDOM.findDOMNode", | ||
| description: "Detected usage of '%s'. ReactDOM.findDOMNode was removed in React 19. Use DOM refs instead.", | ||
| detectors: []detector{ | ||
| &containsBytesDetector{pattern: []byte("findDOMNode(")}, | ||
| }, | ||
| }, | ||
| { | ||
| rule: react19LegacyRender, | ||
| title: "module.js: Uses removed ReactDOM.render or unmountComponentAtNode", | ||
| description: "Detected usage of '%s'. ReactDOM.render and unmountComponentAtNode were removed in React 19. Use createRoot instead.", | ||
| detectors: []detector{ | ||
| &containsBytesDetector{pattern: []byte("ReactDOM.render(")}, | ||
| &containsBytesDetector{pattern: []byte("unmountComponentAtNode(")}, | ||
| }, | ||
| }, | ||
| { | ||
| rule: react19SecretInternals, | ||
| title: "module.js: Uses React internal __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED", | ||
| description: "Detected usage of '%s'. This internal was removed in React 19.", | ||
| detectors: []detector{ | ||
| &containsBytesDetector{pattern: []byte("__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED")}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| func run(pass *analysis.Pass) (interface{}, error) { | ||
| moduleJsMap, ok := pass.ResultOf[modulejs.Analyzer].(map[string][]byte) | ||
| if !ok || len(moduleJsMap) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| for _, pattern := range reactPatterns { | ||
| matched := false | ||
| matchedPattern := "" | ||
|
|
||
| outer: | ||
| for _, content := range moduleJsMap { | ||
| for _, d := range pattern.detectors { | ||
| if d.Detect(content) { | ||
| matched = true | ||
| matchedPattern = d.Pattern() | ||
| break outer | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if matched { | ||
| pass.ReportResult( | ||
| pass.AnalyzerName, | ||
| pattern.rule, | ||
| pattern.title, | ||
| fmt.Sprintf(pattern.description+" See: "+react19UpgradeGuide, matchedPattern), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
As far as I can see, these could be semgrep rules? If that's the case there is no need to create a new rule
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.
+1 all this can work with semgrep and we even have already one for this case
plugin-validator/pkg/analysis/passes/coderules/semgrep-rules.yaml
Line 190 in 2435336