* 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,28 +41,11 @@ function edit_source(source, sveltePath) {
: source;
}
function esm(
program: any,
name: Identifier,
banner: string,
sveltePath: string,
internal_path: string,
helpers: Array<{ name: string; alias: Identifier }>,
function get_internal_globals(
globals: Array<{ name: string; alias: Identifier }>,
imports: ImportDeclaration[],
module_exports: Export[]
helpers: Array<{ name: string; alias: Identifier }>
) {
const import_declaration = {
type: 'ImportDeclaration',
specifiers: helpers.map(h => ({
type: 'ImportSpecifier',
local: h.alias,
imported: { type: 'Identifier', name: h.name }
})),
source: { type: 'Literal', value: internal_path }
};
const internal_globals = globals.length > 0 && {
return globals.length > 0 && {
type: 'VariableDeclaration',
kind: 'const',
declarations: [{
@ -82,6 +65,30 @@ function esm(
init: helpers.find(({ name }) => name === 'globals').alias
}]
};
}
function esm(
program: any,
name: Identifier,
banner: string,
sveltePath: string,
internal_path: string,
helpers: Array<{ name: string; alias: Identifier }>,
globals: Array<{ name: string; alias: Identifier }>,
imports: ImportDeclaration[],
module_exports: Export[]
) {
const import_declaration = {
type: 'ImportDeclaration',
specifiers: helpers.map(h => ({
type: 'ImportSpecifier',
local: h.alias,
imported: { type: 'Identifier', name: h.name }
})),
source: { type: 'Literal', value: internal_path }
};
const internal_globals = get_internal_globals(globals, helpers);
// edit user imports
imports.forEach(node => {
@ -143,26 +150,7 @@ function cjs(
}]
};
const internal_globals = 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
}]
};
const internal_globals = get_internal_globals(globals, helpers);
const user_requires = imports.map(node => ({
type: 'VariableDeclaration',

@ -75,7 +75,6 @@ export default function dom(
const props = component.vars.filter(variable => !variable.module && variable.export_name);
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)
? x`
${$$props} => {
@ -88,7 +87,6 @@ export default function dom(
}
`
: null;
/* eslint-enable @typescript-eslint/indent,indent */
const accessors = [];

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

@ -263,7 +263,6 @@ export default class IfBlockWrapper extends Wrapper {
? x`${current_block_type}(#ctx)`
: x`${current_block_type} && ${current_block_type}(#ctx)`;
/* eslint-disable @typescript-eslint/indent,indent */
if (this.needs_update) {
block.chunks.init.push(b`
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`
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(name);
/* eslint-disable @typescript-eslint/indent,indent */
block.chunks.init.push(b`
const ${if_block_creators} = [
${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) {
block.chunks.init.push(b`

@ -6,39 +6,10 @@ import FragmentWrapper from './Fragment';
import { b, p, x } from 'code-red';
import { sanitize } from '../../../utils/names';
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 is_dynamic from './shared/is_dynamic';
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 {
node: Slot;
@ -125,7 +96,7 @@ export default class SlotWrapper extends Wrapper {
renderer.blocks.push(b`
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 {
get_slot_changes_fn = 'null';

@ -1,37 +1,7 @@
import Renderer, { RenderOptions } from '../Renderer';
import Slot from '../../nodes/Slot';
import { x, p } from 'code-red';
import Attribute from '../../nodes/Attribute';
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;
}
import { x } from 'code-red';
import get_slot_data from '../../utils/get_slot_data';
export default function(node: Slot, renderer: Renderer, options: RenderOptions) {
const slot_data = get_slot_data(node.values);

@ -3,7 +3,7 @@ import { p, x } from 'code-red';
import { string_literal } from './stringify';
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 {
type: 'ObjectExpression',
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) {
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))
.map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : (block ? chunk.manipulate(block) : chunk.node))
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') {

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

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

@ -1,5 +1,4 @@
import * as acorn from '../acorn';
import repeat from '../../utils/repeat';
import { Parser } from '../index';
import { Script } from '../../interfaces';
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`
});
const source =
repeat(' ', script_start) + parser.template.slice(script_start, script_end);
const source = ' '.repeat(script_start) + parser.template.slice(script_start, script_end);
parser.index = script_end + script_closing_tag.length;
let ast: Program;

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

@ -1,5 +1,3 @@
import repeat from './repeat';
function tabs_to_spaces(str: string) {
return str.replace(/^\t+/, match => match.split('\t').join(' '));
}
@ -20,13 +18,10 @@ export default function get_code_frame(
.slice(frame_start, frame_end)
.map((str, i) => {
const isErrorLine = frame_start + i === line;
let line_num = String(i + frame_start + 1);
while (line_num.length < digits) line_num = ` ${line_num}`;
const line_num = String(i + frame_start + 1).padStart(digits, ' ');
if (isErrorLine) {
const indicator =
repeat(' ', digits + 2 + tabs_to_spaces(str.slice(0, column)).length) + '^';
const indicator = ' '.repeat(digits + 2 + tabs_to_spaces(str.slice(0, column)).length) + '^';
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[]) {
// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion
const target = {} as Pick<T, Exclude<keyof T, K>>;
for (const k in obj) {
if (

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

Loading…
Cancel
Save