Skip to content

[v4] allow ember-cli-babel to manage TS transpilation #1018

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

Merged
merged 2 commits into from
Jan 17, 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
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -u"
},
"dependencies": {
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.4.4",
"@babel/plugin-proposal-optional-chaining": "^7.6.0",
"@babel/plugin-transform-typescript": "~7.7.0",
"ansi-to-html": "^0.6.6",
"debug": "^4.0.0",
"ember-cli-babel-plugin-helpers": "^1.0.0",
"execa": "^3.0.0",
"fs-extra": "^8.0.0",
"resolve": "^1.5.0",
Expand Down Expand Up @@ -122,7 +118,7 @@
"in-repo-b": "link:tests/dummy/lib/in-repo-b",
"loader.js": "4.7.0",
"mocha": "6.2.2",
"prettier": "1.18.2",
"prettier": "1.19.1",
"qunit-dom": "0.9.2",
"testdouble": "3.12.4",
"tmp": "0.1.0",
Expand All @@ -140,8 +136,7 @@
"ember-addon": {
"configPath": "tests/dummy/config",
"before": [
"broccoli-watcher",
"ember-cli-babel"
"broccoli-watcher"
]
},
"husky": {
Expand Down
29 changes: 12 additions & 17 deletions tests/unit/build-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,19 @@ module('Unit | Build', function() {
assert.equal(fromTs, 'From test-support');
});

test('property initialization occurs in the right order', function(assert) {
class TestClass {
// we shouldn't encourage folks to write code like this, but tsc ensures
// that constructor param fields are set before field initializers run
field = this.constructorParam;
constructor(private constructorParam: string) {}
test('optional chaining and nullish coalescing are transpiled correctly', function(assert) {
let value = { a: 'hello' } as { a?: string; b?: string };
assert.equal(value?.a, 'hello');
assert.equal(value?.b, undefined);
assert.equal(value?.a ?? 'ok', 'hello');
assert.equal(value?.b ?? 'ok', 'ok');
});

test('class field declarations work', function(assert) {
class MyClass {
declare foo: string;
}

let instance = new TestClass('hello');
assert.equal(instance.field, 'hello');
assert.notOk('foo' in new MyClass());
});

// TODO: enable once a release of Prettier comes out that supports this syntax
// test('optional chaining and nullish coalescing are transpiled correctly', function(assert) {
// let value = { a: 'hello' } as { a?: string; b?: string };
// assert.equal(value?.a, 'hello');
// assert.equal(value?.b, undefined);
// assert.equal(value?.a ?? 'ok', 'hello');
// assert.equal(value?.b ?? 'ok', 'ok');
// });
});
50 changes: 2 additions & 48 deletions ts/addon.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import semver from 'semver';
import { Remote } from 'stagehand';
import { connect } from 'stagehand/lib/adapters/child-process';
import { hasPlugin, addPlugin, AddPluginOptions } from 'ember-cli-babel-plugin-helpers';
import Addon from 'ember-cli/lib/models/addon';
import { addon } from './lib/utilities/ember-cli-entities';
import fork from './lib/utilities/fork';
Expand Down Expand Up @@ -69,28 +68,6 @@ export default addon({
}
},

setupPreprocessorRegistry(type) {
if (type !== 'parent') return;

// Normally this is the sort of logic that would live in `included()`, but
// ember-cli-babel reads the configured extensions when setting up the
// preprocessor registry, so we need to beat it to the punch.
this._registerBabelExtension();

// As of 3.7, TS supports the optional chaining and nullish coalescing proposals.
// https://devblogs.microsoft.com/typescript/announcing-typescript-3-7-beta/
// Since we can't necessarily know what version of TS an addon was developed with,
// we unconditionally add the Babel plugins for both proposals.
this._addBabelPluginIfNotPresent('@babel/plugin-proposal-optional-chaining');
this._addBabelPluginIfNotPresent('@babel/plugin-proposal-nullish-coalescing-operator');

// Needs to come after the class properties plugin (see tests/unit/build-test.ts -
// "property initialization occurs in the right order")
this._addBabelPluginIfNotPresent('@babel/plugin-transform-typescript', {
after: ['@babel/plugin-proposal-class-properties'],
});
},

shouldIncludeChildAddon(addon) {
// For testing, we have dummy in-repo addons set up, but e-c-ts doesn't depend on them;
// its dummy app does. Otherwise we'd have a circular dependency.
Expand All @@ -100,6 +77,8 @@ export default addon({
_checkBabelVersion() {
let babel = this.parent.addons.find(addon => addon.name === 'ember-cli-babel');
let version = babel && babel.pkg.version;

// TODO update this check and warning message once we have a Babel version to target
if (!babel || !(semver.gte(version!, '7.7.3') && semver.lt(version!, '8.0.0'))) {
let versionString = babel ? `version ${babel.pkg.version}` : `no instance of ember-cli-babel`;
this.ui.writeWarnLine(
Expand Down Expand Up @@ -164,31 +143,6 @@ export default addon({
}
},

_getConfigurationTarget() {
// If `this.app` isn't present, we know `this.parent` is an addon
return this.app || (this.parent as Addon);
},

_registerBabelExtension() {
let target = this._getConfigurationTarget();
let options: Record<string, any> = target.options || (target.options = {});
let babelAddonOptions: Record<string, any> =
options['ember-cli-babel'] || (options['ember-cli-babel'] = {});
let extensions: string[] =
babelAddonOptions.extensions || (babelAddonOptions.extensions = ['js']);

if (!extensions.includes('ts')) {
extensions.push('ts');
}
},

_addBabelPluginIfNotPresent(pluginName: string, pluginOptions?: AddPluginOptions) {
let target = this._getConfigurationTarget();
if (!hasPlugin(target, pluginName)) {
addPlugin(target, require.resolve(pluginName), pluginOptions);
}
},

_addTypecheckMiddleware(app: Application) {
let workerPromise = this._getTypecheckWorker();
let middleware = new TypecheckMiddleware(this.project, workerPromise);
Expand Down
53 changes: 8 additions & 45 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,6 @@
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-json-strings" "^7.2.0"

"@babel/plugin-proposal-nullish-coalescing-operator@^7.4.4":
version "7.4.4"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-nullish-coalescing-operator/-/plugin-proposal-nullish-coalescing-operator-7.4.4.tgz#41c360d59481d88e0ce3a3f837df10121a769b39"
integrity sha512-Amph7Epui1Dh/xxUxS2+K22/MUi6+6JVTvy3P58tja3B6yKTSjwwx0/d83rF7551D6PVSSoplQb8GCwqec7HRw==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-nullish-coalescing-operator" "^7.2.0"

"@babel/plugin-proposal-object-rest-spread@^7.3.4":
version "7.3.4"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-object-rest-spread/-/plugin-proposal-object-rest-spread-7.3.4.tgz#47f73cf7f2a721aad5c0261205405c642e424654"
Expand All @@ -473,14 +465,6 @@
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-optional-catch-binding" "^7.2.0"

"@babel/plugin-proposal-optional-chaining@^7.6.0":
version "7.6.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-optional-chaining/-/plugin-proposal-optional-chaining-7.6.0.tgz#e9bf1f9b9ba10c77c033082da75f068389041af8"
integrity sha512-kj4gkZ6qUggkprRq3Uh5KP8XnE1MdIO0J7MhdDX8+rAbB6dJ2UrensGIS+0NPZAaaJ1Vr0PN6oLUgXMU1uMcSg==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-optional-chaining" "^7.2.0"

"@babel/plugin-proposal-unicode-property-regex@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-unicode-property-regex/-/plugin-proposal-unicode-property-regex-7.2.0.tgz#abe7281fe46c95ddc143a65e5358647792039520"
Expand Down Expand Up @@ -526,13 +510,6 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-nullish-coalescing-operator@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-nullish-coalescing-operator/-/plugin-syntax-nullish-coalescing-operator-7.2.0.tgz#f75083dfd5ade73e783db729bbd87e7b9efb7624"
integrity sha512-lRCEaKE+LTxDQtgbYajI04ddt6WW0WJq57xqkAZ+s11h4YgfRHhVA/Y2VhfPzzFD4qeLHWg32DMp9HooY4Kqlg==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-object-rest-spread@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-object-rest-spread/-/plugin-syntax-object-rest-spread-7.2.0.tgz#3b7a3e733510c57e820b9142a6579ac8b0dfad2e"
Expand All @@ -547,13 +524,6 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-optional-chaining@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-optional-chaining/-/plugin-syntax-optional-chaining-7.2.0.tgz#a59d6ae8c167e7608eaa443fda9fa8fa6bf21dff"
integrity sha512-HtGCtvp5Uq/jH/WNUPkK6b7rufnCPLLlDAFN7cmACoIjaOOiXxUt3SswU5loHqrhtqTsa/WoLQ1OQ1AGuZqaWA==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-top-level-await@^7.7.0":
version "7.7.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-top-level-await/-/plugin-syntax-top-level-await-7.7.0.tgz#f5699549f50bbe8d12b1843a4e82f0a37bb65f4d"
Expand Down Expand Up @@ -986,15 +956,6 @@
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-typescript" "^7.2.0"

"@babel/plugin-transform-typescript@~7.7.0":
version "7.7.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.7.0.tgz#182be03fa8bd2ffd0629791a1eaa4373b7589d38"
integrity sha512-y3KYbcfKe+8ziRXiGhhnGrVysDBo5+aJdB+x8sanM0K41cnmK7Q5vBlQLMbOnW/HPjLG9bg7dLgYDQZZG9T09g==
dependencies:
"@babel/helper-create-class-features-plugin" "^7.7.0"
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-typescript" "^7.2.0"

"@babel/plugin-transform-unicode-regex@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-unicode-regex/-/plugin-transform-unicode-regex-7.2.0.tgz#4eb8db16f972f8abb5062c161b8b115546ade08b"
Expand Down Expand Up @@ -9747,10 +9708,12 @@ imurmurhash@^0.1.4:
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=

"in-repo-a@link:tests/dummy/lib/in-repo-a":
version "1.0.0"
version "0.0.0"
uid ""

"in-repo-b@link:tests/dummy/lib/in-repo-b":
version "1.0.0"
version "0.0.0"
uid ""

include-path-searcher@^0.1.0:
version "0.1.0"
Expand Down Expand Up @@ -13395,10 +13358,10 @@ prettier-linter-helpers@^1.0.0:
dependencies:
fast-diff "^1.1.2"

prettier@1.18.2:
version "1.18.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==
prettier@1.19.1:
version "1.19.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb"
integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==

pretty-hrtime@^1.0.3:
version "1.0.3"
Expand Down