Skip to content

Commit 11808f0

Browse files
author
Robert Jackson
authored
Merge pull request #111 from ember-cli/fix-type-casting
Properly transpile `(emberImportedThing as any)()`
2 parents 5fc9527 + 7e3c383 commit 11808f0

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

__tests__/index-test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,46 @@ describe('when used with typescript', () => {
308308

309309
expect(actual).toEqual(``);
310310
});
311+
312+
it('works when type casting', () => {
313+
let source = `
314+
import { addObserver } from '@ember/object/observers';
315+
(addObserver as any)();
316+
`;
317+
318+
let actual = transform7(source, [
319+
'@babel/plugin-transform-typescript',
320+
Plugin,
321+
]);
322+
323+
expect(actual).toEqual(`Ember.addObserver();`);
324+
});
325+
326+
it('works for type assertions', () => {
327+
let source = `
328+
import { addObserver } from '@ember/object/observers';
329+
<foo>addObserver();
330+
`;
331+
332+
let actual = transform7(source, [
333+
'@babel/plugin-transform-typescript',
334+
Plugin,
335+
]);
336+
337+
expect(actual).toEqual(`Ember.addObserver();`);
338+
});
339+
340+
it('works for non-null expression', () => {
341+
let source = `
342+
import { addObserver } from '@ember/object/observers';
343+
addObserver!();
344+
`;
345+
346+
let actual = transform7(source, [
347+
'@babel/plugin-transform-typescript',
348+
Plugin,
349+
]);
350+
351+
expect(actual).toEqual(`Ember.addObserver();`);
352+
});
311353
});

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ function isBlacklisted(blacklist, importPath, exportName) {
1616
module.exports = function (babel) {
1717
const t = babel.types;
1818

19-
const isTypescriptNode = (node) => node.type.startsWith('TS');
19+
const TSTypesRequiringModification = [
20+
'TSAsExpression',
21+
'TSTypeAssertion',
22+
'TSNonNullExpression',
23+
];
24+
const isTypescriptNode = (node) =>
25+
node.type.startsWith('TS') &&
26+
!TSTypesRequiringModification.includes(node.type);
2027

2128
const GLOBALS_MAP = new Map();
2229

0 commit comments

Comments
 (0)