diff --git a/package-lock.json b/package-lock.json index 9cc91dbf6f5d..bd36702370ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "svelte", - "version": "3.10.1", + "version": "3.11.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 9f2a62fe9342..6f381db115ce 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -6,7 +6,12 @@ import Stats from '../Stats'; import { globals, reserved } from '../utils/names'; import { namespaces, valid_namespaces } from '../utils/namespaces'; import create_module from './create_module'; -import { create_scopes, extract_names, Scope, extract_identifiers } from './utils/scope'; +import { + create_scopes, + extract_names, + Scope, + extract_identifiers, +} from './utils/scope'; import Stylesheet from './css/Stylesheet'; import { test } from '../config'; import Fragment from './nodes/Fragment'; @@ -41,7 +46,13 @@ childKeys.EachBlock = childKeys.IfBlock = ['children', 'else']; childKeys.Attribute = ['value']; childKeys.ExportNamedDeclaration = ['declaration', 'specifiers']; -function remove_node(code: MagicString, start: number, end: number, body: Node, node: Node) { +function remove_node( + code: MagicString, + start: number, + end: number, + body: Node, + node: Node +) { const i = body.indexOf(node); if (i === -1) throw new Error('node not in list'); @@ -101,7 +112,12 @@ export default class Component { node_for_declaration: Map = new Map(); partly_hoisted: string[] = []; fully_hoisted: string[] = []; - reactive_declarations: Array<{ assignees: Set; dependencies: Set; node: Node; declaration: Node }> = []; + reactive_declarations: Array<{ + assignees: Set; + dependencies: Set; + node: Node; + declaration: Node; + }> = []; reactive_declaration_nodes: Set = new Set(); has_reactive_assignments = false; injected_reactive_declaration_vars: Set = new Set(); @@ -114,7 +130,10 @@ export default class Component { locate: (c: number) => { line: number; column: number }; // TODO this does the same as component.locate! remove one or the other - locator: (search: number, startIndex?: number) => { + locator: ( + search: number, + startIndex?: number + ) => { line: number; column: number; }; @@ -144,27 +163,46 @@ export default class Component { this.source = source; this.compile_options = compile_options; - this.file = compile_options.filename && ( + this.file = + compile_options.filename && // eslint-disable-next-line no-useless-escape - typeof process !== 'undefined' ? compile_options.filename.replace(process.cwd(), '').replace(/^[\/\\]/, '') : compile_options.filename - ); + (typeof process !== 'undefined' + ? compile_options.filename + .replace(process.cwd(), '') + .replace(/^[\/\\]/, '') + : compile_options.filename); this.locate = getLocator(this.source); this.code = new MagicString(source); // styles - this.stylesheet = new Stylesheet(source, ast, compile_options.filename, compile_options.dev); + this.stylesheet = new Stylesheet( + source, + ast, + compile_options.filename, + compile_options.dev + ); this.stylesheet.validate(this); - this.component_options = process_component_options(this, this.ast.html.children); - this.namespace = namespaces[this.component_options.namespace] || this.component_options.namespace; + this.component_options = process_component_options( + this, + this.ast.html.children + ); + this.namespace = + namespaces[this.component_options.namespace] || + this.component_options.namespace; if (compile_options.customElement) { - if (this.component_options.tag === undefined && compile_options.tag === undefined) { - const svelteOptions = ast.html.children.find(child => child.name === 'svelte:options') || { start: 0, end: 0 }; + if ( + this.component_options.tag === undefined && + compile_options.tag === undefined + ) { + const svelteOptions = ast.html.children.find( + child => child.name === 'svelte:options' + ) || { start: 0, end: 0 }; this.warn(svelteOptions, { code: 'custom-element-no-tag', - message: `No custom element 'tag' option was specified. To automatically register a custom element, specify a name with a hyphen in it, e.g. . To hide this warning, use ` + message: `No custom element 'tag' option was specified. To automatically register a custom element, specify a name with a hyphen in it, e.g. . To hide this warning, use `, }); } this.tag = this.component_options.tag || compile_options.tag; @@ -199,7 +237,7 @@ export default class Component { this.add_var({ name, injected: true, - referenced: true + referenced: true, }); } else if (name[0] === '$') { this.add_var({ @@ -207,7 +245,7 @@ export default class Component { injected: true, referenced: true, mutated: true, - writable: true + writable: true, }); const subscribable_name = name.slice(1); @@ -257,35 +295,52 @@ export default class Component { const { compile_options, name } = this; const { format = 'esm' } = compile_options; - const banner = `/* ${this.file ? `${this.file} ` : ``}generated by Svelte v${"__VERSION__"} */`; + const banner = `/* ${ + this.file ? `${this.file} ` : `` + }generated by Svelte v${'__VERSION__'} */`; result = result .replace(/__svelte:self__/g, this.name) - .replace(compile_options.generate === 'ssr' ? /(@+|#+)(\w*(?:-\w*)?)/g : /(@+)(\w*(?:-\w*)?)/g, (_match: string, sigil: string, name: string) => { - if (sigil === '@') { - if (name[0] === '_') { - return this.global(name.slice(1)); - } + .replace( + compile_options.generate === 'ssr' + ? /(@+|#+)(\w*(?:-\w*)?)/g + : /(@+)(\w*(?:-\w*)?)/g, + (_match: string, sigil: string, name: string) => { + if (sigil === '@') { + if (name[0] === '_') { + return this.global(name.slice(1)); + } - if (!internal_exports.has(name)) { - throw new Error(`compiler error: this shouldn't happen! generated code is trying to use inexistent internal '${name}'`); - } + if (!internal_exports.has(name)) { + throw new Error( + `compiler error: this shouldn't happen! generated code is trying to use inexistent internal '${name}'` + ); + } + + if (compile_options.dev) { + if (internal_exports.has(`${name}_dev`)) name = `${name}_dev`; + else if (internal_exports.has(`${name}Dev`)) + name = `${name}Dev`; + } - if (compile_options.dev && internal_exports.has(`${name}Dev`)) { - name = `${name}Dev`; + return this.helper(name); } - return this.helper(name); + return sigil.slice(1) + name; } + ); - return sigil.slice(1) + name; - }); - - const referenced_globals = Array.from(this.globals, ([name, alias]) => name !== alias && ({ name, alias })).filter(Boolean); + const referenced_globals = Array.from( + this.globals, + ([name, alias]) => name !== alias && { name, alias } + ).filter(Boolean); if (referenced_globals.length) { this.helper('globals'); } - const imported_helpers = Array.from(this.helpers, ([name, alias]) => ({ name, alias })); + const imported_helpers = Array.from(this.helpers, ([name, alias]) => ({ + name, + alias, + })); const module = create_module( result, @@ -296,10 +351,12 @@ export default class Component { imported_helpers, referenced_globals, this.imports, - this.vars.filter(variable => variable.module && variable.export_name).map(variable => ({ - name: variable.name, - as: variable.export_name - })), + this.vars + .filter(variable => variable.module && variable.export_name) + .map(variable => ({ + name: variable.name, + as: variable.export_name, + })), this.source ); @@ -343,16 +400,16 @@ export default class Component { add_string(final_chunk); - css = compile_options.customElement ? - { code: null, map: null } : - this.stylesheet.render(compile_options.cssOutputFilename, true); + css = compile_options.customElement + ? { code: null, map: null } + : this.stylesheet.render(compile_options.cssOutputFilename, true); js = { code: compiled.toString(), map: compiled.generateMap({ includeContent: true, file: compile_options.outputFilename, - }) + }), }; } @@ -361,17 +418,19 @@ export default class Component { css, ast: this.ast, warnings: this.warnings, - vars: this.vars.filter(v => !v.global && !v.internal).map(v => ({ - name: v.name, - export_name: v.export_name || null, - injected: v.injected || false, - module: v.module || false, - mutated: v.mutated || false, - reassigned: v.reassigned || false, - referenced: v.referenced || false, - writable: v.writable || false - })), - stats: this.stats.render() + vars: this.vars + .filter(v => !v.global && !v.internal) + .map(v => ({ + name: v.name, + export_name: v.export_name || null, + injected: v.injected || false, + module: v.module || false, + mutated: v.mutated || false, + reassigned: v.reassigned || false, + referenced: v.referenced || false, + writable: v.writable || false, + })), + stats: this.stats.render(), }; } @@ -406,8 +465,7 @@ export default class Component { let alias = name; for ( let i = 1; - this.used_names.has(alias) || - local_used_names.has(alias); + this.used_names.has(alias) || local_used_names.has(alias); alias = `${name}_${i++}` ); local_used_names.add(alias); @@ -432,7 +490,7 @@ export default class Component { source: this.source, start: pos.start, end: pos.end, - filename: this.compile_options.filename + filename: this.compile_options.filename, }); } @@ -467,7 +525,8 @@ export default class Component { end, pos: pos.start, filename: this.compile_options.filename, - toString: () => `${warning.message} (${start.line + 1}:${start.column})\n${frame}`, + toString: () => + `${warning.message} (${start.line + 1}:${start.column})\n${frame}`, }); } @@ -490,7 +549,7 @@ export default class Component { if (node.type === 'ExportDefaultDeclaration') { this.error(node, { code: `default-export`, - message: `A component cannot have a default export` + message: `A component cannot have a default export`, }); } @@ -498,7 +557,7 @@ export default class Component { if (node.source) { this.error(node, { code: `not-implemented`, - message: `A component currently cannot have an export ... from` + message: `A component currently cannot have an export ... from`, }); } if (node.declaration) { @@ -538,7 +597,8 @@ export default class Component { if (this.hoistable_nodes.has(node)) return false; if (this.reactive_declaration_nodes.has(node)) return false; if (node.type === 'ImportDeclaration') return false; - if (node.type === 'ExportDeclaration' && node.specifiers.length > 0) return false; + if (node.type === 'ExportDeclaration' && node.specifiers.length > 0) + return false; return true; }); @@ -551,8 +611,11 @@ export default class Component { let result = ''; - script.content.body.forEach((node) => { - if (this.hoistable_nodes.has(node) || this.reactive_declaration_nodes.has(node)) { + script.content.body.forEach(node => { + if ( + this.hoistable_nodes.has(node) || + this.reactive_declaration_nodes.has(node) + ) { if (a !== b) result += `[✂${a}-${b}✂]`; a = node.end; } @@ -580,10 +643,10 @@ export default class Component { if (node.type === 'LabeledStatement' && node.label.name === '$') { component.warn(node, { code: 'module-script-reactive-declaration', - message: '$: has no effect in a module script' + message: '$: has no effect in a module script', }); } - } + }, }); this.add_sourcemap_locations(script.content); @@ -595,7 +658,7 @@ export default class Component { if (name[0] === '$') { this.error(node, { code: 'illegal-declaration', - message: `The $ prefix is reserved, and cannot be used for variable and import names` + message: `The $ prefix is reserved, and cannot be used for variable and import names`, }); } @@ -603,7 +666,7 @@ export default class Component { name, module: true, hoistable: true, - writable: node.kind === 'var' || node.kind === 'let' + writable: node.kind === 'var' || node.kind === 'let', }); }); @@ -611,12 +674,12 @@ export default class Component { if (name[0] === '$') { this.error(node, { code: 'illegal-subscription', - message: `Cannot reference store value inside