Skip to content

Add a syntax guide to use after migration to codemods #41

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
160 changes: 160 additions & 0 deletions Migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# After ES6 classes migration

The purpose of this document is to provide a quick syntax reference to usage of ES6 classes to avoid some common mistakes.

**Note** Any new code can be restricted to use only ES6 classes with the help of [eslint plugin](https://github.com/scalvert/eslint-plugin-ember-es6-class) after the [codemods](https://github.com/scalvert/ember-es6-class-codemod) are run.

# Classes

### Before
```
let MyComponent = Component.extend({

});
```
### After
```
class MyComponent extends Component {

}
```
## Default export
### Before
```
export default Component.extend({

});
```
### After
```
export default class MyComponent extends Component {

}
```
**Note:** It is recommended to have class name for default export, though its not mandatory.

## Mixins
### Before
```
let MyComponent = Component.extend(AMixin, BMixin, {

});
```
### After
```
class MyComponent extends Component.extend(AMixin, BMixin) {

}
```

# Properties

### Before
```
let someVal = "value";

let MyComponent = Component.extend({
prop: "defaultValue",
boolProp: true,
numProp: 123,
[MY_VAL]: "val",
queryParams: {},
someVal
});
```
### After
```
let someVal = "value";

class MyComponent extends Component {
prop = "defaultValue";
boolProp = true;
numProp = 123;
[MY_VAL] = "val";
queryParams = {};
someVal = someVal;
}
```
**Note** The property shorthand does not work in classes as in objects. A class property should be assigned a value otherwise it's value `undefined`. See class property `someVal` in above example.

# Methods

### Before
```
let someFunction = () => {};

let MyComponent = Component.extend({
someUtilFunction() {
this._super(...arguments);
return value;
},

someFunction,

_someOtherFunction() {
return value;
}
});
```
### After
```
let someFunction = () => {};

class MyComponent extends Component {
someUtilFunction() {
super.someUtilFunction(...arguments);
return value;
}

someFunction = someFunction;

_someOtherFunction() {
return value;
}
}
```
**Note** The `someUtilFunction` must be present in super class hierarchy. An error will be thrown otherwise at run time

# Actions

### Before
```
let someAction = () => {};

let MyComponent = Component.extend({
actions: {
onButtonClick() {
this._super(...arguments);
return value;
},

someAction,
}
});
```
### After
```
let someAction = () => {};

class MyComponent extends Component {
@action
onButtonClick() {
super.actions.onButtonClick.call(this, ...arguments);
return value;
},

@action
someAction() {
someAction.call(this, ...arguments);
}
}
```
**Notes**
1. The `super` calls in actions are tricky. The `actions` are part of the actions hash on ember objects. So they can be referenced through `super.actions` object. We need to use `call` method to make sure they are bound to correct `this`.

2. Property shorthand does not work correctly in actions. So we have to wrap it inside function. See the action `someAction` in the above example.

3. Make sure that the `onButtonClick` is present in `actions` hash in super class hierarchy. An error will be thrown otherwise at run time

# Decorators
Usage of decorators is well documented in [ember decorators](http://ember-decorators.github.io/ember-decorators/)