diff --git a/src/css/Stylesheet.ts b/src/css/Stylesheet.ts index 4a3630f300..a784713f9d 100644 --- a/src/css/Stylesheet.ts +++ b/src/css/Stylesheet.ts @@ -6,7 +6,7 @@ import getCodeFrame from '../utils/getCodeFrame'; import hash from '../utils/hash'; import Element from '../generators/nodes/Element'; import { Validator } from '../validate/index'; -import { Node, Parsed, Warning } from '../interfaces'; +import { Node, Ast, Warning } from '../interfaces'; class Rule { selectors: Selector[]; @@ -236,7 +236,7 @@ const keys = {}; export default class Stylesheet { source: string; - parsed: Parsed; + ast: Ast; filename: string; dev: boolean; @@ -248,9 +248,9 @@ export default class Stylesheet { nodesWithCssClass: Set; - constructor(source: string, parsed: Parsed, filename: string, dev: boolean) { + constructor(source: string, ast: Ast, filename: string, dev: boolean) { this.source = source; - this.parsed = parsed; + this.ast = ast; this.filename = filename; this.dev = dev; @@ -259,15 +259,15 @@ export default class Stylesheet { this.nodesWithCssClass = new Set(); - if (parsed.css && parsed.css.children.length) { - this.id = `svelte-${hash(parsed.css.content.styles)}`; + if (ast.css && ast.css.children.length) { + this.id = `svelte-${hash(ast.css.content.styles)}`; this.hasStyles = true; const stack: (Rule | Atrule)[] = []; let currentAtrule: Atrule = null; - walk(this.parsed.css, { + walk(this.ast.css, { enter: (node: Node) => { if (node.type === 'Atrule') { const last = stack[stack.length - 1]; @@ -346,7 +346,7 @@ export default class Stylesheet { const code = new MagicString(this.source); - walk(this.parsed.css, { + walk(this.ast.css, { enter: (node: Node) => { code.addSourcemapLocation(node.start); code.addSourcemapLocation(node.end); diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index b06d7f2b9c..668f3a61c7 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -13,12 +13,11 @@ import nodeToString from '../utils/nodeToString'; import wrapModule from './wrapModule'; import annotateWithScopes, { Scope } from '../utils/annotateWithScopes'; import getName from '../utils/getName'; -import clone from '../utils/clone'; import Stylesheet from '../css/Stylesheet'; import { test } from '../config'; import nodes from './nodes/index'; import Fragment from './nodes/Fragment'; -import { Node, GenerateOptions, ShorthandImport, Parsed, CompileOptions, CustomElementOptions } from '../interfaces'; +import { Node, GenerateOptions, ShorthandImport, Ast, CompileOptions, CustomElementOptions } from '../interfaces'; interface Computation { key: string; @@ -79,8 +78,7 @@ childKeys.Attribute = ['value']; export default class Generator { stats: Stats; - ast: Parsed; - parsed: Parsed; + ast: Ast; source: string; name: string; options: CompileOptions; @@ -124,7 +122,7 @@ export default class Generator { usedNames: Set; constructor( - parsed: Parsed, + ast: Ast, source: string, name: string, stylesheet: Stylesheet, @@ -135,9 +133,7 @@ export default class Generator { stats.start('compile'); this.stats = stats; - this.ast = clone(parsed); - - this.parsed = parsed; + this.ast = ast; this.source = source; this.options = options; @@ -193,7 +189,7 @@ export default class Generator { throw new Error(`No tag name specified`); // TODO better error } - this.fragment = new Fragment(this, parsed.html); + this.fragment = new Fragment(this, ast.html); // this.walkTemplate(); if (!this.customElement) this.stylesheet.reify(); } @@ -329,7 +325,7 @@ export default class Generator { imports } = this; - const { js } = this.parsed; + const { js } = this.ast; const componentDefinition = new CodeBuilder(); diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 151dea4109..56bf7cca8d 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -14,7 +14,7 @@ import Stylesheet from '../../css/Stylesheet'; import Stats from '../../Stats'; import Block from './Block'; import { test } from '../../config'; -import { Parsed, CompileOptions, Node } from '../../interfaces'; +import { Ast, CompileOptions, Node } from '../../interfaces'; export class DomGenerator extends Generator { blocks: (Block|string)[]; @@ -31,14 +31,14 @@ export class DomGenerator extends Generator { needsEncapsulateHelper: boolean; constructor( - parsed: Parsed, + ast: Ast, source: string, name: string, stylesheet: Stylesheet, options: CompileOptions, stats: Stats ) { - super(parsed, source, name, stylesheet, options, stats, true); + super(ast, source, name, stylesheet, options, stats, true); this.blocks = []; this.readonly = new Set(); @@ -53,7 +53,7 @@ export class DomGenerator extends Generator { } export default function dom( - parsed: Parsed, + ast: Ast, source: string, stylesheet: Stylesheet, options: CompileOptions, @@ -61,7 +61,7 @@ export default function dom( ) { const format = options.format || 'es'; - const generator = new DomGenerator(parsed, source, options.name || 'SvelteComponent', stylesheet, options, stats); + const generator = new DomGenerator(ast, source, options.name || 'SvelteComponent', stylesheet, options, stats); const { computations, diff --git a/src/generators/server-side-rendering/index.ts b/src/generators/server-side-rendering/index.ts index cc01b67e8b..f49ac60728 100644 --- a/src/generators/server-side-rendering/index.ts +++ b/src/generators/server-side-rendering/index.ts @@ -7,7 +7,7 @@ import visit from './visit'; import { removeNode, removeObjectKey } from '../../utils/removeNode'; import getName from '../../utils/getName'; import globalWhitelist from '../../utils/globalWhitelist'; -import { Parsed, Node, CompileOptions } from '../../interfaces'; +import { Ast, Node, CompileOptions } from '../../interfaces'; import { AppendTarget } from './interfaces'; import { stringify } from '../../utils/stringify'; @@ -17,14 +17,14 @@ export class SsrGenerator extends Generator { appendTargets: AppendTarget[]; constructor( - parsed: Parsed, + ast: Ast, source: string, name: string, stylesheet: Stylesheet, options: CompileOptions, stats: Stats ) { - super(parsed, source, name, stylesheet, options, stats, false); + super(ast, source, name, stylesheet, options, stats, false); this.bindings = []; this.renderCode = ''; this.appendTargets = []; @@ -44,7 +44,7 @@ export class SsrGenerator extends Generator { } export default function ssr( - parsed: Parsed, + ast: Ast, source: string, stylesheet: Stylesheet, options: CompileOptions, @@ -52,7 +52,7 @@ export default function ssr( ) { const format = options.format || 'cjs'; - const generator = new SsrGenerator(parsed, source, options.name || 'SvelteComponent', stylesheet, options, stats); + const generator = new SsrGenerator(ast, source, options.name || 'SvelteComponent', stylesheet, options, stats); const { computations, name, templateProperties } = generator; diff --git a/src/index.ts b/src/index.ts index a82cc48ffe..d4f6ab6eff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import generateSSR from './generators/server-side-rendering/index'; import Stats from './Stats'; import { assign } from './shared/index.js'; import Stylesheet from './css/Stylesheet'; -import { Parsed, CompileOptions, Warning, PreprocessOptions, Preprocessor } from './interfaces'; +import { Ast, CompileOptions, Warning, PreprocessOptions, Preprocessor } from './interfaces'; import { SourceMap } from 'magic-string'; const version = '__VERSION__'; @@ -108,7 +108,7 @@ export async function preprocess(source: string, options: PreprocessOptions) { function compile(source: string, _options: CompileOptions) { const options = normalizeOptions(_options); - let parsed: Parsed; + let ast: Ast; const stats = new Stats({ onwarn: options.onwarn @@ -116,7 +116,7 @@ function compile(source: string, _options: CompileOptions) { try { stats.start('parse'); - parsed = parse(source, options); + ast = parse(source, options); stats.stop('parse'); } catch (err) { options.onerror(err); @@ -124,20 +124,20 @@ function compile(source: string, _options: CompileOptions) { } stats.start('stylesheet'); - const stylesheet = new Stylesheet(source, parsed, options.filename, options.dev); + const stylesheet = new Stylesheet(source, ast, options.filename, options.dev); stats.stop('stylesheet'); stats.start('validate'); - validate(parsed, source, stylesheet, stats, options); + validate(ast, source, stylesheet, stats, options); stats.stop('validate'); if (options.generate === false) { - return { ast: parsed, stats, js: null, css: null }; + return { ast: ast, stats, js: null, css: null }; } const compiler = options.generate === 'ssr' ? generateSSR : generate; - return compiler(parsed, source, stylesheet, options, stats); + return compiler(ast, source, stylesheet, options, stats); }; function create(source: string, _options: CompileOptions = {}) { diff --git a/src/interfaces.ts b/src/interfaces.ts index 04530416b8..414b1f368a 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -20,7 +20,7 @@ export interface Parser { metaTags: {}; } -export interface Parsed { +export interface Ast { html: Node; css: Node; js: Node; diff --git a/src/parse/index.ts b/src/parse/index.ts index 94c115c864..6fb40193d6 100644 --- a/src/parse/index.ts +++ b/src/parse/index.ts @@ -5,7 +5,7 @@ import { whitespace } from '../utils/patterns'; import { trimStart, trimEnd } from '../utils/trim'; import reservedNames from '../utils/reservedNames'; import fullCharCodeAt from '../utils/fullCharCodeAt'; -import { Node, Parsed } from '../interfaces'; +import { Node, Ast } from '../interfaces'; import error from '../utils/error'; interface ParserOptions { @@ -223,7 +223,7 @@ export class Parser { export default function parse( template: string, options: ParserOptions = {} -): Parsed { +): Ast { const parser = new Parser(template, options); return { html: parser.html, diff --git a/src/utils/clone.ts b/src/utils/clone.ts deleted file mode 100644 index 60b90c2208..0000000000 --- a/src/utils/clone.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Node, Parsed } from '../interfaces'; - -export default function clone(node: Node|Parsed) { - const cloned: any = {}; - - for (const key in node) { - const value = node[key]; - if (Array.isArray(value)) { - cloned[key] = value.map(clone); - } else if (value && typeof value === 'object') { - cloned[key] = clone(value); - } else { - cloned[key] = value; - } - } - - return cloned; -} diff --git a/src/validate/index.ts b/src/validate/index.ts index b79a93a0ce..57b717ffdf 100644 --- a/src/validate/index.ts +++ b/src/validate/index.ts @@ -6,7 +6,7 @@ import Stats from '../Stats'; import error from '../utils/error'; import Stylesheet from '../css/Stylesheet'; import Stats from '../Stats'; -import { Node, Parsed, CompileOptions, Warning } from '../interfaces'; +import { Node, Ast, CompileOptions, Warning } from '../interfaces'; export class Validator { readonly source: string; @@ -34,7 +34,7 @@ export class Validator { actions: Set; }; - constructor(parsed: Parsed, source: string, stats: Stats, options: CompileOptions) { + constructor(ast: Ast, source: string, stats: Stats, options: CompileOptions) { this.source = source; this.stats = stats; @@ -93,7 +93,7 @@ export class Validator { } export default function validate( - parsed: Parsed, + ast: Ast, source: string, stylesheet: Stylesheet, stats: Stats, @@ -117,27 +117,27 @@ export default function validate( }); } - const validator = new Validator(parsed, source, stats, { + const validator = new Validator(ast, source, stats, { name, filename, dev, parser }); - if (parsed.js) { - validateJs(validator, parsed.js); + if (ast.js) { + validateJs(validator, ast.js); } - if (parsed.css) { + if (ast.css) { stylesheet.validate(validator); } - if (parsed.html) { - validateHtml(validator, parsed.html); + if (ast.html) { + validateHtml(validator, ast.html); } // need to do a second pass of the JS, now that we've analysed the markup - if (parsed.js && validator.defaultExport) { + if (ast.js && validator.defaultExport) { const categories = { components: 'component', // TODO helpers require a bit more work — need to analyse all expressions