Skip to content

Commit 30b478f

Browse files
preview/ast-grep
1 parent ef04b8d commit 30b478f

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed

.coderabbit.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
2+
early_access: true
3+
reviews:
4+
path_instructions:
5+
- path: "**/*.{js,ts}"
6+
instructions: "Ensure that all constant variables are defined using UPPER CASE letters"
7+
tools:
8+
ast-grep:
9+
rule_dirs:
10+
- "rules"
11+
- "test-missing-dir"
12+
packages:
13+
- "test-peter-rabbit/test-ast-grep-custom-package"

greet.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Prints a greeting to the console.
3+
* @param {string} name
4+
* @param {string} surname
5+
*/
6+
export function greet(name, surname) {
7+
console.log(`Hello, ${name} ${surname}!`)
8+
}
9+
10+
/**
11+
* Prints a welcome message to the console.
12+
* @param {string} name
13+
* @param {string} surname
14+
*/
15+
export function welcome(name, surname) {
16+
console.log(`Welcome, ${name} ${surname}!`)
17+
}
18+
19+
import React from 'react'
20+
import PropTypes from 'prop-types'
21+
22+
function HelloWorld({
23+
greeting = "hello",
24+
greeted = '"World"',
25+
silent = false,
26+
onMouseOver,
27+
}) {
28+
if (!greeting) {
29+
console.log("No greeting")
30+
return null
31+
}
32+
33+
// TODO: Don't use random in render
34+
const num = Math.floor(Math.random() * 1e7)
35+
.toString()
36+
.replace(/.d+/gi, "")
37+
38+
return (
39+
<div
40+
className="HelloWorld"
41+
title={`You are visitor number ${num}`}
42+
onMouseOver={onMouseOver}
43+
>
44+
<strong>
45+
{greeting.slice(0, 1).toUpperCase() + greeting.slice(1).toLowerCase()}
46+
</strong>
47+
{greeting.endsWith(",") ? (
48+
" "
49+
) : (
50+
<span style={{ color: "grey" }}>", "</span>
51+
)}
52+
<em>{greeted}</em>
53+
{silent ? "." : "!"}
54+
</div>
55+
)
56+
}
57+
58+
HelloWorld.propTypes = {
59+
greeting: PropTypes.string,
60+
greeted: PropTypes.string,
61+
silent: PropTypes.bool,
62+
onMouseOver: PropTypes.func,
63+
}
64+
65+
/**
66+
* Fails ast-grep because of console.log used in the function
67+
* @param {string} text
68+
* @param {string} string
69+
* @returns {boolean}
70+
*/
71+
export function findInString(text, string, logFn = console.error) {
72+
logFn("text", text)
73+
return text.includes(string)
74+
}
75+
return text.includes(string)
76+
}
77+
78+
/**
79+
* Fails ast-grep because of console.log used in the catch block
80+
* @param {string} text
81+
* @param {string} string
82+
* @returns {boolean}
83+
*/
84+
export async function findInStringAsync(text, string) {
85+
try {
86+
return text.includes(string)
87+
} catch (error) {
88+
console.log("error", error)
89+
}
90+
}
91+
92+
/**
93+
* Doesn't fail ast-grep since console.error is allowed in catch block
94+
* @param {string} text
95+
* @param {string} string
96+
* @returns {boolean}
97+
*/
98+
export async function findInStringTreated(text, string) {
99+
try {
100+
return text.includes(string)
101+
} catch (error) {
102+
console.error("error", error)
103+
}
104+
}

jsx_ast_grep_failure.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useState } from 'react'
2+
3+
const greeting: string = "Hello, world!";
4+
5+
async function test() {
6+
7+
const [state, setState] = useState<string>("test string")
8+
9+
const [state2, setState2] = useState<string>("test string")
10+
11+
const [first, second] = await Promise.all([
12+
await (new Promise(() => {console.log("logging this long task")})),
13+
new Promise(() => {console.log("logging another long task")}),
14+
])
15+
16+
return {
17+
state,
18+
setState,
19+
first,
20+
second
21+
}
22+
}

rules/no-console-except-error.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
id: no-console-except-error
2+
language: javascript
3+
message: 'Do not use console.$METHOD except for console.error in catch clause'
4+
rule:
5+
any:
6+
- pattern: console.error($$$)
7+
not:
8+
inside:
9+
kind: catch_clause
10+
stopBy: end
11+
- pattern: console.$METHOD($$$)
12+
constraints:
13+
METHOD:
14+
regex: 'log|debug|warn'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
id: replace-unnecessary-type-definition-use-state
2+
language: "typescript"
3+
rule:
4+
any:
5+
- pattern: useState<string>($A)
6+
fix:
7+
useState($A)

rules/ts-const.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
id: ts-const
2+
language: Typescript
3+
rule:
4+
all:
5+
- any:
6+
- pattern: "const $VAR: $TYPE = $VALUE"
7+
- pattern: "const $VAR = $VALUE"
8+
- inside:
9+
kind: program
10+
constraints:
11+
VAR:
12+
regex: '.*[^A-Z_].*'
13+
severity: warning
14+
message: Always capitalize const variables
15+
note: Const variables should be capitalized

0 commit comments

Comments
 (0)