Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a compile time error when passing arguments to regular HTML elements (e.g. <a @foo=) #1102

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/@glimmer/compiler/lib/template-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ export default class TemplateCompiler implements Processor<InputOps> {
typeAttr = attrs[i];
continue;
}
this.attribute([attrs[i]], !simple || actionIsComponent);
this.attribute([attrs[i]], !simple || actionIsComponent, action);
}

if (typeAttr) {
this.attribute([typeAttr], !simple || actionIsComponent);
this.attribute([typeAttr], !simple || actionIsComponent, action);
}

for (let i = 0; i < action.modifiers.length; i++) {
Expand All @@ -161,7 +161,8 @@ export default class TemplateCompiler implements Processor<InputOps> {
}
}

attribute([action]: [AST.AttrNode], isComponent: boolean) {
attribute([action]: [AST.AttrNode], isComponent: boolean, elementNode: AST.ElementNode) {
assertValidArgumentName(action, isComponent, elementNode);
let { name, value } = action;

let namespace = getAttrNamespace(name);
Expand Down Expand Up @@ -690,6 +691,19 @@ function assertIsSimplePath(path: AST.Expression, loc: AST.SourceLocation, conte
}
}

function assertValidArgumentName(
attribute: AST.AttrNode,
isComponent: boolean,
elementNode: AST.ElementNode
) {
if (!isComponent && attribute.name[0] === '@') {
throw new SyntaxError(
`${attribute.name} is not a valid attribute name. @arguments are only allowed on components, but the tag for this element (\`${elementNode.tag}\`) is a regular, non-component HTML element.`,
attribute.loc
);
}
}

function assertValidYield(statement: AST.MustacheStatement): string {
let { pairs } = statement.hash;

Expand Down
13 changes: 13 additions & 0 deletions packages/@glimmer/compiler/test/compiler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ function test(desc: string, template: string, ...expectedStatements: BuilderStat
const Append = Builder.Append;
const Concat = Builder.Concat;

QUnit.test(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't match the test style in the rest of the file, which is a wrapper around QUnit.test and looks at wire format output. i thought it might be more clear to just use QUnit.test directly rather than extend the test function in this file to allow a callback/expect errors. Totally open to change this to whatever the maintainers prefer, let me know!

'@arguments are on regular non-component/regular HTML nodes throws syntax error',
function(assert) {
const template = strip`
<a @onClick={{action "hi"}}>Link</a>
`;
assert.throws(
() => compile(template),
/@onClick is not a valid attribute name. @arguments are only allowed on components, but the tag for this element \(`a`\) is a regular, non-component HTML element/
);
}
);

test('HTML text content', 'content', s`content`);

test('Text curlies', '<div>{{title}}<span>{{title}}</span></div>', [
Expand Down