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

feat(template-compiler): experimental template expressions #3376

Merged
merged 3 commits into from
Mar 15, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@lwc/errors/src/compiler/error-info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
/**
* Next error code: 1189
* Next error code: 1198
*/

export * from './compiler';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,4 +875,76 @@ export const ParserDiagnostics = {
level: DiagnosticLevel.Error,
url: '',
},

// TODO [#3370]: remove this error if template expressions is removed
INVALID_EXPR_ARROW_FN_PARAM: {
code: 1189,
message: "Template expression doesn't allow {0} in arrow function params.",
level: DiagnosticLevel.Error,
url: '',
},

// TODO [#3370]: remove this error if template expressions is removed
INVALID_EXPR_STATEMENTS_PROHIBITED: {
code: 1190,
message: 'Statements are disallowed in template expressions.',
level: DiagnosticLevel.Error,
url: '',
},

// TODO [#3370]: remove this error if template expressions is removed
INVALID_EXPR_MUTATION_OUTSIDE_ARROW: {
code: 1191,
message: 'Field mutations are only permitted within arrow functions.',
level: DiagnosticLevel.Error,
url: '',
},

// TODO [#3370]: remove this error if template expressions is removed
INVALID_EXPR_DELETE_OP: {
code: 1192,
message: 'Use of the delete operator is prohibited within template expressions.',
level: DiagnosticLevel.Error,
url: '',
},

// TODO [#3370]: remove this error if template expressions is removed
INVALID_EXPR_ARROW_FN_BODY: {
code: 1193,
message: 'The body of arrow functions in template expressions must be an expression.',
level: DiagnosticLevel.Error,
url: '',
},

// TODO [#3370]: remove this error if template expressions is removed
INVALID_EXPR_ARROW_FN_KIND: {
code: 1194,
message: 'Arrow functions in template expressions cannot be {0}.',
level: DiagnosticLevel.Error,
url: '',
},

// TODO [#3370]: remove this error if template expressions is removed
INVALID_EXPR_EARLY_STAGE_FEATURE: {
code: 1195,
message: 'Early-stage JS features are disallowed in template expressions.',
level: DiagnosticLevel.Error,
url: '',
},

// TODO [#3370]: remove this error if template expressions is removed
INVALID_EXPR_PROHIBITED_NODE_TYPE: {
code: 1196,
message: 'Use of {0} is disallowed within template expressions.',
level: DiagnosticLevel.Error,
url: '',
},

// TODO [#3370]: remove this error if template expressions is removed
INVALID_EXPR_COMMENTS_DISALLOWED: {
code: 1197,
message: 'Use of comments is disallowed within template expressions.',
level: DiagnosticLevel.Error,
url: '',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ function createPreprocessor(config, emitter, logger) {
.join('\n');
const outro = ancestorDirectories.map(() => `});`).join('\n');

// TODO [#3370]: remove experimental template expression flag
const experimentalComplexExpressions = suiteDir.includes('template-expressions');

const plugins = [
lwcRollupPlugin({
sourcemap: true,
Expand All @@ -50,6 +53,7 @@ function createPreprocessor(config, emitter, logger) {
},
enableLwcSpread: true,
enableDynamicComponents: true,
experimentalComplexExpressions,
disableSyntheticShadowSupport: DISABLE_SYNTHETIC_SHADOW_SUPPORT_IN_COMPILER,
}),
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createElement } from 'lwc';

import UndefinedMemberExpressionObjParent from 'x/undefinedMemberExpressionObjParent';
import ThrowDuringCallParent from 'x/throwDuringCallParent';

it(`should handle member expression with undefined object`, () => {
const parent = createElement('x-parent', { is: UndefinedMemberExpressionObjParent });
document.body.appendChild(parent);
expect(parent.caughtError).toContain('undefined');
});

it(`should handle errors thrown during call expression`, () => {
const parent = createElement('x-parent', { is: ThrowDuringCallParent });
document.body.appendChild(parent);
expect(parent.caughtError).toContain("I'm the Gingerbread man!");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div foo={runRunAsFastAsYouCan()}></div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

export default class Test extends LightningElement {
runRunAsFastAsYouCan() {
throw new Error("You can't catch me, I'm the Gingerbread man!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<x-throw-during-call-child></x-throw-during-call-child>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LightningElement, api } from 'lwc';

export default class Parent extends LightningElement {
@api caughtError = null;
errorCallback(err) {
this.caughtError = err.message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div foo={nonexistent.thing}></div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class Test extends LightningElement {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<x-undefined-member-expression-obj-child></x-undefined-member-expression-obj-child>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LightningElement, api } from 'lwc';

export default class Parent extends LightningElement {
@api caughtError = null;
errorCallback(err) {
this.caughtError = err.message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createElement } from 'lwc';

import Test from 'x/test';

it(`should support call expressions`, () => {
const elm = createElement('x-test', { is: Test });
document.body.appendChild(elm);

expect(elm.shadowRoot.querySelector('div').getAttribute('foo')).toBe('bar');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div foo={bar()}></div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { LightningElement } from 'lwc';

export default class Test extends LightningElement {
bar() {
return 'bar';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<section>
<button onclick={async () => doSomething()}></button>
</section>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"experimentalComplexExpressions": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"warnings": [
{
"code": 1194,
"message": "Invalid expression {async () => doSomething()} - LWC1194: Arrow functions in template expressions cannot be async.",
"level": 1,
"location": {
"line": 3,
"column": 17,
"start": 41,
"length": 35
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<section>
<button onblick={() => { doSomething(); }}></button>
</section>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"experimentalComplexExpressions": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"warnings": [
{
"code": 1193,
"message": "Invalid expression {() => { doSomething(); }} - LWC1193: The body of arrow functions in template expressions must be an expression.",
"level": 1,
"location": {
"line": 3,
"column": 17,
"start": 41,
"length": 34
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<section>
<button onclick={myField = 'foo'}></button>
</section>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"experimentalComplexExpressions": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"warnings": [
{
"code": 1191,
"message": "Invalid expression {myField = 'foo'} - LWC1191: Field mutations are only permitted within arrow functions.",
"level": 1,
"location": {
"line": 3,
"column": 17,
"start": 41,
"length": 25
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<section>
<button onclick={await foo()}></button>
</section>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"experimentalComplexExpressions": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"warnings": [
{
"code": 1196,
"message": "Invalid expression {await foo()} - LWC1196: Use of await expressions is disallowed within template expressions.",
"level": 1,
"location": {
"line": 3,
"column": 17,
"start": 41,
"length": 21
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<section>
<x-child foo={transformBigInt(1n)}></x-child>
</section>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"experimentalComplexExpressions": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"warnings": [
{
"code": 1196,
"message": "Invalid expression {transformBigInt(1n)} - LWC1196: Use of BigInts is disallowed within template expressions.",
"level": 1,
"location": {
"line": 3,
"column": 18,
"start": 42,
"length": 25
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<section>
<x-foo jsclass={
class Bar {
method() {}
}
}></x-foo>
</section>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"experimentalComplexExpressions": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"warnings": [
{
"code": 1196,
"message": "Invalid expression {\n class Bar {\n method() {}\n }\n } - LWC1196: Use of classes is disallowed within template expressions.",
"level": 1,
"location": {
"line": 3,
"column": 16,
"start": 40,
"length": 85
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<section>
<button onclick={(one(), two(), three(), iAmTheCount())}></button>
</section>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"experimentalComplexExpressions": true
}
Loading