-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModule.cfc
More file actions
325 lines (278 loc) · 12 KB
/
Module.cfc
File metadata and controls
325 lines (278 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* Lucee Parser and Linter Module
*
* This is the main entry point for the CFML linting module.
* It uses Lucee's built-in AST parser and applies configurable linting rules.
*/
component accessors="true" extends="modules.BaseModule" {
property name="RuleConfig";
/**
* main entry point
*
* @file path to the file to lint
* @folder path to the folder to lint (optional)
* @format format of the output (text, json, xml, silent (for tests))
* @rules comma-separated list of rules to apply, if left empty, it will look for rules in config file or use all enabled rules
* @config path to config file, if not provided, it will look for cflinter.json in the current directory of the file or folder
* @compact boolean whether to compact the JSON output (default: true)
* @reportPath path to save the report file (optional)
* @configStruct struct with configuration options (optional)
* @return struct with linting results
*/
function main(
string file="",
string folder="",
string format = "text",
string rules = "",
string config = "",
boolean compact = true,
string reportPath = "",
struct configStruct = {},
boolean silent=false,
string logfile = ""
) {
if(variables.verboseEnabled){
out("Lucee Linter Module initialized.");
out("file = " & file);
out("folder = " & folder);
out("format = " & format);
out("rules = " & rules);
out("cwd = " & variables.cwd);
out("config = " & arguments.config);
}
// out( variables );
variables.Timer.start("CFML Linting Process");
// File or Folder is required
if(!len(file) && !len(folder)){
out("No file or folder specified. Showing help:");
return showHelp();
}
variables.Timer.stop("CFML Linting Process");
//Create the rule configuration (with all the rules)
// TODO: Look for config files in this order:
// .lucli-lint.json;
// .cflint.json
// cfmllint.rc
var configPath = Len(arguments.config) ? arguments.config : variables.cwd & "/.lucli-lint.json";
variables.timer.start("Load Rule Configuration");
var RuleConfig = new lib.RuleConfiguration(configPath=configPath, configStruct=configStruct);
variables.RuleConfig = RuleConfig;
// Add the timer for debugging
RuleConfig.setTimer( variables.Timer );
RuleConfig.setLogFile( logfile );
variables.timer.stop("Load Rule Configuration");
// If specific rules are provided, enable only those
if(len(arguments.rules)){
var ruleList = ListToArray(arguments.rules);
RuleConfig.enableOnlyRules(ruleList);
}
// Create linter
var linter = createObject("component", "lib.CFMLLinter").init(RuleConfig);
linter.setCWD( variables.cwd ); //Pass the working dir
linter.setTimer( variables.Timer ); //Pass the timer for debugging
// Check if the folder or file path is relative , or absolute
// Use Java File class for cross-platform path handling
var fileObj = createObject("java", "java.io.File");
// Linting Results
var results = [];
var lintingReport = new lib.LintReport(results);
// Check the file path.
if(!isEmpty(arguments.file)) {
var filePath = fileObj.init(arguments.file);
if(!filePath.isAbsolute()) {
arguments.file = fileObj.init(variables.cwd, arguments.file).getCanonicalPath();
} else {
arguments.file = filePath.getAbsolutePath();
}
lintingReport.setFile(arguments.file);
var fileResults = linter.lintFile(arguments.file);
// Should return something else, including how manyu files we did.
results.append(fileResults, true);
}
// Check the folder path.
if(!isEmpty(arguments.folder)) {
var folderPath = fileObj.init(arguments.folder);
if(!folderPath.isAbsolute()) {
arguments.folder = fileObj.init(variables.cwd, arguments.folder).getCanonicalPath();
} else {
arguments.folder = folderPath.getAbsolutePath();
}
lintingReport.setFolder(arguments.folder);
var folderResults = linter.lintFolder(arguments.folder);
// dump(var=folderResults.results, label="Folder results");
ArrayAppend(results, folderResults.results, true);
}
// dump(results);
// Output results
var outputFormat = arguments.format;
if(outputFormat == "text" && !Len(reportPath)){
if(!isEmpty(arguments.file)){
out("Linting file: " & arguments.file);
} else if(!isEmpty(arguments.folder)){
out("Linting folder: " & arguments.folder);
}
outline("Rules loaded: #RuleConfig.getEnabledRules().KeyList()#");
out("");
if(variables.verboseEnabled){
outline("Rule Access Counts:");
var ruleAccessCount = linter.getRuleAccessCount();
for(var rule in ruleAccessCount){
out(" #rule# checked : #ruleAccessCount[rule]# times");
}
out("");
}
}
var formattedResults = linter.formatResults(results, outputFormat, compact);
if(Len(arguments.reportPath)){
// If we are not absolute, make it relative to cwd
var reportFilePath = fileObj.init(arguments.reportPath);
if(!reportFilePath.isAbsolute()) {
arguments.reportPath = fileObj.init(variables.cwd, arguments.reportPath).getCanonicalPath();
} else {
arguments.reportPath = reportFilePath.getAbsolutePath();
}
// Save to file
fileWrite(arguments.reportPath, formattedResults);
out("Report written to: " & arguments.reportPath);
} else if(!silent){
out(formattedResults);
}
return formattedResults;
}
// function out(any message){
// if(!isSimpleValue(message)){
// message = serializeJson(var=message, compact=false);
// }
// Su(message & chr(10));
// }
/**
* Show available rules and their descriptions
*/
function showAvailableRules() {
try {
// out("Available CFML Linter Rules:");
outline("Available CFML Linter Rules:");
// This should come from the RulesConfiguration
// Create rules directly
var rules = [
createObject("component", "lib.rules.AvoidUsingCFDumpTagRule").init(),
createObject("component", "lib.rules.AvoidUsingCFAbortTagRule").init(),
createObject("component", "lib.rules.VariableNameChecker").init(),
createObject("component", "lib.rules.MissingVarRule").init(),
createObject("component", "lib.rules.QueryParamRule").init(),
createObject("component", "lib.rules.ExcessiveFunctionLengthRule").init(),
createObject("component", "lib.rules.FunctionHintMissingRule").init(),
createObject("component", "lib.rules.GlobalVarRule").init(),
createObject("component", "lib.rules.AvoidUsingCreateObjectRule").init()
];
for (var rule in rules) {
var ruleInfo = rule.getRuleInfo();
out("[" & ruleInfo.ruleCode & "] " & ruleInfo.ruleName);
out(" Description: " & ruleInfo.description);
out(" Severity: " & ruleInfo.severity);
out(" Group: " & ruleInfo.group);
out(" Enabled: " & ruleInfo.enabled);
out("");
}
return "Rules information displayed";
} catch (any e) {
out("Error in showAvailableRules: " & e.message);
if (structKeyExists(e, "detail")) {
out("Detail: " & e.detail);
}
return "Failed to show rules";
}
}
function showHelp() {
out("Lucee Linter Module Help:");
out("=========================");
out("Usage: lucli lint [options]");
out("Options:");
out(" file=<file> Path to the file to lint");
out(" folder=<folder> Path to the folder to lint");
out(" format=text|json|xml|bitbucket Output format (default: text)");
out(" rules=<rules> Only run specified rules (comma-separated)");
return "Help information displayed";
}
function showRuleParams(){
// Build JSON Schema fragments for each rule's config block
var ruleConfig = new lib.RuleConfiguration();
var allRules = ruleConfig.getRules();
// This will become: { "rules": { "SQL_CHECK": { ...schema... }, ... } }
var res = { "rules": {} };
// Helper: infer JSON Schema type from a CFML value
var inferJsonType = function(value){
if (isBoolean(arguments.value)) {
return "boolean";
}
if (isNumeric(arguments.value)) {
// Distinguish integer vs number
return (int(arguments.value) EQ arguments.value) ? "integer" : "number";
}
if (isArray(arguments.value)) {
return "array";
}
if (isStruct(arguments.value)) {
return "object";
}
// We almost never have nulls here because rule params have defaults
return "string";
};
for (var ruleCode in allRules) {
var rule = allRules[ruleCode];
var params = rule.getParameters() ?: {};
var ruleInfo = rule.getRuleInfo();
var paramProps = {};
// Only bother emitting schema for rules that actually have parameters
if (structIsEmpty(params)) {
continue;
}
// Build per‑parameter property schema from the default values
for (var paramName in params) {
var defaultValue = params[paramName];
paramProps[paramName] = {
"type": inferJsonType(defaultValue),
"default": defaultValue
};
}
// Each entry here is what you'll paste under schema.properties.rules.properties.<RULE_CODE>
res.rules[ruleCode] = {
"type": "object",
"description": ruleInfo.description ?: (
"Configuration for " & ruleInfo.ruleCode & " (" & ruleInfo.ruleName & ")"
),
"allOf": [
// Reuse the generic ruleConfig constraints
{ "$ref": "##/definitions/ruleConfig" },
// Add strongly‑typed rule‑specific parameters
{
"properties": paramProps
}
]
};
}
// Pretty JSON for copy‑paste into schema/v1/lucli-lint.schema.json
out( serializeJSON(var = res, compact = false) );
return res;
}
private function verbose(any message){
if(variables.verboseEnabled){
out(message);
}
}
private void function outline(string message, boolean bShowBorder=false){
var lineLength = message.len();
if(bShowBorder){
lineLength += 4;
}
var border = repeatString("=", lineLength);
if(bShowBorder){
out(border);
out("| " & message & " |");
}
else{
out(message);
}
out(border);
}
}