Explorar el Código

Utilities/ast-grep: add rules for `cmHas{Suf,Pre}fix` for character needles

Ben Boeckel hace 1 mes
padre
commit
d9ee22f331

+ 47 - 0
Utilities/ast-grep/rule-tests/__snapshots__/cmhasprefix-char-snapshot.yml

@@ -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

+ 47 - 0
Utilities/ast-grep/rule-tests/__snapshots__/cmhassuffix-char-snapshot.yml

@@ -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 - 0
Utilities/ast-grep/rule-tests/cmhasprefix-char.yml

@@ -0,0 +1,9 @@
+---
+id: cmhasprefix-char
+valid:
+  - "cmHasPrefix(haystack, 'a')"
+  - 'cmHasPrefix(haystack, "foo")'
+invalid:
+  - 'cmHasPrefix(haystack, "a")'
+  - 'cmHasPrefix(haystack, "\n")'
+  - "cmHasPrefix(haystack, \"'\")"

+ 9 - 0
Utilities/ast-grep/rule-tests/cmhassuffix-char.yml

@@ -0,0 +1,9 @@
+---
+id: cmhassuffix-char
+valid:
+  - "cmHasSuffix(haystack, 'a')"
+  - 'cmHasSuffix(haystack, "foo")'
+invalid:
+  - 'cmHasSuffix(haystack, "a")'
+  - 'cmHasSuffix(haystack, "\n")'
+  - "cmHasSuffix(haystack, \"'\")"

+ 34 - 0
Utilities/ast-grep/rules/cmhasprefix-char.yml

@@ -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 - 0
Utilities/ast-grep/rules/cmhassuffix-char.yml

@@ -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 - 0
Utilities/ast-grep/utils/cmhasprefix-call.yml

@@ -0,0 +1,5 @@
+---
+id: cmhasprefix-call
+language: Cpp
+rule:
+  pattern: cmHasPrefix($$$)

+ 5 - 0
Utilities/ast-grep/utils/cmhassuffix-call.yml

@@ -0,0 +1,5 @@
+---
+id: cmhassuffix-call
+language: Cpp
+rule:
+  pattern: cmHasSuffix($$$)