diff --git a/src/compile/Component.ts b/src/compile/Component.ts index 37abcb591a..0afb903b29 100644 --- a/src/compile/Component.ts +++ b/src/compile/Component.ts @@ -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', diff --git a/src/compile/nodes/shared/Expression.ts b/src/compile/nodes/shared/Expression.ts index 6a6598224e..0a0c4c6cb6 100644 --- a/src/compile/nodes/shared/Expression.ts +++ b/src/compile/nodes/shared/Expression.ts @@ -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(); diff --git a/src/parse/read/script.ts b/src/parse/read/script.ts index 0518bc51a6..b6d6cd53a8 100644 --- a/src/parse/read/script.ts +++ b/src/parse/read/script.ts @@ -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, diff --git a/test/parser/samples/script-comment-only/output.json b/test/parser/samples/script-comment-only/output.json index 5e18e97e74..b04d38633d 100644 --- a/test/parser/samples/script-comment-only/output.json +++ b/test/parser/samples/script-comment-only/output.json @@ -21,5 +21,18 @@ ] }, "css": [], - "js": [] + "js": [ + { + "start": 0, + "end": 43, + "attributes": [], + "content": { + "type": "Program", + "start": 8, + "end": 34, + "body": [], + "sourceType": "module" + } + } + ] } \ No newline at end of file diff --git a/test/validator/samples/undefined-value/input.html b/test/validator/samples/undefined-value/input.html new file mode 100644 index 0000000000..03dda2c44f --- /dev/null +++ b/test/validator/samples/undefined-value/input.html @@ -0,0 +1,6 @@ + + +
{potato}
+{Math.max(1, 2)}
\ No newline at end of file diff --git a/test/validator/samples/undefined-value/warnings.json b/test/validator/samples/undefined-value/warnings.json new file mode 100644 index 0000000000..c1813e6ab0 --- /dev/null +++ b/test/validator/samples/undefined-value/warnings.json @@ -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 + } +}] \ No newline at end of file