Skip to content

Commit

Permalink
Merge pull request #111 from ember-cli/fix-type-casting
Browse files Browse the repository at this point in the history
Properly transpile `(emberImportedThing as any)()`
  • Loading branch information
rwjblue authored May 29, 2020
2 parents 5fc9527 + 7e3c383 commit 11808f0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
42 changes: 42 additions & 0 deletions __tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,46 @@ describe('when used with typescript', () => {

expect(actual).toEqual(``);
});

it('works when type casting', () => {
let source = `
import { addObserver } from '@ember/object/observers';
(addObserver as any)();
`;

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

expect(actual).toEqual(`Ember.addObserver();`);
});

it('works for type assertions', () => {
let source = `
import { addObserver } from '@ember/object/observers';
<foo>addObserver();
`;

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

expect(actual).toEqual(`Ember.addObserver();`);
});

it('works for non-null expression', () => {
let source = `
import { addObserver } from '@ember/object/observers';
addObserver!();
`;

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

expect(actual).toEqual(`Ember.addObserver();`);
});
});
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ function isBlacklisted(blacklist, importPath, exportName) {
module.exports = function (babel) {
const t = babel.types;

const isTypescriptNode = (node) => node.type.startsWith('TS');
const TSTypesRequiringModification = [
'TSAsExpression',
'TSTypeAssertion',
'TSNonNullExpression',
];
const isTypescriptNode = (node) =>
node.type.startsWith('TS') &&
!TSTypesRequiringModification.includes(node.type);

const GLOBALS_MAP = new Map();

Expand Down

0 comments on commit 11808f0

Please sign in to comment.