mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-25 07:58:59 -06:00
Utilities/ast-grep: add rules for cmHas{Suf,Pre}fix for character needles
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
id: cmhasprefix-char
|
||||
snapshots:
|
||||
cmHasPrefix(haystack, "'"):
|
||||
fixed: cmHasPrefix(haystack, '\'')
|
||||
labels:
|
||||
- source: '"''"'
|
||||
style: primary
|
||||
start: 22
|
||||
end: 25
|
||||
- source: cmHasPrefix(haystack, "'")
|
||||
style: secondary
|
||||
start: 0
|
||||
end: 26
|
||||
- source: ','
|
||||
style: secondary
|
||||
start: 20
|
||||
end: 21
|
||||
cmHasPrefix(haystack, "\n"):
|
||||
fixed: cmHasPrefix(haystack, '\n')
|
||||
labels:
|
||||
- source: '"\n"'
|
||||
style: primary
|
||||
start: 22
|
||||
end: 26
|
||||
- source: cmHasPrefix(haystack, "\n")
|
||||
style: secondary
|
||||
start: 0
|
||||
end: 27
|
||||
- source: ','
|
||||
style: secondary
|
||||
start: 20
|
||||
end: 21
|
||||
cmHasPrefix(haystack, "a"):
|
||||
fixed: cmHasPrefix(haystack, 'a')
|
||||
labels:
|
||||
- source: '"a"'
|
||||
style: primary
|
||||
start: 22
|
||||
end: 25
|
||||
- source: cmHasPrefix(haystack, "a")
|
||||
style: secondary
|
||||
start: 0
|
||||
end: 26
|
||||
- source: ','
|
||||
style: secondary
|
||||
start: 20
|
||||
end: 21
|
||||
@@ -0,0 +1,47 @@
|
||||
id: cmhassuffix-char
|
||||
snapshots:
|
||||
cmHasSuffix(haystack, "'"):
|
||||
fixed: cmHasSuffix(haystack, '\'')
|
||||
labels:
|
||||
- source: '"''"'
|
||||
style: primary
|
||||
start: 22
|
||||
end: 25
|
||||
- source: cmHasSuffix(haystack, "'")
|
||||
style: secondary
|
||||
start: 0
|
||||
end: 26
|
||||
- source: ','
|
||||
style: secondary
|
||||
start: 20
|
||||
end: 21
|
||||
cmHasSuffix(haystack, "\n"):
|
||||
fixed: cmHasSuffix(haystack, '\n')
|
||||
labels:
|
||||
- source: '"\n"'
|
||||
style: primary
|
||||
start: 22
|
||||
end: 26
|
||||
- source: cmHasSuffix(haystack, "\n")
|
||||
style: secondary
|
||||
start: 0
|
||||
end: 27
|
||||
- source: ','
|
||||
style: secondary
|
||||
start: 20
|
||||
end: 21
|
||||
cmHasSuffix(haystack, "a"):
|
||||
fixed: cmHasSuffix(haystack, 'a')
|
||||
labels:
|
||||
- source: '"a"'
|
||||
style: primary
|
||||
start: 22
|
||||
end: 25
|
||||
- source: cmHasSuffix(haystack, "a")
|
||||
style: secondary
|
||||
start: 0
|
||||
end: 26
|
||||
- source: ','
|
||||
style: secondary
|
||||
start: 20
|
||||
end: 21
|
||||
9
Utilities/ast-grep/rule-tests/cmhasprefix-char.yml
Normal file
9
Utilities/ast-grep/rule-tests/cmhasprefix-char.yml
Normal file
@@ -0,0 +1,9 @@
|
||||
---
|
||||
id: cmhasprefix-char
|
||||
valid:
|
||||
- "cmHasPrefix(haystack, 'a')"
|
||||
- 'cmHasPrefix(haystack, "foo")'
|
||||
invalid:
|
||||
- 'cmHasPrefix(haystack, "a")'
|
||||
- 'cmHasPrefix(haystack, "\n")'
|
||||
- "cmHasPrefix(haystack, \"'\")"
|
||||
9
Utilities/ast-grep/rule-tests/cmhassuffix-char.yml
Normal file
9
Utilities/ast-grep/rule-tests/cmhassuffix-char.yml
Normal file
@@ -0,0 +1,9 @@
|
||||
---
|
||||
id: cmhassuffix-char
|
||||
valid:
|
||||
- "cmHasSuffix(haystack, 'a')"
|
||||
- 'cmHasSuffix(haystack, "foo")'
|
||||
invalid:
|
||||
- 'cmHasSuffix(haystack, "a")'
|
||||
- 'cmHasSuffix(haystack, "\n")'
|
||||
- "cmHasSuffix(haystack, \"'\")"
|
||||
34
Utilities/ast-grep/rules/cmhasprefix-char.yml
Normal file
34
Utilities/ast-grep/rules/cmhasprefix-char.yml
Normal file
@@ -0,0 +1,34 @@
|
||||
---
|
||||
id: cmhasprefix-char
|
||||
language: Cpp
|
||||
severity: warning
|
||||
message: "`cmHasPrefix` with a one-char prefix search should use `cmHasPrefix`"
|
||||
rule:
|
||||
kind: string_literal
|
||||
pattern: $ARG
|
||||
follows:
|
||||
regex: '(,|[(])'
|
||||
inside:
|
||||
matches: cmhasprefix-call
|
||||
stopBy:
|
||||
kind: call_expression
|
||||
constraints:
|
||||
ARG:
|
||||
regex: '^"(.|\\.)"$'
|
||||
transform:
|
||||
ARG_CHANGE_QUOTE:
|
||||
replace:
|
||||
source: $ARG
|
||||
replace: '(^"|"$)'
|
||||
by: "'"
|
||||
ARG_ESCAPE_SINGLE_QUOTE:
|
||||
replace:
|
||||
source: $ARG_CHANGE_QUOTE
|
||||
replace: "'''"
|
||||
by: "'\\''"
|
||||
ARG_OUT:
|
||||
replace:
|
||||
source: $ARG_ESCAPE_SINGLE_QUOTE
|
||||
replace: '\\"'
|
||||
by: '"'
|
||||
fix: $ARG_OUT
|
||||
34
Utilities/ast-grep/rules/cmhassuffix-char.yml
Normal file
34
Utilities/ast-grep/rules/cmhassuffix-char.yml
Normal file
@@ -0,0 +1,34 @@
|
||||
---
|
||||
id: cmhassuffix-char
|
||||
language: Cpp
|
||||
severity: warning
|
||||
message: "`cmHasSuffix` with a one-char suffix search should use `cmHasSuffix`"
|
||||
rule:
|
||||
kind: string_literal
|
||||
pattern: $ARG
|
||||
follows:
|
||||
regex: '(,|[(])'
|
||||
inside:
|
||||
matches: cmhassuffix-call
|
||||
stopBy:
|
||||
kind: call_expression
|
||||
constraints:
|
||||
ARG:
|
||||
regex: '^"(.|\\.)"$'
|
||||
transform:
|
||||
ARG_CHANGE_QUOTE:
|
||||
replace:
|
||||
source: $ARG
|
||||
replace: '(^"|"$)'
|
||||
by: "'"
|
||||
ARG_ESCAPE_SINGLE_QUOTE:
|
||||
replace:
|
||||
source: $ARG_CHANGE_QUOTE
|
||||
replace: "'''"
|
||||
by: "'\\''"
|
||||
ARG_OUT:
|
||||
replace:
|
||||
source: $ARG_ESCAPE_SINGLE_QUOTE
|
||||
replace: '\\"'
|
||||
by: '"'
|
||||
fix: $ARG_OUT
|
||||
5
Utilities/ast-grep/utils/cmhasprefix-call.yml
Normal file
5
Utilities/ast-grep/utils/cmhasprefix-call.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
id: cmhasprefix-call
|
||||
language: Cpp
|
||||
rule:
|
||||
pattern: cmHasPrefix($$$)
|
||||
5
Utilities/ast-grep/utils/cmhassuffix-call.yml
Normal file
5
Utilities/ast-grep/utils/cmhassuffix-call.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
id: cmhassuffix-call
|
||||
language: Cpp
|
||||
rule:
|
||||
pattern: cmHasSuffix($$$)
|
||||
Reference in New Issue
Block a user