Skip to content
This repository has been archived by the owner on Jan 12, 2020. It is now read-only.

refactor(tracked): minimally invasive stage 2 decorators support #22

Merged
merged 20 commits into from Jan 10, 2019

Conversation

buschtoens
Copy link
Contributor

Closes #16.
Supersedes #17.
Relates to #21.

This adds support for stage 2 decorators and the latest @ember-decorators/babel-transforms while changing as few things as possible. It also upgrades to the latest ember-cli-typescript@beta.

In theory, this should be compatible with ember-cli-typescript@1, since this PR uses @ember-decorators/utils, but I would like to add tests for that before merging.

/cc @rwjblue

addon/tracked.ts Outdated Show resolved Hide resolved
@@ -90,15 +90,13 @@ function installTrackedProperty(_target: object, key: string | symbol, descripto
}

return {
configurable: true,
// TODO: correcting a misspelling caused chrome to error
// writable: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FWIW this is intentional. I fixed the same problem in this PR with a detailed analysis: salsify/ember-css-modules#118

@buschtoens
Copy link
Contributor Author

btw, this is actually semantically incorrect:

if (typeof initializer === 'function') {
get = function(this: object) {
if (values.has(this)) {
return values.get(this);
} else {
let value = initializer.call(this);
values.set(this, value);
return value;
}
};
} else {

Initializers are evaluated eagerly. This initializer is only first run, when the field is accessed.

type TrackedDecorator = TSDecorator & ((...args: string[]) => TSDecorator);

export const tracked: TrackedDecorator = decoratorWithParams((desc, params = []) => {
assert(`@tracked - Can only be used on class fields.`, desc.kind === 'field' || desc.kind === 'method');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you wanna be really correct.

Suggested change
assert(`@tracked - Can only be used on class fields.`, desc.kind === 'field' || desc.kind === 'method');
assert(
`@tracked - Can only be used on class fields.`,
desc.kind === 'field' || desc.kind === 'method' && (
typeof desc.descriptor.get === 'function' ||
typeof desc.descriptor.set === 'function'
)
);

Copy link
Owner

@rwjblue rwjblue left a comment

Choose a reason for hiding this comment

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

In order to confirm things still work properly, I'd like to get ember-try scenarios for ember-cli-typescript@1 as well.

package.json Outdated Show resolved Hide resolved
addon/tracked.ts Outdated Show resolved Hide resolved
@buschtoens
Copy link
Contributor Author

@rwjblue I was already working on these scenarios and even found some bugs (upstream 😜). Didn't yet get to push it here though. ☺️

addon/tracked.ts Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
types/@ember-decorators/utils/decorator.d.ts Show resolved Hide resolved
@buschtoens
Copy link
Contributor Author

This PR is pending approval and release of ember-decorators/ember-decorators#336, so I can replace 02546ae with the proper release of @ember-decorators/utils.

But other than that, I am done. Would love another round of 👀 on this!

@buschtoens
Copy link
Contributor Author

@rwjblue ember-decorators/ember-decorators#336 is merged. If you can find time to release it, I can update this PR and you can merge and release it as well. 🚀

@rwjblue rwjblue mentioned this pull request Dec 26, 2018
@NullVoxPopuli
Copy link
Contributor

@buschtoens there is a conflict o.o

@NullVoxPopuli
Copy link
Contributor

but also, I can confirm this branch is 💯

@buschtoens
Copy link
Contributor Author

Once there's a new release of ember-decorators, I can update this PR and remove the merge conflict in package.json.

@buschtoens
Copy link
Contributor Author

@rwjblue I've updated to @ember-decorators v4.0.0 and rebased onto the latest master.

@buschtoens
Copy link
Contributor Author

While trying to pre-publish this as @buschtoens/sparkles-component@1.3.0-beta.1, I noticed that #26 broke the build (specifically the prepublishOnly step), because tsc complains about the following type errors:

addon/index.ts:3:21 - error TS7016: Could not find a declaration file for module 'ember-compatibility-helpers'. '/Users/janbuschtoens/open-source/sparkles-component/node_modules/ember-compatibility-helpers/index.js' implicitly has an 'any' type.
  Try `npm install @types/ember-compatibility-helpers` if it exists or add a new declaration (.d.ts) file containing `declare module 'ember-compatibility-helpers';`

3 import { gte } from 'ember-compatibility-helpers';
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


addon/index.ts:17:23 - error TS2345: Argument of type '(owner: ApplicationInstance) => SparklesComponentManager' is not assignable to parameter of type 'string'.

17   setComponentManager((owner: ApplicationInstance) => {

I've submitted ember-cli/ember-compatibility-helpers#31 in an attempt to generate the types upstream. In the meantime, I've added declaration file for it here.

For the setComponentManager API I've updated the existing declaration. Soon we should be able to just upgrade to an Ember version that ships the correct types and be good to go.

@rwjblue
Copy link
Owner

rwjblue commented Jan 10, 2019

I don't understand, why did #26 pass CI if it broke things?

@buschtoens
Copy link
Contributor Author

I don't understand, why did #26 pass CI if it broke things?

It passed CI, because we do not lint types in CI. If you check the logs, you can see that ember-cli-typescript does in fact complain about it, but does not fail the build: https://travis-ci.com/rwjblue/sparkles-component/jobs/167248765#L559-L572

However, prepublishOnly / ember ts:precompile does fail. If you would try to cut a release from master now, it would not let you.

@rwjblue
Copy link
Owner

rwjblue commented Jan 10, 2019

It passed CI, because we do not lint types in CI.

Gotcha! We should add this to prevent exactly this kind of regression...

@rwjblue rwjblue merged commit e3e51e8 into rwjblue:master Jan 10, 2019
@rwjblue
Copy link
Owner

rwjblue commented Jan 10, 2019

Released in 1.3.0

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

Successfully merging this pull request may close these issues.

None yet

4 participants