Skip to content
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

Improve output of super() #16194

Merged
merged 8 commits into from Jan 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 15 additions & 20 deletions .prettierrc
Expand Up @@ -8,25 +8,20 @@
"bracketSameLine": false,
"tabWidth": 2,
"printWidth": 80,
"overrides": [{
"files": [
"**/{codemods,eslint,packages}/*/{src,test}/**/*.{js,ts,cjs}"
],
"excludeFiles": ["**/packages/babel-helpers/src/helpers/**/*.js"],
"options": {
"trailingComma": "all"
"overrides": [
{
"files": [
"**/{codemods,eslint,packages}/*/{src,test}/**/*.{js,cjs,mjs,ts,cts,mts}"
],
"options": {
"trailingComma": "all"
}
},
{
"files": ["**/{codemods,eslint,packages}/*/test/fixtures/**/*.{js,ts}"],
"options": {
"trailingComma": "es5"
}
}
}, {
"files": "**/packages/babel-helpers/src/helpers/**/*.js",
"options": {
"trailingComma": "es5"
}
}, {
"files": [
"**/{codemods,eslint,packages}/*/test/fixtures/**/*.{js,ts}"
],
"options": {
"trailingComma": "es5"
}
}]
]
}
2 changes: 2 additions & 0 deletions eslint.config.js
Expand Up @@ -272,8 +272,10 @@ module.exports = [
"no-var": "off",
"comma-dangle": "off",
"no-func-assign": "off",
"prefer-spread": "off",
"import/no-extraneous-dependencies": "off",
"import/no-unresolved": "off",
"@typescript-eslint/prefer-optional-chain": "off",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoshuaKGoldberg If we set parserOptions.ecmaVersion to 5, will typescript-eslint respect the ecmaVersion and automatically turn off this rule?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea! No, rules in the preset configs won't be configured differently based on your parserOptions. I don't believe ESLint configs can be dynamic in this way. At least legacy ones aren't, and we haven't added official support for flat configs yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoshuaKGoldberg Thank you! I think this is not a very common use case, since most TS projects will use tsc to transpile ts sources anyway.

@liuxingbaoyu Can we set parserOptions.ecmaVersion to 5 for helpers? So the linter should throw if there are non-ES5 codes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parsing error: ImportDeclaration should appear when the mode is ES6 and in the module context.
I tried it and it didn't work.
Our current ts source actually has some es6 syntax such as trailingComma handled by terser which I think is fine.
I have now unified js with ts and explicitly specified terser as es5.

},
},
{
Expand Down
1 change: 1 addition & 0 deletions packages/babel-helpers/scripts/generate-helpers.js
Expand Up @@ -96,6 +96,7 @@ export default Object.freeze({
}).code;
code = (
await minify(code, {
ecma: 5,
mangle: {
keep_fnames: mangleFns ? new RegExp(noMangleFns.join("|")) : true,
},
Expand Down
15 changes: 15 additions & 0 deletions packages/babel-helpers/src/helpers-generated.ts
Expand Up @@ -63,11 +63,21 @@ export default Object.freeze({
"7.0.0-beta.0",
'import OverloadYield from"OverloadYield";export default function _awaitAsyncGenerator(e){return new OverloadYield(e,0)}',
),
// size: 366, gzip size: 187
callSuper: helper(
"7.23.8",
'import getPrototypeOf from"getPrototypeOf";import isNativeReflectConstruct from"isNativeReflectConstruct";import possibleConstructorReturn from"possibleConstructorReturn";export default function _callSuper(t,o,e){return o=getPrototypeOf(o),possibleConstructorReturn(t,isNativeReflectConstruct()?Reflect.construct(o,e||[],getPrototypeOf(t).constructor):o.apply(t,e))}',
),
// size: 161, gzip size: 154
checkInRHS: helper(
"7.20.5",
'export default function _checkInRHS(e){if(Object(e)!==e)throw TypeError("right-hand side of \'in\' should be an object, got "+(null!==e?typeof e:"null"));return e}',
),
// size: 327, gzip size: 200
construct: helper(
"7.0.0-beta.0",
'import setPrototypeOf from"setPrototypeOf";import isNativeReflectConstruct from"isNativeReflectConstruct";export default function _construct(t,e,r){if(isNativeReflectConstruct())return Reflect.construct.apply(null,arguments);var o=[null];o.push.apply(o,e);var p=new(t.bind.apply(t,o));return r&&setPrototypeOf(p,r.prototype),p}',
),
// size: 130, gzip size: 130
defineAccessor: helper(
"7.20.7",
Expand All @@ -88,6 +98,11 @@ export default Object.freeze({
"7.14.0",
'function _getRequireWildcardCache(e){if("function"!=typeof WeakMap)return null;var r=new WeakMap,t=new WeakMap;return(_getRequireWildcardCache=function(e){return e?t:r})(e)}export default function _interopRequireWildcard(e,r){if(!r&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var t=_getRequireWildcardCache(r);if(t&&t.has(e))return t.get(e);var n={__proto__:null},a=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var u in e)if("default"!==u&&Object.prototype.hasOwnProperty.call(e,u)){var i=a?Object.getOwnPropertyDescriptor(e,u):null;i&&(i.get||i.set)?Object.defineProperty(n,u,i):n[u]=e[u]}return n.default=e,t&&t.set(e,n),n}',
),
// size: 208, gzip size: 154
isNativeReflectConstruct: helper(
"7.9.0",
"export default function _isNativeReflectConstruct(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(t){}return(_isNativeReflectConstruct=function(){return!!t})()}",
),
// size: 431, gzip size: 303
iterableToArrayLimit: helper(
"7.0.0-beta.0",
Expand Down
137 changes: 43 additions & 94 deletions packages/babel-helpers/src/helpers.ts
Expand Up @@ -97,31 +97,33 @@ helpers.createClass = helper("7.0.0-beta.0")`
}
`;

helpers.defineEnumerableProperties = helper("7.0.0-beta.0")`
export default function _defineEnumerableProperties(obj, descs) {
for (var key in descs) {
var desc = descs[key];
desc.configurable = desc.enumerable = true;
if ("value" in desc) desc.writable = true;
Object.defineProperty(obj, key, desc);
}

// Symbols are not enumerated over by for-in loops. If native
// Symbols are available, fetch all of the descs object's own
// symbol properties and define them on our target object too.
if (Object.getOwnPropertySymbols) {
var objectSymbols = Object.getOwnPropertySymbols(descs);
for (var i = 0; i < objectSymbols.length; i++) {
var sym = objectSymbols[i];
var desc = descs[sym];
if (!process.env.BABEL_8_BREAKING) {
helpers.defineEnumerableProperties = helper("7.0.0-beta.0")`
export default function _defineEnumerableProperties(obj, descs) {
for (var key in descs) {
var desc = descs[key];
desc.configurable = desc.enumerable = true;
if ("value" in desc) desc.writable = true;
Object.defineProperty(obj, sym, desc);
Object.defineProperty(obj, key, desc);
}

// Symbols are not enumerated over by for-in loops. If native
// Symbols are available, fetch all of the descs object's own
// symbol properties and define them on our target object too.
if (Object.getOwnPropertySymbols) {
var objectSymbols = Object.getOwnPropertySymbols(descs);
for (var i = 0; i < objectSymbols.length; i++) {
var sym = objectSymbols[i];
var desc = descs[sym];
desc.configurable = desc.enumerable = true;
if ("value" in desc) desc.writable = true;
Object.defineProperty(obj, sym, desc);
}
}
return obj;
}
return obj;
}
`;
}

helpers.defaults = helper("7.0.0-beta.0")`
export default function _defaults(obj, defaults) {
Expand Down Expand Up @@ -257,60 +259,6 @@ helpers.setPrototypeOf = helper("7.0.0-beta.0")`
}
`;

helpers.isNativeReflectConstruct = helper("7.9.0")`
export default function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;

// core-js@3
if (Reflect.construct.sham) return false;

// Proxy can't be polyfilled. Every browser implemented
// proxies before or at the same time as Reflect.construct,
// so if they support Proxy they also support Reflect.construct.
if (typeof Proxy === "function") return true;

// Since Reflect.construct can't be properly polyfilled, some
// implementations (e.g. core-js@2) don't set the correct internal slots.
// Those polyfills don't allow us to subclass built-ins, so we need to
// use our fallback implementation.
try {
// If the internal slots aren't set, this throws an error similar to
// TypeError: this is not a Boolean object.

Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
return true;
} catch (e) {
return false;
}
}
`;

// need a bind because https://github.com/babel/babel/issues/14527
helpers.construct = helper("7.0.0-beta.0")`
import setPrototypeOf from "setPrototypeOf";
import isNativeReflectConstruct from "isNativeReflectConstruct";

export default function _construct(Parent, args, Class) {
if (isNativeReflectConstruct()) {
_construct = Reflect.construct.bind();
} else {
// NOTE: If Parent !== Class, the correct __proto__ is set *after*
// calling the constructor.
_construct = function _construct(Parent, args, Class) {
var a = [null];
a.push.apply(a, args);
var Constructor = Function.bind.apply(Parent, a);
var instance = new Constructor();
if (Class) setPrototypeOf(instance, Class.prototype);
return instance;
};
}
// Avoid issues with Class being present but undefined when it wasn't
// present in the original call.
return _construct.apply(null, arguments);
}
`;

helpers.isNativeFunction = helper("7.0.0-beta.0")`
export default function _isNativeFunction(fn) {
// Note: This function returns "true" for core-js functions.
Expand Down Expand Up @@ -455,28 +403,29 @@ helpers.possibleConstructorReturn = helper("7.0.0-beta.0")`
}
`;

// This is duplicated to packages/babel-plugin-transform-classes/src/inline-createSuper-helpers.js
helpers.createSuper = helper("7.9.0")`
import getPrototypeOf from "getPrototypeOf";
import isNativeReflectConstruct from "isNativeReflectConstruct";
import possibleConstructorReturn from "possibleConstructorReturn";

export default function _createSuper(Derived) {
var hasNativeReflectConstruct = isNativeReflectConstruct();

return function _createSuperInternal() {
var Super = getPrototypeOf(Derived), result;
if (hasNativeReflectConstruct) {
// NOTE: This doesn't work if this.__proto__.constructor has been modified.
var NewTarget = getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
if (!process.env.BABEL_8_BREAKING) {
helpers.createSuper = helper("7.9.0")`
import getPrototypeOf from "getPrototypeOf";
import isNativeReflectConstruct from "isNativeReflectConstruct";
import possibleConstructorReturn from "possibleConstructorReturn";

export default function _createSuper(Derived) {
var hasNativeReflectConstruct = isNativeReflectConstruct();

return function _createSuperInternal() {
var Super = getPrototypeOf(Derived), result;
if (hasNativeReflectConstruct) {
// NOTE: This doesn't work if this.__proto__.constructor has been modified.
var NewTarget = getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return possibleConstructorReturn(this, result);
}
return possibleConstructorReturn(this, result);
}
}
`;
`;
}

helpers.superPropBase = helper("7.0.0-beta.0")`
import getPrototypeOf from "getPrototypeOf";
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers/AsyncGenerator.js
Expand Up @@ -59,7 +59,7 @@ export default function AsyncGenerator(gen) {
},
function (err) {
resume("throw", err);
}
},
);
} catch (err) {
settle("throw", err);
Expand Down