@@ -34,6 +34,8 @@ import { apply_preprocessor_sourcemap } from '../utils/mapped_code';
34
34
import Element from './nodes/Element' ;
35
35
import { DecodedSourceMap , RawSourceMap } from '@ampproject/remapping/dist/types/types' ;
36
36
import { clone } from '../utils/clone' ;
37
+ import compiler_warnings from './compiler_warnings' ;
38
+ import compiler_errors from './compiler_errors' ;
37
39
38
40
interface ComponentOptions {
39
41
namespace ?: string ;
@@ -161,10 +163,7 @@ export default class Component {
161
163
const svelteOptions = ast . html . children . find (
162
164
child => child . name === 'svelte:options'
163
165
) || { start : 0 , end : 0 } ;
164
- this . warn ( svelteOptions , {
165
- code : 'custom-element-no-tag' ,
166
- message : 'No custom element \'tag\' option was specified. To automatically register a custom element, specify a name with a hyphen in it, e.g. <svelte:options tag="my-thing"/>. To hide this warning, use <svelte:options tag={null}/>'
167
- } ) ;
166
+ this . warn ( svelteOptions , compiler_warnings . custom_element_no_tag ) ;
168
167
}
169
168
this . tag = this . component_options . tag || compile_options . tag ;
170
169
} else {
@@ -478,18 +477,12 @@ export default class Component {
478
477
479
478
extract_exports ( node ) {
480
479
if ( node . type === 'ExportDefaultDeclaration' ) {
481
- this . error ( node , {
482
- code : 'default-export' ,
483
- message : 'A component cannot have a default export'
484
- } ) ;
480
+ this . error ( node , compiler_errors . default_export ) ;
485
481
}
486
482
487
483
if ( node . type === 'ExportNamedDeclaration' ) {
488
484
if ( node . source ) {
489
- this . error ( node , {
490
- code : 'not-implemented' ,
491
- message : 'A component currently cannot have an export ... from'
492
- } ) ;
485
+ this . error ( node , compiler_errors . not_implemented ) ;
493
486
}
494
487
if ( node . declaration ) {
495
488
if ( node . declaration . type === 'VariableDeclaration' ) {
@@ -498,10 +491,7 @@ export default class Component {
498
491
const variable = this . var_lookup . get ( name ) ;
499
492
variable . export_name = name ;
500
493
if ( variable . writable && ! ( variable . referenced || variable . referenced_from_script || variable . subscribable ) ) {
501
- this . warn ( declarator , {
502
- code : 'unused-export-let' ,
503
- message : `${ this . name . name } has unused export property '${ name } '. If it is for external reference only, please consider using \`export const ${ name } \``
504
- } ) ;
494
+ this . warn ( declarator , compiler_warnings . unused_export_let ( this . name . name , name ) ) ;
505
495
}
506
496
} ) ;
507
497
} ) ;
@@ -521,10 +511,7 @@ export default class Component {
521
511
variable . export_name = specifier . exported . name ;
522
512
523
513
if ( variable . writable && ! ( variable . referenced || variable . referenced_from_script || variable . subscribable ) ) {
524
- this . warn ( specifier , {
525
- code : 'unused-export-let' ,
526
- 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 } \``
527
- } ) ;
514
+ this . warn ( specifier , compiler_warnings . unused_export_let ( this . name . name , specifier . exported . name ) ) ;
528
515
}
529
516
}
530
517
} ) ;
@@ -555,10 +542,7 @@ export default class Component {
555
542
walk ( script . content , {
556
543
enter ( node : Node ) {
557
544
if ( node . type === 'LabeledStatement' && node . label . name === '$' ) {
558
- component . warn ( node as any , {
559
- code : 'module-script-reactive-declaration' ,
560
- message : '$: has no effect in a module script'
561
- } ) ;
545
+ component . warn ( node as any , compiler_warnings . module_script_reactive_declaration ) ;
562
546
}
563
547
}
564
548
} ) ;
@@ -568,10 +552,7 @@ export default class Component {
568
552
569
553
scope . declarations . forEach ( ( node , name ) => {
570
554
if ( name [ 0 ] === '$' ) {
571
- this . error ( node as any , {
572
- code : 'illegal-declaration' ,
573
- message : 'The $ prefix is reserved, and cannot be used for variable and import names'
574
- } ) ;
555
+ this . error ( node as any , compiler_errors . illegal_declaration ) ;
575
556
}
576
557
577
558
const writable = node . type === 'VariableDeclaration' && ( node . kind === 'var' || node . kind === 'let' ) ;
@@ -586,10 +567,7 @@ export default class Component {
586
567
587
568
globals . forEach ( ( node , name ) => {
588
569
if ( name [ 0 ] === '$' ) {
589
- this . error ( node as any , {
590
- code : 'illegal-subscription' ,
591
- message : 'Cannot reference store value inside <script context="module">'
592
- } ) ;
570
+ this . error ( node as any , compiler_errors . illegal_subscription ) ;
593
571
} else {
594
572
this . add_var ( {
595
573
name,
@@ -647,10 +625,7 @@ export default class Component {
647
625
648
626
instance_scope . declarations . forEach ( ( node , name ) => {
649
627
if ( name [ 0 ] === '$' ) {
650
- this . error ( node as any , {
651
- code : 'illegal-declaration' ,
652
- message : 'The $ prefix is reserved, and cannot be used for variable and import names'
653
- } ) ;
628
+ this . error ( node as any , compiler_errors . illegal_declaration ) ;
654
629
}
655
630
656
631
const writable = node . type === 'VariableDeclaration' && ( node . kind === 'var' || node . kind === 'let' ) ;
@@ -684,10 +659,7 @@ export default class Component {
684
659
} ) ;
685
660
} else if ( name [ 0 ] === '$' ) {
686
661
if ( name === '$' || name [ 1 ] === '$' ) {
687
- this . error ( node as any , {
688
- code : 'illegal-global' ,
689
- message : `${ name } is an illegal variable name`
690
- } ) ;
662
+ this . error ( node as any , compiler_errors . illegal_global ( name ) ) ;
691
663
}
692
664
693
665
this . add_var ( {
@@ -870,10 +842,7 @@ export default class Component {
870
842
node . label . name === '$' &&
871
843
parent . type !== 'Program'
872
844
) {
873
- this . warn ( node as any , {
874
- code : 'non-top-level-reactive-declaration' ,
875
- message : '$: has no effect outside of the top-level'
876
- } ) ;
845
+ this . warn ( node as any , compiler_warnings . non_top_level_reactive_declaration ) ;
877
846
}
878
847
879
848
if ( is_reference ( node , parent ) ) {
@@ -887,10 +856,7 @@ export default class Component {
887
856
888
857
if ( name [ 1 ] !== '$' && scope . has ( name . slice ( 1 ) ) && scope . find_owner ( name . slice ( 1 ) ) !== this . instance_scope ) {
889
858
if ( ! ( ( / F u n c t i o n / . test ( parent . type ) && prop === 'params' ) || ( parent . type === 'VariableDeclarator' && prop === 'id' ) ) ) {
890
- this . error ( node as any , {
891
- code : 'contextual-store' ,
892
- message : 'Stores must be declared at the top level of the component (this may change in a future version of Svelte)'
893
- } ) ;
859
+ this . error ( node as any , compiler_errors . contextual_store ) ;
894
860
}
895
861
}
896
862
}
@@ -955,10 +921,7 @@ export default class Component {
955
921
956
922
if ( variable . export_name ) {
957
923
// TODO is this still true post-#3539?
958
- component . error ( declarator as any , {
959
- code : 'destructured-prop' ,
960
- message : 'Cannot declare props in destructured declaration'
961
- } ) ;
924
+ component . error ( declarator as any , compiler_errors . destructured_prop ) ;
962
925
}
963
926
964
927
if ( variable . subscribable ) {
@@ -1248,10 +1211,7 @@ export default class Component {
1248
1211
variable . is_reactive_dependency = true ;
1249
1212
if ( variable . module ) {
1250
1213
should_add_as_dependency = false ;
1251
- component . warn ( node as any , {
1252
- code : 'module-script-reactive-declaration' ,
1253
- message : `"${ name } " is declared in a module script and will not be reactive`
1254
- } ) ;
1214
+ component . warn ( node as any , compiler_warnings . module_script_variable_reactive_declaration ( name ) ) ;
1255
1215
}
1256
1216
}
1257
1217
const is_writable_or_mutated =
@@ -1316,10 +1276,7 @@ export default class Component {
1316
1276
if ( cycle && cycle . length ) {
1317
1277
const declarationList = lookup . get ( cycle [ 0 ] ) ;
1318
1278
const declaration = declarationList [ 0 ] ;
1319
- this . error ( declaration . node , {
1320
- code : 'cyclical-reactive-declaration' ,
1321
- message : `Cyclical dependency detected: ${ cycle . join ( ' → ' ) } `
1322
- } ) ;
1279
+ this . error ( declaration . node , compiler_errors . cyclical_reactive_declaration ( cycle ) ) ;
1323
1280
}
1324
1281
1325
1282
const add_declaration = declaration => {
@@ -1342,10 +1299,7 @@ export default class Component {
1342
1299
warn_if_undefined ( name : string , node , template_scope : TemplateScope ) {
1343
1300
if ( name [ 0 ] === '$' ) {
1344
1301
if ( name === '$' || name [ 1 ] === '$' && ! is_reserved_keyword ( name ) ) {
1345
- this . error ( node , {
1346
- code : 'illegal-global' ,
1347
- message : `${ name } is an illegal variable name`
1348
- } ) ;
1302
+ this . error ( node , compiler_errors . illegal_global ( name ) ) ;
1349
1303
}
1350
1304
1351
1305
this . has_reactive_assignments = true ; // TODO does this belong here?
@@ -1359,15 +1313,7 @@ export default class Component {
1359
1313
if ( template_scope && template_scope . names . has ( name ) ) return ;
1360
1314
if ( globals . has ( name ) && node . type !== 'InlineComponent' ) return ;
1361
1315
1362
- let message = `'${ name } ' is not defined` ;
1363
- if ( ! this . ast . instance ) {
1364
- message += `. Consider adding a <script> block with 'export let ${ name } ' to declare a prop` ;
1365
- }
1366
-
1367
- this . warn ( node , {
1368
- code : 'missing-declaration' ,
1369
- message
1370
- } ) ;
1316
+ this . warn ( node , compiler_warnings . missing_declaration ( name , ! ! this . ast . instance ) ) ;
1371
1317
}
1372
1318
1373
1319
push_ignores ( ignores ) {
@@ -1395,7 +1341,7 @@ function process_component_options(component: Component, nodes) {
1395
1341
1396
1342
const node = nodes . find ( node => node . name === 'svelte:options' ) ;
1397
1343
1398
- function get_value ( attribute , code , message ) {
1344
+ function get_value ( attribute , { code, message} ) {
1399
1345
const { value } = attribute ;
1400
1346
const chunk = value [ 0 ] ;
1401
1347
@@ -1421,54 +1367,34 @@ function process_component_options(component: Component, nodes) {
1421
1367
1422
1368
switch ( name ) {
1423
1369
case 'tag' : {
1424
- const code = 'invalid-tag-attribute' ;
1425
- const message = "'tag' must be a string literal" ;
1426
- const tag = get_value ( attribute , code , message ) ;
1370
+ const tag = get_value ( attribute , compiler_errors . invalid_tag_attribute ) ;
1427
1371
1428
1372
if ( typeof tag !== 'string' && tag !== null ) {
1429
- component . error ( attribute , { code , message } ) ;
1373
+ component . error ( attribute , compiler_errors . invalid_tag_attribute ) ;
1430
1374
}
1431
1375
1432
1376
if ( tag && ! / ^ [ a - z A - Z ] [ a - z A - Z 0 - 9 ] * - [ a - z A - Z 0 - 9 - ] + $ / . test ( tag ) ) {
1433
- component . error ( attribute , {
1434
- code : 'invalid-tag-property' ,
1435
- message : "tag name must be two or more words joined by the '-' character"
1436
- } ) ;
1377
+ component . error ( attribute , compiler_errors . invalid_tag_property ) ;
1437
1378
}
1438
1379
1439
1380
if ( tag && ! component . compile_options . customElement ) {
1440
- component . warn ( attribute , {
1441
- code : 'missing-custom-element-compile-options' ,
1442
- message : "The 'tag' option is used when generating a custom element. Did you forget the 'customElement: true' compile option?"
1443
- } ) ;
1381
+ component . warn ( attribute , compiler_warnings . missing_custom_element_compile_options ) ;
1444
1382
}
1445
1383
1446
1384
component_options . tag = tag ;
1447
1385
break ;
1448
1386
}
1449
1387
1450
1388
case 'namespace' : {
1451
- const code = 'invalid-namespace-attribute' ;
1452
- const message = "The 'namespace' attribute must be a string literal representing a valid namespace" ;
1453
- const ns = get_value ( attribute , code , message ) ;
1389
+ const ns = get_value ( attribute , compiler_errors . invalid_namespace_attribute ) ;
1454
1390
1455
1391
if ( typeof ns !== 'string' ) {
1456
- component . error ( attribute , { code , message } ) ;
1392
+ component . error ( attribute , compiler_errors . invalid_namespace_attribute ) ;
1457
1393
}
1458
1394
1459
1395
if ( valid_namespaces . indexOf ( ns ) === - 1 ) {
1460
1396
const match = fuzzymatch ( ns , valid_namespaces ) ;
1461
- if ( match ) {
1462
- component . error ( attribute , {
1463
- code : 'invalid-namespace-property' ,
1464
- message : `Invalid namespace '${ ns } ' (did you mean '${ match } '?)`
1465
- } ) ;
1466
- } else {
1467
- component . error ( attribute , {
1468
- code : 'invalid-namespace-property' ,
1469
- message : `Invalid namespace '${ ns } '`
1470
- } ) ;
1471
- }
1397
+ component . error ( attribute , compiler_errors . invalid_namespace_property ( ns , match ) ) ;
1472
1398
}
1473
1399
1474
1400
component_options . namespace = ns ;
@@ -1478,29 +1404,21 @@ function process_component_options(component: Component, nodes) {
1478
1404
case 'accessors' :
1479
1405
case 'immutable' :
1480
1406
case 'preserveWhitespace' : {
1481
- const code = `invalid-${ name } -value` ;
1482
- const message = `${ name } attribute must be true or false` ;
1483
- const value = get_value ( attribute , code , message ) ;
1407
+ const value = get_value ( attribute , compiler_errors . invalid_attribute_value ( name ) ) ;
1484
1408
1485
1409
if ( typeof value !== 'boolean' ) {
1486
- component . error ( attribute , { code , message } ) ;
1410
+ component . error ( attribute , compiler_errors . invalid_attribute_value ( name ) ) ;
1487
1411
}
1488
1412
1489
1413
component_options [ name ] = value ;
1490
1414
break ;
1491
1415
}
1492
1416
1493
1417
default :
1494
- component . error ( attribute , {
1495
- code : 'invalid-options-attribute' ,
1496
- message : '<svelte:options> unknown attribute'
1497
- } ) ;
1418
+ component . error ( attribute , compiler_errors . invalid_options_attribute_unknown ) ;
1498
1419
}
1499
1420
} else {
1500
- component . error ( attribute , {
1501
- code : 'invalid-options-attribute' ,
1502
- message : "<svelte:options> can only have static 'tag', 'namespace', 'accessors', 'immutable' and 'preserveWhitespace' attributes"
1503
- } ) ;
1421
+ component . error ( attribute , compiler_errors . invalid_options_attribute ) ;
1504
1422
}
1505
1423
} ) ;
1506
1424
}
0 commit comments