From 349aa0bbac4a5940e39d40c1fbc48ee11836f309 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Nov 2018 22:52:36 -0500 Subject: [PATCH] remove some more unused code --- src/Stats.ts | 2 +- src/compile/Component.ts | 70 ------ src/compile/nodes/Element.ts | 2 +- src/compile/nodes/EventHandler.ts | 5 - src/compile/nodes/Window.ts | 2 +- src/compile/render-dom/index.ts | 2 +- .../validate/js/utils/checkForAccessors.ts | 17 -- .../validate/js/utils/checkForComputedKeys.ts | 16 -- .../validate/js/utils/checkForDupes.ts | 23 -- .../validate/js/utils/usesThisOrArguments.ts | 33 --- src/compile/validate/utils/fuzzymatch.ts | 8 - src/parse/read/directives.ts | 213 ------------------ src/parse/state/mustache.ts | 1 - src/parse/state/tag.ts | 2 - .../utils/FuzzySet.ts => utils/fuzzymatch.ts} | 9 +- src/utils/getName.ts | 6 - src/utils/isThisGetCallExpression.ts | 8 - src/utils/nodeToString.ts | 11 - src/utils/removeNode.ts | 15 +- src/utils/validCalleeObjects.ts | 3 - src/utils/walkThroughTopFunctionScope.ts | 21 -- .../samples/component-binding/_config.js | 1 - 22 files changed, 13 insertions(+), 457 deletions(-) delete mode 100644 src/compile/validate/js/utils/checkForAccessors.ts delete mode 100644 src/compile/validate/js/utils/checkForComputedKeys.ts delete mode 100644 src/compile/validate/js/utils/checkForDupes.ts delete mode 100644 src/compile/validate/js/utils/usesThisOrArguments.ts delete mode 100644 src/compile/validate/utils/fuzzymatch.ts delete mode 100644 src/parse/read/directives.ts rename src/{compile/validate/utils/FuzzySet.ts => utils/fuzzymatch.ts} (95%) delete mode 100644 src/utils/getName.ts delete mode 100644 src/utils/isThisGetCallExpression.ts delete mode 100644 src/utils/nodeToString.ts delete mode 100644 src/utils/validCalleeObjects.ts delete mode 100644 src/utils/walkThroughTopFunctionScope.ts diff --git a/src/Stats.ts b/src/Stats.ts index 210475e608..5e60db4d3c 100644 --- a/src/Stats.ts +++ b/src/Stats.ts @@ -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) diff --git a/src/compile/Component.ts b/src/compile/Component.ts index 6674325a57..2af546dd4d 100644 --- a/src/compile/Component.ts +++ b/src/compile/Component.ts @@ -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 diff --git a/src/compile/nodes/Element.ts b/src/compile/nodes/Element.ts index 33955919ee..406e9ec174 100644 --- a/src/compile/nodes/Element.ts +++ b/src/compile/nodes/Element.ts @@ -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'; diff --git a/src/compile/nodes/EventHandler.ts b/src/compile/nodes/EventHandler.ts index f5119c8a02..20af76e305 100644 --- a/src/compile/nodes/EventHandler.ts +++ b/src/compile/nodes/EventHandler.ts @@ -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; diff --git a/src/compile/nodes/Window.ts b/src/compile/nodes/Window.ts index ae6fb9baca..5d41db1d7f 100644 --- a/src/compile/nodes/Window.ts +++ b/src/compile/nodes/Window.ts @@ -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 = [ diff --git a/src/compile/render-dom/index.ts b/src/compile/render-dom/index.ts index 0d599f6832..85728c014f 100644 --- a/src/compile/render-dom/index.ts +++ b/src/compile/render-dom/index.ts @@ -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'; diff --git a/src/compile/validate/js/utils/checkForAccessors.ts b/src/compile/validate/js/utils/checkForAccessors.ts deleted file mode 100644 index 6208a33e0c..0000000000 --- a/src/compile/validate/js/utils/checkForAccessors.ts +++ /dev/null @@ -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` - }); - } - }); -} diff --git a/src/compile/validate/js/utils/checkForComputedKeys.ts b/src/compile/validate/js/utils/checkForComputedKeys.ts deleted file mode 100644 index 8ae323b5e6..0000000000 --- a/src/compile/validate/js/utils/checkForComputedKeys.ts +++ /dev/null @@ -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` - }); - } - }); -} diff --git a/src/compile/validate/js/utils/checkForDupes.ts b/src/compile/validate/js/utils/checkForDupes.ts deleted file mode 100644 index d53e80a5ab..0000000000 --- a/src/compile/validate/js/utils/checkForDupes.ts +++ /dev/null @@ -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); - }); -} diff --git a/src/compile/validate/js/utils/usesThisOrArguments.ts b/src/compile/validate/js/utils/usesThisOrArguments.ts deleted file mode 100644 index ac8b2225ed..0000000000 --- a/src/compile/validate/js/utils/usesThisOrArguments.ts +++ /dev/null @@ -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; -} diff --git a/src/compile/validate/utils/fuzzymatch.ts b/src/compile/validate/utils/fuzzymatch.ts deleted file mode 100644 index 5d70372191..0000000000 --- a/src/compile/validate/utils/fuzzymatch.ts +++ /dev/null @@ -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; -} diff --git a/src/parse/read/directives.ts b/src/parse/read/directives.ts deleted file mode 100644 index 6993f3ebda..0000000000 --- a/src/parse/read/directives.ts +++ /dev/null @@ -1,213 +0,0 @@ -import { parseExpressionAt } from 'acorn'; -import { Parser } from '../index'; - -const DIRECTIVES: Record { 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, - }, - }, - ]; -} diff --git a/src/parse/state/mustache.ts b/src/parse/state/mustache.ts index fdcaad5e36..7a3b714d5c 100644 --- a/src/parse/state/mustache.ts +++ b/src/parse/state/mustache.ts @@ -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'; diff --git a/src/parse/state/tag.ts b/src/parse/state/tag.ts index c3973e0b77..f245e85f07 100644 --- a/src/parse/state/tag.ts +++ b/src/parse/state/tag.ts @@ -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'; diff --git a/src/compile/validate/utils/FuzzySet.ts b/src/utils/fuzzymatch.ts similarity index 95% rename from src/compile/validate/utils/FuzzySet.ts rename to src/utils/fuzzymatch.ts index f52242cf5d..ff528a79c1 100644 --- a/src/compile/validate/utils/FuzzySet.ts +++ b/src/utils/fuzzymatch.ts @@ -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; diff --git a/src/utils/getName.ts b/src/utils/getName.ts deleted file mode 100644 index d236bbaec8..0000000000 --- a/src/utils/getName.ts +++ /dev/null @@ -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); -} \ No newline at end of file diff --git a/src/utils/isThisGetCallExpression.ts b/src/utils/isThisGetCallExpression.ts deleted file mode 100644 index 81642a389d..0000000000 --- a/src/utils/isThisGetCallExpression.ts +++ /dev/null @@ -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'; -} diff --git a/src/utils/nodeToString.ts b/src/utils/nodeToString.ts deleted file mode 100644 index 1f72233596..0000000000 --- a/src/utils/nodeToString.ts +++ /dev/null @@ -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; - } -} diff --git a/src/utils/removeNode.ts b/src/utils/removeNode.ts index 26095b483c..5d183b6c94 100644 --- a/src/utils/removeNode.ts +++ b/src/utils/removeNode.ts @@ -1,5 +1,4 @@ import MagicString from 'magic-string'; -import getName from '../utils/getName'; import { Node } from '../interfaces'; const keys = { @@ -44,16 +43,4 @@ export function removeNode(code: MagicString, parent: Node, node: Node) { code.remove(a, b); 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); - } - } -} +} \ No newline at end of file diff --git a/src/utils/validCalleeObjects.ts b/src/utils/validCalleeObjects.ts deleted file mode 100644 index ad6070995a..0000000000 --- a/src/utils/validCalleeObjects.ts +++ /dev/null @@ -1,3 +0,0 @@ -const validCalleeObjects = new Set(['this', 'event', 'console']); - -export default validCalleeObjects; \ No newline at end of file diff --git a/src/utils/walkThroughTopFunctionScope.ts b/src/utils/walkThroughTopFunctionScope.ts deleted file mode 100644 index b773bf9a97..0000000000 --- a/src/utils/walkThroughTopFunctionScope.ts +++ /dev/null @@ -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; - } - }, - }); -} diff --git a/test/runtime/samples/component-binding/_config.js b/test/runtime/samples/component-binding/_config.js index 0a282c4656..e773226ba9 100644 --- a/test/runtime/samples/component-binding/_config.js +++ b/test/runtime/samples/component-binding/_config.js @@ -1,5 +1,4 @@ export default { - solo: 1, 'skip-ssr': true, // TODO delete this line, once binding works html: `