|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -const { compile } = require('svelte/compiler'); |
| 3 | +const { compile, walk } = require('svelte/compiler'); |
4 | 4 |
|
5 |
| -let compiler_options, messages, transformed_code, ignore_warnings, translations; |
| 5 | +let compiler_options, messages, transformed_code, ignore_warnings, lint_template, translations; |
6 | 6 |
|
7 | 7 | // get the total length, number of lines, and length of the last line of a string
|
8 | 8 | const get_offsets = str => {
|
@@ -205,6 +205,48 @@ const preprocess = text => {
|
205 | 205 | transformed_code += `\n{${reassigned_vars.map(v => v.name + '=0').join(';')}}`;
|
206 | 206 | }
|
207 | 207 |
|
| 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 | + |
208 | 250 | // reverse sort the translations
|
209 | 251 | translations.sort((a, b) => b.unoffsets.length - a.unoffsets.length);
|
210 | 252 |
|
@@ -262,6 +304,7 @@ Linter.prototype.verify = function(code, config, options) {
|
262 | 304 | // lint this Svelte file
|
263 | 305 | options = Object.assign({}, options, { preprocess, postprocess });
|
264 | 306 | ignore_warnings = get_setting_function(config, 'svelte3/ignore-warnings', false);
|
| 307 | + lint_template = get_setting_function(config, 'svelte3/lint-template', () => false)(options.filename); |
265 | 308 | const ignore_styles = get_setting_function(config, 'svelte3/ignore-styles', false);
|
266 | 309 | if (ignore_styles) {
|
267 | 310 | // wipe the appropriate <style> tags in the file
|
|
0 commit comments