-
Notifications
You must be signed in to change notification settings - Fork 3
preview/ast-grep #65
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
base: main
Are you sure you want to change the base?
preview/ast-grep #65
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json | ||
early_access: true | ||
reviews: | ||
path_instructions: | ||
- path: "**/*.{js,ts}" | ||
instructions: "Ensure that all constant variables are defined using UPPER CASE letters" | ||
tools: | ||
ast-grep: | ||
rule_dirs: | ||
- "rules" | ||
- "test-missing-dir" | ||
packages: | ||
- "test-peter-rabbit/test-ast-grep-custom-package" |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,104 @@ | ||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* Prints a greeting to the console. | ||||||||||||||||||||||||||||||
* @param {string} name | ||||||||||||||||||||||||||||||
* @param {string} surname | ||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
export function greet(name, surname) { | ||||||||||||||||||||||||||||||
console.log(`Hello, ${name} ${surname}!`) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* Prints a welcome message to the console. | ||||||||||||||||||||||||||||||
* @param {string} name | ||||||||||||||||||||||||||||||
* @param {string} surname | ||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
export function welcome(name, surname) { | ||||||||||||||||||||||||||||||
console.log(`Welcome, ${name} ${surname}!`) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
import React from 'react' | ||||||||||||||||||||||||||||||
import PropTypes from 'prop-types' | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
function HelloWorld({ | ||||||||||||||||||||||||||||||
greeting = "hello", | ||||||||||||||||||||||||||||||
greeted = '"World"', | ||||||||||||||||||||||||||||||
silent = false, | ||||||||||||||||||||||||||||||
onMouseOver, | ||||||||||||||||||||||||||||||
}) { | ||||||||||||||||||||||||||||||
if (!greeting) { | ||||||||||||||||||||||||||||||
console.log("No greeting") | ||||||||||||||||||||||||||||||
return null | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
Comment on lines
+28
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Console.log usage violates linting rules. The Consider replacing with - console.log("No greeting")
+ console.error("No greeting") 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// TODO: Don't use random in render | ||||||||||||||||||||||||||||||
const num = Math.floor(Math.random() * 1e7) | ||||||||||||||||||||||||||||||
.toString() | ||||||||||||||||||||||||||||||
.replace(/.d+/gi, "") | ||||||||||||||||||||||||||||||
Comment on lines
+33
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix regex pattern and address TODO comment. Two issues need attention:
Apply this diff to fix the regex: - .replace(/.d+/gi, "")
+ .replace(/\d+/gi, "") Would you like me to suggest an alternative approach to generate the visitor number that doesn't use random in render? 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||
<div | ||||||||||||||||||||||||||||||
className="HelloWorld" | ||||||||||||||||||||||||||||||
title={`You are visitor number ${num}`} | ||||||||||||||||||||||||||||||
onMouseOver={onMouseOver} | ||||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||||
<strong> | ||||||||||||||||||||||||||||||
{greeting.slice(0, 1).toUpperCase() + greeting.slice(1).toLowerCase()} | ||||||||||||||||||||||||||||||
</strong> | ||||||||||||||||||||||||||||||
{greeting.endsWith(",") ? ( | ||||||||||||||||||||||||||||||
" " | ||||||||||||||||||||||||||||||
) : ( | ||||||||||||||||||||||||||||||
<span style={{ color: "grey" }}>", "</span> | ||||||||||||||||||||||||||||||
)} | ||||||||||||||||||||||||||||||
<em>{greeted}</em> | ||||||||||||||||||||||||||||||
{silent ? "." : "!"} | ||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
HelloWorld.propTypes = { | ||||||||||||||||||||||||||||||
greeting: PropTypes.string, | ||||||||||||||||||||||||||||||
greeted: PropTypes.string, | ||||||||||||||||||||||||||||||
silent: PropTypes.bool, | ||||||||||||||||||||||||||||||
onMouseOver: PropTypes.func, | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* Fails ast-grep because of console.log used in the function | ||||||||||||||||||||||||||||||
* @param {string} text | ||||||||||||||||||||||||||||||
* @param {string} string | ||||||||||||||||||||||||||||||
* @returns {boolean} | ||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
export function findInString(text, string, logFn = console.error) { | ||||||||||||||||||||||||||||||
logFn("text", text) | ||||||||||||||||||||||||||||||
return text.includes(string) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
return text.includes(string) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
Comment on lines
+71
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix duplicate code causing parse errors. There are duplicate lines 75-76 that repeat the return statement and closing brace, causing parse errors as flagged by static analysis. Remove the duplicate lines: export function findInString(text, string, logFn = console.error) {
logFn("text", text)
return text.includes(string)
}
- return text.includes(string)
-} 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Biome (1.9.4)[error] 75-75: Illegal return statement outside of a function (parse) [error] 75-76: Expected a statement but instead found '}'. Expected a statement here. (parse) 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* Fails ast-grep because of console.log used in the catch block | ||||||||||||||||||||||||||||||
* @param {string} text | ||||||||||||||||||||||||||||||
* @param {string} string | ||||||||||||||||||||||||||||||
* @returns {boolean} | ||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
export async function findInStringAsync(text, string) { | ||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||
return text.includes(string) | ||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||
console.log("error", error) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
Comment on lines
+84
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Console.log in catch block violates linting rules. The Replace with } catch (error) {
- console.log("error", error)
+ console.error("error", error)
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* Doesn't fail ast-grep since console.error is allowed in catch block | ||||||||||||||||||||||||||||||
* @param {string} text | ||||||||||||||||||||||||||||||
* @param {string} string | ||||||||||||||||||||||||||||||
* @returns {boolean} | ||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
export async function findInStringTreated(text, string) { | ||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||
return text.includes(string) | ||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||
console.error("error", error) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,22 @@ | ||||||||||||||||||||||||||||||
import { useState } from 'react' | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const greeting: string = "Hello, world!"; | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constant variable name violates coding guidelines. According to the coding guidelines, all constant variables in JS/TS files must be defined using UPPER CASE letters. -const greeting: string = "Hello, world!";
+const GREETING: string = "Hello, world!"; 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
async function test() { | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const [state, setState] = useState<string>("test string") | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const [state2, setState2] = useState<string>("test string") | ||||||||||||||||||||||||||||||
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary TypeScript type annotations. The explicit - const [state, setState] = useState<string>("test string")
+ const [state, setState] = useState("test string")
- const [state2, setState2] = useState<string>("test string")
+ const [state2, setState2] = useState("test string") 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const [first, second] = await Promise.all([ | ||||||||||||||||||||||||||||||
await (new Promise(() => {console.log("logging this long task")})), | ||||||||||||||||||||||||||||||
new Promise(() => {console.log("logging another long task")}), | ||||||||||||||||||||||||||||||
]) | ||||||||||||||||||||||||||||||
Comment on lines
+11
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix Promise.all usage and console.log violations. Multiple issues with this code:
- const [first, second] = await Promise.all([
- await (new Promise(() => {console.log("logging this long task")})),
- new Promise(() => {console.log("logging another long task")}),
- ])
+ const [first, second] = await Promise.all([
+ new Promise((resolve) => {
+ // Removed console.log to comply with no-console-except-error rule
+ resolve("first task completed");
+ }),
+ new Promise((resolve) => {
+ // Removed console.log to comply with no-console-except-error rule
+ resolve("second task completed");
+ }),
+ ]) 📝 Committable suggestion
Suggested change
🧰 Tools🪛 ast-grep (0.38.6)[hint] 11-11: (no-await-in-promise-all-custom) 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||
state, | ||||||||||||||||||||||||||||||
setState, | ||||||||||||||||||||||||||||||
first, | ||||||||||||||||||||||||||||||
second | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
id: no-console-except-error | ||
language: javascript | ||
message: 'Do not use console.$METHOD except for console.error in catch clause' | ||
rule: | ||
any: | ||
- pattern: console.error($$$) | ||
not: | ||
inside: | ||
kind: catch_clause | ||
stopBy: end | ||
- pattern: console.$METHOD($$$) | ||
constraints: | ||
METHOD: | ||
regex: 'log|debug|warn' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
id: replace-unnecessary-type-definition-use-state | ||
language: "typescript" | ||
rule: | ||
any: | ||
- pattern: useState<string>($A) | ||
fix: | ||
useState($A) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,15 @@ | ||||||
id: ts-const | ||||||
language: Typescript | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix language specification capitalization. The language should be lowercase "typescript" to match ast-grep's language specifications. -language: Typescript
+language: typescript 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||
rule: | ||||||
all: | ||||||
- any: | ||||||
- pattern: "const $VAR: $TYPE = $VALUE" | ||||||
- pattern: "const $VAR = $VALUE" | ||||||
- inside: | ||||||
kind: program | ||||||
constraints: | ||||||
VAR: | ||||||
regex: '.*[^A-Z_].*' | ||||||
severity: warning | ||||||
message: Always capitalize const variables | ||||||
note: Const variables should be capitalized |
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.
Console.log usage violates linting rules.
The
console.log
statement violates the AST-grep linting rule that disallowsconsole.log
,console.debug
, andconsole.warn
except forconsole.error
inside catch clauses.Consider replacing with
console.error
if this is for error logging, or remove the console statement if it's for debugging purposes:📝 Committable suggestion
🤖 Prompt for AI Agents