Skip to content

Commit e882cc7

Browse files
committed
add opt-in linting of templates
1 parent ea62454 commit e882cc7

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

index.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const { compile } = require('svelte/compiler');
3+
const { compile, walk } = require('svelte/compiler');
44

5-
let compiler_options, messages, transformed_code, ignore_warnings, translations;
5+
let compiler_options, messages, transformed_code, ignore_warnings, lint_template, translations;
66

77
// get the total length, number of lines, and length of the last line of a string
88
const get_offsets = str => {
@@ -205,6 +205,48 @@ const preprocess = text => {
205205
transformed_code += `\n{${reassigned_vars.map(v => v.name + '=0').join(';')}}`;
206206
}
207207

208+
// add expressions from template to the constructed string
209+
if (lint_template && ast.html) {
210+
transformed_code += '\n/* eslint-enable *//* eslint indent: 0, quotes: 0, semi: 0 */';
211+
// find all expressions in the AST
212+
walk(ast.html, {
213+
enter(node) {
214+
if (node.context && typeof node.context === 'object') {
215+
// find all the variables declared in this context (potentially involving spreads)
216+
const names = [];
217+
walk(node.context, {
218+
enter(node) {
219+
if (node.name) {
220+
names.push(node.name);
221+
}
222+
delete node.key;
223+
},
224+
});
225+
transformed_code += `/* eslint-disable */{${names.map(name => `let ${name}=0;`).join('')}/* eslint-enable */\n`;
226+
}
227+
if (node.index && typeof node.index === 'string') {
228+
// declare the index variable, if present
229+
transformed_code += `/* eslint-disable */{let ${node.index}=0;/* eslint-enable */\n`;
230+
}
231+
if (node.expression && typeof node.expression === 'object') {
232+
// add the expression in question to the constructed string
233+
get_translation(node.expression);
234+
transformed_code += ';\n';
235+
delete node.expression;
236+
}
237+
},
238+
leave(node) {
239+
// close nested scopes created for context or index
240+
if (node.context && typeof node.context === 'object') {
241+
transformed_code += '/* eslint-disable */}/* eslint-enable */\n';
242+
}
243+
if (node.index && typeof node.index === 'string') {
244+
transformed_code += '/* eslint-disable */}/* eslint-enable */\n';
245+
}
246+
},
247+
});
248+
}
249+
208250
// reverse sort the translations
209251
translations.sort((a, b) => b.unoffsets.length - a.unoffsets.length);
210252

@@ -262,6 +304,7 @@ Linter.prototype.verify = function(code, config, options) {
262304
// lint this Svelte file
263305
options = Object.assign({}, options, { preprocess, postprocess });
264306
ignore_warnings = get_setting_function(config, 'svelte3/ignore-warnings', false);
307+
lint_template = get_setting_function(config, 'svelte3/lint-template', () => false)(options.filename);
265308
const ignore_styles = get_setting_function(config, 'svelte3/ignore-styles', false);
266309
if (ignore_styles) {
267310
// wipe the appropriate <style> tags in the file

0 commit comments

Comments
 (0)