Skip to content

Commit ddbb0d0

Browse files
Update shared query template to use QL modules instead of abstract classes.
1 parent e140430 commit ddbb0d0

File tree

4 files changed

+33
-35
lines changed

4 files changed

+33
-35
lines changed

scripts/generate_rules/generate_package_files.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
repo_root = Path(__file__).parent.parent.parent
121121
rule_packages_file_path = repo_root.joinpath("rule_packages")
122122
env = Environment(loader=FileSystemLoader(Path(__file__).parent.joinpath(
123-
"templates")), trim_blocks=True, lstrip_blocks=True)
123+
"templates")), trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True)
124124

125125
def write_shared_implementation(package_name, rule_id, query, language_name, ql_language_name, common_src_pack_dir, common_test_pack_dir, test_src_dir, skip_tests=False):
126126

@@ -164,6 +164,7 @@ def write_shared_implementation(package_name, rule_id, query, language_name, ql_
164164
# Write out the test. Test are always stored under the `language_name`
165165
# directory.
166166
if not skip_tests:
167+
167168
shared_impl_test_dir = common_test_pack_dir.joinpath(
168169
"rules",
169170
shared_impl_dir_name
@@ -176,31 +177,17 @@ def write_shared_implementation(package_name, rule_id, query, language_name, ql_
176177
f"{query['shared_implementation_short_name']}.ql"
177178
)
178179

179-
with open(shared_impl_test_query_path, "w", newline="\n") as f:
180-
f.write("// GENERATED FILE - DO NOT MODIFY\n")
181-
f.write(
182-
"import "
183-
+ str(shared_impl_query_library_path.relative_to(common_src_pack_dir).with_suffix(''))
184-
.replace("\\", "/")
185-
.replace("/", ".")
186-
+ "\n"
180+
shared_library_test_template = env.get_template(
181+
"shared_library_test.ql.template"
182+
)
183+
184+
if not shared_impl_test_query_path.exists():
185+
write_template(
186+
shared_library_test_template,
187+
query,
188+
package_name,
189+
shared_impl_test_query_path
187190
)
188-
f.write("\n")
189-
class_name = str(query["shared_implementation_short_name"]) + "SharedQuery"
190-
f.write("class TestFileQuery extends " + class_name + ",")
191-
# ql formatting of this line depends on the line length
192-
if len(class_name) > 61:
193-
# Line break required after comma
194-
f.write("\n TestQuery\n{ }\n")
195-
elif len(class_name) == 61:
196-
# Line break required before `{`
197-
f.write(" TestQuery\n{ }\n")
198-
elif len(class_name) > 57:
199-
# Line break required after `{`
200-
f.write(" TestQuery {\n}\n")
201-
else:
202-
# Under 100 characters, can be formatted on the same line
203-
f.write(" TestQuery { }\n")
204191

205192
# Create an empty test file, if one doesn't already exist
206193
shared_impl_test_dir.joinpath(

scripts/generate_rules/templates/query.ql.template

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import codingstandards.{{ language_name }}.{{ standard_short_name }}
66
{% if shared_implementation_short_name is defined %}
77
import codingstandards.{{ ql_language_name }}.rules.{{ shared_implementation_short_name.lower() }}.{{ shared_implementation_short_name }}
88

9-
class {{short_name}}Query extends {{shared_implementation_short_name}}SharedQuery {
10-
{{short_name}}Query() {
11-
this = {{package_name}}Package::{{shortname_camelcase}}Query()
12-
}
9+
module {{short_name}}Config implements {{shared_implementation_short_name}}ConfigSig {
10+
Query getQuery() { result = {{package_name}}Package::{{shortname_camelcase}}Query() }
1311
}
12+
13+
import {{shared_implementation_short_name}}<{{short_name}}Config>
1414
{% else %}
1515

1616
from

scripts/generate_rules/templates/shared_library.ql.template

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
the first matching query in `rule_packages`.
44
#}
55
/**
6-
* Provides a library with a `problems` predicate for the following issue:
6+
* Provides a configurable module {{shared_implementation_short_name}} with a `problems` predicate
7+
* for the following issue:
78
* {{ description|join('\n * ') }}
89
*/
910

1011
import cpp
1112
import codingstandards.{{ ql_language_name }}.Customizations
1213
import codingstandards.{{ ql_language_name }}.Exclusions
1314

14-
abstract class {{shared_implementation_short_name}}SharedQuery extends Query { }
15-
16-
Query getQuery() { result instanceof {{shared_implementation_short_name}}SharedQuery }
15+
signature module {{shared_implementation_short_name}}ConfigSig {
16+
Query getQuery();
17+
}
1718

18-
query predicate problems(Element e, string message) {
19-
not isExcluded(e, getQuery()) and message = "<replace with problem alert message for >"
19+
module {{shared_implementation_short_name}}<{{shared_implementation_short_name}}ConfigSig Config> {
20+
query predicate problems(Element e, string message) {
21+
not isExcluded(e, Config::getQuery()) and message = "<replace with problem alert message for >"
22+
}
2023
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.{{ ql_language_name }}.rules.{{ shared_implementation_short_name.lower() }}.{{ shared_implementation_short_name }}
3+
4+
module TestFileConfig implements {{shared_implementation_short_name}}ConfigSig {
5+
Query getQuery() { result instanceof TestQuery }
6+
}
7+
8+
import {{shared_implementation_short_name}}<TestFileConfig>

0 commit comments

Comments
 (0)