-
Notifications
You must be signed in to change notification settings - Fork 393
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
fix(compiler): prevent underscore attr name camelcasing #1385
Conversation
Benchmark resultsBase commit: lwc-engine-benchmark
|
|
||
it('attribute name separated by underscore and immediate hyphen', () => { | ||
const { root } = parseTemplate(`<template> | ||
<x-button under_-hyphen="bar"></x-button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one should probably be invalid... but hey! IMO it should throw.
|
||
it('attribute name with leading underscore and hyphen', () => { | ||
const { root } = parseTemplate(`<template> | ||
<x-button _leading-underscore="bar"></x-button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, leading underscore should not be allowed in LWC. IMO
|
||
it('attribute name with trailing underscore and hyphen', () => { | ||
const { root } = parseTemplate(`<template> | ||
<x-button trailing-underscore_="bar"></x-button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here... should throw...
const attrToSplit = ATTRS_PROPS_TRANFORMS[propName] || propName; | ||
propName = attrToSplit | ||
.split('_') | ||
.map(part => camelcase(part)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line is equivalent to: .map(camelcase)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I feel that we should be a little bit more restrictive for now... leading _, _ before or after -, and _ at the end should be considered illegal
Benchmark resultsBase commit: lwc-engine-benchmark
|
// disallow attr name with an underscore combined with non alphabetic characters | ||
if (name.match(/_[^a-zA-Z]|[^a-zA-Z]_/)) { | ||
const node = element.__original as parse5.AST.Default.Element; | ||
warnAt(ParserDiagnostics.ATTRIBUTE_NAME_CANNOT_CONTAIN_UNDERSCORE_WITH_HYPHEN, [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the message name doesn't reflect the actual error... it is not longer about -_
} | ||
|
||
// disallow attr name with an underscore combined with non alphabetic characters | ||
if (name.match(/_[^a-zA-Z]|[^a-zA-Z]_/)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since attributes are parsed to lowercase by parse5, you only need to match a-z
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
few minor stuff
Benchmark resultsBase commit: lwc-engine-benchmark
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
fix the tests, and let's move on. |
} | ||
|
||
// disallow attr name with an underscore combined with non alphabetic characters | ||
if (name.match(/_[^a-z]|[^a-z]_/)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we excluding numbers on purpose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a good catch, it should support numbers as well... e.g.: foo_1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loosened up the restriction to allow trailing numbers; but not leading.
Benchmark resultsBase commit: lwc-engine-benchmark
|
code: 1124, | ||
message: | ||
'{0} is not valid attribute for {1}. Attribute name cannot start or end with an underscore.', | ||
'{0} is not valid attribute for {1}. Attribute name cannot start with non alphabetic character.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably avoid negation on this messag,e something like: Attribute name must start with alphabetic character.
instead
code: 1125, | ||
message: | ||
'{0} is not valid attribute for {1}. Attribute name cannot contain combination of underscore and hyphen characters.', | ||
'{0} is not valid attribute for {1}. Attribute name cannot start or end with non alpha-numeric character.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just adjust the error messages to avoid double negation
Benchmark resultsBase commit: lwc-engine-benchmark
|
Details
Fixes #1369 - prevent camel-casing of the attribute names with underscore.
Rules:
Does this PR introduce breaking changes?
No, it does not introduce breaking changes.