babel-plugin-transform-catch-scoping
v1.0.0
Published
A Babel plugin that fix jscript catch scoping.
Maintainers
Readme
babel-plugin-transform-catch-scoping
A Babel plugin that fixes the JScript (IE6/7/8) catch scope bug by automatically renaming conflicting catch parameters and registering them in the enclosing function scope.
When to Use This Plugin?
Recommended Workflow
| Scenario | Solution | Advantages |
|---------------------------|-----------------------------------|-------------------------------------|
| Production builds | Terser with ie8: true | Smaller bundle + IE8 fixes |
| Development debugging | This plugin | Readable code with IE8 compatibility|
⚠️ Note: Terser's ie8 option already handles catch scope renaming in production builds. Use this plugin only if you need uncompressed IE8-compatible code during development.
The Problem
In IE6/7/8 (JScript), the catch parameter does not have block scope — it leaks into the enclosing function scope, shadowing any variable with the same name:
function () {
var e = '123';
try {
throw new Error();
} catch (e) {
console.error(e);
}
console.log(e); // IE8: Error object! Modern browsers: '123'
}What This Plugin Does
This plugin detects when a catch parameter name conflicts with a binding or reference in the enclosing function scope, and renames the parameter to avoid the conflict:
// Before
function () {
var e = '123';
try { } catch (e) { console.error(e); }
console.log(e);
}
// After
function () {
var e = '123';
try { } catch (_e) { console.error(_e); }
console.log(e);
}It also registers the catch parameter name in the function scope so that other plugins (like @babel/plugin-transform-block-scoping) will avoid generating variables with the same name.
Installation
npm install --save-dev babel-plugin-transform-catch-scopingUsage
Add to your Babel configuration:
{
"plugins": ["babel-plugin-transform-catch-scoping"]
}Recommended Setup for IE8 Compatibility
This plugin should be placed last in the plugins array so that it can detect all variable names generated by preceding plugins and avoid conflicts:
{
"plugins": [
"babel-plugin-anonymize-function-expression",
"@babel/plugin-transform-jscript",
"babel-plugin-transform-catch-scoping"
]
}License
MIT
