Skip to content

Commit

Permalink
Merge pull request #2 from ember-codemods/suchita/prefix
Browse files Browse the repository at this point in the history
Fix the inline prefix issue and added an option for default prefixing
  • Loading branch information
suchitadoshi1987 committed Dec 24, 2019
2 parents e554c23 + 2f08b8e commit 7fc0a23
Show file tree
Hide file tree
Showing 14 changed files with 1,688 additions and 830 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
*.log
*.log
.DS_Store
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ A codemod for transforming your ember app code to start using `@tracked` propert
npx ember-tracked-properties-codemod path/of/files/ or/some**/*glob.hbs
```


The codemod accepts the following options:

| Option | Value | Default | Details |
| --------------------- | ------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `--always-prefix` | boolean | `true` | Always prefix `@tracked` inline |

## Contributing

Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ember-tracked-properties-codemod",
"description": "A codemod for transforming your ember app code to start using `@tracked` properties.",
"version": "0.1.0",
"version": "0.2.0",
"author": {
"name": "Suchita Doshi",
"email": "suchitadoshi1987@gmail.com",
Expand All @@ -14,10 +14,14 @@
},
"bin": "./bin/cli.js",
"keywords": [
"codemod-cli"
"codemod-cli",
"codemods",
"ember",
"ember-codemod",
"ember-tracked-properties"
],
"dependencies": {
"codemod-cli": "^0.2.7"
"codemod-cli": "^2.1.0"
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.7.4",
Expand Down
65 changes: 50 additions & 15 deletions transforms/tracked-properties/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ npx ember-tracked-properties-codemod path/of/files/ or/some**/*glob.hbs
## Input / Output

<!--FIXTURES_TOC_START-->
* [basic](#basic)
* [chained-complex-computed](#chained-complex-computed)
* [chained-computed](#chained-computed)
* [complex](#complex)
* [with-tracked](#with-tracked)
<!--FIXTURES_TOC_END-->

- [basic](#basic)
- [basic-with-prefix-false](#basic-with-prefix-false)
- [chained-complex-computed](#chained-complex-computed)
- [chained-computed](#chained-computed)
- [complex](#complex)
- [with-tracked](#with-tracked)
<!--FIXTURES_TOC_END-->

## <!--FIXTURES_CONTENT_START-->

Expand Down Expand Up @@ -44,6 +46,44 @@ import { tracked } from '@glimmer/tracking';
import Component from '@ember/component';
import { computed, get } from '@ember/object';

export default class Foo extends Component {
bar;
@tracked baz = 'barBaz';

get bazInfo() {
return `Name: ${get(this, 'baz')}`;
}
}
```

---

<a id="basic-with-prefix-false">**basic-with-prefix-false ({alwaysPrefix: `false`})**</a>

**Input** (<small>[basic-with-prefix-false.input.js](__testfixtures__/basic-with-prefix-false.input.js)</small>):

```js
import Component from '@ember/component';
import { computed, get } from '@ember/object';

export default class Foo extends Component {
bar;
baz = 'barBaz';

@computed('baz')
get bazInfo() {
return `Name: ${get(this, 'baz')}`;
}
}
```

**Output** (<small>[basic-with-prefix-false.output.js](__testfixtures__/basic-with-prefix-false.output.js)</small>):

```js
import { tracked } from '@glimmer/tracking';
import Component from '@ember/component';
import { computed, get } from '@ember/object';

export default class Foo extends Component {
bar;
@tracked
Expand Down Expand Up @@ -91,8 +131,7 @@ import Component from '@ember/component';
import { computed, get } from '@ember/object';

export default class Foo extends Component {
@tracked
firstName = 'Foo';
@tracked firstName = 'Foo';
@tracked lastName;
@tracked phone;
zipcode;
Expand Down Expand Up @@ -147,8 +186,7 @@ import Component from '@ember/component';
import { computed, get } from '@ember/object';

export default class Foo extends Component {
@tracked
foo = 'bar';
@tracked foo = 'bar';
baz;

get fooBar() {
Expand Down Expand Up @@ -250,7 +288,6 @@ export default class Foo extends Component {
return `Name: ${get(this, 'baz')}`;
}
}

```

**Output** (<small>[with-tracked.output.js](__testfixtures__/with-tracked.output.js)</small>):
Expand All @@ -262,14 +299,12 @@ import { computed, get } from '@ember/object';

export default class Foo extends Component {
@tracked bar;
@tracked
baz = 'barBaz';
@tracked baz = 'barBaz';

get bazInfo() {
return `Name: ${get(this, 'baz')}`;
}
}

```

---
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Component from '@ember/component';
import { computed, get } from '@ember/object';

export default class Foo extends Component {
bar;
baz = 'barBaz';

@computed('baz')
get bazInfo() {
return `Name: ${get(this, 'baz')}`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"alwaysPrefix": "false"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { tracked } from '@glimmer/tracking';
import Component from '@ember/component';
import { computed, get } from '@ember/object';

export default class Foo extends Component {
bar;
@tracked
baz = 'barBaz';

get bazInfo() {
return `Name: ${get(this, 'baz')}`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { computed, get } from '@ember/object';

export default class Foo extends Component {
bar;
@tracked
baz = 'barBaz';
@tracked baz = 'barBaz';

get bazInfo() {
return `Name: ${get(this, 'baz')}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import Component from '@ember/component';
import { computed, get } from '@ember/object';

export default class Foo extends Component {
@tracked
foo = 'bar';
@tracked foo = 'bar';
baz;

get fooBar() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import Component from '@ember/component';
import { computed, get } from '@ember/object';

export default class Foo extends Component {
@tracked
firstName = 'Foo';
@tracked firstName = 'Foo';
@tracked lastName;
@tracked phone;
zipcode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { computed, get } from '@ember/object';

export default class Foo extends Component {
@tracked bar;
@tracked
baz = 'barBaz';
@tracked baz = 'barBaz';

get bazInfo() {
return `Name: ${get(this, 'baz')}`;
Expand Down
13 changes: 12 additions & 1 deletion transforms/tracked-properties/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ const {
addTrackedImport,
getDependentKeys,
buildTrackedDecorator,
reformatTrackedDecorators,
} = require('./utils/helper');

const { getOptions } = require('codemod-cli');
const DEFAULT_OPTIONS = {
alwaysPrefix: 'true',
};

module.exports = function transformer(file, api) {
const configOptions = Object.assign({}, DEFAULT_OPTIONS, getOptions());
const classProps = [];
let computedProps = [];
let computedPropsMap = {};
Expand Down Expand Up @@ -51,7 +58,7 @@ module.exports = function transformer(file, api) {
// with @tracked. Also, set the `shouldImportBeAdded` to true which would help
// determine if the import statement `@glimmer/tracking` needs to be added to
// the file.
const trackedConvertedSource = j(file.source)
let trackedConvertedSource = j(file.source)
.find(j.ClassProperty)
.forEach(path => {
if (computedProps.includes(path.node.key.name)) {
Expand All @@ -67,6 +74,10 @@ module.exports = function transformer(file, api) {
})
.toSource();

if (configOptions.alwaysPrefix === 'true') {
trackedConvertedSource = reformatTrackedDecorators(trackedConvertedSource);
}

// Iterate on all the `computed` decorators and for each node, check if
// all the arguments are a part of the `classProps` array, if so, then
// remove the `@computed` decorator, else remove the arguments that are
Expand Down
33 changes: 32 additions & 1 deletion transforms/tracked-properties/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,35 @@ function buildTrackedDecorator(name, value, j) {
return node;
}

module.exports = { addTrackedImport, getDependentKeys, buildTrackedDecorator };
/**
* Returns the formatted @tracked signature. After adding the @tracked decorator
* to the source, if the class property has a value associated with it,
* the @tracked decorator is add above the property instead of prefixing it inline.
* This function will check if the length of characters including the @tracked addition
* doesn't add up to more than 50 characters, then reformat it to be prefixed instead
* of being on a separate line.
* @param {string} trackedConvertedSource
*/
function reformatTrackedDecorators(trackedConvertedSource) {
const matchedTracked = trackedConvertedSource.match(/@tracked\n(.*)\n/g);
if (matchedTracked) {
matchedTracked.forEach(matchedData => {
const convertedMatchedData = matchedData.replace(
/@tracked\n\s+/,
'@tracked '
);
trackedConvertedSource = trackedConvertedSource.replace(
matchedData,
convertedMatchedData
);
});
}
return trackedConvertedSource;
}

module.exports = {
addTrackedImport,
getDependentKeys,
buildTrackedDecorator,
reformatTrackedDecorators,
};
Loading

0 comments on commit 7fc0a23

Please sign in to comment.