Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ber-modules-api-polyfill

� Conflicts:
�	__tests__/index-test.js
�	package.json
�	src/index.js
�	yarn.lock
  • Loading branch information
mdeanjones committed Feb 11, 2021
2 parents f0ff8a4 + d6bfa5a commit 68ac71e
Show file tree
Hide file tree
Showing 6 changed files with 474 additions and 596 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## v3.2.1 (2021-01-15)

#### :bug: Bug Fix
* [#170](https://github.com/ember-cli/babel-plugin-ember-modules-api-polyfill/pull/170) Ensure decorators are transpiled properly when the decorator identifier is aliased within the decorated method ([@dwickern](https://github.com/dwickern))

#### :house: Internal
* [#172](https://github.com/ember-cli/babel-plugin-ember-modules-api-polyfill/pull/172) Update eslint related packages. ([@rwjblue](https://github.com/rwjblue))
* [#171](https://github.com/ember-cli/babel-plugin-ember-modules-api-polyfill/pull/171) Update release automation setup. ([@rwjblue](https://github.com/rwjblue))
* [#171](https://github.com/ember-cli/babel-plugin-ember-modules-api-polyfill/pull/171) Update release automation setup. ([@rwjblue](https://github.com/rwjblue))

#### Committers: 3
- Derek Wickern ([@dwickern](https://github.com/dwickern))
- Robert Jackson ([@rwjblue](https://github.com/rwjblue))
- [@dependabot-preview[bot]](https://github.com/apps/dependabot-preview)


## v3.2.0 (2020-10-02)

#### :rocket: Enhancement
Expand Down
39 changes: 16 additions & 23 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Release
# Release Process

Releases are mostly automated using
[release-it](https://github.com/release-it/release-it/) and
[lerna-changelog](https://github.com/lerna/lerna-changelog/).


## Preparation

Since the majority of the actual release process is automated, the primary
Expand All @@ -25,39 +24,33 @@ When reviewing merged PR's the labels to be used are:
* internal - Used for internal changes that still require a mention in the
changelog/release notes.


## Release

Once the prep work is completed, the actual release is straight forward:

* First ensure that you have `release-it` installed globally, generally done by
using one of the following commands:
* First, ensure that you have installed your projects dependencies:

```sh
yarn install
```
# using https://volta.sh
volta install release-it

# using Yarn
yarn global add release-it
* Second, ensure that you have obtained a
[GitHub personal access token][generate-token] with the `repo` scope (no
other permissions are needed). Make sure the token is available as the
`GITHUB_AUTH` environment variable.

# using npm
npm install --global release-it
```
For instance:

* Second, ensure that you have installed your projects dependencies:
```bash
export GITHUB_AUTH=abc123def456
```

```
yarn install
```
[generate-token]: https://github.com/settings/tokens/new?scopes=repo&description=GITHUB_AUTH+env+variable

* And last (but not least 😁) do your release. It requires a
[GitHub personal access token](https://github.com/settings/tokens) as
`$GITHUB_AUTH` environment variable. Only "repo" access is needed; no "admin"
or other scopes are required.
* And last (but not least 😁) do your release.

```
export GITHUB_AUTH="f941e0..."
release-it
```sh
npx release-it
```

[release-it](https://github.com/release-it/release-it/) manages the actual
Expand Down
39 changes: 37 additions & 2 deletions __tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ function transform7(source, _plugins) {
return result.code;
}

function transformWithPresetEnv(source) {
function transformWithPresetEnv(source, _plugins) {
let plugins = [].concat([[Plugin]], _plugins || []);
let result = babel7.transformSync(source, {
plugins: [[Plugin]],
plugins,

presets: [['@babel/preset-env', { targets: { ie: '8' }, modules: false }]],
});

Expand Down Expand Up @@ -423,6 +425,39 @@ describe('when used with typescript', () => {
});
});

describe('when used with native classes and decorators', () => {
it('allows "action" to be used as a variable name', () => {
let source = `
import { action } from '@ember/object';
import Controller from '@ember/controller';
export default class MyController extends Controller {
@action
addAction(action) {
this.actions.pushObject(action);
}
}
`;

let actual = transform7(source, [
[Plugin],
['@babel/plugin-proposal-decorators', { legacy: true }],
]);

expect(actual).toEqual(`var _dec, _class;
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
let MyController = (_dec = Ember._action, (_class = class MyController extends Ember.Controller {
addAction(action) {
this.actions.pushObject(action);
}
}, (_applyDecoratedDescriptor(_class.prototype, "addAction", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "addAction"), _class.prototype)), _class));
export { MyController as default };`);
});
});

describe('when used with babel-plugin-istanbul', () => {
// babel-plugin-istanbul won't run on <= Node 6
const majorVersion = parseInt(process.version.match(/^v(\d+)\./)[1], 10);
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-ember-modules-api-polyfill",
"version": "3.2.0",
"version": "3.2.1",
"description": "Polyfill for Ember JS API.",
"keywords": [
"babel-plugin"
Expand Down Expand Up @@ -31,19 +31,20 @@
"ember-rfc176-data": "^0.3.16"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/core": "^7.12.10",
"@babel/plugin-proposal-decorators": "^7.12.12",
"@babel/plugin-transform-typescript": "^7.12.1",
"@babel/preset-env": "^7.12.7",
"@babel/preset-env": "^7.12.11",
"babel-core": "^6.25.0",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"babel-plugin-istanbul": "^6.0.0",
"eslint": "^7.14.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^24.9.0",
"prettier": "^2.2.1",
"release-it": "^14.2.2",
"release-it-lerna-changelog": "^2.4.0"
"release-it-lerna-changelog": "^3.1.0"
},
"engines": {
"node": "6.* || 8.* || >= 10.*"
Expand Down
47 changes: 46 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ function isIgnored(ignore, importPath, exportName) {
}
}

function isDecorator(moduleName, importName) {
switch (moduleName) {
case '@ember/service':
return importName === 'inject';
case '@ember/controller':
return importName === 'inject';
case '@glimmer/tracking':
return importName === 'tracked';
case '@ember/object/compat':
return importName === 'dependentKeyCompat';
case '@ember/object':
return ['action', 'computed'].includes(importName);
case '@ember/object/computed':
// only the default import of this module is not a decorator
return importName !== 'default';
}
}

module.exports = function (babel) {
const t = babel.types;

Expand Down Expand Up @@ -169,8 +187,35 @@ module.exports = function (babel) {

// Replace the occurences of the imported name with the global name.
let binding = path.scope.getBinding(local.name);
let referencePaths = binding.referencePaths;

if (isDecorator(importPath, importName)) {
// tldr; decorator paths are not always included in `path.scope.getBinding(local.name)`
//
// In some circumstances, decorators are not included in the
// reference paths for a local binding when the decorator
// identifier name is also defined _within_ the method being
// decorated. This is likely a bug in Babel, that should be
// reported and fixed.
//
// in order to fix that, we have to manually traverse to gather
// the decorator references **before** the
// @babel/plugin-proposal-decorators runs (because it removes
// them)
path.parentPath.traverse({
Decorator(decoratorPath) {
if (
decoratorPath.node.expression.type === 'Identifier' &&
decoratorPath.node.expression.name === local.name
) {
referencePaths.push(decoratorPath.get('expression'));
}
},
});
}

binding.referencePaths.forEach((referencePath) => {
// Replace the occurences of the imported name with the global name.
referencePaths.forEach((referencePath) => {
if (!isTypescriptNode(referencePath.parentPath)) {
referencePath.replaceWith(getMemberExpressionFor(global));
}
Expand Down
Loading

0 comments on commit 68ac71e

Please sign in to comment.