-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RFC for this change: salesforce/lwc-rfcs#4 The logic for this PR is as follows: When the compiler compiles the class, it extracts any field that is not decorated with @api, @track or @wire, and pass the metadata through the registerDecorators call. Given that in order to observe a field, we need a vm, and we need to register the fields of all classes and save it in the decoratorsMeta, we need to wait until we create the ComponentDefinition to know which classes will be used as components. For every class field we will observe in a Component, we will create a getter and a setter in the prototype of the ComponentDefinition (and the class inheritance chain until BaseLightningElement). No change is needed to the logic in the engine.
- Loading branch information
Showing
12 changed files
with
314 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
packages/@lwc/babel-plugin-component/src/__tests__/observable-fields.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
const pluginTest = require('./utils/test-transform').pluginTest(require('../index')); | ||
|
||
describe('observable fields', () => { | ||
pluginTest( | ||
'should be added to the registerComponentCall when a field is not decorated with @api, @track or @wire', | ||
` | ||
import { api, wire, track, createElement } from 'lwc'; | ||
export default class Test { | ||
state; | ||
@track foo; | ||
@track bar; | ||
@api label; | ||
record = { | ||
value: 'test' | ||
}; | ||
@api | ||
someMethod() {} | ||
@wire(createElement) wiredProp; | ||
} | ||
`, | ||
{ | ||
output: { | ||
code: ` | ||
import { registerDecorators as _registerDecorators } from "lwc"; | ||
import _tmpl from "./test.html"; | ||
import { registerComponent as _registerComponent } from "lwc"; | ||
import { createElement } from "lwc"; | ||
class Test { | ||
constructor() { | ||
this.state = void 0; | ||
this.foo = void 0; | ||
this.bar = void 0; | ||
this.label = void 0; | ||
this.record = { | ||
value: "test" | ||
}; | ||
this.wiredProp = void 0; | ||
} | ||
someMethod() {} | ||
} | ||
_registerDecorators(Test, { | ||
publicProps: { | ||
label: { | ||
config: 0 | ||
} | ||
}, | ||
publicMethods: ["someMethod"], | ||
wire: { | ||
wiredProp: { | ||
adapter: createElement | ||
} | ||
}, | ||
track: { | ||
foo: 1, | ||
bar: 1 | ||
}, | ||
fields: ["state", "record"] | ||
}); | ||
export default _registerComponent(Test, { | ||
tmpl: _tmpl | ||
}); | ||
`, | ||
}, | ||
} | ||
); | ||
|
||
pluginTest( | ||
'should transform export default that is not a class', | ||
` | ||
const DATA_FROM_NETWORK = [ | ||
{ | ||
id: '1', | ||
}, | ||
{ | ||
id: '2', | ||
}, | ||
]; | ||
export default DATA_FROM_NETWORK; | ||
`, | ||
{ | ||
output: { | ||
code: ` | ||
import _tmpl from "./test.html"; | ||
import { registerComponent as _registerComponent } from "lwc"; | ||
const DATA_FROM_NETWORK = [ | ||
{ | ||
id: "1" | ||
}, | ||
{ | ||
id: "2" | ||
} | ||
]; | ||
export default _registerComponent(DATA_FROM_NETWORK, { | ||
tmpl: _tmpl | ||
}); | ||
`, | ||
}, | ||
} | ||
); | ||
|
||
pluginTest( | ||
'should add observed fields in class expression', | ||
` | ||
import { api, wire, track, createElement } from 'lwc'; | ||
const Test = class { | ||
state; | ||
@track foo; | ||
@track bar; | ||
@api label; | ||
record = { | ||
value: 'test' | ||
}; | ||
@api | ||
someMethod() {} | ||
@wire(createElement) wiredProp; | ||
} | ||
const foo = Test; | ||
export default foo; | ||
`, | ||
{ | ||
output: { | ||
code: ` | ||
import _tmpl from "./test.html"; | ||
import { registerComponent as _registerComponent } from "lwc"; | ||
import { registerDecorators as _registerDecorators } from "lwc"; | ||
import { createElement } from "lwc"; | ||
const Test = _registerDecorators( | ||
class { | ||
constructor() { | ||
this.state = void 0; | ||
this.foo = void 0; | ||
this.bar = void 0; | ||
this.label = void 0; | ||
this.record = { | ||
value: "test" | ||
}; | ||
this.wiredProp = void 0; | ||
} | ||
someMethod() {} | ||
}, | ||
{ | ||
publicProps: { | ||
label: { | ||
config: 0 | ||
} | ||
}, | ||
publicMethods: ["someMethod"], | ||
wire: { | ||
wiredProp: { | ||
adapter: createElement | ||
} | ||
}, | ||
track: { | ||
foo: 1, | ||
bar: 1 | ||
}, | ||
fields: ["state", "record"] | ||
} | ||
); | ||
const foo = Test; | ||
export default _registerComponent(foo, { | ||
tmpl: _tmpl | ||
}); | ||
`, | ||
}, | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/@lwc/compiler/src/__tests__/fixtures/expected-prod-mode.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.