Skip to content
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

Avoid errors when replacing Ember modules imports that are used as both a type and a value #110

Merged
merged 3 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
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
42 changes: 42 additions & 0 deletions __tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,45 @@ describe('when used with @babel/preset-env', () => {
expect(actual).toEqual(`export default Ember.Application.extend({});`);
});
});

describe('when used with typescript', () => {
// Example taken from Ember Data:
// https://github.com/emberjs/data/blob/70b0c55e1a950bed1da64d0ecb4eaa0d5df92f9f/packages/store/addon/-private/system/fetch-manager.ts#L124
// https://github.com/emberjs/data/blob/70b0c55e1a950bed1da64d0ecb4eaa0d5df92f9f/packages/store/addon/-private/system/fetch-manager.ts#L124
// https://github.com/emberjs/data/blob/70b0c55e1a950bed1da64d0ecb4eaa0d5df92f9f/packages/store/addon/-private/system/fetch-manager.ts#L77
it(`works when you use an import as both a type and as a TSQualifiedName value`, () => {
let source = `
import { default as RSVP, Promise } from 'rsvp';
RSVP.Promise.resolve().then(() => {});
function scheduleSave(identifier: RecordIdentifier, options: any = {}): RSVP.Promise<null | SingleResourceDocument> {
}
`;

let actual = transform7(source, [
require('@babel/plugin-transform-typescript'),
Plugin,
]);

expect(actual).toEqual(
`Ember.RSVP.Promise.resolve().then(() => {});\n\nfunction scheduleSave(identifier, options = {}) {}`
);
});

it(`works when you use an import as both a type and a TSDeclareFunction`, () => {
let source = `
import { capabilities } from '@ember/component';

declare module '@ember/component' {
export function capabilities(
);
}
`;

let actual = transform7(source, [
require('@babel/plugin-transform-typescript'),
Plugin,
]);

expect(actual).toEqual(``);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"devDependencies": {
"@babel/core": "^7.10.1",
"@babel/plugin-transform-typescript": "^7.10.1",
"@babel/preset-env": "^7.10.1",
"babel-core": "^6.25.0",
"eslint": "^7.1.0",
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function isBlacklisted(blacklist, importPath, exportName) {
module.exports = function (babel) {
const t = babel.types;

const isTypescriptNode = (node) => node.type.startsWith('TS');

const GLOBALS_MAP = new Map();

// Flips the ember-rfc176-data mapping into an 'import' indexed object, that exposes the
Expand Down Expand Up @@ -167,7 +169,9 @@ module.exports = function (babel) {
let binding = path.scope.getBinding(local.name);

binding.referencePaths.forEach((referencePath) => {
referencePath.replaceWith(getMemberExpressionFor(global));
if (!isTypescriptNode(referencePath.parentPath)) {
referencePath.replaceWith(getMemberExpressionFor(global));
}
});
}
});
Expand Down
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@
dependencies:
"@babel/helper-plugin-utils" "^7.10.1"

"@babel/plugin-syntax-typescript@^7.10.1":
version "7.10.1"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.10.1.tgz#5e82bc27bb4202b93b949b029e699db536733810"
integrity sha512-X/d8glkrAtra7CaQGMiGs/OGa6XgUzqPcBXCIGFCpCqnfGlT0Wfbzo/B89xHhnInTaItPK8LALblVXcUOEh95Q==
dependencies:
"@babel/helper-plugin-utils" "^7.10.1"

"@babel/plugin-transform-arrow-functions@^7.10.1":
version "7.10.1"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-arrow-functions/-/plugin-transform-arrow-functions-7.10.1.tgz#cb5ee3a36f0863c06ead0b409b4cc43a889b295b"
Expand Down Expand Up @@ -669,6 +676,15 @@
dependencies:
"@babel/helper-plugin-utils" "^7.10.1"

"@babel/plugin-transform-typescript@^7.10.1":
version "7.10.1"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.10.1.tgz#2c54daea231f602468686d9faa76f182a94507a6"
integrity sha512-v+QWKlmCnsaimLeqq9vyCsVRMViZG1k2SZTlcZvB+TqyH570Zsij8nvVUZzOASCRiQFUxkLrn9Wg/kH0zgy5OQ==
dependencies:
"@babel/helper-create-class-features-plugin" "^7.10.1"
"@babel/helper-plugin-utils" "^7.10.1"
"@babel/plugin-syntax-typescript" "^7.10.1"

"@babel/plugin-transform-unicode-escapes@^7.10.1":
version "7.10.1"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-unicode-escapes/-/plugin-transform-unicode-escapes-7.10.1.tgz#add0f8483dab60570d9e03cecef6c023aa8c9940"
Expand Down