forked from jenkinsci/analysis-model
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYuiCompressorParser.java
More file actions
132 lines (115 loc) · 5.36 KB
/
YuiCompressorParser.java
File metadata and controls
132 lines (115 loc) · 5.36 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
package edu.hm.hafner.analysis.parser;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.LookaheadParser;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.util.LookaheadStream;
/**
* A parser for the YUI Compressor warnings.
*/
public class YuiCompressorParser extends LookaheadParser {
private static final long serialVersionUID = -4807932429496693096L;
private static final String YUI_COMPRESSOR_WARNING_PATTERN = "\\[WARNING\\] (.*)";
private static final Pattern UNUSED_SYMBOL_PATTERN = Pattern
.compile("The symbol [^ ]+ is declared but is apparently never used.*");
private static final Pattern UNUSED_VARIABLE_PATTERN = Pattern
.compile("The variable [^ ]+ has already been declared in the same scope.*");
private static final Pattern UNUSED_FUNCTION_PATTERN = Pattern
.compile("The function [^ ]+ has already been declared in the same scope.*");
private static final Pattern INVALID_HINT_PATTERN = Pattern.compile("Invalid hint syntax: [^ ]+");
private static final Pattern UNSUPPORTED_HINT_PATTERN = Pattern.compile("Unsupported hint value: [^ ]+");
private static final Pattern UNKNOWN_HINT_PATTERN = Pattern.compile("Hint refers to an unknown identifier: [^ ]+");
private static final Pattern PRINT_SYMBOL_PATTERN = Pattern.compile("This symbol cannot be printed: [^ ]+");
/**
* Creates a new instance of {@code YuiCompressorParser}.
*/
public YuiCompressorParser() {
super(YUI_COMPRESSOR_WARNING_PATTERN);
}
@Override
protected Optional<Issue> createIssue(final Matcher matcher, final LookaheadStream lookahead,
final IssueBuilder builder) {
String messageHeader = matcher.group(1);
String messageDetails = lookahead.hasNext() ? lookahead.next() : "";
CategoryAndPriority categoryAndPriority = getCategoryAndPriority(messageHeader);
String message = messageHeader + " [" + messageDetails + "]";
return builder.setFileName("unknown.file").setLineStart(0).setCategory(categoryAndPriority.getCategory())
.setMessage(message).setSeverity(categoryAndPriority.getPriority()).buildOptional();
}
@SuppressWarnings("npathcomplexity")
private CategoryAndPriority getCategoryAndPriority(final String message) { // NOPMD
if (message.startsWith("Found an undeclared symbol")) {
return CategoryAndPriority.UNDECLARED_SYMBOL;
}
if (message.startsWith("Try to use a single 'var' statement per scope")) {
return CategoryAndPriority.USE_SINGLE_VAR;
}
if (message.startsWith("Using JScript conditional comments is not recommended")) {
return CategoryAndPriority.USE_JSCRIPT;
}
if (message.startsWith("Using 'eval' is not recommended")) {
return CategoryAndPriority.USE_EVAL;
}
if (message.startsWith("Using 'with' is not recommended")) {
return CategoryAndPriority.USE_WITH;
}
if (UNUSED_SYMBOL_PATTERN.matcher(message).matches()) {
return CategoryAndPriority.UNUSED_SYMBOL;
}
if (UNUSED_VARIABLE_PATTERN.matcher(message).matches()) {
return CategoryAndPriority.DUPLICATE_VAR;
}
if (UNUSED_FUNCTION_PATTERN.matcher(message).matches()) {
return CategoryAndPriority.DUPLICATE_FUN;
}
if (INVALID_HINT_PATTERN.matcher(message).matches()) {
return CategoryAndPriority.INVALID_HINT;
}
if (UNSUPPORTED_HINT_PATTERN.matcher(message).matches()) {
return CategoryAndPriority.UNSUPPORTED_HINT;
}
if (UNKNOWN_HINT_PATTERN.matcher(message).matches()) {
return CategoryAndPriority.UNKNOWN_HINT;
}
if (PRINT_SYMBOL_PATTERN.matcher(message).matches()) {
return CategoryAndPriority.PRINT_SYMBOL;
}
return CategoryAndPriority.UNKNOWN;
}
/**
* Handles category and priority of the warning.
*/
private enum CategoryAndPriority {
UNDECLARED_SYMBOL("Undeclared symbol"),
USE_SINGLE_VAR("Use single 'var' per scope", Severity.WARNING_LOW),
UNUSED_SYMBOL("Unused symbol"),
DUPLICATE_VAR("Duplicate variable", Severity.WARNING_HIGH),
UNKNOWN(""),
DUPLICATE_FUN("Duplicate function", Severity.WARNING_HIGH),
INVALID_HINT("Invalid hint"),
UNSUPPORTED_HINT("Unsupported hint", Severity.WARNING_LOW),
UNKNOWN_HINT("Unknown hint", Severity.WARNING_LOW),
USE_JSCRIPT("Use Jscript", Severity.WARNING_HIGH),
USE_EVAL("Use eval", Severity.WARNING_HIGH),
USE_WITH("Use with", Severity.WARNING_HIGH),
PRINT_SYMBOL("Cannot print symbol", Severity.WARNING_LOW);
private final String category;
private final Severity priority;
CategoryAndPriority(final String category) {
this(category, Severity.WARNING_NORMAL);
}
CategoryAndPriority(final String category, final Severity priority) {
this.category = category;
this.priority = priority;
}
public String getCategory() {
return category;
}
public Severity getPriority() {
return priority;
}
}
}