* remove unnecessary eslint disable comments

* DRY out get_slot_data

* further dry out get_slot_data

* use native repeat function

* use padStart

* delete more eslint disable comments

* update

* use whitespace pattern

* DRY out internal_globals

* remove unnecessary checks
pull/4019/head
das 6 years ago committed by Rich Harris
parent 310a4845cf
commit 2b0762f930

@ -41,6 +41,32 @@ function edit_source(source, sveltePath) {
: source; : source;
} }
function get_internal_globals(
globals: Array<{ name: string; alias: Identifier }>,
helpers: Array<{ name: string; alias: Identifier }>
) {
return globals.length > 0 && {
type: 'VariableDeclaration',
kind: 'const',
declarations: [{
type: 'VariableDeclarator',
id: {
type: 'ObjectPattern',
properties: globals.map(g => ({
type: 'Property',
method: false,
shorthand: false,
computed: false,
key: { type: 'Identifier', name: g.name },
value: g.alias,
kind: 'init'
}))
},
init: helpers.find(({ name }) => name === 'globals').alias
}]
};
}
function esm( function esm(
program: any, program: any,
name: Identifier, name: Identifier,
@ -62,26 +88,7 @@ function esm(
source: { type: 'Literal', value: internal_path } source: { type: 'Literal', value: internal_path }
}; };
const internal_globals = globals.length > 0 && { const internal_globals = get_internal_globals(globals, helpers);
type: 'VariableDeclaration',
kind: 'const',
declarations: [{
type: 'VariableDeclarator',
id: {
type: 'ObjectPattern',
properties: globals.map(g => ({
type: 'Property',
method: false,
shorthand: false,
computed: false,
key: { type: 'Identifier', name: g.name },
value: g.alias,
kind: 'init'
}))
},
init: helpers.find(({ name }) => name === 'globals').alias
}]
};
// edit user imports // edit user imports
imports.forEach(node => { imports.forEach(node => {
@ -143,26 +150,7 @@ function cjs(
}] }]
}; };
const internal_globals = globals.length > 0 && { const internal_globals = get_internal_globals(globals, helpers);
type: 'VariableDeclaration',
kind: 'const',
declarations: [{
type: 'VariableDeclarator',
id: {
type: 'ObjectPattern',
properties: globals.map(g => ({
type: 'Property',
method: false,
shorthand: false,
computed: false,
key: { type: 'Identifier', name: g.name },
value: g.alias,
kind: 'init'
}))
},
init: helpers.find(({ name }) => name === 'globals').alias
}]
};
const user_requires = imports.map(node => ({ const user_requires = imports.map(node => ({
type: 'VariableDeclaration', type: 'VariableDeclaration',

@ -75,7 +75,6 @@ export default function dom(
const props = component.vars.filter(variable => !variable.module && variable.export_name); const props = component.vars.filter(variable => !variable.module && variable.export_name);
const writable_props = props.filter(variable => variable.writable); const writable_props = props.filter(variable => variable.writable);
/* eslint-disable @typescript-eslint/indent,indent */
const set = (uses_props || writable_props.length > 0 || component.slots.size > 0) const set = (uses_props || writable_props.length > 0 || component.slots.size > 0)
? x` ? x`
${$$props} => { ${$$props} => {
@ -88,7 +87,6 @@ export default function dom(
} }
` `
: null; : null;
/* eslint-enable @typescript-eslint/indent,indent */
const accessors = []; const accessors = [];

@ -87,14 +87,12 @@ function optimize_style(value: Array<Text|Expression>) {
const remaining_data = chunk.data.slice(offset); const remaining_data = chunk.data.slice(offset);
if (remaining_data) { if (remaining_data) {
/* eslint-disable @typescript-eslint/no-object-literal-type-assertion */
chunks[0] = { chunks[0] = {
start: chunk.start + offset, start: chunk.start + offset,
end: chunk.end, end: chunk.end,
type: 'Text', type: 'Text',
data: remaining_data data: remaining_data
} as Text; } as Text;
/* eslint-enable @typescript-eslint/no-object-literal-type-assertion */
} else { } else {
chunks.shift(); chunks.shift();
} }

@ -263,7 +263,6 @@ export default class IfBlockWrapper extends Wrapper {
? x`${current_block_type}(#ctx)` ? x`${current_block_type}(#ctx)`
: x`${current_block_type} && ${current_block_type}(#ctx)`; : x`${current_block_type} && ${current_block_type}(#ctx)`;
/* eslint-disable @typescript-eslint/indent,indent */
if (this.needs_update) { if (this.needs_update) {
block.chunks.init.push(b` block.chunks.init.push(b`
function ${select_block_type}(#ctx, #dirty) { function ${select_block_type}(#ctx, #dirty) {
@ -287,7 +286,6 @@ export default class IfBlockWrapper extends Wrapper {
} }
`); `);
} }
/* eslint-enable @typescript-eslint/indent,indent */
block.chunks.init.push(b` block.chunks.init.push(b`
let ${current_block_type} = ${select_block_type}(#ctx, -1); let ${current_block_type} = ${select_block_type}(#ctx, -1);
@ -375,7 +373,6 @@ export default class IfBlockWrapper extends Wrapper {
block.add_variable(current_block_type_index); block.add_variable(current_block_type_index);
block.add_variable(name); block.add_variable(name);
/* eslint-disable @typescript-eslint/indent,indent */
block.chunks.init.push(b` block.chunks.init.push(b`
const ${if_block_creators} = [ const ${if_block_creators} = [
${this.branches.map(branch => branch.block.name)} ${this.branches.map(branch => branch.block.name)}
@ -407,7 +404,6 @@ export default class IfBlockWrapper extends Wrapper {
} }
`} `}
`); `);
/* eslint-enable @typescript-eslint/indent,indent */
if (has_else) { if (has_else) {
block.chunks.init.push(b` block.chunks.init.push(b`

@ -6,39 +6,10 @@ import FragmentWrapper from './Fragment';
import { b, p, x } from 'code-red'; import { b, p, x } from 'code-red';
import { sanitize } from '../../../utils/names'; import { sanitize } from '../../../utils/names';
import add_to_set from '../../utils/add_to_set'; import add_to_set from '../../utils/add_to_set';
import get_slot_data from '../../utils/get_slot_data';
import Expression from '../../nodes/shared/Expression'; import Expression from '../../nodes/shared/Expression';
import is_dynamic from './shared/is_dynamic'; import is_dynamic from './shared/is_dynamic';
import { Identifier, ObjectExpression } from 'estree'; import { Identifier, ObjectExpression } from 'estree';
import Attribute from '../../nodes/Attribute';
import { string_literal } from '../../utils/stringify';
function get_slot_data(block: Block, values: Map<string, Attribute>) {
return {
type: 'ObjectExpression',
properties: Array.from(values.values())
.filter(attribute => attribute.name !== 'name')
.map(attribute => {
const value = get_value(block, attribute);
return p`${attribute.name}: ${value}`;
})
};
}
// TODO fairly sure this is duplicated at least once
function get_value(block: Block, attribute: Attribute) {
if (attribute.is_true) return x`true`;
if (attribute.chunks.length === 0) return x`""`;
let value = attribute.chunks
.map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block))
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') {
value = x`"" + ${value}`;
}
return value;
}
export default class SlotWrapper extends Wrapper { export default class SlotWrapper extends Wrapper {
node: Slot; node: Slot;
@ -125,7 +96,7 @@ export default class SlotWrapper extends Wrapper {
renderer.blocks.push(b` renderer.blocks.push(b`
const ${get_slot_changes_fn} = #dirty => ${changes}; const ${get_slot_changes_fn} = #dirty => ${changes};
const ${get_slot_context_fn} = #ctx => ${get_slot_data(block, this.node.values)}; const ${get_slot_context_fn} = #ctx => ${get_slot_data(this.node.values, block)};
`); `);
} else { } else {
get_slot_changes_fn = 'null'; get_slot_changes_fn = 'null';

@ -1,37 +1,7 @@
import Renderer, { RenderOptions } from '../Renderer'; import Renderer, { RenderOptions } from '../Renderer';
import Slot from '../../nodes/Slot'; import Slot from '../../nodes/Slot';
import { x, p } from 'code-red'; import { x } from 'code-red';
import Attribute from '../../nodes/Attribute'; import get_slot_data from '../../utils/get_slot_data';
import { string_literal } from '../../utils/stringify';
// TODO this is *almost* but not quite duplicated with non-SSR
function get_slot_data(values: Map<string, Attribute>) {
return {
type: 'ObjectExpression',
properties: Array.from(values.values())
.filter(attribute => attribute.name !== 'name')
.map(attribute => {
const value = get_value(attribute);
return p`${attribute.name}: ${value}`;
})
};
}
// TODO fairly sure this is duplicated at least once
function get_value(attribute: Attribute) {
if (attribute.is_true) return x`true`;
if (attribute.chunks.length === 0) return x`""`;
let value = attribute.chunks
.map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : chunk.node)
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') {
value = x`"" + ${value}`;
}
return value;
}
export default function(node: Slot, renderer: Renderer, options: RenderOptions) { export default function(node: Slot, renderer: Renderer, options: RenderOptions) {
const slot_data = get_slot_data(node.values); const slot_data = get_slot_data(node.values);

@ -3,7 +3,7 @@ import { p, x } from 'code-red';
import { string_literal } from './stringify'; import { string_literal } from './stringify';
import Block from '../render_dom/Block'; import Block from '../render_dom/Block';
export default function get_slot_data(block: Block, values: Map<string, Attribute>) { export default function get_slot_data(values: Map<string, Attribute>, block: Block = null) {
return { return {
type: 'ObjectExpression', type: 'ObjectExpression',
properties: Array.from(values.values()) properties: Array.from(values.values())
@ -15,13 +15,12 @@ export default function get_slot_data(block: Block, values: Map<string, Attribut
}; };
} }
// TODO fairly sure this is duplicated at least once
function get_value(block: Block, attribute: Attribute) { function get_value(block: Block, attribute: Attribute) {
if (attribute.is_true) return x`true`; if (attribute.is_true) return x`true`;
if (attribute.chunks.length === 0) return x`""`; if (attribute.chunks.length === 0) return x`""`;
let value = attribute.chunks let value = attribute.chunks
.map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block)) .map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : (block ? chunk.manipulate(block) : chunk.node))
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`); .reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') { if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') {

@ -65,11 +65,11 @@ export class Parser {
} }
if (this.html.children.length) { if (this.html.children.length) {
let start = this.html.children[0] && this.html.children[0].start; let start = this.html.children[0].start;
while (/\s/.test(template[start])) start += 1; while (whitespace.test(template[start])) start += 1;
let end = this.html.children[this.html.children.length - 1] && this.html.children[this.html.children.length - 1].end; let end = this.html.children[this.html.children.length - 1].end;
while (/\s/.test(template[end - 1])) end -= 1; while (whitespace.test(template[end - 1])) end -= 1;
this.html.start = start; this.html.start = start;
this.html.end = end; this.html.end = end;

@ -13,7 +13,6 @@ export default function read_expression(parser: Parser): Node {
const end = start + name.length; const end = start + name.length;
if (literals.has(name)) { if (literals.has(name)) {
// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion
return { return {
type: 'Literal', type: 'Literal',
start, start,
@ -23,7 +22,6 @@ export default function read_expression(parser: Parser): Node {
} as SimpleLiteral; } as SimpleLiteral;
} }
// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion
return { return {
type: 'Identifier', type: 'Identifier',
start, start,

@ -1,5 +1,4 @@
import * as acorn from '../acorn'; import * as acorn from '../acorn';
import repeat from '../../utils/repeat';
import { Parser } from '../index'; import { Parser } from '../index';
import { Script } from '../../interfaces'; import { Script } from '../../interfaces';
import { Node, Program } from 'estree'; import { Node, Program } from 'estree';
@ -38,8 +37,7 @@ export default function read_script(parser: Parser, start: number, attributes: N
message: `<script> must have a closing tag` message: `<script> must have a closing tag`
}); });
const source = const source = ' '.repeat(script_start) + parser.template.slice(script_start, script_end);
repeat(' ', script_start) + parser.template.slice(script_start, script_end);
parser.index = script_end + script_closing_tag.length; parser.index = script_end + script_closing_tag.length;
let ast: Program; let ast: Program;

@ -45,7 +45,7 @@ async function replace_async(str: string, re: RegExp, func: (...any) => Promise<
str.replace(re, (...args) => { str.replace(re, (...args) => {
replacements.push( replacements.push(
func(...args).then( func(...args).then(
res => // eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion res =>
({ ({
offset: args[args.length - 2], offset: args[args.length - 2],
length: args[0].length, length: args[0].length,

@ -1,5 +1,3 @@
import repeat from './repeat';
function tabs_to_spaces(str: string) { function tabs_to_spaces(str: string) {
return str.replace(/^\t+/, match => match.split('\t').join(' ')); return str.replace(/^\t+/, match => match.split('\t').join(' '));
} }
@ -20,13 +18,10 @@ export default function get_code_frame(
.slice(frame_start, frame_end) .slice(frame_start, frame_end)
.map((str, i) => { .map((str, i) => {
const isErrorLine = frame_start + i === line; const isErrorLine = frame_start + i === line;
const line_num = String(i + frame_start + 1).padStart(digits, ' ');
let line_num = String(i + frame_start + 1);
while (line_num.length < digits) line_num = ` ${line_num}`;
if (isErrorLine) { if (isErrorLine) {
const indicator = const indicator = ' '.repeat(digits + 2 + tabs_to_spaces(str.slice(0, column)).length) + '^';
repeat(' ', digits + 2 + tabs_to_spaces(str.slice(0, column)).length) + '^';
return `${line_num}: ${tabs_to_spaces(str)}\n${indicator}`; return `${line_num}: ${tabs_to_spaces(str)}\n${indicator}`;
} }

@ -1,5 +0,0 @@
export default function repeat(str: string, i: number) {
let result = '';
while (i--) result += str;
return result;
}

@ -27,7 +27,6 @@ export function element_is<K extends keyof HTMLElementTagNameMap>(name: K, is: s
} }
export function object_without_properties<T, K extends keyof T>(obj: T, exclude: K[]) { export function object_without_properties<T, K extends keyof T>(obj: T, exclude: K[]) {
// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion
const target = {} as Pick<T, Exclude<keyof T, K>>; const target = {} as Pick<T, Exclude<keyof T, K>>;
for (const k in obj) { for (const k in obj) {
if ( if (

@ -79,7 +79,6 @@ export function spring<T=any>(value?: T, opts: SpringOpts = {}): Spring<T> {
let inv_mass_recovery_rate = 0; let inv_mass_recovery_rate = 0;
let cancel_task = false; let cancel_task = false;
/* eslint-disable @typescript-eslint/no-use-before-define */
function set(new_value: T, opts: SpringUpdateOpts={}): Promise<void> { function set(new_value: T, opts: SpringUpdateOpts={}): Promise<void> {
target_value = new_value; target_value = new_value;
const token = current_token = {}; const token = current_token = {};
@ -134,7 +133,6 @@ export function spring<T=any>(value?: T, opts: SpringOpts = {}): Spring<T> {
}); });
}); });
} }
/* eslint-enable @typescript-eslint/no-use-before-define */
const spring: Spring<T> = { const spring: Spring<T> = {
set, set,

Loading…
Cancel
Save