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

fix(compiler): prevent underscore attr name camelcasing #1385

Merged
merged 5 commits into from
Jul 8, 2019

Conversation

apapko
Copy link
Collaborator

@apapko apapko commented Jun 29, 2019

Details

Fixes #1369 - prevent camel-casing of the attribute names with underscore.
Rules:

  • attribute name must start with alphabetic character
  • attribute name must end with alpha-numeric character
  • attribute name cannot contain a combination of underscore and non-alpha-numeric characters

Does this PR introduce breaking changes?

  • No, it does not introduce breaking changes.

@apapko apapko requested review from caridy and pmdartus June 29, 2019 00:29
@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: 5d4dc4d | Target commit: 32a6627

lwc-engine-benchmark

table-append-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table/append/1k duration 138.70 (±4.35 ms) 139.85 (±4.00 ms) +1.2ms (0.8%) 👌
table-clear-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table/clear/1k duration 10.10 (±1.05 ms) 10.20 (±1.00 ms) +0.1ms (1.0%) 👌
table-create-10k metric base(5d4dc4d) target(32a6627) trend
benchmark-table/create/10k duration 858.95 (±7.40 ms) 837.45 (±6.05 ms) -21.5ms (2.5%) 👍
table-create-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table/create/1k duration 107.05 (±2.55 ms) 106.25 (±2.15 ms) -0.8ms (0.7%) 👌
table-update-10th-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table/update-10th/1k duration 68.55 (±2.80 ms) 76.10 (±3.90 ms) +7.5ms (11.0%) 👎
tablecmp-append-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table-component/append/1k duration 211.15 (±16.40 ms) 224.00 (±9.30 ms) +12.8ms (6.1%) 👎
tablecmp-clear-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table-component/clear/1k duration 6.05 (±1.00 ms) 6.55 (±1.35 ms) +0.5ms (8.3%) 👌
tablecmp-create-10k metric base(5d4dc4d) target(32a6627) trend
benchmark-table-component/create/10k duration 1658.60 (±12.55 ms) 1613.45 (±8.40 ms) -45.2ms (2.7%) 👍
tablecmp-create-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table-component/create/1k duration 192.55 (±4.30 ms) 180.05 (±4.45 ms) -12.5ms (6.5%) 👍
tablecmp-update-10th-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table-component/update-10th/1k duration 64.70 (±5.85 ms) 66.50 (±4.70 ms) +1.8ms (2.8%) 👌
wc-append-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table-wc/append/1k duration 231.85 (±9.45 ms) 225.00 (±8.30 ms) -6.8ms (3.0%) 👍
wc-clear-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table-wc/clear/1k duration 10.90 (±2.05 ms) 10.85 (±1.50 ms) -0.0ms (0.5%) 👌
wc-create-10k metric base(5d4dc4d) target(32a6627) trend
benchmark-table-wc/create/10k duration 1855.60 (±11.50 ms) 1873.65 (±16.25 ms) +18.1ms (1.0%) 👎
wc-create-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table-wc/create/1k duration 224.10 (±5.15 ms) 219.05 (±4.90 ms) -5.1ms (2.3%) 👍
wc-update-10th-1k metric base(5d4dc4d) target(32a6627) trend
benchmark-table-wc/update-10th/1k duration 66.60 (±4.15 ms) 64.85 (±5.10 ms) -1.8ms (2.6%) 👌


it('attribute name separated by underscore and immediate hyphen', () => {
const { root } = parseTemplate(`<template>
<x-button under_-hyphen="bar"></x-button>
Copy link
Contributor

@caridy caridy Jun 29, 2019

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>
Copy link
Contributor

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>
Copy link
Contributor

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))
Copy link
Contributor

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)

Copy link
Contributor

@caridy caridy left a 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

@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: 5d4dc4d | Target commit: 759b2df

lwc-engine-benchmark

table-append-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table/append/1k duration 138.70 (±4.35 ms) 139.10 (±3.95 ms) +0.4ms (0.3%) 👌
table-clear-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table/clear/1k duration 10.10 (±1.05 ms) 9.75 (±0.95 ms) -0.3ms (3.5%) 👌
table-create-10k metric base(5d4dc4d) target(759b2df) trend
benchmark-table/create/10k duration 858.95 (±7.40 ms) 852.80 (±3.35 ms) -6.2ms (0.7%) 👍
table-create-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table/create/1k duration 107.05 (±2.55 ms) 105.30 (±2.70 ms) -1.8ms (1.6%) 👍
table-update-10th-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table/update-10th/1k duration 68.55 (±2.80 ms) 68.20 (±2.35 ms) -0.4ms (0.5%) 👌
tablecmp-append-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table-component/append/1k duration 211.15 (±16.40 ms) 222.30 (±15.60 ms) +11.2ms (5.3%) 👎
tablecmp-clear-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table-component/clear/1k duration 6.05 (±1.00 ms) 6.35 (±1.15 ms) +0.3ms (5.0%) 👌
tablecmp-create-10k metric base(5d4dc4d) target(759b2df) trend
benchmark-table-component/create/10k duration 1658.60 (±12.55 ms) 1580.20 (±12.80 ms) -78.4ms (4.7%) 👍
tablecmp-create-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table-component/create/1k duration 192.55 (±4.30 ms) 182.00 (±5.90 ms) -10.6ms (5.5%) 👍
tablecmp-update-10th-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table-component/update-10th/1k duration 64.70 (±5.85 ms) 66.90 (±5.00 ms) +2.2ms (3.4%) 👌
wc-append-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table-wc/append/1k duration 231.85 (±9.45 ms) 225.70 (±12.55 ms) -6.2ms (2.7%) 👍
wc-clear-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table-wc/clear/1k duration 10.90 (±2.05 ms) 10.35 (±1.40 ms) -0.5ms (5.0%) 👌
wc-create-10k metric base(5d4dc4d) target(759b2df) trend
benchmark-table-wc/create/10k duration 1855.60 (±11.50 ms) 1863.80 (±15.05 ms) +8.2ms (0.4%) 👎
wc-create-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table-wc/create/1k duration 224.10 (±5.15 ms) 218.10 (±5.05 ms) -6.0ms (2.7%) 👍
wc-update-10th-1k metric base(5d4dc4d) target(759b2df) trend
benchmark-table-wc/update-10th/1k duration 66.60 (±4.15 ms) 65.95 (±4.00 ms) -0.6ms (1.0%) 👌

// 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, [
Copy link
Contributor

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]_/)) {
Copy link
Contributor

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

Copy link
Contributor

@caridy caridy left a comment

Choose a reason for hiding this comment

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

few minor stuff

@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: 4960afe | Target commit: 9f41ce9

lwc-engine-benchmark

table-append-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table/append/1k duration 145.35 (±4.00 ms) 140.60 (±3.80 ms) -4.8ms (3.3%) 👍
table-clear-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table/clear/1k duration 10.20 (±0.80 ms) 10.35 (±1.15 ms) +0.2ms (1.5%) 👌
table-create-10k metric base(4960afe) target(9f41ce9) trend
benchmark-table/create/10k duration 870.35 (±6.40 ms) 850.30 (±5.65 ms) -20.1ms (2.3%) 👍
table-create-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table/create/1k duration 108.60 (±3.55 ms) 105.95 (±2.45 ms) -2.6ms (2.4%) 👍
table-update-10th-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table/update-10th/1k duration 77.55 (±3.15 ms) 76.50 (±2.90 ms) -1.0ms (1.4%) 👌
tablecmp-append-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table-component/append/1k duration 225.00 (±12.80 ms) 224.50 (±7.25 ms) -0.5ms (0.2%) 👌
tablecmp-clear-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table-component/clear/1k duration 5.95 (±0.85 ms) 6.60 (±1.35 ms) +0.6ms (10.9%) 👌
tablecmp-create-10k metric base(4960afe) target(9f41ce9) trend
benchmark-table-component/create/10k duration 1635.45 (±12.80 ms) 1605.90 (±7.35 ms) -29.5ms (1.8%) 👍
tablecmp-create-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table-component/create/1k duration 189.85 (±4.25 ms) 181.30 (±4.75 ms) -8.6ms (4.5%) 👍
tablecmp-update-10th-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table-component/update-10th/1k duration 69.30 (±5.80 ms) 65.65 (±5.55 ms) -3.7ms (5.3%) 👍
wc-append-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table-wc/append/1k duration 234.50 (±8.20 ms) 231.60 (±12.95 ms) -2.9ms (1.2%) 👌
wc-clear-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table-wc/clear/1k duration 10.40 (±1.60 ms) 11.20 (±1.75 ms) +0.8ms (7.7%) 👌
wc-create-10k metric base(4960afe) target(9f41ce9) trend
benchmark-table-wc/create/10k duration 1864.50 (±13.65 ms) 1884.45 (±13.25 ms) +20.0ms (1.1%) 👎
wc-create-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table-wc/create/1k duration 227.50 (±6.65 ms) 219.60 (±7.20 ms) -7.9ms (3.5%) 👍
wc-update-10th-1k metric base(4960afe) target(9f41ce9) trend
benchmark-table-wc/update-10th/1k duration 67.80 (±5.35 ms) 63.50 (±4.20 ms) -4.3ms (6.3%) 👍

Copy link
Contributor

@caridy caridy left a comment

Choose a reason for hiding this comment

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

LGTM

@caridy
Copy link
Contributor

caridy commented Jul 4, 2019

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]_/)) {
Copy link
Member

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?

Copy link
Contributor

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

Copy link
Collaborator Author

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.

@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: 2f7a1d7 | Target commit: 14f08ca

lwc-engine-benchmark

table-append-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table/append/1k duration 141.95 (±3.00 ms) 139.35 (±4.20 ms) -2.6ms (1.8%) 👍
table-clear-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table/clear/1k duration 10.25 (±0.90 ms) 10.15 (±1.05 ms) -0.1ms (1.0%) 👌
table-create-10k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table/create/10k duration 856.20 (±5.75 ms) 844.55 (±6.35 ms) -11.7ms (1.4%) 👍
table-create-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table/create/1k duration 106.30 (±1.90 ms) 105.50 (±2.75 ms) -0.8ms (0.8%) 👌
table-update-10th-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table/update-10th/1k duration 76.00 (±4.25 ms) 74.75 (±3.65 ms) -1.3ms (1.6%) 👌
tablecmp-append-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table-component/append/1k duration 214.15 (±17.05 ms) 221.05 (±13.70 ms) +6.9ms (3.2%) 👌
tablecmp-clear-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table-component/clear/1k duration 5.75 (±1.05 ms) 6.55 (±1.10 ms) +0.8ms (13.9%) 👌
tablecmp-create-10k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table-component/create/10k duration 1677.70 (±13.60 ms) 1587.45 (±9.15 ms) -90.3ms (5.4%) 👍
tablecmp-create-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table-component/create/1k duration 190.50 (±4.15 ms) 183.90 (±4.20 ms) -6.6ms (3.5%) 👍
tablecmp-update-10th-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table-component/update-10th/1k duration 65.75 (±4.75 ms) 66.35 (±5.45 ms) +0.6ms (0.9%) 👌
wc-append-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table-wc/append/1k duration 237.05 (±10.80 ms) 228.30 (±9.70 ms) -8.8ms (3.7%) 👍
wc-clear-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table-wc/clear/1k duration 10.90 (±1.75 ms) 10.90 (±1.50 ms) 0.0ms (0.0%) 👌
wc-create-10k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table-wc/create/10k duration 1865.65 (±11.10 ms) 1851.75 (±13.15 ms) -13.9ms (0.7%) 👍
wc-create-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table-wc/create/1k duration 228.40 (±3.95 ms) 219.15 (±4.10 ms) -9.3ms (4.0%) 👍
wc-update-10th-1k metric base(2f7a1d7) target(14f08ca) trend
benchmark-table-wc/update-10th/1k duration 65.20 (±4.70 ms) 65.35 (±3.90 ms) +0.2ms (0.2%) 👌

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.',
Copy link
Contributor

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.',
Copy link
Contributor

Choose a reason for hiding this comment

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

same

Copy link
Contributor

@caridy caridy left a 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

@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: 7f7c7ad | Target commit: 4774bcf

lwc-engine-benchmark

table-append-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table/append/1k duration 140.60 (±4.55 ms) 140.10 (±2.85 ms) -0.5ms (0.4%) 👌
table-clear-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table/clear/1k duration 10.40 (±1.00 ms) 10.10 (±1.10 ms) -0.3ms (2.9%) 👌
table-create-10k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table/create/10k duration 862.45 (±5.10 ms) 878.35 (±5.55 ms) +15.9ms (1.8%) 👎
table-create-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table/create/1k duration 107.50 (±2.50 ms) 107.25 (±2.95 ms) -0.3ms (0.2%) 👌
table-update-10th-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table/update-10th/1k duration 66.95 (±1.85 ms) 67.25 (±2.25 ms) +0.3ms (0.4%) 👌
tablecmp-append-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table-component/append/1k duration 227.65 (±14.25 ms) 223.20 (±11.90 ms) -4.4ms (2.0%) 👌
tablecmp-clear-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table-component/clear/1k duration 6.05 (±0.85 ms) 6.45 (±1.15 ms) +0.4ms (6.6%) 👌
tablecmp-create-10k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table-component/create/10k duration 1666.30 (±24.15 ms) 1611.70 (±12.80 ms) -54.6ms (3.3%) 👍
tablecmp-create-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table-component/create/1k duration 185.50 (±3.90 ms) 184.10 (±3.55 ms) -1.4ms (0.8%) 👌
tablecmp-update-10th-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table-component/update-10th/1k duration 68.90 (±5.15 ms) 67.30 (±3.60 ms) -1.6ms (2.3%) 👌
wc-append-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table-wc/append/1k duration 231.75 (±10.25 ms) 225.10 (±11.15 ms) -6.7ms (2.9%) 👍
wc-clear-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table-wc/clear/1k duration 10.60 (±2.00 ms) 11.25 (±1.70 ms) +0.7ms (6.1%) 👌
wc-create-10k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table-wc/create/10k duration 1852.10 (±10.85 ms) 1842.65 (±13.60 ms) -9.4ms (0.5%) 👍
wc-create-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table-wc/create/1k duration 229.95 (±6.00 ms) 224.00 (±3.65 ms) -5.9ms (2.6%) 👍
wc-update-10th-1k metric base(7f7c7ad) target(4774bcf) trend
benchmark-table-wc/update-10th/1k duration 66.80 (±4.05 ms) 65.80 (±5.30 ms) -1.0ms (1.5%) 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[compiler] compiler doesn't validate public properties with underscore names
3 participants