Skip to content

Commit 2d88dc4

Browse files
authored
warn for possible use of component without uppercase tag name (#5302)
1 parent 99e2cfc commit 2d88dc4

File tree

6 files changed

+37
-2
lines changed

6 files changed

+37
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Support `<slot slot="...">` ([#2079](https://github.com/sveltejs/svelte/issues/2079))
66
* Fix unmounting components with a bidirectional transition with a delay ([#4954](https://github.com/sveltejs/svelte/issues/4954))
77
* Add types to `get` function in `svelte/store` ([#5269](https://github.com/sveltejs/svelte/pull/5269))
8+
* Add a warning when a component looks like it's trying to use another component without beginning with a capital letter ([#5302](https://github.com/sveltejs/svelte/pull/5302))
89
* Add `EventSource` to known globals ([#5463](https://github.com/sveltejs/svelte/issues/5463))
910
* Fix compiler exception with `~`/`+` combinators and `{...spread}` attributes ([#5465](https://github.com/sveltejs/svelte/issues/5465))
1011

src/compiler/compile/Component.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,13 @@ export default class Component {
634634
}
635635

636636
const writable = node.type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let');
637+
const imported = node.type.startsWith('Import');
637638

638639
this.add_var({
639640
name,
640641
initialised: instance_scope.initialised_declarations.has(name),
641-
writable
642+
writable,
643+
imported
642644
});
643645

644646
this.node_for_declaration.set(name, node);

src/compiler/compile/nodes/Element.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export default class Element extends Node {
186186

187187
case 'Attribute':
188188
case 'Spread':
189-
// special case
189+
// special case
190190
if (node.name === 'xmlns') this.namespace = node.value[0].data;
191191

192192
this.attributes.push(new Attribute(component, this, scope, node));
@@ -241,6 +241,13 @@ export default class Element extends Node {
241241
}
242242

243243
validate() {
244+
if (this.component.var_lookup.has(this.name) && this.component.var_lookup.get(this.name).imported) {
245+
this.component.warn(this, {
246+
code: 'component-name-lowercase',
247+
message: `<${this.name}> will be treated as an HTML element unless it begins with a capital letter`
248+
});
249+
}
250+
244251
if (a11y_distracting_elements.has(this.name)) {
245252
// no-distracting-elements
246253
this.component.warn(this, {

src/compiler/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export interface Var {
161161
hoistable?: boolean;
162162
subscribable?: boolean;
163163
is_reactive_dependency?: boolean;
164+
imported?: boolean;
164165
}
165166

166167
export interface CssResult {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import thisShouldWarnMe from './MyComponent.svelte';
3+
let i;
4+
</script>
5+
6+
<thisShouldWarnMe />
7+
<i />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"code": "component-name-lowercase",
4+
"message": "<thisShouldWarnMe> will be treated as an HTML element unless it begins with a capital letter",
5+
"pos": 82,
6+
"start": {
7+
"character": 82,
8+
"column": 0,
9+
"line": 6
10+
},
11+
"end": {
12+
"character": 102,
13+
"column": 20,
14+
"line": 6
15+
}
16+
}
17+
]

0 commit comments

Comments
 (0)