diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 9c81546b64..d90cfaab38 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -8,8 +8,7 @@ import globalWhitelist from '../utils/globalWhitelist'; import reservedNames from '../utils/reservedNames'; import namespaces from '../utils/namespaces'; import { removeNode, removeObjectKey } from '../utils/removeNode'; -import getIntro from './shared/utils/getIntro'; -import getOutro from './shared/utils/getOutro'; +import wrapModule from './shared/utils/wrapModule'; import annotateWithScopes from '../utils/annotateWithScopes'; import clone from '../utils/clone'; import DomBlock from './dom/Block'; @@ -303,55 +302,12 @@ export default class Generator { return (expression._dependencies = dependencies); } - generate(result: string, options: CompileOptions, { name, format }: GenerateOptions ) { - if (this.imports.length) { - const statements: string[] = []; - - this.imports.forEach((declaration, i) => { - if (format === 'es') { - statements.push( - this.source.slice(declaration.start, declaration.end) - ); - return; - } - - const defaultImport = declaration.specifiers.find( - (x: Node) => - x.type === 'ImportDefaultSpecifier' || - (x.type === 'ImportSpecifier' && x.imported.name === 'default') - ); - const namespaceImport = declaration.specifiers.find( - (x: Node) => x.type === 'ImportNamespaceSpecifier' - ); - const namedImports = declaration.specifiers.filter( - (x: Node) => - x.type === 'ImportSpecifier' && x.imported.name !== 'default' - ); - - const name = defaultImport || namespaceImport - ? (defaultImport || namespaceImport).local.name - : `__import${i}`; - declaration.name = name; // hacky but makes life a bit easier later - - namedImports.forEach((specifier: Node) => { - statements.push( - `var ${specifier.local.name} = ${name}.${specifier.imported.name}` - ); - }); - - if (defaultImport) { - statements.push( - `${name} = (${name} && ${name}.__esModule) ? ${name}['default'] : ${name};` - ); - } - }); - - result = `${statements.join('\n')}\n\n${result}`; - } - + generate(result: string, options: CompileOptions, { banner = '', sharedPath, helpers, name, format }: GenerateOptions ) { const pattern = /\[✂(\d+)-(\d+)$/; - const parts = result.split('✂]'); + const module = wrapModule(result, format, name, options, banner, sharedPath, helpers, this.imports, this.source); + + const parts = module.split('✂]'); const finalChunk = parts.pop(); const compiled = new Bundle({ separator: '' }); @@ -362,9 +318,6 @@ export default class Generator { }); } - const intro = getIntro(format, options, this.imports); - if (intro) addString(intro); - const { filename } = options; // special case — the source file doesn't actually get used anywhere. we need @@ -391,7 +344,6 @@ export default class Generator { }); addString(finalChunk); - addString('\n\n' + getOutro(format, name, options, this.imports)); const { css, cssMap } = this.customElement ? { css: null, cssMap: null } : diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 9633eb34a2..22371c71bc 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -356,34 +356,20 @@ export default function dom( return generator.alias(name); }); - if (sharedPath) { - const used = Array.from(usedHelpers).sort(); - if (format === 'es') { - const names = used.map(name => { - const alias = generator.alias(name); - return name !== alias ? `${name} as ${alias}` : name; - }); - - result = - `import { ${names.join(', ')} } from ${JSON.stringify(sharedPath)};\n\n` + - result; - } - - else if (format === 'cjs') { - const SHARED = '__shared'; - let requires = `var ${SHARED} = require(${JSON.stringify(sharedPath)});`; - used.forEach(name => { - const alias = generator.alias(name); - requires += `\nvar ${alias} = ${SHARED}.${name};`; - }); + let helpers; - result = `${requires}\n\n${result}`; - } - - else { + if (sharedPath) { + if (format !== 'es' && format !== 'cjs') { throw new Error(`Components with shared helpers must be compiled with \`format: 'es'\` or \`format: 'cjs'\``); } + const used = Array.from(usedHelpers).sort(); + helpers = used.map(name => { + const alias = generator.alias(name); + return { name, alias }; + }); } else { + let inlineHelpers = ''; + usedHelpers.forEach(key => { const str = shared[key]; const code = new MagicString(str); @@ -421,20 +407,27 @@ export default function dom( // special case const global = `_svelteTransitionManager`; - result += `\n\nvar ${generator.alias('transitionManager')} = window.${global} || (window.${global} = ${code});`; + inlineHelpers += `\n\nvar ${generator.alias('transitionManager')} = window.${global} || (window.${global} = ${code});\n\n`; } else { const alias = generator.alias(expression.id.name); if (alias !== expression.id.name) code.overwrite(expression.id.start, expression.id.end, alias); - result += `\n\n${code}`; + inlineHelpers += `\n\n${code}`; } }); + + result += inlineHelpers; } - result = `/* ${options.filename ? `${options.filename} ` : ``}generated by Svelte v${version} */\n\n${result}`; + const filename = options.filename && ( + typeof process !== 'undefined' ? options.filename.replace(process.cwd(), '').replace(/^[\/\\]/, '') : options.filename + ); return generator.generate(result, options, { + banner: `/* ${filename ? `${filename} ` : ``}generated by Svelte v${version} */`, + sharedPath, + helpers, name, format, }); diff --git a/src/generators/dom/visitors/EachBlock.ts b/src/generators/dom/visitors/EachBlock.ts index 330dba6b81..a17285a8d2 100644 --- a/src/generators/dom/visitors/EachBlock.ts +++ b/src/generators/dom/visitors/EachBlock.ts @@ -122,7 +122,7 @@ export default function visitEachBlock( ); block.builders.destroy.addBlock(deindent` - if (${each_block_else}) ${each_block_else}.destroy(false); + if (${each_block_else}) ${each_block_else}.destroy(); `); } @@ -351,7 +351,7 @@ function keyed( block.builders.destroy.addBlock(deindent` var ${iteration} = ${head}; while (${iteration}) { - ${iteration}.destroy(false); + ${iteration}.destroy(); ${iteration} = ${iteration}.next; } `); @@ -486,5 +486,5 @@ function unkeyed( } `); - block.builders.destroy.addBlock(`@destroyEach(${iterations}, false, 0);`); + block.builders.destroy.addBlock(`@destroyEach(${iterations});`); } diff --git a/src/generators/dom/visitors/Element/EventHandler.ts b/src/generators/dom/visitors/Element/EventHandler.ts index cf71794de4..ded9abc44b 100644 --- a/src/generators/dom/visitors/Element/EventHandler.ts +++ b/src/generators/dom/visitors/Element/EventHandler.ts @@ -1,5 +1,6 @@ import deindent from '../../../../utils/deindent'; import flattenReference from '../../../../utils/flattenReference'; +import validCalleeObjects from '../../../../utils/validCalleeObjects'; import { DomGenerator } from '../../index'; import Block from '../../Block'; import { Node } from '../../../../interfaces'; @@ -23,7 +24,7 @@ export default function visitEventHandler( generator.addSourcemapLocations(attribute.expression); const flattened = flattenReference(attribute.expression.callee); - if (flattened.name !== 'event' && flattened.name !== 'this') { + if (!validCalleeObjects.has(flattened.name)) { // allow event.stopPropagation(), this.select() etc // TODO verify that it's a valid callee (i.e. built-in or declared method) generator.code.prependRight( diff --git a/src/generators/shared/utils/getGlobals.ts b/src/generators/shared/utils/getGlobals.ts deleted file mode 100644 index eb0db21276..0000000000 --- a/src/generators/shared/utils/getGlobals.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { CompileOptions, Node } from '../../../interfaces'; - - -export default function getGlobals(imports: Node[], options: CompileOptions) { - const { globals, onerror, onwarn } = options; - const globalFn = getGlobalFn(globals); - - return imports.map(x => { - let name = globalFn(x.source.value); - - if (!name) { - if (x.name.startsWith('__import')) { - const error = new Error( - `Could not determine name for imported module '${x.source.value}' – use options.globals` - ); - if (onerror) { - onerror(error); - } else { - throw error; - } - } else { - const warning = { - message: `No name was supplied for imported module '${x.source.value}'. Guessing '${x.name}', but you should use options.globals`, - }; - - if (onwarn) { - onwarn(warning); - } else { - console.warn(warning); // eslint-disable-line no-console - } - } - - name = x.name; - } - - return name; - }); -} - -function getGlobalFn(globals: any): (id: string) => string { - if (typeof globals === 'function') return globals; - if (typeof globals === 'object') { - return id => globals[id]; - } - - return () => undefined; -} diff --git a/src/generators/shared/utils/getIntro.ts b/src/generators/shared/utils/getIntro.ts deleted file mode 100644 index 599bc5f7b3..0000000000 --- a/src/generators/shared/utils/getIntro.ts +++ /dev/null @@ -1,95 +0,0 @@ -import deindent from '../../../utils/deindent'; -import getGlobals from './getGlobals'; -import { CompileOptions, ModuleFormat, Node } from '../../../interfaces'; - -export default function getIntro( - format: ModuleFormat, - options: CompileOptions, - imports: Node[] -) { - if (format === 'es') return ''; - if (format === 'amd') return getAmdIntro(options, imports); - if (format === 'cjs') return getCjsIntro(options, imports); - if (format === 'iife') return getIifeIntro(options, imports); - if (format === 'umd') return getUmdIntro(options, imports); - if (format === 'eval') return getEvalIntro(options, imports); - - throw new Error(`Not implemented: ${format}`); -} - -function getAmdIntro(options: CompileOptions, imports: Node[]) { - const sourceString = imports.length - ? `[${imports - .map(declaration => `'${removeExtension(declaration.source.value)}'`) - .join(', ')}], ` - : ''; - - const id = options.amd && options.amd.id; - - return `define(${id - ? `"${id}", ` - : ''}${sourceString}function(${paramString(imports)}) { 'use strict';\n\n`; -} - -function getCjsIntro(options: CompileOptions, imports: Node[]) { - const requireBlock = imports - .map( - declaration => - `var ${declaration.name} = require('${declaration.source.value}');` - ) - .join('\n\n'); - - if (requireBlock) { - return `'use strict';\n\n${requireBlock}\n\n`; - } - - return `'use strict';\n\n`; -} - -function getIifeIntro(options: CompileOptions, imports: Node[]) { - if (!options.name) { - throw new Error(`Missing required 'name' option for IIFE export`); - } - - return `var ${options.name} = (function(${paramString(imports)}) { 'use strict';\n\n`; -} - -function getUmdIntro(options: CompileOptions, imports: Node[]) { - if (!options.name) { - throw new Error(`Missing required 'name' option for UMD export`); - } - - const amdId = options.amd && options.amd.id ? `'${options.amd.id}', ` : ''; - - const amdDeps = imports.length - ? `[${imports - .map(declaration => `'${removeExtension(declaration.source.value)}'`) - .join(', ')}], ` - : ''; - const cjsDeps = imports - .map(declaration => `require('${declaration.source.value}')`) - .join(', '); - const globalDeps = getGlobals(imports, options); - - return ( - deindent` - (function(global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(${cjsDeps}) : - typeof define === 'function' && define.amd ? define(${amdId}${amdDeps}factory) : - (global.${options.name} = factory(${globalDeps})); - }(this, (function (${paramString(imports)}) { 'use strict';` + '\n\n' - ); -} - -function getEvalIntro(options: CompileOptions, imports: Node[]) { - return `(function (${paramString(imports)}) { 'use strict';\n\n`; -} - -function paramString(imports: Node[]) { - return imports.map(dep => dep.name).join(', '); -} - -function removeExtension(file: string) { - const index = file.lastIndexOf('.'); - return ~index ? file.slice(0, index) : file; -} diff --git a/src/generators/shared/utils/getOutro.ts b/src/generators/shared/utils/getOutro.ts deleted file mode 100644 index 682a3cc870..0000000000 --- a/src/generators/shared/utils/getOutro.ts +++ /dev/null @@ -1,37 +0,0 @@ -import getGlobals from './getGlobals'; -import { CompileOptions, Node } from '../../../interfaces'; - -export default function getOutro( - format: string, - name: string, - options: CompileOptions, - imports: Node[] -) { - if (format === 'es') { - return `export default ${name};`; - } - - if (format === 'amd') { - return `return ${name};\n\n});`; - } - - if (format === 'cjs') { - return `module.exports = ${name};`; - } - - if (format === 'iife') { - const globals = getGlobals(imports, options); - return `return ${name};\n\n}(${globals.join(', ')}));`; - } - - if (format === 'eval') { - const globals = getGlobals(imports, options); - return `return ${name};\n\n}(${globals.join(', ')}));`; - } - - if (format === 'umd') { - return `return ${name};\n\n})));`; - } - - throw new Error(`Not implemented: ${format}`); -} diff --git a/src/generators/shared/utils/wrapModule.ts b/src/generators/shared/utils/wrapModule.ts new file mode 100644 index 0000000000..77fea53446 --- /dev/null +++ b/src/generators/shared/utils/wrapModule.ts @@ -0,0 +1,306 @@ +import deindent from '../../../utils/deindent'; +import { CompileOptions, ModuleFormat, Node } from '../../../interfaces'; + +interface Dependency { + name: string; + statements: string[]; + source: string; +} + +const wrappers = { es, amd, cjs, iife, umd, eval: expr }; + +export default function wrapModule( + code: string, + format: ModuleFormat, + name: string, + options: CompileOptions, + banner: string, + sharedPath: string, + helpers: { name: string, alias: string }[], + imports: Node[], + source: string +): string { + if (format === 'es') return es(code, name, options, banner, sharedPath, helpers, imports, source); + + const dependencies = imports.map((declaration, i) => { + const defaultImport = declaration.specifiers.find( + (x: Node) => + x.type === 'ImportDefaultSpecifier' || + (x.type === 'ImportSpecifier' && x.imported.name === 'default') + ); + + const namespaceImport = declaration.specifiers.find( + (x: Node) => x.type === 'ImportNamespaceSpecifier' + ); + + const namedImports = declaration.specifiers.filter( + (x: Node) => + x.type === 'ImportSpecifier' && x.imported.name !== 'default' + ); + + const name = defaultImport || namespaceImport + ? (defaultImport || namespaceImport).local.name + : `__import${i}`; + + const statements: string[] = []; + + namedImports.forEach((specifier: Node) => { + statements.push( + `var ${specifier.local.name} = ${name}.${specifier.imported.name};` + ); + }); + + if (defaultImport) { + statements.push( + `${name} = (${name} && ${name}.__esModule) ? ${name}["default"] : ${name};` + ); + } + + return { name, statements, source: declaration.source.value }; + }); + + if (format === 'amd') return amd(code, name, options, banner, dependencies); + if (format === 'cjs') return cjs(code, name, options, banner, sharedPath, helpers, dependencies); + if (format === 'iife') return iife(code, name, options, banner, dependencies); + if (format === 'umd') return umd(code, name, options, banner, dependencies); + if (format === 'eval') return expr(code, name, options, banner, dependencies); + + throw new Error(`Not implemented: ${format}`); +} + +function es( + code: string, + name: string, + options: CompileOptions, + banner: string, + sharedPath: string, + helpers: { name: string, alias: string }[], + imports: Node[], + source: string +) { + const importHelpers = helpers && ( + `import { ${helpers.map(h => h.name === h.alias ? h.name : `${h.name} as ${h.alias}`).join(', ')} } from ${JSON.stringify(sharedPath)};` + ); + + const importBlock = imports.length > 0 && ( + imports + .map((declaration: Node) => source.slice(declaration.start, declaration.end)) + .join('\n') + ); + + return deindent` + ${banner} + ${importHelpers} + ${importBlock} + + ${code} + export default ${name};`; +} + +function amd( + code: string, + name: string, + options: CompileOptions, + banner: string, + dependencies: Dependency[] +) { + const sourceString = dependencies.length + ? `[${dependencies.map(d => `"${removeExtension(d.source)}"`).join(', ')}], ` + : ''; + + const id = options.amd && options.amd.id; + + return deindent` + define(${id ? `"${id}", ` : ''}${sourceString}function(${paramString(dependencies)}) { "use strict"; + ${getCompatibilityStatements(dependencies)} + + ${code} + return ${name}; + });`; +} + +function cjs( + code: string, + name: string, + options: CompileOptions, + banner: string, + sharedPath: string, + helpers: { name: string, alias: string }[], + dependencies: Dependency[] +) { + const SHARED = '__shared'; + const helperBlock = helpers && ( + `var ${SHARED} = require(${JSON.stringify(sharedPath)});\n` + + helpers.map(helper => { + return `var ${helper.alias} = ${SHARED}.${helper.name};`; + }).join('\n') + ); + + const requireBlock = dependencies.length > 0 && ( + dependencies + .map(d => `var ${d.name} = require("${d.source}");`) + .join('\n\n') + ); + + return deindent` + ${banner} + "use strict"; + + ${helperBlock} + ${requireBlock} + ${getCompatibilityStatements(dependencies)} + + ${code} + + module.exports = ${name};` +} + +function iife( + code: string, + name: string, + options: CompileOptions, + banner: string, + dependencies: Dependency[] +) { + if (!options.name) { + throw new Error(`Missing required 'name' option for IIFE export`); + } + + const globals = getGlobals(dependencies, options); + + return deindent` + ${banner} + var ${options.name} = (function(${paramString(dependencies)}) { "use strict"; + ${getCompatibilityStatements(dependencies)} + + ${code} + return ${name}; + }(${globals.join(', ')}));`; +} + +function umd( + code: string, + name: string, + options: CompileOptions, + banner: string, + dependencies: Dependency[] +) { + if (!options.name) { + throw new Error(`Missing required 'name' option for UMD export`); + } + + const amdId = options.amd && options.amd.id ? `'${options.amd.id}', ` : ''; + + const amdDeps = dependencies.length + ? `[${dependencies.map(d => `"${removeExtension(d.source)}"`).join(', ')}], ` + : ''; + + const cjsDeps = dependencies + .map(d => `require("${d.source}")`) + .join(', '); + + const globals = getGlobals(dependencies, options); + + return deindent` + ${banner} + (function(global, factory) { + typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory(${cjsDeps}) : + typeof define === "function" && define.amd ? define(${amdId}${amdDeps}factory) : + (global.${options.name} = factory(${globals})); + }(this, (function (${paramString(dependencies)}) { "use strict"; + + ${getCompatibilityStatements(dependencies)} + + ${code} + + return ${name}; + + })));`; +} + +function expr( + code: string, + name: string, + options: CompileOptions, + banner: string, + dependencies: Dependency[] +) { + const globals = getGlobals(dependencies, options); + + return deindent` + (function (${paramString(dependencies)}) { "use strict"; + ${banner} + + ${getCompatibilityStatements(dependencies)} + + ${code} + + return ${name}; + }(${globals.join(', ')}))`; +} + +function paramString(dependencies: Dependency[]) { + return dependencies.map(dep => dep.name).join(', '); +} + +function removeExtension(file: string) { + const index = file.lastIndexOf('.'); + return ~index ? file.slice(0, index) : file; +} + +function getCompatibilityStatements(dependencies: Dependency[]) { + if (!dependencies.length) return null; + + const statements: string[] = []; + + dependencies.forEach(dependency => { + statements.push(...dependency.statements); + }); + + return statements.join('\n'); +} + +function getGlobals(dependencies: Dependency[], options: CompileOptions) { + const { globals, onerror, onwarn } = options; + const globalFn = getGlobalFn(globals); + + return dependencies.map(d => { + let name = globalFn(d.source); + + if (!name) { + if (d.name.startsWith('__import')) { + const error = new Error( + `Could not determine name for imported module '${d.source}' – use options.globals` + ); + if (onerror) { + onerror(error); + } else { + throw error; + } + } else { + const warning = { + message: `No name was supplied for imported module '${d.source}'. Guessing '${d.name}', but you should use options.globals`, + }; + + if (onwarn) { + onwarn(warning); + } else { + console.warn(warning); // eslint-disable-line no-console + } + } + + name = d.name; + } + + return name; + }); +} + +function getGlobalFn(globals: any): (id: string) => string { + if (typeof globals === 'function') return globals; + if (typeof globals === 'object') { + return id => globals[id]; + } + + return () => undefined; +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index f2a2af1483..475cfcaceb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -61,7 +61,7 @@ export function create(source: string, _options: CompileOptions = {}) { } try { - return new Function('return ' + compiled.code)(); + return (0,eval)(compiled.code); } catch (err) { if (_options.onerror) { _options.onerror(err); diff --git a/src/interfaces.ts b/src/interfaces.ts index 93d1b608f2..2d876c6a9a 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -64,6 +64,9 @@ export interface CompileOptions { export interface GenerateOptions { name: string; format: ModuleFormat; + banner?: string; + sharedPath?: string | boolean; + helpers?: { name: string, alias: string }[]; } export interface Visitor { diff --git a/src/shared/dom.js b/src/shared/dom.js index ffbda89b6d..bae9ed86c4 100644 --- a/src/shared/dom.js +++ b/src/shared/dom.js @@ -47,10 +47,9 @@ export function reinsertBefore(after, target) { while (parent.firstChild !== after) target.appendChild(parent.firstChild); } -// TODO this is out of date -export function destroyEach(iterations, detach, start) { - for (var i = start; i < iterations.length; i += 1) { - if (iterations[i]) iterations[i].destroy(detach); +export function destroyEach(iterations) { + for (var i = 0; i < iterations.length; i += 1) { + if (iterations[i]) iterations[i].destroy(); } } diff --git a/src/utils/validCalleeObjects.ts b/src/utils/validCalleeObjects.ts new file mode 100644 index 0000000000..ad6070995a --- /dev/null +++ b/src/utils/validCalleeObjects.ts @@ -0,0 +1,3 @@ +const validCalleeObjects = new Set(['this', 'event', 'console']); + +export default validCalleeObjects; \ No newline at end of file diff --git a/src/validate/html/validateEventHandler.ts b/src/validate/html/validateEventHandler.ts index bc0bb23aff..9a22bdd6c8 100644 --- a/src/validate/html/validateEventHandler.ts +++ b/src/validate/html/validateEventHandler.ts @@ -1,6 +1,7 @@ import flattenReference from '../../utils/flattenReference'; import list from '../utils/list'; import { Validator } from '../index'; +import validCalleeObjects from '../../utils/validCalleeObjects'; import { Node } from '../../interfaces'; const validBuiltins = new Set(['set', 'fire', 'destroy']); @@ -20,7 +21,7 @@ export default function validateEventHandlerCallee( const { name } = flattenReference(callee); - if (name === 'this' || name === 'event') return; + if (validCalleeObjects.has(name)) return; if (name === 'refs') { refCallees.push(callee); @@ -33,7 +34,7 @@ export default function validateEventHandlerCallee( ) return; - const validCallees = ['this.*', 'event.*'].concat( + const validCallees = ['this.*', 'event.*', 'console.*'].concat( Array.from(validBuiltins), Array.from(validator.methods.keys()) ); diff --git a/test/js/samples/collapses-text-around-comments/expected-bundle.js b/test/js/samples/collapses-text-around-comments/expected-bundle.js index dcc47255b3..5d44708a27 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle.js @@ -190,7 +190,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { data: function () { diff --git a/test/js/samples/collapses-text-around-comments/expected.js b/test/js/samples/collapses-text-around-comments/expected.js index caa1bc886b..83841b4eb8 100644 --- a/test/js/samples/collapses-text-around-comments/expected.js +++ b/test/js/samples/collapses-text-around-comments/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js"; var template = (function() { @@ -69,5 +68,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/component-static/expected-bundle.js b/test/js/samples/component-static/expected-bundle.js index 2dbef4a1b1..a9769110cb 100644 --- a/test/js/samples/component-static/expected-bundle.js +++ b/test/js/samples/component-static/expected-bundle.js @@ -166,7 +166,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { components: { diff --git a/test/js/samples/component-static/expected.js b/test/js/samples/component-static/expected.js index e6f5d12eb7..8a22292037 100644 --- a/test/js/samples/component-static/expected.js +++ b/test/js/samples/component-static/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, callAll, init, noop, proto } from "svelte/shared.js"; var template = (function() { @@ -63,5 +62,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/computed-collapsed-if/expected-bundle.js b/test/js/samples/computed-collapsed-if/expected-bundle.js index 752455d407..e6e9152b1e 100644 --- a/test/js/samples/computed-collapsed-if/expected-bundle.js +++ b/test/js/samples/computed-collapsed-if/expected-bundle.js @@ -166,7 +166,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { computed: { diff --git a/test/js/samples/computed-collapsed-if/expected.js b/test/js/samples/computed-collapsed-if/expected.js index 097e5557c8..7b9207a955 100644 --- a/test/js/samples/computed-collapsed-if/expected.js +++ b/test/js/samples/computed-collapsed-if/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, differs, init, noop, proto } from "svelte/shared.js"; var template = (function() { @@ -47,5 +46,4 @@ SvelteComponent.prototype._recompute = function _recompute(changed, state) { if (differs(state.b, (state.b = template.computed.b(state.x)))) changed.b = true; } } - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/css-media-query/expected-bundle.js b/test/js/samples/css-media-query/expected-bundle.js index 8268700417..e37322f9e6 100644 --- a/test/js/samples/css-media-query/expected-bundle.js +++ b/test/js/samples/css-media-query/expected-bundle.js @@ -186,7 +186,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function encapsulateStyles(node) { setAttribute(node, "svelte-2363328337", ""); } diff --git a/test/js/samples/css-media-query/expected.js b/test/js/samples/css-media-query/expected.js index a87dfb878d..58d4b5e991 100644 --- a/test/js/samples/css-media-query/expected.js +++ b/test/js/samples/css-media-query/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createElement, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js"; function encapsulateStyles(node) { @@ -55,5 +54,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js index 0ef287405a..6155fc8ecb 100644 --- a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js +++ b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js @@ -178,7 +178,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div; diff --git a/test/js/samples/css-shadow-dom-keyframes/expected.js b/test/js/samples/css-shadow-dom-keyframes/expected.js index 87d308ea06..06a9f08bf6 100644 --- a/test/js/samples/css-shadow-dom-keyframes/expected.js +++ b/test/js/samples/css-shadow-dom-keyframes/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -61,5 +60,4 @@ assign(SvelteComponent.prototype, proto, { this.parentNode.removeChild(this); } }); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index e26de86305..882e55dfbb 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -31,10 +31,9 @@ function detachAfter(before) { } } -// TODO this is out of date -function destroyEach(iterations, detach, start) { - for (var i = start; i < iterations.length; i += 1) { - if (iterations[i]) iterations[i].destroy(detach); +function destroyEach(iterations) { + for (var i = 0; i < iterations.length; i += 1) { + if (iterations[i]) iterations[i].destroy(); } } @@ -199,7 +198,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var text, p, text_1; @@ -268,7 +266,7 @@ function create_main_fragment(state, component) { }, destroy: function() { - destroyEach(each_blocks, false, 0); + destroyEach(each_blocks); } }; } diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index efc1bfd1dd..613fea0bf5 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createElement, createText, destroyEach, detachAfter, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -70,7 +69,7 @@ function create_main_fragment(state, component) { }, destroy: function() { - destroyEach(each_blocks, false, 0); + destroyEach(each_blocks); } }; } @@ -153,5 +152,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/event-handlers-custom/expected-bundle.js b/test/js/samples/event-handlers-custom/expected-bundle.js index de3cf62dda..7b896bf8f1 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -178,7 +178,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { methods: { diff --git a/test/js/samples/event-handlers-custom/expected.js b/test/js/samples/event-handlers-custom/expected.js index 0ca93c84b2..34832b6000 100644 --- a/test/js/samples/event-handlers-custom/expected.js +++ b/test/js/samples/event-handlers-custom/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; var template = (function() { @@ -63,5 +62,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, template.methods, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/if-block-no-update/expected-bundle.js b/test/js/samples/if-block-no-update/expected-bundle.js index 3c6ccb79cb..6b6a6255b9 100644 --- a/test/js/samples/if-block-no-update/expected-bundle.js +++ b/test/js/samples/if-block-no-update/expected-bundle.js @@ -182,7 +182,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var if_block_anchor; diff --git a/test/js/samples/if-block-no-update/expected.js b/test/js/samples/if-block-no-update/expected.js index 272ca56261..10ecf2297b 100644 --- a/test/js/samples/if-block-no-update/expected.js +++ b/test/js/samples/if-block-no-update/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createComment, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -102,5 +101,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/if-block-simple/expected-bundle.js b/test/js/samples/if-block-simple/expected-bundle.js index 2aa8fa23d7..9f3c06d6a0 100644 --- a/test/js/samples/if-block-simple/expected-bundle.js +++ b/test/js/samples/if-block-simple/expected-bundle.js @@ -182,7 +182,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var if_block_anchor; diff --git a/test/js/samples/if-block-simple/expected.js b/test/js/samples/if-block-simple/expected.js index 136cac62a1..67ef669468 100644 --- a/test/js/samples/if-block-simple/expected.js +++ b/test/js/samples/if-block-simple/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createComment, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -78,5 +77,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js index 3f794ba7ac..745f0a2f26 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js @@ -182,7 +182,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div; diff --git a/test/js/samples/inline-style-optimized-multiple/expected.js b/test/js/samples/inline-style-optimized-multiple/expected.js index 45f2a50c2a..35de49a17c 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected.js +++ b/test/js/samples/inline-style-optimized-multiple/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -51,5 +50,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized-url/expected-bundle.js b/test/js/samples/inline-style-optimized-url/expected-bundle.js index 18205758cc..2985f7cb15 100644 --- a/test/js/samples/inline-style-optimized-url/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-url/expected-bundle.js @@ -182,7 +182,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div; diff --git a/test/js/samples/inline-style-optimized-url/expected.js b/test/js/samples/inline-style-optimized-url/expected.js index 9452de7328..659bc4b20b 100644 --- a/test/js/samples/inline-style-optimized-url/expected.js +++ b/test/js/samples/inline-style-optimized-url/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -46,5 +45,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized/expected-bundle.js b/test/js/samples/inline-style-optimized/expected-bundle.js index db5b7c8a2c..fa4dfc93a9 100644 --- a/test/js/samples/inline-style-optimized/expected-bundle.js +++ b/test/js/samples/inline-style-optimized/expected-bundle.js @@ -182,7 +182,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div; diff --git a/test/js/samples/inline-style-optimized/expected.js b/test/js/samples/inline-style-optimized/expected.js index bc4dc0eeda..8b07a3eaad 100644 --- a/test/js/samples/inline-style-optimized/expected.js +++ b/test/js/samples/inline-style-optimized/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -46,5 +45,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-unoptimized/expected-bundle.js b/test/js/samples/inline-style-unoptimized/expected-bundle.js index 98235d2576..e0770b55c7 100644 --- a/test/js/samples/inline-style-unoptimized/expected-bundle.js +++ b/test/js/samples/inline-style-unoptimized/expected-bundle.js @@ -182,7 +182,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div, text, div_1, div_1_style_value; diff --git a/test/js/samples/inline-style-unoptimized/expected.js b/test/js/samples/inline-style-unoptimized/expected.js index c15408b3be..9dcb2296bb 100644 --- a/test/js/samples/inline-style-unoptimized/expected.js +++ b/test/js/samples/inline-style-unoptimized/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -57,5 +56,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/input-without-blowback-guard/expected-bundle.js b/test/js/samples/input-without-blowback-guard/expected-bundle.js index 948cd943b1..8a0c36accc 100644 --- a/test/js/samples/input-without-blowback-guard/expected-bundle.js +++ b/test/js/samples/input-without-blowback-guard/expected-bundle.js @@ -186,7 +186,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var input; diff --git a/test/js/samples/input-without-blowback-guard/expected.js b/test/js/samples/input-without-blowback-guard/expected.js index 86e92e2fc4..074a6126eb 100644 --- a/test/js/samples/input-without-blowback-guard/expected.js +++ b/test/js/samples/input-without-blowback-guard/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { addListener, assign, createElement, detachNode, init, insertNode, proto, removeListener } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -53,5 +52,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/legacy-input-type/expected-bundle.js b/test/js/samples/legacy-input-type/expected-bundle.js index 036c77f3c8..6475e22fa8 100644 --- a/test/js/samples/legacy-input-type/expected-bundle.js +++ b/test/js/samples/legacy-input-type/expected-bundle.js @@ -184,7 +184,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var input; diff --git a/test/js/samples/legacy-input-type/expected.js b/test/js/samples/legacy-input-type/expected.js index 9a4d4782cd..c7785c6ed4 100644 --- a/test/js/samples/legacy-input-type/expected.js +++ b/test/js/samples/legacy-input-type/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, detachNode, init, insertNode, noop, proto, setInputType } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -42,5 +41,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/legacy-quote-class/expected-bundle.js b/test/js/samples/legacy-quote-class/expected-bundle.js index 552f1c22bc..139958519b 100644 --- a/test/js/samples/legacy-quote-class/expected-bundle.js +++ b/test/js/samples/legacy-quote-class/expected-bundle.js @@ -201,7 +201,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div; diff --git a/test/js/samples/legacy-quote-class/expected.js b/test/js/samples/legacy-quote-class/expected.js index 93121a357a..dd10330458 100644 --- a/test/js/samples/legacy-quote-class/expected.js +++ b/test/js/samples/legacy-quote-class/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, children, claimElement, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -52,5 +51,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/media-bindings/expected-bundle.js b/test/js/samples/media-bindings/expected-bundle.js index 7ed611bf35..d784e6fabe 100644 --- a/test/js/samples/media-bindings/expected-bundle.js +++ b/test/js/samples/media-bindings/expected-bundle.js @@ -194,7 +194,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var audio, audio_updating = false, audio_animationframe, audio_paused_value = true; diff --git a/test/js/samples/media-bindings/expected.js b/test/js/samples/media-bindings/expected.js index 5e67ebfb1f..531a2a1b58 100644 --- a/test/js/samples/media-bindings/expected.js +++ b/test/js/samples/media-bindings/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { addListener, assign, callAll, createElement, detachNode, init, insertNode, proto, removeListener, timeRangesToArray } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -128,5 +127,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/non-imported-component/expected-bundle.js b/test/js/samples/non-imported-component/expected-bundle.js index 817ab2c4f8..2252eb1635 100644 --- a/test/js/samples/non-imported-component/expected-bundle.js +++ b/test/js/samples/non-imported-component/expected-bundle.js @@ -180,7 +180,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { components: { diff --git a/test/js/samples/non-imported-component/expected.js b/test/js/samples/non-imported-component/expected.js index b2f77857c4..e424bb6ffd 100644 --- a/test/js/samples/non-imported-component/expected.js +++ b/test/js/samples/non-imported-component/expected.js @@ -1,8 +1,6 @@ -import Imported from 'Imported.html'; - /* generated by Svelte vX.Y.Z */ - import { assign, callAll, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import Imported from 'Imported.html'; var template = (function() { return { @@ -76,5 +74,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js index 2bec66e3e1..bebd336b6c 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js @@ -166,7 +166,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { // this test should be removed in v2 diff --git a/test/js/samples/onrender-onteardown-rewritten/expected.js b/test/js/samples/onrender-onteardown-rewritten/expected.js index 6eb6b55a1e..d29f9fd752 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, callAll, init, noop, proto } from "svelte/shared.js"; var template = (function() { @@ -50,5 +49,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/setup-method/expected-bundle.js b/test/js/samples/setup-method/expected-bundle.js index 0752ea7839..85bc643281 100644 --- a/test/js/samples/setup-method/expected-bundle.js +++ b/test/js/samples/setup-method/expected-bundle.js @@ -166,7 +166,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { methods: { diff --git a/test/js/samples/setup-method/expected.js b/test/js/samples/setup-method/expected.js index dcf0b20e8d..b3bd9fcc8e 100644 --- a/test/js/samples/setup-method/expected.js +++ b/test/js/samples/setup-method/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, init, noop, proto } from "svelte/shared.js"; var template = (function() { @@ -51,5 +50,4 @@ function SvelteComponent(options) { assign(SvelteComponent.prototype, template.methods, proto); template.setup(SvelteComponent); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/use-elements-as-anchors/expected-bundle.js b/test/js/samples/use-elements-as-anchors/expected-bundle.js index 5b346ab81f..1541e69175 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle.js @@ -190,7 +190,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div, text, p, text_2, text_3, text_4, p_1, text_6, text_8, if_block_4_anchor; diff --git a/test/js/samples/use-elements-as-anchors/expected.js b/test/js/samples/use-elements-as-anchors/expected.js index 6acbe936b2..2a1b93eb36 100644 --- a/test/js/samples/use-elements-as-anchors/expected.js +++ b/test/js/samples/use-elements-as-anchors/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createComment, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -260,5 +259,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/runtime/index.js b/test/runtime/index.js index a31f9531f8..122fe645b6 100644 --- a/test/runtime/index.js +++ b/test/runtime/index.js @@ -73,11 +73,12 @@ describe("runtime", () => { ); const { code } = svelte.compile(source, compileOptions); const startIndex = code.indexOf("function create_main_fragment"); // may change! - if (startIndex === -1) - throw new Error("missing create_main_fragment"); + if (startIndex === -1) throw new Error("missing create_main_fragment"); + const endIndex = code.lastIndexOf("export default"); const es5 = code.slice(0, startIndex).split('\n').map(x => spaces(x.length)).join('\n') + - code.slice(startIndex).replace(/export default .+/, ""); + code.slice(startIndex, endIndex); + acorn.parse(es5, { ecmaVersion: 5 }); if (/Object\.assign/.test(es5)) { diff --git a/test/runtime/samples/event-handler-console-log/_config.js b/test/runtime/samples/event-handler-console-log/_config.js new file mode 100644 index 0000000000..26d8f9ae29 --- /dev/null +++ b/test/runtime/samples/event-handler-console-log/_config.js @@ -0,0 +1,23 @@ +export default { + data: { + foo: 42 + }, + + html: ` + + `, + + test (assert, component, target, window) { + const button = target.querySelector('button'); + const event = new window.MouseEvent('click'); + + const messages = []; + + const log = console.log; + console.log = msg => messages.push(msg); + button.dispatchEvent(event); + console.log = log; + + assert.deepEqual(messages, [42]); + } +}; diff --git a/test/runtime/samples/event-handler-console-log/main.html b/test/runtime/samples/event-handler-console-log/main.html new file mode 100644 index 0000000000..3500b96373 --- /dev/null +++ b/test/runtime/samples/event-handler-console-log/main.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/sourcemaps/samples/each-block/test.js b/test/sourcemaps/samples/each-block/test.js index add51d628b..d31c565ced 100644 --- a/test/sourcemaps/samples/each-block/test.js +++ b/test/sourcemaps/samples/each-block/test.js @@ -1,7 +1,8 @@ -export function test ({ assert, smc, locateInSource, locateInGenerated }) { - const expected = locateInSource( 'each' ); +export function test({ assert, code, smc, locateInSource, locateInGenerated }) { + const startIndex = code.indexOf('create_main_fragment'); - const loc = locateInGenerated( 'length' ); + const expected = locateInSource('each'); + const loc = locateInGenerated('length', startIndex ); const actual = smc.originalPositionFor({ line: loc.line + 1, diff --git a/test/validator/samples/method-nonexistent-helper/warnings.json b/test/validator/samples/method-nonexistent-helper/warnings.json index 02512426a1..d090c4df94 100644 --- a/test/validator/samples/method-nonexistent-helper/warnings.json +++ b/test/validator/samples/method-nonexistent-helper/warnings.json @@ -1,5 +1,5 @@ [{ - "message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire, destroy or bar). 'foo' exists on 'helpers', did you put it in the wrong place?", + "message": "'foo' is an invalid callee (should be one of this.*, event.*, console.*, set, fire, destroy or bar). 'foo' exists on 'helpers', did you put it in the wrong place?", "pos": 18, "loc": { "line": 1, diff --git a/test/validator/samples/method-nonexistent/warnings.json b/test/validator/samples/method-nonexistent/warnings.json index 8debbf8132..d8f4e0e0cf 100644 --- a/test/validator/samples/method-nonexistent/warnings.json +++ b/test/validator/samples/method-nonexistent/warnings.json @@ -1,5 +1,5 @@ [{ - "message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire, destroy or bar)", + "message": "'foo' is an invalid callee (should be one of this.*, event.*, console.*, set, fire, destroy or bar)", "pos": 18, "loc": { "line": 1, diff --git a/test/validator/samples/window-event-invalid/warnings.json b/test/validator/samples/window-event-invalid/warnings.json index 425c657cb1..20dc4c79fa 100644 --- a/test/validator/samples/window-event-invalid/warnings.json +++ b/test/validator/samples/window-event-invalid/warnings.json @@ -1,5 +1,5 @@ [{ - "message": "'resize' is an invalid callee (should be one of this.*, event.*, set, fire or destroy)", + "message": "'resize' is an invalid callee (should be one of this.*, event.*, console.*, set, fire or destroy)", "loc": { "line": 1, "column": 20