no need to clone ast

pull/1367/head
Rich Harris 7 years ago
parent 4c8a5c598d
commit f892389b00

@ -6,7 +6,7 @@ import getCodeFrame from '../utils/getCodeFrame';
import hash from '../utils/hash'; import hash from '../utils/hash';
import Element from '../generators/nodes/Element'; import Element from '../generators/nodes/Element';
import { Validator } from '../validate/index'; import { Validator } from '../validate/index';
import { Node, Parsed, Warning } from '../interfaces'; import { Node, Ast, Warning } from '../interfaces';
class Rule { class Rule {
selectors: Selector[]; selectors: Selector[];
@ -236,7 +236,7 @@ const keys = {};
export default class Stylesheet { export default class Stylesheet {
source: string; source: string;
parsed: Parsed; ast: Ast;
filename: string; filename: string;
dev: boolean; dev: boolean;
@ -248,9 +248,9 @@ export default class Stylesheet {
nodesWithCssClass: Set<Node>; nodesWithCssClass: Set<Node>;
constructor(source: string, parsed: Parsed, filename: string, dev: boolean) { constructor(source: string, ast: Ast, filename: string, dev: boolean) {
this.source = source; this.source = source;
this.parsed = parsed; this.ast = ast;
this.filename = filename; this.filename = filename;
this.dev = dev; this.dev = dev;
@ -259,15 +259,15 @@ export default class Stylesheet {
this.nodesWithCssClass = new Set(); this.nodesWithCssClass = new Set();
if (parsed.css && parsed.css.children.length) { if (ast.css && ast.css.children.length) {
this.id = `svelte-${hash(parsed.css.content.styles)}`; this.id = `svelte-${hash(ast.css.content.styles)}`;
this.hasStyles = true; this.hasStyles = true;
const stack: (Rule | Atrule)[] = []; const stack: (Rule | Atrule)[] = [];
let currentAtrule: Atrule = null; let currentAtrule: Atrule = null;
walk(this.parsed.css, { walk(this.ast.css, {
enter: (node: Node) => { enter: (node: Node) => {
if (node.type === 'Atrule') { if (node.type === 'Atrule') {
const last = stack[stack.length - 1]; const last = stack[stack.length - 1];
@ -346,7 +346,7 @@ export default class Stylesheet {
const code = new MagicString(this.source); const code = new MagicString(this.source);
walk(this.parsed.css, { walk(this.ast.css, {
enter: (node: Node) => { enter: (node: Node) => {
code.addSourcemapLocation(node.start); code.addSourcemapLocation(node.start);
code.addSourcemapLocation(node.end); code.addSourcemapLocation(node.end);

@ -13,12 +13,11 @@ import nodeToString from '../utils/nodeToString';
import wrapModule from './wrapModule'; import wrapModule from './wrapModule';
import annotateWithScopes, { Scope } from '../utils/annotateWithScopes'; import annotateWithScopes, { Scope } from '../utils/annotateWithScopes';
import getName from '../utils/getName'; import getName from '../utils/getName';
import clone from '../utils/clone';
import Stylesheet from '../css/Stylesheet'; import Stylesheet from '../css/Stylesheet';
import { test } from '../config'; import { test } from '../config';
import nodes from './nodes/index'; import nodes from './nodes/index';
import Fragment from './nodes/Fragment'; 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 { interface Computation {
key: string; key: string;
@ -79,8 +78,7 @@ childKeys.Attribute = ['value'];
export default class Generator { export default class Generator {
stats: Stats; stats: Stats;
ast: Parsed; ast: Ast;
parsed: Parsed;
source: string; source: string;
name: string; name: string;
options: CompileOptions; options: CompileOptions;
@ -124,7 +122,7 @@ export default class Generator {
usedNames: Set<string>; usedNames: Set<string>;
constructor( constructor(
parsed: Parsed, ast: Ast,
source: string, source: string,
name: string, name: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
@ -135,9 +133,7 @@ export default class Generator {
stats.start('compile'); stats.start('compile');
this.stats = stats; this.stats = stats;
this.ast = clone(parsed); this.ast = ast;
this.parsed = parsed;
this.source = source; this.source = source;
this.options = options; this.options = options;
@ -193,7 +189,7 @@ export default class Generator {
throw new Error(`No tag name specified`); // TODO better error 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(); // this.walkTemplate();
if (!this.customElement) this.stylesheet.reify(); if (!this.customElement) this.stylesheet.reify();
} }
@ -329,7 +325,7 @@ export default class Generator {
imports imports
} = this; } = this;
const { js } = this.parsed; const { js } = this.ast;
const componentDefinition = new CodeBuilder(); const componentDefinition = new CodeBuilder();

@ -14,7 +14,7 @@ import Stylesheet from '../../css/Stylesheet';
import Stats from '../../Stats'; import Stats from '../../Stats';
import Block from './Block'; import Block from './Block';
import { test } from '../../config'; import { test } from '../../config';
import { Parsed, CompileOptions, Node } from '../../interfaces'; import { Ast, CompileOptions, Node } from '../../interfaces';
export class DomGenerator extends Generator { export class DomGenerator extends Generator {
blocks: (Block|string)[]; blocks: (Block|string)[];
@ -31,14 +31,14 @@ export class DomGenerator extends Generator {
needsEncapsulateHelper: boolean; needsEncapsulateHelper: boolean;
constructor( constructor(
parsed: Parsed, ast: Ast,
source: string, source: string,
name: string, name: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
options: CompileOptions, options: CompileOptions,
stats: Stats stats: Stats
) { ) {
super(parsed, source, name, stylesheet, options, stats, true); super(ast, source, name, stylesheet, options, stats, true);
this.blocks = []; this.blocks = [];
this.readonly = new Set(); this.readonly = new Set();
@ -53,7 +53,7 @@ export class DomGenerator extends Generator {
} }
export default function dom( export default function dom(
parsed: Parsed, ast: Ast,
source: string, source: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
options: CompileOptions, options: CompileOptions,
@ -61,7 +61,7 @@ export default function dom(
) { ) {
const format = options.format || 'es'; 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 { const {
computations, computations,

@ -7,7 +7,7 @@ import visit from './visit';
import { removeNode, removeObjectKey } from '../../utils/removeNode'; import { removeNode, removeObjectKey } from '../../utils/removeNode';
import getName from '../../utils/getName'; import getName from '../../utils/getName';
import globalWhitelist from '../../utils/globalWhitelist'; import globalWhitelist from '../../utils/globalWhitelist';
import { Parsed, Node, CompileOptions } from '../../interfaces'; import { Ast, Node, CompileOptions } from '../../interfaces';
import { AppendTarget } from './interfaces'; import { AppendTarget } from './interfaces';
import { stringify } from '../../utils/stringify'; import { stringify } from '../../utils/stringify';
@ -17,14 +17,14 @@ export class SsrGenerator extends Generator {
appendTargets: AppendTarget[]; appendTargets: AppendTarget[];
constructor( constructor(
parsed: Parsed, ast: Ast,
source: string, source: string,
name: string, name: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
options: CompileOptions, options: CompileOptions,
stats: Stats stats: Stats
) { ) {
super(parsed, source, name, stylesheet, options, stats, false); super(ast, source, name, stylesheet, options, stats, false);
this.bindings = []; this.bindings = [];
this.renderCode = ''; this.renderCode = '';
this.appendTargets = []; this.appendTargets = [];
@ -44,7 +44,7 @@ export class SsrGenerator extends Generator {
} }
export default function ssr( export default function ssr(
parsed: Parsed, ast: Ast,
source: string, source: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
options: CompileOptions, options: CompileOptions,
@ -52,7 +52,7 @@ export default function ssr(
) { ) {
const format = options.format || 'cjs'; 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; const { computations, name, templateProperties } = generator;

@ -5,7 +5,7 @@ import generateSSR from './generators/server-side-rendering/index';
import Stats from './Stats'; import Stats from './Stats';
import { assign } from './shared/index.js'; import { assign } from './shared/index.js';
import Stylesheet from './css/Stylesheet'; 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'; import { SourceMap } from 'magic-string';
const version = '__VERSION__'; const version = '__VERSION__';
@ -108,7 +108,7 @@ export async function preprocess(source: string, options: PreprocessOptions) {
function compile(source: string, _options: CompileOptions) { function compile(source: string, _options: CompileOptions) {
const options = normalizeOptions(_options); const options = normalizeOptions(_options);
let parsed: Parsed; let ast: Ast;
const stats = new Stats({ const stats = new Stats({
onwarn: options.onwarn onwarn: options.onwarn
@ -116,7 +116,7 @@ function compile(source: string, _options: CompileOptions) {
try { try {
stats.start('parse'); stats.start('parse');
parsed = parse(source, options); ast = parse(source, options);
stats.stop('parse'); stats.stop('parse');
} catch (err) { } catch (err) {
options.onerror(err); options.onerror(err);
@ -124,20 +124,20 @@ function compile(source: string, _options: CompileOptions) {
} }
stats.start('stylesheet'); 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.stop('stylesheet');
stats.start('validate'); stats.start('validate');
validate(parsed, source, stylesheet, stats, options); validate(ast, source, stylesheet, stats, options);
stats.stop('validate'); stats.stop('validate');
if (options.generate === false) { 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; 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 = {}) { function create(source: string, _options: CompileOptions = {}) {

@ -20,7 +20,7 @@ export interface Parser {
metaTags: {}; metaTags: {};
} }
export interface Parsed { export interface Ast {
html: Node; html: Node;
css: Node; css: Node;
js: Node; js: Node;

@ -5,7 +5,7 @@ import { whitespace } from '../utils/patterns';
import { trimStart, trimEnd } from '../utils/trim'; import { trimStart, trimEnd } from '../utils/trim';
import reservedNames from '../utils/reservedNames'; import reservedNames from '../utils/reservedNames';
import fullCharCodeAt from '../utils/fullCharCodeAt'; import fullCharCodeAt from '../utils/fullCharCodeAt';
import { Node, Parsed } from '../interfaces'; import { Node, Ast } from '../interfaces';
import error from '../utils/error'; import error from '../utils/error';
interface ParserOptions { interface ParserOptions {
@ -223,7 +223,7 @@ export class Parser {
export default function parse( export default function parse(
template: string, template: string,
options: ParserOptions = {} options: ParserOptions = {}
): Parsed { ): Ast {
const parser = new Parser(template, options); const parser = new Parser(template, options);
return { return {
html: parser.html, html: parser.html,

@ -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;
}

@ -6,7 +6,7 @@ import Stats from '../Stats';
import error from '../utils/error'; import error from '../utils/error';
import Stylesheet from '../css/Stylesheet'; import Stylesheet from '../css/Stylesheet';
import Stats from '../Stats'; import Stats from '../Stats';
import { Node, Parsed, CompileOptions, Warning } from '../interfaces'; import { Node, Ast, CompileOptions, Warning } from '../interfaces';
export class Validator { export class Validator {
readonly source: string; readonly source: string;
@ -34,7 +34,7 @@ export class Validator {
actions: Set<string>; actions: Set<string>;
}; };
constructor(parsed: Parsed, source: string, stats: Stats, options: CompileOptions) { constructor(ast: Ast, source: string, stats: Stats, options: CompileOptions) {
this.source = source; this.source = source;
this.stats = stats; this.stats = stats;
@ -93,7 +93,7 @@ export class Validator {
} }
export default function validate( export default function validate(
parsed: Parsed, ast: Ast,
source: string, source: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
stats: Stats, 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, name,
filename, filename,
dev, dev,
parser parser
}); });
if (parsed.js) { if (ast.js) {
validateJs(validator, parsed.js); validateJs(validator, ast.js);
} }
if (parsed.css) { if (ast.css) {
stylesheet.validate(validator); stylesheet.validate(validator);
} }
if (parsed.html) { if (ast.html) {
validateHtml(validator, parsed.html); validateHtml(validator, ast.html);
} }
// need to do a second pass of the JS, now that we've analysed the markup // 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 = { const categories = {
components: 'component', components: 'component',
// TODO helpers require a bit more work — need to analyse all expressions // TODO helpers require a bit more work — need to analyse all expressions

Loading…
Cancel
Save