Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
Rename service to inject when needed (#2)
Browse files Browse the repository at this point in the history
This adds a Babel plugin which renames `service` imports to `inject` if the current Ember version doesn't support this import yet.

This is a polyfill for RFC 752. More information can be found in the RFC PR: emberjs/rfcs#752
  • Loading branch information
Windvis authored Jan 31, 2022
1 parent 0417e30 commit 097059a
Show file tree
Hide file tree
Showing 12 changed files with 3,263 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ module.exports = {
'./testem.js',
'./blueprints/*/index.js',
'./config/**/*.js',
'./lib/**/*.js',
'./tests/dummy/config/**/*.js',
'./tests/node/**/*.js',
],
parserOptions: {
sourceType: 'script',
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
run: npm run lint
- name: Run Tests
run: npm run test:ember
- name: Run Node Tests
run: npm run test:node

floating:
name: "Floating Dependencies"
Expand Down
23 changes: 23 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
'use strict';

const { hasPlugin, addPlugin } = require('ember-cli-babel-plugin-helpers');
const { resolve } = require('path');
const VersionChecker = require('ember-cli-version-checker');

module.exports = {
name: require('./package').name,

included(parent) {
this._super.included.apply(this, arguments);

if (this.shouldPolyfillServiceImport()) {
let pluginPath = resolve(__dirname, './lib/plugin-rename-service-import');

if (!hasPlugin(parent, pluginPath)) {
addPlugin(parent, pluginPath);
}
}
},

shouldPolyfillServiceImport() {
let checker = new VersionChecker(this.project);

// https://github.com/emberjs/ember.js/pull/19776/commits/12b89c42d1987fd782d06f1f170eb7ce25612558
return checker.for('ember-source').lt('v4.1.0-alpha.5');
},
};
31 changes: 31 additions & 0 deletions lib/plugin-rename-service-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

module.exports = function () {
return {
name: 'plugin-rename-service-import',
visitor: {
ImportSpecifier(path) {
let { node: importSpecifier, parent: importDeclaration } = path;

if (
isEmberServiceModuleImport(importDeclaration) &&
isNewImportName(importSpecifier)
) {
renameServiceImportToInject(importSpecifier);
}
},
},
};
};

function isEmberServiceModuleImport(importDeclaration) {
return importDeclaration.source.value === '@ember/service';
}

function isNewImportName(importSpecifier) {
return importSpecifier.imported.name === 'service';
}

function renameServiceImportToInject(importSpecifier) {
importSpecifier.imported.name = 'inject';
}
Loading

0 comments on commit 097059a

Please sign in to comment.