Skip to content

Commit ea62454

Browse files
committed
prepare for arbitrary number of segments of code
1 parent 2e654e3 commit ea62454

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

index.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { compile } = require('svelte/compiler');
44

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

77
// get the total length, number of lines, and length of the last line of a string
88
const get_offsets = str => {
@@ -54,6 +54,9 @@ const dedent_code = str => {
5454

5555
// transform a linting message according to the module/instance script info we've gathered
5656
const transform_message = (message, { unoffsets, dedent, offsets, range }) => {
57+
if (message.line < unoffsets.lines) {
58+
return false;
59+
}
5760
// strip out the start and end of the fix if they are not actually changes
5861
if (message.fix) {
5962
while (transformed_code[message.fix.range[0]] === message.fix.text[0]) {
@@ -124,7 +127,7 @@ const transform_message = (message, { unoffsets, dedent, offsets, range }) => {
124127
message.fix.range[1] = range[1];
125128
}
126129
}
127-
return message;
130+
return true;
128131
};
129132

130133
/// PRE- AND POSTPROCESSING FUNCTIONS FOR SVELTE COMPONENTS ///
@@ -166,29 +169,30 @@ const preprocess = text => {
166169
endColumn: end && end.column + 1,
167170
}));
168171

169-
if (!ast.module && !ast.instance) {
170-
return [];
171-
}
172-
173172
// build a string that we can send along to ESLint to get the remaining messages
174173

175174
// include declarations of all injected identifiers
176175
transformed_code = injected_vars.length ? `/* eslint-disable */let ${injected_vars.map(v => v.name).join(',')};\n/* eslint-enable */` : '';
177176

178-
// get module_info/instance_info and include the processed scripts in transformed_code
179-
const get_info = script => {
180-
const info = { unoffsets: get_offsets(transformed_code) };
181-
const { content } = script;
182-
info.range = [content.start, content.end];
183-
const { dedented, offsets } = dedent_code(text.slice(content.start, content.end));
177+
// get translation info and include the processed scripts in transformed_code
178+
const get_translation = node => {
179+
const translation = { unoffsets: get_offsets(transformed_code) };
180+
translation.range = [node.start, node.end];
181+
const { dedented, offsets } = dedent_code(text.slice(node.start, node.end));
184182
transformed_code += dedented;
185-
info.offsets = get_offsets(text.slice(0, content.start));
186-
info.dedent = offsets;
187-
return info;
183+
translation.offsets = get_offsets(text.slice(0, node.start));
184+
translation.dedent = offsets;
185+
translations.push(translation);
188186
};
189-
module_info = ast.module && get_info(ast.module);
187+
188+
translations = [];
189+
if (ast.module) {
190+
get_translation(ast.module.content);
191+
}
190192
transformed_code += '/* eslint-disable */\n/* eslint-enable */';
191-
instance_info = ast.instance && get_info(ast.instance);
193+
if (ast.instance) {
194+
get_translation(ast.instance.content);
195+
}
192196
transformed_code += '/* eslint-disable */';
193197

194198
// no-unused-vars: create references to all identifiers referred to by the template
@@ -201,6 +205,9 @@ const preprocess = text => {
201205
transformed_code += `\n{${reassigned_vars.map(v => v.name + '=0').join(';')}}`;
202206
}
203207

208+
// reverse sort the translations
209+
translations.sort((a, b) => b.unoffsets.length - a.unoffsets.length);
210+
204211
// return processed string
205212
return [transformed_code];
206213
};
@@ -212,10 +219,11 @@ const postprocess = ([raw_messages]) => {
212219
for (let i = 0; i < raw_messages.length; i++) {
213220
const message = raw_messages[i];
214221
if (message.ruleId !== 'no-self-assign' && (message.ruleId !== 'no-unused-labels' || !message.message.includes("'$:'"))) {
215-
if (instance_info && message.line >= instance_info.unoffsets.lines) {
216-
messages.push(transform_message(message, instance_info));
217-
} else if (module_info) {
218-
messages.push(transform_message(message, module_info));
222+
for (let k = 0; k < translations.length; k++) {
223+
if (transform_message(message, translations[k])) {
224+
messages.push(message);
225+
break;
226+
}
219227
}
220228
}
221229
}

0 commit comments

Comments
 (0)