[babel 8] Enable allowDeclareFields option by default with TS (#12461)

This commit is contained in:
Nicolò Ribaudo 2021-01-08 01:28:20 +01:00 committed by GitHub
parent ff52acee79
commit 50462eb5e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
49 changed files with 584 additions and 453 deletions

View File

@ -149,6 +149,14 @@ jobs:
at: /tmp/verdaccio-workspace at: /tmp/verdaccio-workspace
- run: ./scripts/integration-tests/e2e-babel.sh - 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: e2e-babel-old-version:
executor: node-executor executor: node-executor
steps: steps:
@ -185,6 +193,14 @@ jobs:
at: /tmp/verdaccio-workspace at: /tmp/verdaccio-workspace
- run: ./scripts/integration-tests/e2e-jest.sh - 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: workflows:
version: 2 version: 2
build-standalone: build-standalone:
@ -247,7 +263,7 @@ workflows:
filters: filters:
branches: branches:
only: [main, next-8-dev, next-8-rebased] only: [main, next-8-dev, next-8-rebased]
- e2e-babel: - e2e-babel-breaking:
requires: requires:
- publish-verdaccio-babel-8-breaking - publish-verdaccio-babel-8-breaking
- e2e-create-react-app: - e2e-create-react-app:
@ -256,7 +272,7 @@ workflows:
- e2e-vue-cli: - e2e-vue-cli:
requires: requires:
- publish-verdaccio-babel-8-breaking - publish-verdaccio-babel-8-breaking
- e2e-jest: - e2e-jest-breaking:
requires: requires:
- publish-verdaccio-babel-8-breaking - publish-verdaccio-babel-8-breaking
@ -270,7 +286,7 @@ workflows:
- publish-verdaccio-babel-8-breaking: - publish-verdaccio-babel-8-breaking:
requires: requires:
- approve-e2e-breaking-run - approve-e2e-breaking-run
- e2e-babel: - e2e-babel-breaking:
requires: requires:
- publish-verdaccio-babel-8-breaking - publish-verdaccio-babel-8-breaking
- e2e-create-react-app: - e2e-create-react-app:
@ -279,7 +295,7 @@ workflows:
- e2e-vue-cli: - e2e-vue-cli:
requires: requires:
- publish-verdaccio-babel-8-breaking - publish-verdaccio-babel-8-breaking
- e2e-jest: - e2e-jest-breaking:
requires: requires:
- publish-verdaccio-babel-8-breaking - publish-verdaccio-babel-8-breaking

View File

@ -0,0 +1,6 @@
class C {
// Output should not use `_initialiseProps`
x: T;
y = 0;
constructor(T) {}
}

View File

@ -0,0 +1,4 @@
{
"BABEL_8_BREAKING": false,
"plugins": ["transform-typescript", "proposal-class-properties"]
}

View File

@ -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; } 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 { class C {
constructor() { // Output should not use `_initialiseProps`
_defineProperty(this, "y", void 0); constructor(T) {
_defineProperty(this, "y", 0);
} }
} }

View File

@ -0,0 +1,6 @@
class C {
// Output should not use `_initialiseProps`
x: T;
y = 0;
constructor(T) {}
}

View File

@ -0,0 +1,7 @@
{
"BABEL_8_BREAKING": false,
"plugins": [
"transform-typescript",
["proposal-class-properties", { "loose": true }]
]
}

View File

@ -0,0 +1,7 @@
class C {
// Output should not use `_initialiseProps`
constructor(T) {
this.y = 0;
}
}

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": true,
"plugins": [ "plugins": [
"transform-typescript", "transform-typescript",
["proposal-class-properties", { "loose": true }] ["proposal-class-properties", { "loose": true }]

View File

@ -1,6 +1,7 @@
class C { class C {
// Output should not use `_initialiseProps` // Output should not use `_initialiseProps`
constructor(T) { constructor(T) {
this.x = void 0;
this.y = 0; this.y = 0;
} }

View File

@ -1,3 +1,4 @@
{ {
"BABEL_8_BREAKING": true,
"plugins": ["transform-typescript", "proposal-class-properties"] "plugins": ["transform-typescript", "proposal-class-properties"]
} }

View File

@ -3,6 +3,8 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
class C { class C {
// Output should not use `_initialiseProps` // Output should not use `_initialiseProps`
constructor(T) { constructor(T) {
_defineProperty(this, "x", void 0);
_defineProperty(this, "y", 0); _defineProperty(this, "y", 0);
} }

View File

@ -45,31 +45,35 @@ function registerGlobalType(programScope, name) {
GLOBAL_TYPES.get(programScope.path.node).add(name); GLOBAL_TYPES.get(programScope.path.node).add(name);
} }
export default declare( export default declare((api, opts) => {
(
api,
{
jsxPragma = "React.createElement",
jsxPragmaFrag = "React.Fragment",
allowNamespaces = false,
allowDeclareFields = false,
onlyRemoveTypeImports = false,
},
) => {
api.assertVersion(7); api.assertVersion(7);
const JSX_PRAGMA_REGEX = /\*?\s*@jsx((?:Frag)?)\s+([^\s]+)/; 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 = { const classMemberVisitors = {
field(path) { field(path) {
const { node } = path; const { node } = path;
if (!process.env.BABEL_8_BREAKING) {
if (!allowDeclareFields && node.declare) { if (!allowDeclareFields && node.declare) {
throw path.buildCodeFrameError( throw path.buildCodeFrameError(
`The 'declare' modifier is only allowed when the 'allowDeclareFields' option of ` + `The 'declare' modifier is only allowed when the 'allowDeclareFields' option of ` +
`@babel/plugin-transform-typescript or @babel/preset-typescript is enabled.`, `@babel/plugin-transform-typescript or @babel/preset-typescript is enabled.`,
); );
} }
}
if (node.declare) { if (node.declare) {
if (node.value) { if (node.value) {
throw path.buildCodeFrameError( throw path.buildCodeFrameError(
@ -85,12 +89,15 @@ export default declare(
`Definitely assigned fields cannot be initialized here, but only in the constructor`, `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 // keep the definitely assigned fields only when `allowDeclareFields` (equivalent of
// Typescript's `useDefineForClassFields`) is true // Typescript's `useDefineForClassFields`) is true
if (!allowDeclareFields && !node.decorators) { if (!allowDeclareFields && !node.decorators) {
path.remove(); path.remove();
} }
} else if ( }
} else if (!process.env.BABEL_8_BREAKING) {
if (
!allowDeclareFields && !allowDeclareFields &&
!node.value && !node.value &&
!node.decorators && !node.decorators &&
@ -98,6 +105,7 @@ export default declare(
) { ) {
path.remove(); path.remove();
} }
}
if (node.accessibility) node.accessibility = null; if (node.accessibility) node.accessibility = null;
if (node.abstract) node.abstract = null; if (node.abstract) node.abstract = null;
@ -498,5 +506,4 @@ export default declare(
}); });
return !sourceFileHasJsx; return !sourceFileHasJsx;
} }
}, });
);

View File

@ -0,0 +1,4 @@
class A {
declare x;
@foo declare y: string;
}

View File

@ -1,6 +1,7 @@
{ {
"BABEL_8_BREAKING": false,
"plugins": [ "plugins": [
["transform-typescript", { "allowDeclareFields": true }], ["transform-typescript", { "allowDeclareFields": true }],
"proposal-class-properties" ["syntax-decorators", { "legacy": true }]
] ]
} }

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": false,
"plugins": ["transform-typescript"], "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." "throws": "The 'declare' modifier is only allowed when the 'allowDeclareFields' option of @babel/plugin-transform-typescript or @babel/preset-typescript is enabled."
} }

View File

@ -1,3 +0,0 @@
{
"plugins": [["transform-typescript", { "allowDeclareFields": true }]]
}

View File

@ -1,6 +1,7 @@
{ {
"BABEL_8_BREAKING": true,
"plugins": [ "plugins": [
["transform-typescript", { "allowDeclareFields": true }], "transform-typescript",
["syntax-decorators", { "legacy": true }] ["syntax-decorators", { "legacy": true }]
] ]
} }

View File

@ -0,0 +1,4 @@
{
"BABEL_8_BREAKING": false,
"plugins": ["transform-typescript"]
}

View File

@ -0,0 +1,3 @@
class A {
x;
}

View File

@ -0,0 +1,4 @@
{
"BABEL_8_BREAKING": true,
"plugins": ["transform-typescript"]
}

View 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;
}

View File

@ -0,0 +1,8 @@
{
"BABEL_8_BREAKING": false,
"plugins": [
"transform-typescript",
["syntax-decorators", { "legacy": true }],
"syntax-class-properties"
]
}

View File

@ -0,0 +1,12 @@
class C {
b = 0;
c = 1;
@foo
d;
@foo
e = 3;
@foo
g;
#h;
#i = 10;
}

View File

@ -1,4 +1,5 @@
{ {
"BABEL_8_BREAKING": true,
"plugins": [ "plugins": [
"transform-typescript", "transform-typescript",
["syntax-decorators", { "legacy": true }], ["syntax-decorators", { "legacy": true }],

View File

@ -1,10 +1,12 @@
class C { class C {
a;
b = 0; b = 0;
c = 1; c = 1;
@foo @foo
d; d;
@foo @foo
e = 3; e = 3;
f;
@foo @foo
g; g;
#h; #h;

View File

@ -1,7 +1,7 @@
{ {
"plugins": [ "plugins": [
"proposal-class-properties", "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" "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"
} }

View File

@ -1,3 +1,4 @@
{ {
"BABEL_8_BREAKING": false,
"plugins": [["transform-typescript", { "allowDeclareFields": true }]] "plugins": [["transform-typescript", { "allowDeclareFields": true }]]
} }

View File

@ -1,3 +1,4 @@
{ {
"BABEL_8_BREAKING": false,
"plugins": [["transform-typescript", { "allowDeclareFields": false }]] "plugins": [["transform-typescript", { "allowDeclareFields": false }]]
} }

View File

@ -0,0 +1,3 @@
class A {
x!;
}

View File

@ -0,0 +1,4 @@
{
"BABEL_8_BREAKING": true,
"plugins": ["transform-typescript"]
}

View File

@ -0,0 +1,3 @@
class A {
x;
}

View File

@ -1,12 +1,12 @@
export module src { export module src {
export namespace ns1 { export namespace ns1 {
export class foo { export class foo {
F1: string; F1: string = "";
} }
} }
export namespace ns2 { export namespace ns2 {
export class foo { export class foo {
F1: string; F1: string = "";
} }
} }
} }

View File

@ -4,7 +4,9 @@ export let src;
let ns1; let ns1;
(function (_ns) { (function (_ns) {
class foo {} class foo {
F1 = "";
}
_ns.foo = foo; _ns.foo = foo;
})(ns1 || (ns1 = _src.ns1 || (_src.ns1 = {}))); })(ns1 || (ns1 = _src.ns1 || (_src.ns1 = {})));
@ -12,7 +14,9 @@ export let src;
let ns2; let ns2;
(function (_ns2) { (function (_ns2) {
class foo {} class foo {
F1 = "";
}
_ns2.foo = foo; _ns2.foo = foo;
})(ns2 || (ns2 = _src.ns2 || (_src.ns2 = {}))); })(ns2 || (ns2 = _src.ns2 || (_src.ns2 = {})));

View File

@ -1,12 +1,12 @@
module src { module src {
export namespace ns1 { export namespace ns1 {
export class foo { export class foo {
F1: string; F1: string = "";
} }
} }
export namespace ns2 { export namespace ns2 {
export class foo { export class foo {
F1: string; F1: string = "";
} }
} }
} }

View File

@ -4,7 +4,9 @@ let src;
let ns1; let ns1;
(function (_ns) { (function (_ns) {
class foo {} class foo {
F1 = "";
}
_ns.foo = foo; _ns.foo = foo;
})(ns1 || (ns1 = _src.ns1 || (_src.ns1 = {}))); })(ns1 || (ns1 = _src.ns1 || (_src.ns1 = {})));
@ -12,7 +14,9 @@ let src;
let ns2; let ns2;
(function (_ns2) { (function (_ns2) {
class foo {} class foo {
F1 = "";
}
_ns2.foo = foo; _ns2.foo = foo;
})(ns2 || (ns2 = _src.ns2 || (_src.ns2 = {}))); })(ns2 || (ns2 = _src.ns2 || (_src.ns2 = {})));

View File

@ -7,7 +7,6 @@ export default declare((api, opts) => {
const { const {
allExtensions, allExtensions,
allowDeclareFields,
allowNamespaces, allowNamespaces,
isTSX, isTSX,
jsxPragma, jsxPragma,
@ -15,8 +14,16 @@ export default declare((api, opts) => {
onlyRemoveTypeImports, onlyRemoveTypeImports,
} = normalizeOptions(opts); } = normalizeOptions(opts);
const pluginOptions = isTSX => ({ const pluginOptions = process.env.BABEL_8_BREAKING
allowDeclareFields, ? isTSX => ({
allowNamespaces,
isTSX,
jsxPragma,
jsxPragmaFrag,
onlyRemoveTypeImports,
})
: isTSX => ({
allowDeclareFields: opts.allowDeclareFields,
allowNamespaces, allowNamespaces,
isTSX, isTSX,
jsxPragma, jsxPragma,

View File

@ -2,16 +2,10 @@ import { OptionValidator } from "@babel/helper-validator-option";
const v = new OptionValidator("@babel/preset-typescript"); const v = new OptionValidator("@babel/preset-typescript");
export default function normalizeOptions(options = {}) { export default function normalizeOptions(options = {}) {
let { let { allowNamespaces, jsxPragma, onlyRemoveTypeImports } = options;
allowDeclareFields,
allowNamespaces,
jsxPragma,
onlyRemoveTypeImports,
} = options;
if (process.env.BABEL_8_BREAKING) { if (process.env.BABEL_8_BREAKING) {
const TopLevelOptions = { const TopLevelOptions = {
allowDeclareFields: "allowDeclareFields",
allExtensions: "allExtensions", allExtensions: "allExtensions",
allowNamespaces: "allowNamespaces", allowNamespaces: "allowNamespaces",
isTSX: "isTSX", isTSX: "isTSX",
@ -20,11 +14,6 @@ export default function normalizeOptions(options = {}) {
onlyRemoveTypeImports: "onlyRemoveTypeImports", onlyRemoveTypeImports: "onlyRemoveTypeImports",
}; };
v.validateTopLevelOptions(options, TopLevelOptions); v.validateTopLevelOptions(options, TopLevelOptions);
allowDeclareFields = v.validateBooleanOption(
TopLevelOptions.allowDeclareFields,
options.allowDeclareFields,
true,
);
allowNamespaces = v.validateBooleanOption( allowNamespaces = v.validateBooleanOption(
TopLevelOptions.allowNamespaces, TopLevelOptions.allowNamespaces,
options.allowNamespaces, options.allowNamespaces,
@ -62,7 +51,6 @@ export default function normalizeOptions(options = {}) {
return { return {
allExtensions, allExtensions,
allowDeclareFields,
allowNamespaces, allowNamespaces,
isTSX, isTSX,
jsxPragma, jsxPragma,

View File

@ -7,7 +7,6 @@ describe("normalize options", () => {
); );
}); });
it.each([ it.each([
"allowDeclareFields",
"allExtensions", "allExtensions",
"allowNamespaces", "allowNamespaces",
"isTSX", "isTSX",
@ -32,7 +31,6 @@ describe("normalize options", () => {
expect(normalizeOptions({})).toMatchInlineSnapshot(` expect(normalizeOptions({})).toMatchInlineSnapshot(`
Object { Object {
"allExtensions": false, "allExtensions": false,
"allowDeclareFields": true,
"allowNamespaces": true, "allowNamespaces": true,
"isTSX": false, "isTSX": false,
"jsxPragma": "React", "jsxPragma": "React",
@ -80,7 +78,6 @@ describe("normalize options", () => {
expect(normalizeOptions({})).toMatchInlineSnapshot(` expect(normalizeOptions({})).toMatchInlineSnapshot(`
Object { Object {
"allExtensions": false, "allExtensions": false,
"allowDeclareFields": undefined,
"allowNamespaces": undefined, "allowNamespaces": undefined,
"isTSX": false, "isTSX": false,
"jsxPragma": undefined, "jsxPragma": undefined,

View File

@ -20,6 +20,11 @@ cd ../..
# TEST # # 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 startLocalRegistry "$PWD"/scripts/integration-tests/verdaccio-config.yml
# We only bump dependencies in the top-level package.json, because workspaces # 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 # already use the workspace: protocol so will get the version in the monorepo

View File

@ -37,11 +37,6 @@ python --version
# TEST # # TEST #
#==============================================================================# #==============================================================================#
startLocalRegistry "$root"/verdaccio-config.yml
yarn install
yarn dedupe '@babel/*'
yarn build
# Workaround for https://github.com/babel/babel/pull/12567 # Workaround for https://github.com/babel/babel/pull/12567
node -e ' node -e '
let snapshots = fs.readFileSync("packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap", "utf8"); 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); 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 # The full test suite takes about 20mins on CircleCI. We run only a few of them
# to speed it up. # to speed it up.
# The goals of this e2e test are: # The goals of this e2e test are: