warn on undefined values in template

pull/1839/head
Rich Harris 7 years ago
parent e6eb10c581
commit 61e2896f33

@ -22,6 +22,7 @@ import fuzzymatch from '../utils/fuzzymatch';
import { remove_indentation, add_indentation } from '../utils/indentation';
import getObject from '../utils/getObject';
import deindent from '../utils/deindent';
import globalWhitelist from '../utils/globalWhitelist';
type Meta = {
namespace?: string;
@ -879,11 +880,14 @@ export default class Component {
return `ctx.${name}`;
}
warn_if_undefined(node, template_scope: TemplateScope) {
warn_if_undefined(node, template_scope: TemplateScope, allow_implicit?: boolean) {
const { name } = node;
if (this.module_scope && this.module_scope.declarations.has(name)) return;
if (allow_implicit && !this.instance_script) return;
if (this.instance_scope && this.instance_scope.declarations.has(name)) return;
if (this.module_scope && this.module_scope.declarations.has(name)) return;
if (template_scope.names.has(name)) return;
if (globalWhitelist.has(name)) return;
this.warn(node, {
code: 'missing-declaration',

@ -141,7 +141,7 @@ export default class Expression {
}
if (isReference(node, parent)) {
const { name } = flattenReference(node);
const { name, nodes } = flattenReference(node);
if (scope.has(name)) return;
if (globalWhitelist.has(name) && component.declarations.indexOf(name) === -1) return;
@ -155,6 +155,8 @@ export default class Expression {
} else {
add_dependency(name);
component.template_references.add(name);
component.warn_if_undefined(nodes[0], template_scope, true);
}
this.skip();

@ -35,8 +35,6 @@ export default function readScript(parser: Parser, start: number, attributes: No
parser.acornError(err);
}
if (!ast.body.length) return null;
ast.start = scriptStart;
return {
start,

@ -21,5 +21,18 @@
]
},
"css": [],
"js": []
"js": [
{
"start": 0,
"end": 43,
"attributes": [],
"content": {
"type": "Program",
"start": 8,
"end": 34,
"body": [],
"sourceType": "module"
}
}
]
}

@ -0,0 +1,6 @@
<script>
// script block prevents auto-declaration
</script>
<p>{potato}</p>
<p>{Math.max(1, 2)}</p>

@ -0,0 +1,15 @@
[{
"code": "missing-declaration",
"message": "'potato' is not defined",
"pos": 67,
"start": {
"line": 5,
"column": 4,
"character": 67
},
"end": {
"line": 5,
"column": 10,
"character": 73
}
}]
Loading…
Cancel
Save