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(babel-plugin-transform-lwc-class): Transform imported decorators #49

Merged
merged 22 commits into from
Feb 2, 2018

Conversation

pmdartus
Copy link
Member

Details

Currently, the compiler assumes that decorators like api, track and wire are globally available. This PR updates the compiler to only transform decorators that are imported by from engine.

A component previously written like ...

import { Element } from 'engine';

export default class Test extends Element {
	@track state = {};
}

... should be updated into.

import { Element, track } from 'engine';

export default class Test extends Element {
	@track state = {};
}

Does this PR introduce a breaking change?

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The compiler will now throw if it find a decorator which is not imported from engine. A codemod has been developed on the side to help developers migrate to the new syntax.

@pmdartus pmdartus requested review from yungcheng and apapko January 31, 2018 01:04
@salesforce-best-lwc-internal
Copy link

Benchmark comparison

Base commit: 29cb560 | Target commit: cce129e

benchmark base(29cb560) target(cce129e) trend


transformTest('throws if a decorator is dereferenced', `
import { track } from 'engine';
const trok = track;
Copy link
Contributor

Choose a reason for hiding this comment

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

const trock = track;

}
`, {
error: {
message: 'test.js: "track" can only be used as a class decorator',
Copy link
Contributor

Choose a reason for hiding this comment

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

the error message is confusing, can we say something regarding the fact that track has been referenced by trock and that's not supported?

Copy link
Member Author

Choose a reason for hiding this comment

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

This one is tricky to implement. There are many ways to use track we can't create customize messages for each cases.

});

test('decorator expects an oject as second parameter', `
transformTest('decorator expects an oject as second parameter', `
Copy link
Contributor

Choose a reason for hiding this comment

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

as @kevinv11n would notice, oject is missing a b

});

test('throws when using 2 wired decorators', `
transformTest('throws when using 2 wired decorators', `
import { wire } from 'engine';
export default class Test {
@wire('record', { recordId: '$recordId', fields: ['Address'] })
@wire('record', { recordId: '$recordId', fields: ['Name'] })
wiredWithTrack
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick - wiredWithTrack is a bad name for this test case

Copy link
Contributor

@yungcheng yungcheng left a comment

Choose a reason for hiding this comment

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

some minor changes are needed

class Test1 extends Element {}

export default class Test2 extends Element {
render() {
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick, fix the indention for render() {...}

@@ -1,162 +1,188 @@
const test = require('./utils/test-transform').test(
const transformTest = require('./utils/test-transform').transformTest(
Copy link
Contributor

@yungcheng yungcheng Jan 31, 2018

Choose a reason for hiding this comment

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

i actually think it would be less confusing if you do
const test = require('./utils/test-transform').transformTest(require('../index'));

test is really transformTester, yet you name it transformTest so when I read between util/test-transform.js and this test file, i got confused.

just my 2 cents, i am okay if you don't want to change it

@salesforce-best-lwc-internal
Copy link

Benchmark comparison

Base commit: 29cb560 | Target commit: b7dbb4b

benchmark base(29cb560) target(b7dbb4b) trend

@caridy
Copy link
Contributor

caridy commented Feb 1, 2018

should we wait for @diervo to finish the work on decorators in general to make this change?

@pmdartus
Copy link
Member Author

pmdartus commented Feb 1, 2018

What is the decorator work @diervo is working on?

@yungcheng
Copy link
Contributor

are you referring to @context? I think that's independent to this PR (it might even benefit from this PR implementation wise)

@caridy
Copy link
Contributor

caridy commented Feb 1, 2018

I don't know, maybe we should chat with @diervo. My understand is that we could move out wire and context once he finishes the changes.

@pmdartus pmdartus merged commit 172c75b into master Feb 2, 2018
@pmdartus pmdartus deleted the pmdartus/imported-decorators branch February 2, 2018 19:12
pmdartus pushed a commit that referenced this pull request Nov 12, 2024
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.

3 participants