From b2861022694e21ca63fb3511c7464cb3323ea3ff Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Wed, 7 Jul 2021 16:16:35 +0200 Subject: [PATCH] [chore] move compiler warnings/errors to dedicate files (#6503) --- src/compiler/compile/Component.ts | 144 ++------- src/compiler/compile/compiler_errors.ts | 253 ++++++++++++++++ src/compiler/compile/compiler_warnings.ts | 143 +++++++++ src/compiler/compile/css/Selector.ts | 11 +- src/compiler/compile/css/Stylesheet.ts | 6 +- src/compiler/compile/nodes/Animation.ts | 11 +- src/compiler/compile/nodes/Binding.ts | 26 +- src/compiler/compile/nodes/EachBlock.ts | 6 +- src/compiler/compile/nodes/Element.ts | 282 ++++-------------- src/compiler/compile/nodes/Head.ts | 6 +- src/compiler/compile/nodes/InlineComponent.ts | 21 +- src/compiler/compile/nodes/Let.ts | 6 +- src/compiler/compile/nodes/Slot.ts | 16 +- src/compiler/compile/nodes/SlotTemplate.ts | 16 +- src/compiler/compile/nodes/Title.ts | 11 +- src/compiler/compile/nodes/Transition.ts | 17 +- src/compiler/compile/nodes/Window.ts | 18 +- .../compile/nodes/shared/AbstractBlock.ts | 6 +- .../compile/nodes/shared/Expression.ts | 6 +- .../wrappers/InlineComponent/index.ts | 6 +- tsconfig.json | 3 + 21 files changed, 535 insertions(+), 479 deletions(-) create mode 100644 src/compiler/compile/compiler_errors.ts create mode 100644 src/compiler/compile/compiler_warnings.ts diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 241a2c05d4..9001d09f68 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -34,6 +34,8 @@ import { apply_preprocessor_sourcemap } from '../utils/mapped_code'; import Element from './nodes/Element'; import { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping/dist/types/types'; import { clone } from '../utils/clone'; +import compiler_warnings from './compiler_warnings'; +import compiler_errors from './compiler_errors'; interface ComponentOptions { namespace?: string; @@ -161,10 +163,7 @@ export default class Component { 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 ' - }); + this.warn(svelteOptions, compiler_warnings.custom_element_no_tag); } this.tag = this.component_options.tag || compile_options.tag; } else { @@ -478,18 +477,12 @@ export default class Component { extract_exports(node) { if (node.type === 'ExportDefaultDeclaration') { - this.error(node, { - code: 'default-export', - message: 'A component cannot have a default export' - }); + this.error(node, compiler_errors.default_export); } if (node.type === 'ExportNamedDeclaration') { if (node.source) { - this.error(node, { - code: 'not-implemented', - message: 'A component currently cannot have an export ... from' - }); + this.error(node, compiler_errors.not_implemented); } if (node.declaration) { if (node.declaration.type === 'VariableDeclaration') { @@ -498,10 +491,7 @@ export default class Component { const variable = this.var_lookup.get(name); variable.export_name = name; if (variable.writable && !(variable.referenced || variable.referenced_from_script || variable.subscribable)) { - this.warn(declarator, { - code: 'unused-export-let', - message: `${this.name.name} has unused export property '${name}'. If it is for external reference only, please consider using \`export const ${name}\`` - }); + this.warn(declarator, compiler_warnings.unused_export_let(this.name.name, name)); } }); }); @@ -521,10 +511,7 @@ export default class Component { variable.export_name = specifier.exported.name; if (variable.writable && !(variable.referenced || variable.referenced_from_script || variable.subscribable)) { - this.warn(specifier, { - code: 'unused-export-let', - message: `${this.name.name} has unused export property '${specifier.exported.name}'. If it is for external reference only, please consider using \`export const ${specifier.exported.name}\`` - }); + this.warn(specifier, compiler_warnings.unused_export_let(this.name.name, specifier.exported.name)); } } }); @@ -555,10 +542,7 @@ export default class Component { walk(script.content, { enter(node: Node) { if (node.type === 'LabeledStatement' && node.label.name === '$') { - component.warn(node as any, { - code: 'module-script-reactive-declaration', - message: '$: has no effect in a module script' - }); + component.warn(node as any, compiler_warnings.module_script_reactive_declaration); } } }); @@ -568,10 +552,7 @@ export default class Component { scope.declarations.forEach((node, name) => { if (name[0] === '$') { - this.error(node as any, { - code: 'illegal-declaration', - message: 'The $ prefix is reserved, and cannot be used for variable and import names' - }); + this.error(node as any, compiler_errors.illegal_declaration); } const writable = node.type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let'); @@ -586,10 +567,7 @@ export default class Component { globals.forEach((node, name) => { if (name[0] === '$') { - this.error(node as any, { - code: 'illegal-subscription', - message: 'Cannot reference store value inside