more tidying up

pull/2252/head
Richard Harris 7 years ago
parent cc92f8fdcd
commit bbb5b1c57a

@ -163,7 +163,7 @@ export default class Component {
if (!compile_options.customElement) this.stylesheet.reify();
this.stylesheet.warnOnUnusedSelectors(this);
this.stylesheet.warn_on_unused_selectors(this);
}
add_var(variable: Var) {

@ -210,7 +210,7 @@ const operators = {
'*=': (value: string, flags: string) => new RegExp(value, flags)
};
function attribute_matches(node: Node, name: string, expected_value: string, operator: string, caseInsensitive: boolean) {
function attribute_matches(node: Node, name: string, expected_value: string, operator: string, case_insensitive: boolean) {
const spread = node.attributes.find(attr => attr.type === 'Spread');
if (spread) return true;
@ -220,7 +220,7 @@ function attribute_matches(node: Node, name: string, expected_value: string, ope
if (attr.chunks.length > 1) return true;
if (!expected_value) return true;
const pattern = operators[operator](expected_value, caseInsensitive ? 'i' : '');
const pattern = operators[operator](expected_value, case_insensitive ? 'i' : '');
const value = attr.chunks[0];
if (!value) return false;

@ -391,7 +391,7 @@ export default class Stylesheet {
});
}
warnOnUnusedSelectors(component: Component) {
warn_on_unused_selectors(component: Component) {
this.children.forEach(child => {
child.warn_on_unused_selector((selector: Selector) => {
component.warn(selector.node, {

@ -1,8 +1,8 @@
import { assign } from '../internal';
import Stats from '../Stats';
import parse from '../parse/index';
import renderDOM from './render-dom/index';
import renderSSR from './render-ssr/index';
import render_dom from './render-dom/index';
import render_ssr from './render-ssr/index';
import { CompileOptions, Ast, Warning } from '../interfaces';
import Component from './Component';
import fuzzymatch from '../utils/fuzzymatch';
@ -100,8 +100,8 @@ export default function compile(source: string, options: CompileOptions = {}) {
const js = options.generate === false
? null
: options.generate === 'ssr'
? renderSSR(component, options)
: renderDOM(component, options);
? render_ssr(component, options)
: render_dom(component, options);
return component.generate(js);
}

@ -27,8 +27,8 @@ export default class Slot extends Element {
});
}
const slotName = attr.value[0].data;
if (slotName === 'default') {
const slot_name = attr.value[0].data;
if (slot_name === 'default') {
component.error(attr, {
code: `invalid-slot-name`,
message: `default is a reserved word — it cannot be used as a slot name`
@ -39,11 +39,11 @@ export default class Slot extends Element {
// TODO should duplicate slots be disallowed? Feels like it's more likely to be a
// bug than anything. Perhaps it should be a warning
// if (validator.slots.has(slotName)) {
// validator.error(`duplicate '${slotName}' <slot> element`, nameAttribute.start);
// if (validator.slots.has(slot_name)) {
// validator.error(`duplicate '${slot_name}' <slot> element`, nameAttribute.start);
// }
// validator.slots.add(slotName);
// validator.slots.add(slot_name);
});
// if (node.attributes.length === 0) && validator.slots.has('default')) {

@ -13,7 +13,7 @@ import get_object from '../../utils/get_object';
import { nodes_match } from '../../../utils/nodes_match';
import Block from '../../render-dom/Block';
const binaryOperators: Record<string, number> = {
const binary_operators: Record<string, number> = {
'**': 15,
'*': 14,
'/': 14,
@ -38,7 +38,7 @@ const binaryOperators: Record<string, number> = {
'|': 7
};
const logicalOperators: Record<string, number> = {
const logical_operators: Record<string, number> = {
'&&': 6,
'||': 5
};
@ -52,8 +52,8 @@ const precedence: Record<string, (node?: Node) => number> = {
CallExpression: () => 19,
UpdateExpression: () => 17,
UnaryExpression: () => 16,
BinaryExpression: (node: Node) => binaryOperators[node.operator],
LogicalExpression: (node: Node) => logicalOperators[node.operator],
BinaryExpression: (node: Node) => binary_operators[node.operator],
LogicalExpression: (node: Node) => logical_operators[node.operator],
ConditionalExpression: () => 4,
AssignmentExpression: () => 3,
YieldExpression: () => 2,

@ -35,7 +35,7 @@ export default function dom(
const css = component.stylesheet.render(options.filename, !options.customElement);
const styles = component.stylesheet.has_styles && stringify(options.dev ?
`${css.code}\n/*# sourceMappingURL=${css.map.toUrl()} */` :
css.code, { onlyEscapeAtSymbol: true });
css.code, { only_escape_at_symbol: true });
if (styles && component.compile_options.css !== false && !options.customElement) {
builder.add_block(deindent`
@ -417,7 +417,7 @@ export default function dom(
constructor(options) {
super();
${css.code && `this.shadowRoot.innerHTML = \`<style>${escape(css.code, { onlyEscapeAtSymbol: true }).replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}
${css.code && `this.shadowRoot.innerHTML = \`<style>${escape(css.code, { only_escape_at_symbol: true }).replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}
@init(this, { target: this.shadowRoot }, ${definition}, create_fragment, ${not_equal}, ${prop_names});

@ -91,8 +91,8 @@ export default class BindingWrapper {
this.node.expression.dependencies.forEach((prop: string) => {
const indirect_dependencies = this.parent.renderer.component.indirect_dependencies.get(prop);
if (indirect_dependencies) {
indirect_dependencies.forEach(indirectDependency => {
dependencies.add(indirectDependency);
indirect_dependencies.forEach(indirect_dependency => {
dependencies.add(indirect_dependency);
});
}
});
@ -127,14 +127,14 @@ export default class BindingWrapper {
// special cases
switch (this.node.name) {
case 'group':
const bindingGroup = get_binding_group(parent.renderer, this.node.expression.node);
const binding_group = get_binding_group(parent.renderer, this.node.expression.node);
block.builders.hydrate.add_line(
`ctx.$$binding_groups[${bindingGroup}].push(${parent.var});`
`ctx.$$binding_groups[${binding_group}].push(${parent.var});`
);
block.builders.destroy.add_line(
`ctx.$$binding_groups[${bindingGroup}].splice(ctx.$$binding_groups[${bindingGroup}].indexOf(${parent.var}), 1);`
`ctx.$$binding_groups[${binding_group}].splice(ctx.$$binding_groups[${binding_group}].indexOf(${parent.var}), 1);`
);
break;
@ -295,9 +295,9 @@ function get_value_from_dom(
// <input type='checkbox' bind:group='foo'>
if (name === 'group') {
const bindingGroup = get_binding_group(renderer, binding.node.expression.node);
const binding_group = get_binding_group(renderer, binding.node.expression.node);
if (type === 'checkbox') {
return `@get_binding_group_value($$binding_groups[${bindingGroup}])`;
return `@get_binding_group_value($$binding_groups[${binding_group}])`;
}
return `this.__value`;

@ -777,8 +777,8 @@ export default class ElementWrapper extends Wrapper {
}
add_classes(block: Block) {
this.node.classes.forEach(classDir => {
const { expression, name } = classDir;
this.node.classes.forEach(class_directive => {
const { expression, name } = class_directive;
let snippet, dependencies;
if (expression) {
snippet = expression.render(block);
@ -821,16 +821,16 @@ export default class ElementWrapper extends Wrapper {
return null;
}
add_css_class(className = this.component.stylesheet.id) {
add_css_class(class_name = this.component.stylesheet.id) {
const class_attribute = this.attributes.find(a => a.name === 'class');
if (class_attribute && !class_attribute.is_true) {
if (class_attribute.chunks.length === 1 && class_attribute.chunks[0].type === 'Text') {
(class_attribute.chunks[0] as Text).data += ` ${className}`;
(class_attribute.chunks[0] as Text).data += ` ${class_name}`;
} else {
(class_attribute.chunks as Node[]).push(
new Text(this.component, this, this.scope, {
type: 'Text',
data: ` ${className}`
data: ` ${class_name}`
})
);
}
@ -839,7 +839,7 @@ export default class ElementWrapper extends Wrapper {
new Attribute(this.component, this, this.scope, {
type: 'Attribute',
name: 'class',
value: [{ type: 'Text', data: className }]
value: [{ type: 'Text', data: class_name }]
})
);
}

@ -139,9 +139,9 @@ export default class FragmentWrapper {
}
}
render(block: Block, parentNode: string, parent_nodes: string) {
render(block: Block, parent_node: string, parent_nodes: string) {
for (let i = 0; i < this.nodes.length; i += 1) {
this.nodes[i].render(block, parentNode, parent_nodes);
this.nodes[i].render(block, parent_node, parent_nodes);
}
}
}

@ -100,7 +100,7 @@ export default class InlineComponentWrapper extends Wrapper {
render(
block: Block,
parentNode: string,
parent_node: string,
parent_nodes: string
) {
const { renderer } = this;
@ -122,7 +122,7 @@ export default class InlineComponentWrapper extends Wrapper {
const slot_props = Array.from(this.slots).map(([name, slot]) => `$$slot_${sanitize(name)}: [${slot.block.name}${slot.fn ? `, ${slot.fn}` : ''}]`);
if (slot_props.length > 0) slot_props.push(`$$scope: { ctx }`);
const attributeObject = uses_spread
const attribute_object = uses_spread
? stringify_props(slot_props)
: stringify_props(
this.node.attributes.map(attr => `${quote_name_if_necessary(attr.name)}: ${attr.get_value(block)}`).concat(slot_props)
@ -130,7 +130,7 @@ export default class InlineComponentWrapper extends Wrapper {
if (this.node.attributes.length || this.node.bindings.length || slot_props.length) {
if (!uses_spread && this.node.bindings.length === 0) {
component_opts.push(`props: ${attributeObject}`);
component_opts.push(`props: ${attribute_object}`);
} else {
props = block.get_unique_name(`${name}_props`);
component_opts.push(`props: ${props}`);
@ -375,7 +375,7 @@ export default class InlineComponentWrapper extends Wrapper {
function ${switch_props}(ctx) {
${(this.node.attributes.length || this.node.bindings.length) && deindent`
${props && `let ${props} = ${attributeObject};`}`}
${props && `let ${props} = ${attribute_object};`}`}
${statements}
return ${stringify_props(component_opts)};
}
@ -400,11 +400,11 @@ export default class InlineComponentWrapper extends Wrapper {
block.builders.mount.add_block(deindent`
if (${name}) {
@mount_component(${name}, ${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});
@mount_component(${name}, ${parent_node || '#target'}, ${parent_node ? 'null' : 'anchor'});
}
`);
const anchor = this.get_or_create_anchor(block, parentNode, parent_nodes);
const anchor = this.get_or_create_anchor(block, parent_node, parent_nodes);
const update_mount_node = this.get_update_mount_node(anchor);
if (updates.length) {
@ -458,7 +458,7 @@ export default class InlineComponentWrapper extends Wrapper {
`if (${name}) ${name}.$$.fragment.o(#local);`
);
block.builders.destroy.add_line(`if (${name}) ${name}.$destroy(${parentNode ? '' : 'detaching'});`);
block.builders.destroy.add_line(`if (${name}) ${name}.$destroy(${parent_node ? '' : 'detaching'});`);
} else {
const expression = this.node.name === 'svelte:self'
? '__svelte:self__' // TODO conflict-proof this
@ -466,7 +466,7 @@ export default class InlineComponentWrapper extends Wrapper {
block.builders.init.add_block(deindent`
${(this.node.attributes.length || this.node.bindings.length) && deindent`
${props && `let ${props} = ${attributeObject};`}`}
${props && `let ${props} = ${attribute_object};`}`}
${statements}
var ${name} = new ${expression}(${stringify_props(component_opts)});
@ -483,7 +483,7 @@ export default class InlineComponentWrapper extends Wrapper {
}
block.builders.mount.add_line(
`@mount_component(${name}, ${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});`
`@mount_component(${name}, ${parent_node || '#target'}, ${parent_node ? 'null' : 'anchor'});`
);
block.builders.intro.add_block(deindent`
@ -499,7 +499,7 @@ export default class InlineComponentWrapper extends Wrapper {
}
block.builders.destroy.add_block(deindent`
${name}.$destroy(${parentNode ? '' : 'detaching'});
${name}.$destroy(${parent_node ? '' : 'detaching'});
`);
block.builders.outro.add_line(

@ -99,7 +99,7 @@ export default class SlotWrapper extends Wrapper {
const ${slot} = @create_slot(${slot_definition}, ctx, ${get_slot_context});
`);
let mountBefore = block.builders.mount.toString();
let mount_before = block.builders.mount.toString();
block.builders.create.push_condition(`!${slot}`);
block.builders.hydrate.push_condition(`!${slot}`);
@ -127,7 +127,7 @@ export default class SlotWrapper extends Wrapper {
`if (${slot}) ${slot}.l(${parent_nodes});`
);
const mount_leadin = block.builders.mount.toString() !== mountBefore
const mount_leadin = block.builders.mount.toString() !== mount_before
? `else`
: `if (${slot})`;

@ -46,8 +46,8 @@ export default class Renderer {
append(code: string) {
if (this.targets.length) {
const target = this.targets[this.targets.length - 1];
const slotName = target.slot_stack[target.slot_stack.length - 1];
target.slots[slotName] += code;
const slot_name = target.slot_stack[target.slot_stack.length - 1];
target.slots[slot_name] += code;
} else {
this.code += code;
}

@ -53,12 +53,12 @@ export default function(node, renderer, options) {
const slot = node.get_static_attribute_value('slot');
if (slot && node.has_ancestor('InlineComponent')) {
const slot = node.attributes.find((attribute: Node) => attribute.name === 'slot');
const slotName = slot.chunks[0].data;
const slot_name = slot.chunks[0].data;
const target = renderer.targets[renderer.targets.length - 1];
target.slot_stack.push(slotName);
target.slots[slotName] = '';
target.slot_stack.push(slot_name);
target.slots[slot_name] = '';
options.slot_scopes.set(slotName, get_slot_scope(node.lets));
options.slot_scopes.set(slot_name, get_slot_scope(node.lets));
}
const class_expression = node.classes.map((class_directive: Class) => {

@ -74,9 +74,9 @@ function find_line(chunk: BlockChunk) {
return false;
}
function chunk_to_string(chunk: Chunk, level: number = 0, lastBlock?: boolean, first?: boolean): string {
function chunk_to_string(chunk: Chunk, level: number = 0, last_block?: boolean, first?: boolean): string {
if (chunk.type === 'line') {
return `${lastBlock || (!first && chunk.block) ? '\n' : ''}${chunk.line.replace(/^/gm, repeat('\t', level))}`;
return `${last_block || (!first && chunk.block) ? '\n' : ''}${chunk.line.replace(/^/gm, repeat('\t', level))}`;
} else if (chunk.type === 'condition') {
let t = false;
const lines = chunk.children.map((c, i) => {
@ -87,7 +87,7 @@ function chunk_to_string(chunk: Chunk, level: number = 0, lastBlock?: boolean, f
if (!lines.length) return '';
return `${lastBlock || (!first) ? '\n' : ''}${repeat('\t', level)}if (${chunk.condition}) {\n${lines.join('\n')}\n${repeat('\t', level)}}`;
return `${last_block || (!first) ? '\n' : ''}${repeat('\t', level)}if (${chunk.condition}) {\n${lines.join('\n')}\n${repeat('\t', level)}}`;
} else if (chunk.type === 'root') {
let t = false;
const lines = chunk.children.map((c, i) => {

@ -4,7 +4,7 @@ export default function flatten_reference(node: Node) {
if (node.type === 'Expression') throw new Error('bad');
const nodes = [];
const parts = [];
const propEnd = node.end;
const prop_end = node.end;
while (node.type === 'MemberExpression') {
if (node.computed) return null;
@ -15,7 +15,7 @@ export default function flatten_reference(node: Node) {
node = node.object;
}
const propStart = node.end;
const prop_start = node.end;
const name = node.type === 'Identifier'
? node.name
: node.type === 'ThisExpression' ? 'this' : null;
@ -25,5 +25,5 @@ export default function flatten_reference(node: Node) {
parts.unshift(name);
nodes.unshift(node);
return { name, nodes, parts, keypath: `${name}[✂${propStart}-${propEnd}✂]` };
return { name, nodes, parts, keypath: `${name}[✂${prop_start}-${prop_end}✂]` };
}

@ -2,8 +2,8 @@ export function stringify(data: string, options = {}) {
return JSON.stringify(escape(data, options));
}
export function escape(data: string, { onlyEscapeAtSymbol = false } = {}) {
return data.replace(onlyEscapeAtSymbol ? /@+/g : /(@+|#+)/g, (match: string) => {
export function escape(data: string, { only_escape_at_symbol = false } = {}) {
return data.replace(only_escape_at_symbol ? /@+/g : /(@+|#+)/g, (match: string) => {
return match + match[0];
});
}

@ -2,7 +2,7 @@ export interface Node {
start: number;
end: number;
type: string;
[propName: string]: any;
[prop_name: string]: any;
}
export interface Parser {

@ -22,11 +22,11 @@ export function create_animation(node, from, fn, params) {
let started = false;
let name;
const cssText = node.style.cssText;
const css_text = node.style.cssText;
function start() {
if (css) {
if (delay) node.style.cssText = cssText; // TODO create delayed animation instead?
if (delay) node.style.cssText = css_text; // TODO create delayed animation instead?
name = create_rule(node, 0, 1, duration, 0, easing, css);
}

@ -8,25 +8,25 @@ import { Node } from '../../interfaces';
function trim_whitespace(block: Node, trim_before: boolean, trim_after: boolean) {
if (!block.children || block.children.length === 0) return; // AwaitBlock
const firstChild = block.children[0];
const lastChild = block.children[block.children.length - 1];
const first_child = block.children[0];
const last_child = block.children[block.children.length - 1];
if (firstChild.type === 'Text' && trim_before) {
firstChild.data = trim_start(firstChild.data);
if (!firstChild.data) block.children.shift();
if (first_child.type === 'Text' && trim_before) {
first_child.data = trim_start(first_child.data);
if (!first_child.data) block.children.shift();
}
if (lastChild.type === 'Text' && trim_after) {
lastChild.data = trim_end(lastChild.data);
if (!lastChild.data) block.children.pop();
if (last_child.type === 'Text' && trim_after) {
last_child.data = trim_end(last_child.data);
if (!last_child.data) block.children.pop();
}
if (block.else) {
trim_whitespace(block.else, trim_before, trim_after);
}
if (firstChild.elseif) {
trim_whitespace(firstChild, trim_before, trim_after);
if (first_child.elseif) {
trim_whitespace(first_child, trim_before, trim_after);
}
}
@ -310,9 +310,9 @@ export default function mustache(parser: Parser) {
parser.stack.push(block);
if (type === 'AwaitBlock') {
const childBlock = await_block_shorthand ? block.then : block.pending;
childBlock.start = parser.index;
parser.stack.push(childBlock);
const child_block = await_block_shorthand ? block.then : block.pending;
child_block.start = parser.index;
parser.stack.push(child_block);
}
} else if (parser.eat('@html')) {
// {@html content} tag

@ -185,7 +185,7 @@ export default function tag(parser: Parser) {
const unique_names = new Set();
let attribute;
while ((attribute = readAttribute(parser, unique_names))) {
while ((attribute = read_attribute(parser, unique_names))) {
element.attributes.push(attribute);
parser.allow_whitespace();
}
@ -314,7 +314,7 @@ function read_tag_name(parser: Parser) {
return name;
}
function readAttribute(parser: Parser, unique_names: Set<string>) {
function read_attribute(parser: Parser, unique_names: Set<string>) {
const start = parser.index;
if (parser.eat('{')) {

@ -58,14 +58,14 @@ async function replace_async(str: string, re: RegExp, func: (...any) => Promise<
return '';
});
let out = '';
let lastEnd = 0;
let last_end = 0;
for (const { offset, length, replacement } of await Promise.all(
replacements
)) {
out += str.slice(lastEnd, offset) + replacement;
lastEnd = offset + length;
out += str.slice(last_end, offset) + replacement;
last_end = offset + length;
}
out += str.slice(lastEnd);
out += str.slice(last_end);
return out;
}

@ -202,11 +202,11 @@ class FuzzySet {
let match_score;
// build a results list of [score, str]
for (const matchIndex in matches) {
match_score = matches[matchIndex];
for (const match_index in matches) {
match_score = matches[match_index];
results.push([
match_score / (vector_normal * items[matchIndex][0]),
items[matchIndex][1],
match_score / (vector_normal * items[match_index][0]),
items[match_index][1],
]);
}

@ -1,6 +1,6 @@
import repeat from './repeat';
function tabsToSpaces(str: string) {
function tabs_to_spaces(str: string) {
return str.replace(/^\t+/, match => match.split('\t').join(' '));
}
@ -11,26 +11,26 @@ export default function get_code_frame(
) {
const lines = source.split('\n');
const frameStart = Math.max(0, line - 2);
const frameEnd = Math.min(line + 3, lines.length);
const frame_start = Math.max(0, line - 2);
const frame_end = Math.min(line + 3, lines.length);
const digits = String(frameEnd + 1).length;
const digits = String(frame_end + 1).length;
return lines
.slice(frameStart, frameEnd)
.slice(frame_start, frame_end)
.map((str, i) => {
const isErrorLine = frameStart + i === line;
const isErrorLine = frame_start + i === line;
let lineNum = String(i + frameStart + 1);
while (lineNum.length < digits) lineNum = ` ${lineNum}`;
let line_num = String(i + frame_start + 1);
while (line_num.length < digits) line_num = ` ${line_num}`;
if (isErrorLine) {
const indicator =
repeat(' ', digits + 2 + tabsToSpaces(str.slice(0, column)).length) + '^';
return `${lineNum}: ${tabsToSpaces(str)}\n${indicator}`;
repeat(' ', digits + 2 + tabs_to_spaces(str.slice(0, column)).length) + '^';
return `${line_num}: ${tabs_to_spaces(str)}\n${indicator}`;
}
return `${lineNum}: ${tabsToSpaces(str)}`;
return `${line_num}: ${tabs_to_spaces(str)}`;
})
.join('\n');
}

Loading…
Cancel
Save