/
Linter.ts
292 lines (270 loc) 路 9.57 KB
/
Linter.ts
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
import { Block, LiteralBlock, IfBlock, RepeatBlock, CommentBlock, BlockType } from './block/index';
import { Builder, SchemaResolver } from './Builder';
import { Utils } from './Utils';
type Range = {
begin: number;
end: number;
};
type Position = {
begin: { line: number; column: number };
end: { line: number; column: number };
};
export type LintSeverity = 'error' | 'warning';
export interface Lint {
offset: Range;
position: Position;
severity: LintSeverity;
blockType: BlockType;
block: Block;
scope: any;
expression: string;
message: string;
}
export interface LinterResult {
lints: Lint[];
hasErrors: boolean;
hasWarnings: boolean;
}
export class Linter<T = any> {
public builder: Builder<T>;
public constructor(code: string, schemaResolver?: T | SchemaResolver<T>, private strictOverride?: boolean) {
this.builder = new Builder(code, schemaResolver, false);
}
public build() {
return this.builder.build();
}
public get strict() {
if (this.strictOverride !== undefined) {
return this.strictOverride;
}
return !!this.builder.schema;
}
protected getBlockPrefix(blockType: BlockType) {
const prefixes = { comment: '[--', if: '[if ', repeat: '[repeat ', literal: '' };
return prefixes[blockType];
}
private getParamsMergedForBlock(block: Block, params: any, childParams?: any) {
return Utils.getParamsMergedForBlock(block, params, childParams);
}
private createLintResult(res: {
offsetBegin: number;
offsetEnd: number;
severity: LintSeverity;
block: Block;
expression: string;
message: string;
}): Lint {
return {
offset: { begin: res.offsetBegin, end: res.offsetEnd },
// line: { begin: 1, end: 1 },
// column: { begin: 0, end: 0 },
position: { begin: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
severity: res.severity,
expression: res.expression,
message: res.message,
blockType: res.block.type,
block: res.block,
scope: res.block.scope,
};
}
private scanLiteralBlock(block: LiteralBlock, schema: any, params: any) {
const results: Lint[] = [];
const maxIterations = 10000;
let tplInner = block.outerContent;
let offset = 0 + block.offsetBegin;
let offsetBegin: number;
let offsetEnd: number;
let expression: string;
const getLintResult = (message: string, strict = false): Lint => {
return this.createLintResult({
severity: strict ? 'warning' : 'error',
block,
offsetBegin,
offsetEnd,
expression,
message,
});
};
for (let i = 0; i < maxIterations; i++) {
const idx = tplInner.indexOf('[');
const endIdx = tplInner.indexOf(']');
if (idx === -1) {
break;
}
let validIdentifier = false;
if (endIdx === -1) {
expression = tplInner.substr(idx, 10) + '...';
offsetBegin = offset + idx + 1;
offsetEnd = offset + tplInner.length;
results.push(getLintResult('Invalid: Expression "' + expression + '"'));
return results;
}
const identifier = tplInner.substring(idx + 1, endIdx);
expression = '[' + identifier + ']';
offsetBegin = offset + idx + 1;
validIdentifier = true;
offset = offset + endIdx + 1;
tplInner = tplInner.substr(endIdx + 1);
let keyFound = '';
let subkeyFound = '';
for (const key in params) {
if (identifier.indexOf(key + ' ') !== 0) {
continue;
}
keyFound = key;
for (const subkey in params[key]) {
if ((identifier + ']').substr(key.length).indexOf(' ' + subkey + ']') !== 0) {
continue;
}
subkeyFound = subkey;
break;
}
break;
}
offsetEnd = offsetBegin + identifier.length;
const identifiers = this.strict && schema ? this.builder.getIdentifiers(schema) : false;
const isValidIdentifier = identifiers && this.builder.isValidIdentifier(identifiers, identifier);
if (keyFound && subkeyFound) {
if (identifiers && !isValidIdentifier) {
results.push(getLintResult('(STRICT) Illegal: Identifier "' + identifier + '"', true));
}
continue;
}
if (identifiers && isValidIdentifier) {
results.push(getLintResult('(STRICT) Unavailable: Identifier "' + identifier + '"', true));
continue;
}
offsetBegin += keyFound ? (keyFound + ' ').length : 0;
offsetEnd = offsetBegin + identifier.length - (keyFound ? (keyFound + ' ').length : 0);
const message = keyFound
? 'Unavailable: Property "' + identifier.replace(keyFound + ' ', '') + '" on Object "' + keyFound + '"'
: 'Unavailable: Identifier "' + identifier + '"';
results.push(getLintResult(message));
}
return results;
}
private scanBlocks(blocks: Block[], schema: any, params: any, results: Lint[]) {
let offsetBegin: number;
let offsetEnd: number;
for (const block of blocks) {
const getLintResult = (message: string, strict = false): Lint => {
return this.createLintResult({
severity: strict ? 'warning' : 'error',
block,
offsetBegin,
offsetEnd,
expression: block.expression,
message,
});
};
if (block instanceof IfBlock) {
if (!(params[block.key] && typeof params[block.key][block.subkey] === 'boolean')) {
if (this.strict && schema[block.key] && typeof schema[block.key][block.subkey] === 'boolean') {
const keyFound = params[block.key] && typeof params[block.key] === 'object';
offsetBegin =
block.offsetBegin +
this.getBlockPrefix('if').length +
(keyFound ? (block.key + ' ' + block.operator + ' ').length : 0);
offsetEnd = block.offsetBegin + block.expression.length - 1;
results.push(
getLintResult(
'(STRICT) Unavailable: ' +
(keyFound ? '' : 'Object "' + block.key + '" and ') +
'Boolean "' +
block.subkey +
'"',
true,
),
);
} else {
const keyFound = params[block.key] && typeof params[block.key] === 'object';
offsetBegin =
block.offsetBegin +
this.getBlockPrefix('if').length +
(keyFound ? (block.key + ' ' + block.operator + ' ').length : 0);
offsetEnd = block.offsetBegin + block.expression.length - 1;
results.push(
getLintResult(
'Unavailable: ' +
(keyFound ? '' : 'Object "' + block.key + '" and ') +
'Boolean "' +
block.subkey +
'"',
),
);
}
} else if (this.strict && !(schema[block.key] && typeof schema[block.key][block.subkey] === 'boolean')) {
offsetBegin = block.offsetBegin + this.getBlockPrefix('if').length;
offsetEnd = block.offsetBegin + block.expression.length - 1;
results.push(
getLintResult(
'(STRICT) Illegal: Condition "' + (block.key + ' ' + block.operator + ' ' + block.subkey) + '"',
true,
),
);
}
this.scanBlocks(block.children, schema, params, results);
if (block.elseChildren.length) {
this.scanBlocks(block.elseChildren, schema, params, results);
}
continue;
} else if (block instanceof RepeatBlock) {
offsetBegin = block.offsetBegin + this.getBlockPrefix('repeat').length;
offsetEnd = block.offsetBegin + block.expression.length - 1;
if (!Array.isArray(params[block.key])) {
if (this.strict && Array.isArray(schema[block.key])) {
results.push(getLintResult('(STRICT) Unavailable: Array "' + block.key + '"', true));
} else {
results.push(getLintResult('Unavailable: Array "' + block.key + '"'));
}
} else if (this.strict && !Array.isArray(schema[block.key])) {
results.push(getLintResult('(STRICT) Illegal: Array "' + block.key + '"', true));
}
const mergedSchema = this.getParamsMergedForBlock(block, schema);
const mergedParams = this.getParamsMergedForBlock(block, params);
this.scanBlocks(block.children, mergedSchema, mergedParams, results);
continue;
} else if (block instanceof LiteralBlock) {
results.push(...this.scanLiteralBlock(block, schema, params));
continue;
} else {
continue;
}
}
}
private resolveLineAndColumn(res: Lint) {
const position = Utils.getPositionAt(this.builder.code, res.offset.begin);
res.position.begin.line = position.line;
res.position.begin.column = position.column;
res.position.end.line = position.line;
res.position.end.column = position.column + (res.offset.end - res.offset.begin);
return res;
}
public lint(params?: T, resolveLines = true): LinterResult {
this.build();
const lints: Lint[] = [];
const schema = this.builder.schema || {};
if (params === undefined) {
params = schema as T;
}
this.scanBlocks(this.builder.blocks, schema, params, lints);
let hasErrors = false;
let hasWarnings = false;
lints.map((lint) => {
if (lint.severity === 'error') {
hasErrors = true;
}
if (lint.severity === 'warning') {
hasWarnings = true;
}
if (resolveLines) {
this.resolveLineAndColumn(lint);
}
});
return {
lints,
hasErrors,
hasWarnings,
};
}
}