Skip to content

Commit 86c08b8

Browse files
committed
make some code style adjustments
1 parent b7ca052 commit 86c08b8

File tree

5 files changed

+17
-23
lines changed

5 files changed

+17
-23
lines changed

src/mapping.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class GeneratedFragmentMapper {
1212
}
1313

1414
offset_in_fragment(offset) {
15-
return offset - this.diff.generated_start
15+
return offset - this.diff.generated_start;
1616
}
1717
}
1818

@@ -91,7 +91,7 @@ export class DocumentMapper {
9191
generated_fragment_mapper: new GeneratedFragmentMapper(generated_code, diff),
9292
source_mapper: new SourceMapper(diff.map),
9393
original_fragment_mapper: new OriginalFragmentMapper(original_code, diff)
94-
}
94+
};
9595
});
9696
}
9797

src/postprocess.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const transform_message = ({ transformed_code }, { unoffsets, dedent, indentatio
5757
// We need the offset at the line relative to dedent's total_offsets. The dedents
5858
// start at the point in the total transformed code where a subset of the code was transformed.
5959
// Therefore substract (unoffsets.lines + 1) which marks the start of that transformation.
60-
// Add +1 afterwards because total_offsets are 1-index-based.
60+
// Add +1 afterwards because total_offsets are 1-index-based.
6161
message.fix.range[0] += total_offsets[fix_pos_start.line - unoffsets.lines + 2];
6262
message.fix.range[1] += total_offsets[fix_pos_end.line - unoffsets.lines + 2];
6363
}
@@ -120,9 +120,9 @@ const is_valid_message = (block, message, translation) => {
120120
case 'no-restricted-syntax': return message.nodeType !== 'LabeledStatement' || get_identifier(get_referenced_string(block, message)) !== '$';
121121
case 'no-self-assign': return !state.var_names.has(get_identifier(get_referenced_string(block, message)));
122122
case 'no-undef': return get_referenced_string(block, message) !== '$$Generic';
123+
case 'no-unused-labels': return get_referenced_string(block, message) !== '$';
123124
case '@typescript-eslint/no-unused-vars':
124125
case 'no-unused-vars': return !['$$Props', '$$Slots', '$$Events'].includes(get_referenced_string(block, message));
125-
case 'no-unused-labels': return get_referenced_string(block, message) !== '$';
126126
case '@typescript-eslint/quotes':
127127
case 'quotes': return !translation.options.in_quoted_attribute;
128128
}

src/preprocess.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ const find_contextual_names = (compiler, node) => {
2222
}
2323
}
2424
};
25-
// let-declaration when using TypeScript which is able to infer the type value of a autosubscribed store
26-
const tsLet = (name) =>
27-
name[0] === '$' ?
25+
// let-declaration that (when using TypeScript) is able to infer the type value of a autosubscribed store
26+
const make_let = (name, is_typescript) =>
27+
is_typescript && name[0] === '$' ?
2828
// Disable eslint on that line because it may result in a "used before defined" error
2929
`declare let ${name}:Parameters<Parameters<typeof ${name.slice(1)}.subscribe>[0]>[0]; // eslint-disable-line\n` :
3030
`let ${name};`;
31-
// ignore_styles when a `lang=` or `type=` attribute is present on the <style> tag
32-
const ignoreStylesFallback = ({ type, lang }) => !!type || !!lang;
3331

32+
// ignore_styles when a `lang=` or `type=` attribute is present on the <style> tag
33+
const ignore_styles_fallback = ({ type, lang }) => !!type || !!lang;
3434

3535
// extract scripts to lint from component definition
3636
export const preprocess = text => {
3737
const compiler = processor_options.custom_compiler || default_compiler || (default_compiler = require('svelte/compiler'));
38-
const ignore_styles = processor_options.ignore_styles ? processor_options.ignore_styles : ignoreStylesFallback;
38+
const ignore_styles = processor_options.ignore_styles ? processor_options.ignore_styles : ignore_styles_fallback;
3939
if (ignore_styles) {
4040
// wipe the appropriate <style> tags in the file
4141
text = text.replace(/<style(\s[^]*?)?>[^]*?<\/style>/gi, (match, attributes = '') => {
@@ -121,15 +121,9 @@ export const preprocess = text => {
121121
const block = new_block();
122122
state.blocks.set(with_file_ending('instance'), block);
123123

124+
block.transformed_code = vars.filter(v => v.injected || !processor_options.typescript && v.module).map(v => make_let(v.name, processor_options.typescript)).join('');
124125
if (ast.module && processor_options.typescript) {
125-
block.transformed_code = vars.filter(v => v.injected).map(v => tsLet(v.name)).join('');
126126
block.transformed_code += text.slice(ast.module.content.start, ast.module.content.end);
127-
} else {
128-
if (processor_options.typescript) {
129-
block.transformed_code = vars.filter(v => v.injected || v.module).map(v => tsLet(v.name)).join('');
130-
} else {
131-
block.transformed_code = vars.filter(v => v.injected || v.module).map(v => `let ${v.name};`).join('');
132-
}
133127
}
134128

135129
get_translation(text, block, ast.instance.content);
@@ -150,7 +144,7 @@ export const preprocess = text => {
150144
if (ast.instance || vars.length) {
151145
block.transformed_code += '\n';
152146
}
153-
block.transformed_code += vars.filter(v => v.injected).map(v => tsLet(v.name)).join('');
147+
block.transformed_code += vars.filter(v => v.injected).map(v => make_let(v.name, true)).join('');
154148
if (ast.instance) {
155149
block.transformed_code += text.slice(ast.instance.content.start, ast.instance.content.end);
156150
}
@@ -257,10 +251,10 @@ function compile_code(text, compiler, processor_options) {
257251
transformers: {
258252
before: [ts_import_transformer]
259253
}
260-
}
254+
};
261255

262256
// See if we can use `preserveValueImports` instead of the transformer (TS >= 4.5)
263-
const ts_version = ts.version.split(".").map(str => parseInt(str, 10));
257+
const ts_version = ts.version.split('.').map(str => parseInt(str, 10));
264258
if (ts_version[0] > 4 || (ts_version[0] === 4 && ts_version[1] >= 5)) {
265259
ts_options.compilerOptions.preserveValueImports = true;
266260
ts_options.transformers = {};
@@ -307,7 +301,7 @@ function compile_code(text, compiler, processor_options) {
307301
// if we do a full recompile Svelte can fail due to the blank script tag not declaring anything
308302
// so instead we just parse for the AST (which is likely faster, anyways)
309303
const ast = compiler.parse(text, { ...processor_options.compiler_options });
310-
const{ warnings, vars } = ts_result;
304+
const { warnings, vars } = ts_result;
311305
return { ast, warnings, vars, mapper };
312306
}
313307
}

src/processor_options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (!linter_paths.length) {
88
// There may be more than one instance of the linter when we're in a workspace with multiple directories.
99
// We first try to find the one that's inside the same node_modules directory as this plugin.
1010
// If that can't be found for some reason, we assume the one we want is the last one in the array.
11-
const current_node_modules_path = __dirname.replace(/(?<=[/\\]node_modules[/\\]).*$/, '')
11+
const current_node_modules_path = __dirname.replace(/(?<=[/\\]node_modules[/\\]).*$/, '');
1212
const linter_path = linter_paths.find(path => path.startsWith(current_node_modules_path)) || linter_paths.pop();
1313
const { Linter } = require(linter_path);
1414

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function run() {
3535

3636
assert.equal(actual.length, expected.length);
3737
assert.deepStrictEqual(actual, actual.map((msg, i) => ({ ...msg, ...expected[i] })));
38-
38+
3939
if (fs.existsSync(path_fixed)) {
4040
const fixed = SourceCodeFixer.applyFixes(fs.readFileSync(path_input, 'utf-8'), actual).output;
4141
assert.deepStrictEqual(fixed, fs.readFileSync(path_fixed, 'utf-8'))

0 commit comments

Comments
 (0)