remove some more unused code

pull/1864/head
Rich Harris 7 years ago
parent a023f2e049
commit 349aa0bbac

@ -1,4 +1,4 @@
import { Node, Warning } from './interfaces';
import { Warning } from './interfaces';
import Component from './compile/Component';
const now = (typeof process !== 'undefined' && process.hrtime)

@ -1,17 +1,12 @@
import { parseExpressionAt } from 'acorn';
import MagicString, { Bundle } from 'magic-string';
import isReference from 'is-reference';
import { walk, childKeys } from 'estree-walker';
import { getLocator } from 'locate-character';
import Stats from '../Stats';
import deindent from '../utils/deindent';
import reservedNames from '../utils/reservedNames';
import namespaces from '../utils/namespaces';
import { removeNode } from '../utils/removeNode';
import nodeToString from '../utils/nodeToString';
import wrapModule from './wrapModule';
import { createScopes, extractNames, Scope } from '../utils/annotateWithScopes';
import getName from '../utils/getName';
import Stylesheet from './css/Stylesheet';
import { test } from '../config';
import Fragment from './nodes/Fragment';
@ -19,73 +14,8 @@ import * as internal from '../internal/index';
import { Node, Ast, CompileOptions, CustomElementOptions } from '../interfaces';
import error from '../utils/error';
import getCodeFrame from '../utils/getCodeFrame';
import checkForComputedKeys from './validate/js/utils/checkForComputedKeys';
import checkForDupes from './validate/js/utils/checkForDupes';
import fuzzymatch from './validate/utils/fuzzymatch';
import flattenReference from '../utils/flattenReference';
interface Computation {
key: string;
deps: string[];
hasRestParam: boolean;
}
interface Declaration {
type: string;
name: string;
node: Node;
block: string;
}
function detectIndentation(str: string) {
const pattern = /^[\t\s]{1,4}/gm;
let match;
while (match = pattern.exec(str)) {
if (match[0][0] === '\t') return '\t';
if (match[0].length === 2) return ' ';
}
return ' ';
}
function getIndentationLevel(str: string, b: number) {
let a = b;
while (a > 0 && str[a - 1] !== '\n') a -= 1;
return /^\s*/.exec(str.slice(a, b))[0];
}
function getIndentExclusionRanges(node: Node) {
// TODO can we fold this into a different pass?
const ranges: Node[] = [];
walk(node, {
enter(node: Node) {
if (node.type === 'TemplateElement') ranges.push(node);
}
});
return ranges;
}
function increaseIndentation(
code: MagicString,
start: number,
end: number,
indentationLevel: string,
ranges: Node[]
) {
const str = code.original.slice(start, end);
const lines = str.split('\n');
let c = start;
lines.forEach(line => {
if (line) {
code.prependRight(c, '\t\t\t'); // TODO detect indentation
}
c += line.length + 1;
});
}
// We need to tell estree-walker that it should always
// look for an `else` block, otherwise it might get
// the wrong idea about the shape of each/if blocks

@ -12,7 +12,7 @@ import Text from './Text';
import * as namespaces from '../../utils/namespaces';
import mapChildren from './shared/mapChildren';
import { dimensions } from '../../utils/patterns';
import fuzzymatch from '../validate/utils/fuzzymatch';
import fuzzymatch from '../../utils/fuzzymatch';
import Ref from './Ref';
import list from '../../utils/list';

@ -1,14 +1,9 @@
import Node from './shared/Node';
import Expression from './shared/Expression';
import addToSet from '../../utils/addToSet';
import flattenReference from '../../utils/flattenReference';
import validCalleeObjects from '../../utils/validCalleeObjects';
import list from '../../utils/list';
import { createScopes } from '../../utils/annotateWithScopes';
import { walk } from 'estree-walker';
const validBuiltins = new Set(['set', 'fire', 'destroy']);
export default class EventHandler extends Node {
name: string;
modifiers: Set<string>;

@ -2,7 +2,7 @@ import Node from './shared/Node';
import Binding from './Binding';
import EventHandler from './EventHandler';
import flattenReference from '../../utils/flattenReference';
import fuzzymatch from '../validate/utils/fuzzymatch';
import fuzzymatch from '../../utils/fuzzymatch';
import list from '../../utils/list';
const validBindings = [

@ -1,5 +1,5 @@
import deindent from '../../utils/deindent';
import { stringify, escape } from '../../utils/stringify';
import { stringify } from '../../utils/stringify';
import CodeBuilder from '../../utils/CodeBuilder';
import globalWhitelist from '../../utils/globalWhitelist';
import Component from '../Component';

@ -1,17 +0,0 @@
import { Node } from '../../../../interfaces';
import Component from '../../../Component';
export default function checkForAccessors(
component: Component,
properties: Node[],
label: string
) {
properties.forEach(prop => {
if (prop.kind !== 'init') {
component.error(prop, {
code: `illegal-accessor`,
message: `${label} cannot use getters and setters`
});
}
});
}

@ -1,16 +0,0 @@
import { Node } from '../../../../interfaces';
import Component from '../../../Component';
export default function checkForComputedKeys(
component: Component,
properties: Node[]
) {
properties.forEach(prop => {
if (prop.key.computed) {
component.error(prop, {
code: `computed-key`,
message: `Cannot use computed keys`
});
}
});
}

@ -1,23 +0,0 @@
import { Node } from '../../../../interfaces';
import getName from '../../../../utils/getName';
import Component from '../../../Component';
export default function checkForDupes(
component: Component,
properties: Node[]
) {
const seen = new Set();
properties.forEach(prop => {
const name = getName(prop.key);
if (seen.has(name)) {
component.error(prop, {
code: `duplicate-property`,
message: `Duplicate property '${name}'`
});
}
seen.add(name);
});
}

@ -1,33 +0,0 @@
import { walk } from 'estree-walker';
import isReference from 'is-reference';
import { Node } from '../../../../interfaces';
export default function usesThisOrArguments(node: Node) {
let result = false;
walk(node, {
enter(node: Node, parent: Node) {
if (
result ||
node.type === 'FunctionExpression' ||
node.type === 'FunctionDeclaration'
) {
return this.skip();
}
if (node.type === 'ThisExpression') {
result = true;
}
if (
node.type === 'Identifier' &&
isReference(node, parent) &&
node.name === 'arguments'
) {
result = true;
}
},
});
return result;
}

@ -1,8 +0,0 @@
import FuzzySet from './FuzzySet';
export default function fuzzymatch(name: string, names: string[]) {
const set = new FuzzySet(names);
const matches = set.get(name);
return matches && matches[0] && matches[0][0] > 0.7 ? matches[0][1] : null;
}

@ -1,213 +0,0 @@
import { parseExpressionAt } from 'acorn';
import { Parser } from '../index';
const DIRECTIVES: Record<string, {
names: string[];
attribute: (
start: number,
end: number,
type: string,
name: string,
expression?: any,
directiveName?: string
) => { start: number, end: number, type: string, name: string, value?: any, expression?: any };
allowedExpressionTypes: string[];
error: string;
}> = {
Ref: {
names: ['ref'],
attribute(start, end, type, name) {
return { start, end, type, name };
},
allowedExpressionTypes: [],
error: 'ref directives cannot have a value'
},
EventHandler: {
names: ['on'],
attribute(start, end, type, lhs, expression) {
const [name, ...modifiers] = lhs.split('|');
return { start, end, type, name, modifiers, expression };
},
allowedExpressionTypes: ['CallExpression'],
error: 'Expected a method call'
},
Binding: {
names: ['bind'],
attribute(start, end, type, name, expression) {
return {
start, end, type, name,
value: expression || {
type: 'Identifier',
start: start + 5,
end,
name,
}
};
},
allowedExpressionTypes: ['Identifier', 'MemberExpression'],
error: 'Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)'
},
Transition: {
names: ['in', 'out', 'transition'],
attribute(start, end, type, name, expression, directiveName) {
return {
start, end, type, name, expression,
intro: directiveName === 'in' || directiveName === 'transition',
outro: directiveName === 'out' || directiveName === 'transition',
};
},
allowedExpressionTypes: ['ObjectExpression'],
error: 'Transition argument must be an object literal, e.g. `{ duration: 400 }`'
},
Animation: {
names: ['animate'],
attribute(start, end, type, name, expression) {
return { start, end, type, name, expression };
},
allowedExpressionTypes: ['ObjectExpression'],
error: 'Animation argument must be an object literal, e.g. `{ duration: 400 }`'
},
Action: {
names: ['use'],
attribute(start, end, type, name, expression) {
return { start, end, type, name, expression };
},
allowedExpressionTypes: ['*'],
error: 'Data passed to actions must be an identifier (e.g. `foo`), a member expression ' +
'(e.g. `foo.bar` or `foo[baz]`), a method call (e.g. `foo()`), or a literal (e.g. `true` or `\'a string\'`'
},
Class: {
names: ['class'],
attribute(start, end, type, name, expression) {
return { start, end, type, name, expression };
},
allowedExpressionTypes: ['*'],
error: 'Data passed to class directives must be an expression'
},
};
const lookupByName = {};
Object.keys(DIRECTIVES).forEach(name => {
const directive = DIRECTIVES[name];
directive.names.forEach(type => lookupByName[type] = name);
});
function readExpression(parser: Parser, start: number, quoteMark: string|null) {
let i = start;
let escaped = false;
for (; i < parser.template.length; i += 1) {
const char = parser.template[i];
if (quoteMark) {
if (char === quoteMark) {
if (!escaped) break;
} else if (escaped) {
escaped = false;
} else if (char === '\\') {
escaped = true;
}
} else if (/[\s\/>]/.test(char)) {
break;
}
}
const expression = parseExpressionAt(parser.template.slice(0, i), start, {
ecmaVersion: 9,
});
parser.index = expression.end;
parser.allowWhitespace();
if (quoteMark) parser.eat(quoteMark, true);
return expression;
}
export function readDirective(
parser: Parser,
start: number,
attrName: string
) {
const [directiveName, name] = attrName.split(':');
if (name === undefined) return; // No colon in the name
if (directiveName === '') {
// not a directive — :foo is short for foo={{foo}}
return {
start: start,
end: start + name.length + 1,
type: 'Attribute',
name,
value: getShorthandValue(start + 1, name)
};
}
const type = lookupByName[directiveName];
if (!type) return; // not a registered directive
const directive = DIRECTIVES[type];
let expression = null;
if (parser.eat('=')) {
const quoteMark = parser.eat(`'`) ? `'` : parser.eat(`"`) ? `"` : null;
const expressionStart = parser.index;
try {
expression = readExpression(parser, expressionStart, quoteMark);
const allowed = directive.allowedExpressionTypes;
if (allowed[0] !== '*' && allowed.indexOf(expression.type) === -1) {
parser.error({
code: `invalid-directive-value`,
message: directive.error
}, expressionStart);
}
} catch (err) {
if (parser.template[expressionStart] === '{') {
// assume the mistake was wrapping the directive arguments.
// this could yield false positives! but hopefully not too many
let message = 'directive values should not be wrapped';
const expressionEnd = parser.template.indexOf('}', expressionStart);
if (expressionEnd !== -1) {
const value = parser.template.slice(expressionStart + 1, expressionEnd);
message += ` — use '${value}', not '{${value}}'`;
}
parser.error({
code: `invalid-directive-value`,
message
}, expressionStart);
}
throw err;
}
}
return directive.attribute(start, parser.index, type, name, expression, directiveName);
}
function getShorthandValue(start: number, name: string) {
const end = start + name.length;
return [
{
type: 'AttributeShorthand',
start,
end,
expression: {
type: 'Identifier',
start,
end,
name,
},
},
];
}

@ -2,7 +2,6 @@ import readContext from '../read/context';
import readExpression from '../read/expression';
import { whitespace } from '../../utils/patterns';
import { trimStart, trimEnd } from '../../utils/trim';
import reservedNames from '../../utils/reservedNames';
import { Parser } from '../index';
import { Node } from '../../interfaces';

@ -1,8 +1,6 @@
import readExpression from '../read/expression';
import readScript from '../read/script';
import readStyle from '../read/style';
import { readDirective } from '../read/directives';
import { trimStart, trimEnd } from '../../utils/trim';
import { decodeCharacterReferences } from '../utils/html';
import isVoidElementName from '../../utils/isVoidElementName';
import { Parser } from '../index';

@ -1,3 +1,10 @@
export default function fuzzymatch(name: string, names: string[]) {
const set = new FuzzySet(names);
const matches = set.get(name);
return matches && matches[0] && matches[0][0] > 0.7 ? matches[0][1] : null;
}
// adapted from https://github.com/Glench/fuzzyset.js/blob/master/lib/fuzzyset.js
// BSD Licensed
@ -86,7 +93,7 @@ function sortDescending(a, b) {
return b[0] - a[0];
}
export default class FuzzySet {
class FuzzySet {
exactSet: object;
matchDict: object;
items: object;

@ -1,6 +0,0 @@
import { Node } from '../interfaces';
export default function getMethodName(node: Node) {
if (node.type === 'Identifier') return node.name;
if (node.type === 'Literal') return String(node.value);
}

@ -1,8 +0,0 @@
import { Node } from '../interfaces';
export default function isThisGetCallExpression(node: Node): boolean {
return node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'ThisExpression' &&
node.callee.property.name === 'get';
}

@ -1,11 +0,0 @@
import { Node } from '../interfaces';
export default function nodeToString(node: Node) {
if (node.type === 'Literal' && typeof node.value === 'string') {
return node.value;
} else if (node.type === 'TemplateLiteral'
&& node.quasis.length === 1
&& node.expressions.length === 0) {
return node.quasis[0].value.raw;
}
}

@ -1,5 +1,4 @@
import MagicString from 'magic-string';
import getName from '../utils/getName';
import { Node } from '../interfaces';
const keys = {
@ -45,15 +44,3 @@ export function removeNode(code: MagicString, parent: Node, node: Node) {
list.splice(i, 1);
return;
}
export function removeObjectKey(code: MagicString, node: Node, key: string) {
if (node.type !== 'ObjectExpression') return;
let i = node.properties.length;
while (i--) {
const property = node.properties[i];
if (property.key.type === 'Identifier' && getName(property.key) === key) {
removeNode(code, node, property);
}
}
}

@ -1,3 +0,0 @@
const validCalleeObjects = new Set(['this', 'event', 'console']);
export default validCalleeObjects;

@ -1,21 +0,0 @@
import { Node } from '../interfaces';
import { walk } from 'estree-walker';
export default function walkThroughTopFunctionScope(body: Node, callback: Function) {
let lexicalDepth = 0;
walk(body, {
enter(node: Node) {
if (/^Function/.test(node.type)) {
lexicalDepth += 1;
} else if (lexicalDepth === 0) {
callback(node)
}
},
leave(node: Node) {
if (/^Function/.test(node.type)) {
lexicalDepth -= 1;
}
},
});
}

@ -1,5 +1,4 @@
export default {
solo: 1,
'skip-ssr': true, // TODO delete this line, once binding works
html: `

Loading…
Cancel
Save