[babel 8] Enable allowDeclareFields option by default with TS (#12461)
This commit is contained in:
parent
ff52acee79
commit
50462eb5e4
@ -149,6 +149,14 @@ jobs:
|
||||
at: /tmp/verdaccio-workspace
|
||||
- run: ./scripts/integration-tests/e2e-babel.sh
|
||||
|
||||
e2e-babel-breaking:
|
||||
executor: node-executor
|
||||
steps:
|
||||
- checkout
|
||||
- attach_workspace:
|
||||
at: /tmp/verdaccio-workspace
|
||||
- run: BABEL_8_BREAKING=true ./scripts/integration-tests/e2e-babel.sh
|
||||
|
||||
e2e-babel-old-version:
|
||||
executor: node-executor
|
||||
steps:
|
||||
@ -185,6 +193,14 @@ jobs:
|
||||
at: /tmp/verdaccio-workspace
|
||||
- run: ./scripts/integration-tests/e2e-jest.sh
|
||||
|
||||
e2e-jest-breaking:
|
||||
executor: node-python-executor
|
||||
steps:
|
||||
- checkout
|
||||
- attach_workspace:
|
||||
at: /tmp/verdaccio-workspace
|
||||
- run: BABEL_8_BREAKING=true ./scripts/integration-tests/e2e-jest.sh
|
||||
|
||||
workflows:
|
||||
version: 2
|
||||
build-standalone:
|
||||
@ -247,7 +263,7 @@ workflows:
|
||||
filters:
|
||||
branches:
|
||||
only: [main, next-8-dev, next-8-rebased]
|
||||
- e2e-babel:
|
||||
- e2e-babel-breaking:
|
||||
requires:
|
||||
- publish-verdaccio-babel-8-breaking
|
||||
- e2e-create-react-app:
|
||||
@ -256,7 +272,7 @@ workflows:
|
||||
- e2e-vue-cli:
|
||||
requires:
|
||||
- publish-verdaccio-babel-8-breaking
|
||||
- e2e-jest:
|
||||
- e2e-jest-breaking:
|
||||
requires:
|
||||
- publish-verdaccio-babel-8-breaking
|
||||
|
||||
@ -270,7 +286,7 @@ workflows:
|
||||
- publish-verdaccio-babel-8-breaking:
|
||||
requires:
|
||||
- approve-e2e-breaking-run
|
||||
- e2e-babel:
|
||||
- e2e-babel-breaking:
|
||||
requires:
|
||||
- publish-verdaccio-babel-8-breaking
|
||||
- e2e-create-react-app:
|
||||
@ -279,7 +295,7 @@ workflows:
|
||||
- e2e-vue-cli:
|
||||
requires:
|
||||
- publish-verdaccio-babel-8-breaking
|
||||
- e2e-jest:
|
||||
- e2e-jest-breaking:
|
||||
requires:
|
||||
- publish-verdaccio-babel-8-breaking
|
||||
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
class C {
|
||||
// Output should not use `_initialiseProps`
|
||||
x: T;
|
||||
y = 0;
|
||||
constructor(T) {}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": false,
|
||||
"plugins": ["transform-typescript", "proposal-class-properties"]
|
||||
}
|
||||
@ -1,8 +1,9 @@
|
||||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
||||
|
||||
class A {
|
||||
constructor() {
|
||||
_defineProperty(this, "y", void 0);
|
||||
class C {
|
||||
// Output should not use `_initialiseProps`
|
||||
constructor(T) {
|
||||
_defineProperty(this, "y", 0);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
class C {
|
||||
// Output should not use `_initialiseProps`
|
||||
x: T;
|
||||
y = 0;
|
||||
constructor(T) {}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": false,
|
||||
"plugins": [
|
||||
"transform-typescript",
|
||||
["proposal-class-properties", { "loose": true }]
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
class C {
|
||||
// Output should not use `_initialiseProps`
|
||||
constructor(T) {
|
||||
this.y = 0;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": true,
|
||||
"plugins": [
|
||||
"transform-typescript",
|
||||
["proposal-class-properties", { "loose": true }]
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
class C {
|
||||
// Output should not use `_initialiseProps`
|
||||
constructor(T) {
|
||||
this.x = void 0;
|
||||
this.y = 0;
|
||||
}
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": true,
|
||||
"plugins": ["transform-typescript", "proposal-class-properties"]
|
||||
}
|
||||
|
||||
@ -3,6 +3,8 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
|
||||
class C {
|
||||
// Output should not use `_initialiseProps`
|
||||
constructor(T) {
|
||||
_defineProperty(this, "x", void 0);
|
||||
|
||||
_defineProperty(this, "y", 0);
|
||||
}
|
||||
|
||||
|
||||
@ -45,31 +45,35 @@ function registerGlobalType(programScope, name) {
|
||||
GLOBAL_TYPES.get(programScope.path.node).add(name);
|
||||
}
|
||||
|
||||
export default declare(
|
||||
(
|
||||
api,
|
||||
{
|
||||
jsxPragma = "React.createElement",
|
||||
jsxPragmaFrag = "React.Fragment",
|
||||
allowNamespaces = false,
|
||||
allowDeclareFields = false,
|
||||
onlyRemoveTypeImports = false,
|
||||
},
|
||||
) => {
|
||||
export default declare((api, opts) => {
|
||||
api.assertVersion(7);
|
||||
|
||||
const JSX_PRAGMA_REGEX = /\*?\s*@jsx((?:Frag)?)\s+([^\s]+)/;
|
||||
|
||||
const {
|
||||
jsxPragma = "React.createElement",
|
||||
jsxPragmaFrag = "React.Fragment",
|
||||
allowNamespaces = false,
|
||||
onlyRemoveTypeImports = false,
|
||||
} = opts;
|
||||
|
||||
if (!process.env.BABEL_8_BREAKING) {
|
||||
// eslint-disable-next-line no-var
|
||||
var { allowDeclareFields = false } = opts;
|
||||
}
|
||||
|
||||
const classMemberVisitors = {
|
||||
field(path) {
|
||||
const { node } = path;
|
||||
|
||||
if (!process.env.BABEL_8_BREAKING) {
|
||||
if (!allowDeclareFields && node.declare) {
|
||||
throw path.buildCodeFrameError(
|
||||
`The 'declare' modifier is only allowed when the 'allowDeclareFields' option of ` +
|
||||
`@babel/plugin-transform-typescript or @babel/preset-typescript is enabled.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (node.declare) {
|
||||
if (node.value) {
|
||||
throw path.buildCodeFrameError(
|
||||
@ -85,12 +89,15 @@ export default declare(
|
||||
`Definitely assigned fields cannot be initialized here, but only in the constructor`,
|
||||
);
|
||||
}
|
||||
if (!process.env.BABEL_8_BREAKING) {
|
||||
// keep the definitely assigned fields only when `allowDeclareFields` (equivalent of
|
||||
// Typescript's `useDefineForClassFields`) is true
|
||||
if (!allowDeclareFields && !node.decorators) {
|
||||
path.remove();
|
||||
}
|
||||
} else if (
|
||||
}
|
||||
} else if (!process.env.BABEL_8_BREAKING) {
|
||||
if (
|
||||
!allowDeclareFields &&
|
||||
!node.value &&
|
||||
!node.decorators &&
|
||||
@ -98,6 +105,7 @@ export default declare(
|
||||
) {
|
||||
path.remove();
|
||||
}
|
||||
}
|
||||
|
||||
if (node.accessibility) node.accessibility = null;
|
||||
if (node.abstract) node.abstract = null;
|
||||
@ -498,5 +506,4 @@ export default declare(
|
||||
});
|
||||
return !sourceFileHasJsx;
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
4
packages/babel-plugin-transform-typescript/test/fixtures/class/declare-babel-7/input.ts
vendored
Normal file
4
packages/babel-plugin-transform-typescript/test/fixtures/class/declare-babel-7/input.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
class A {
|
||||
declare x;
|
||||
@foo declare y: string;
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": false,
|
||||
"plugins": [
|
||||
["transform-typescript", { "allowDeclareFields": true }],
|
||||
"proposal-class-properties"
|
||||
["syntax-decorators", { "legacy": true }]
|
||||
]
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
class A {
|
||||
declare x;
|
||||
@foo
|
||||
y;
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": false,
|
||||
"plugins": ["transform-typescript"],
|
||||
"throws": "The 'declare' modifier is only allowed when the 'allowDeclareFields' option of @babel/plugin-transform-typescript or @babel/preset-typescript is enabled."
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"plugins": [["transform-typescript", { "allowDeclareFields": true }]]
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": true,
|
||||
"plugins": [
|
||||
["transform-typescript", { "allowDeclareFields": true }],
|
||||
"transform-typescript",
|
||||
["syntax-decorators", { "legacy": true }]
|
||||
]
|
||||
}
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": false,
|
||||
"plugins": ["transform-typescript"]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
class A {
|
||||
x;
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": true,
|
||||
"plugins": ["transform-typescript"]
|
||||
}
|
||||
11
packages/babel-plugin-transform-typescript/test/fixtures/class/properties-babel-7/input.ts
vendored
Normal file
11
packages/babel-plugin-transform-typescript/test/fixtures/class/properties-babel-7/input.ts
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
class C {
|
||||
public a?: number;
|
||||
private b: number = 0;
|
||||
readonly c: number = 1;
|
||||
@foo d: number;
|
||||
@foo e: number = 3;
|
||||
f!: number;
|
||||
@foo g!: number;
|
||||
#h: string;
|
||||
#i: number = 10;
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": false,
|
||||
"plugins": [
|
||||
"transform-typescript",
|
||||
["syntax-decorators", { "legacy": true }],
|
||||
"syntax-class-properties"
|
||||
]
|
||||
}
|
||||
12
packages/babel-plugin-transform-typescript/test/fixtures/class/properties-babel-7/output.js
vendored
Normal file
12
packages/babel-plugin-transform-typescript/test/fixtures/class/properties-babel-7/output.js
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
class C {
|
||||
b = 0;
|
||||
c = 1;
|
||||
@foo
|
||||
d;
|
||||
@foo
|
||||
e = 3;
|
||||
@foo
|
||||
g;
|
||||
#h;
|
||||
#i = 10;
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": true,
|
||||
"plugins": [
|
||||
"transform-typescript",
|
||||
["syntax-decorators", { "legacy": true }],
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
class C {
|
||||
a;
|
||||
b = 0;
|
||||
c = 1;
|
||||
@foo
|
||||
d;
|
||||
@foo
|
||||
e = 3;
|
||||
f;
|
||||
@foo
|
||||
g;
|
||||
#h;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
{
|
||||
"plugins": [
|
||||
"proposal-class-properties",
|
||||
["transform-typescript", { "allowDeclareFields": true }]
|
||||
"transform-typescript"
|
||||
],
|
||||
"throws": "TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.\nIf you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:\n - @babel/plugin-proposal-class-properties\n - @babel/plugin-proposal-private-methods\n - @babel/plugin-proposal-decorators"
|
||||
}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": false,
|
||||
"plugins": [["transform-typescript", { "allowDeclareFields": true }]]
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": false,
|
||||
"plugins": [["transform-typescript", { "allowDeclareFields": false }]]
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
class A {}
|
||||
@ -0,0 +1,3 @@
|
||||
class A {
|
||||
x!;
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": true,
|
||||
"plugins": ["transform-typescript"]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
class A {
|
||||
x;
|
||||
}
|
||||
@ -1,12 +1,12 @@
|
||||
export module src {
|
||||
export namespace ns1 {
|
||||
export class foo {
|
||||
F1: string;
|
||||
F1: string = "";
|
||||
}
|
||||
}
|
||||
export namespace ns2 {
|
||||
export class foo {
|
||||
F1: string;
|
||||
F1: string = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,9 @@ export let src;
|
||||
let ns1;
|
||||
|
||||
(function (_ns) {
|
||||
class foo {}
|
||||
class foo {
|
||||
F1 = "";
|
||||
}
|
||||
|
||||
_ns.foo = foo;
|
||||
})(ns1 || (ns1 = _src.ns1 || (_src.ns1 = {})));
|
||||
@ -12,7 +14,9 @@ export let src;
|
||||
let ns2;
|
||||
|
||||
(function (_ns2) {
|
||||
class foo {}
|
||||
class foo {
|
||||
F1 = "";
|
||||
}
|
||||
|
||||
_ns2.foo = foo;
|
||||
})(ns2 || (ns2 = _src.ns2 || (_src.ns2 = {})));
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
module src {
|
||||
export namespace ns1 {
|
||||
export class foo {
|
||||
F1: string;
|
||||
F1: string = "";
|
||||
}
|
||||
}
|
||||
export namespace ns2 {
|
||||
export class foo {
|
||||
F1: string;
|
||||
F1: string = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,9 @@ let src;
|
||||
let ns1;
|
||||
|
||||
(function (_ns) {
|
||||
class foo {}
|
||||
class foo {
|
||||
F1 = "";
|
||||
}
|
||||
|
||||
_ns.foo = foo;
|
||||
})(ns1 || (ns1 = _src.ns1 || (_src.ns1 = {})));
|
||||
@ -12,7 +14,9 @@ let src;
|
||||
let ns2;
|
||||
|
||||
(function (_ns2) {
|
||||
class foo {}
|
||||
class foo {
|
||||
F1 = "";
|
||||
}
|
||||
|
||||
_ns2.foo = foo;
|
||||
})(ns2 || (ns2 = _src.ns2 || (_src.ns2 = {})));
|
||||
|
||||
@ -7,7 +7,6 @@ export default declare((api, opts) => {
|
||||
|
||||
const {
|
||||
allExtensions,
|
||||
allowDeclareFields,
|
||||
allowNamespaces,
|
||||
isTSX,
|
||||
jsxPragma,
|
||||
@ -15,8 +14,16 @@ export default declare((api, opts) => {
|
||||
onlyRemoveTypeImports,
|
||||
} = normalizeOptions(opts);
|
||||
|
||||
const pluginOptions = isTSX => ({
|
||||
allowDeclareFields,
|
||||
const pluginOptions = process.env.BABEL_8_BREAKING
|
||||
? isTSX => ({
|
||||
allowNamespaces,
|
||||
isTSX,
|
||||
jsxPragma,
|
||||
jsxPragmaFrag,
|
||||
onlyRemoveTypeImports,
|
||||
})
|
||||
: isTSX => ({
|
||||
allowDeclareFields: opts.allowDeclareFields,
|
||||
allowNamespaces,
|
||||
isTSX,
|
||||
jsxPragma,
|
||||
|
||||
@ -2,16 +2,10 @@ import { OptionValidator } from "@babel/helper-validator-option";
|
||||
const v = new OptionValidator("@babel/preset-typescript");
|
||||
|
||||
export default function normalizeOptions(options = {}) {
|
||||
let {
|
||||
allowDeclareFields,
|
||||
allowNamespaces,
|
||||
jsxPragma,
|
||||
onlyRemoveTypeImports,
|
||||
} = options;
|
||||
let { allowNamespaces, jsxPragma, onlyRemoveTypeImports } = options;
|
||||
|
||||
if (process.env.BABEL_8_BREAKING) {
|
||||
const TopLevelOptions = {
|
||||
allowDeclareFields: "allowDeclareFields",
|
||||
allExtensions: "allExtensions",
|
||||
allowNamespaces: "allowNamespaces",
|
||||
isTSX: "isTSX",
|
||||
@ -20,11 +14,6 @@ export default function normalizeOptions(options = {}) {
|
||||
onlyRemoveTypeImports: "onlyRemoveTypeImports",
|
||||
};
|
||||
v.validateTopLevelOptions(options, TopLevelOptions);
|
||||
allowDeclareFields = v.validateBooleanOption(
|
||||
TopLevelOptions.allowDeclareFields,
|
||||
options.allowDeclareFields,
|
||||
true,
|
||||
);
|
||||
allowNamespaces = v.validateBooleanOption(
|
||||
TopLevelOptions.allowNamespaces,
|
||||
options.allowNamespaces,
|
||||
@ -62,7 +51,6 @@ export default function normalizeOptions(options = {}) {
|
||||
|
||||
return {
|
||||
allExtensions,
|
||||
allowDeclareFields,
|
||||
allowNamespaces,
|
||||
isTSX,
|
||||
jsxPragma,
|
||||
|
||||
@ -7,7 +7,6 @@ describe("normalize options", () => {
|
||||
);
|
||||
});
|
||||
it.each([
|
||||
"allowDeclareFields",
|
||||
"allExtensions",
|
||||
"allowNamespaces",
|
||||
"isTSX",
|
||||
@ -32,7 +31,6 @@ describe("normalize options", () => {
|
||||
expect(normalizeOptions({})).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"allExtensions": false,
|
||||
"allowDeclareFields": true,
|
||||
"allowNamespaces": true,
|
||||
"isTSX": false,
|
||||
"jsxPragma": "React",
|
||||
@ -80,7 +78,6 @@ describe("normalize options", () => {
|
||||
expect(normalizeOptions({})).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"allExtensions": false,
|
||||
"allowDeclareFields": undefined,
|
||||
"allowNamespaces": undefined,
|
||||
"isTSX": false,
|
||||
"jsxPragma": undefined,
|
||||
|
||||
@ -20,6 +20,11 @@ cd ../..
|
||||
# TEST #
|
||||
#==============================================================================#
|
||||
|
||||
if [ "$BABEL_8_BREAKING" = true ] ; then
|
||||
# This option is removed in Babel 8
|
||||
sed -i 's/allowDeclareFields: true,\?/\/* allowDeclareFields: true *\//g' babel.config.js
|
||||
fi
|
||||
|
||||
startLocalRegistry "$PWD"/scripts/integration-tests/verdaccio-config.yml
|
||||
# We only bump dependencies in the top-level package.json, because workspaces
|
||||
# already use the workspace: protocol so will get the version in the monorepo
|
||||
|
||||
@ -37,11 +37,6 @@ python --version
|
||||
# TEST #
|
||||
#==============================================================================#
|
||||
|
||||
startLocalRegistry "$root"/verdaccio-config.yml
|
||||
yarn install
|
||||
yarn dedupe '@babel/*'
|
||||
yarn build
|
||||
|
||||
# Workaround for https://github.com/babel/babel/pull/12567
|
||||
node -e '
|
||||
let snapshots = fs.readFileSync("packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap", "utf8");
|
||||
@ -49,6 +44,16 @@ node -e '
|
||||
fs.writeFileSync("packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap", snapshots);
|
||||
'
|
||||
|
||||
if [ "$BABEL_8_BREAKING" = true ] ; then
|
||||
# This option is removed in Babel 8
|
||||
sed -i 's/allowDeclareFields: true,\?/\/* allowDeclareFields: true *\//g' babel.config.js
|
||||
fi
|
||||
|
||||
startLocalRegistry "$root"/verdaccio-config.yml
|
||||
yarn install
|
||||
yarn dedupe '@babel/*'
|
||||
yarn build
|
||||
|
||||
# The full test suite takes about 20mins on CircleCI. We run only a few of them
|
||||
# to speed it up.
|
||||
# The goals of this e2e test are:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user