-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Ignored catch parameter #17517
Ignored catch parameter #17517
Changes from 9 commits
b17bd97
99c662b
b917eb0
148a494
e349943
90ea10e
c65e5a1
de38ef7
58c8a2c
81fb3f7
dbdbb05
1926285
053b708
d5c24f3
f9e85ec
4f3e13a
3da1a53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -481,7 +481,7 @@ namespace ts { | |
Type, | ||
ResolvedBaseConstructorType, | ||
DeclaredType, | ||
ResolvedReturnType | ||
ResolvedReturnType, | ||
} | ||
|
||
const enum CheckMode { | ||
|
@@ -20885,6 +20885,17 @@ namespace ts { | |
} | ||
} | ||
} | ||
else if (/* !catchClause.variableDeclaration && */ languageVersion < ScriptTarget.ESNext) { | ||
const blockLocals = catchClause.block.locals; | ||
if (blockLocals) { | ||
forEachKey(blockLocals, caughtName => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a name? We can generate a name in the transforms. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed this block of change. Since we don't need check and error on a named variable anymore. |
||
if (caughtName === "_ignoredCatchParameter") { | ||
const localSymbol = blockLocals.get(caughtName); | ||
grammarErrorOnNode(localSymbol.valueDeclaration, Diagnostics.Duplicate_identifier_ignoredCatchParameter_Compiler_uses_the_parameter_declaration_ignoredCatchParameter_to_bind_ignored_catched_exceptions); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
checkBlock(catchClause.block); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2196,6 +2196,10 @@ | |
"category": "Error", | ||
"code": 2713 | ||
}, | ||
"Duplicate identifier '_ignoredCatchParameter'. Compiler uses the parameter declaration '_ignoredCatchParameter' to bind ignored catched exceptions.": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you still need this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed. |
||
"category": "Error", | ||
"code": 2714 | ||
}, | ||
|
||
"Import declaration '{0}' is using private name '{1}'.": { | ||
"category": "Error", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2131,10 +2131,12 @@ namespace ts { | |
function emitCatchClause(node: CatchClause) { | ||
const openParenPos = writeToken(SyntaxKind.CatchKeyword, node.pos); | ||
write(" "); | ||
writeToken(SyntaxKind.OpenParenToken, openParenPos); | ||
emit(node.variableDeclaration); | ||
writeToken(SyntaxKind.CloseParenToken, node.variableDeclaration ? node.variableDeclaration.end : openParenPos); | ||
write(" "); | ||
if (node.variableDeclaration) { | ||
writeToken(SyntaxKind.OpenParenToken, openParenPos); | ||
emit(node.variableDeclaration); | ||
writeToken(SyntaxKind.CloseParenToken, node.variableDeclaration ? node.variableDeclaration.end : openParenPos); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The conditional on this line is no longer required since you are checking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✔️ |
||
write(" "); | ||
} | ||
emit(node.block); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4778,11 +4778,12 @@ namespace ts { | |
function parseCatchClause(): CatchClause { | ||
const result = <CatchClause>createNode(SyntaxKind.CatchClause); | ||
parseExpected(SyntaxKind.CatchKeyword); | ||
if (parseExpected(SyntaxKind.OpenParenToken)) { | ||
|
||
if (parseOptional(SyntaxKind.OpenParenToken)) { | ||
result.variableDeclaration = parseVariableDeclaration(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conditionally setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✔️ |
||
parseExpected(SyntaxKind.CloseParenToken); | ||
} | ||
|
||
parseExpected(SyntaxKind.CloseParenToken); | ||
result.block = parseBlock(/*ignoreMissingOpenBrace*/ false); | ||
return finishNode(result); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3173,7 +3173,7 @@ namespace ts { | |
function visitCatchClause(node: CatchClause): CatchClause { | ||
const ancestorFacts = enterSubtree(HierarchyFacts.BlockScopeExcludes, HierarchyFacts.BlockScopeIncludes); | ||
let updated: CatchClause; | ||
if (isBindingPattern(node.variableDeclaration.name)) { | ||
if (node.variableDeclaration && isBindingPattern(node.variableDeclaration.name)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the time we get to the es2015 transform we must already have a variableDeclaration. Filling in the missing binding should be handled in the esnext transform. At best we should have a debug assertion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed! Debug.assert(!!node.variableDeclaration, "Catch clauses should always be present when downleveling ES2015 code."); |
||
const temp = createTempVariable(/*recordTempVariable*/ undefined); | ||
const newVariableDeclaration = createVariableDeclaration(temp); | ||
setTextRange(newVariableDeclaration, node.variableDeclaration); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,8 @@ namespace ts { | |
return visitExpressionStatement(node as ExpressionStatement); | ||
case SyntaxKind.ParenthesizedExpression: | ||
return visitParenthesizedExpression(node as ParenthesizedExpression, noDestructuringValue); | ||
case SyntaxKind.CatchClause: | ||
return visitCatchClause(node as CatchClause); | ||
default: | ||
return visitEachChild(node, visitor, context); | ||
} | ||
|
@@ -212,6 +214,13 @@ namespace ts { | |
return visitEachChild(node, noDestructuringValue ? visitorNoDestructuringValue : visitor, context); | ||
} | ||
|
||
function visitCatchClause(node: CatchClause): CatchClause { | ||
if (!node.variableDeclaration) { | ||
return updateCatchClause(node, createVariableDeclaration("_ignoredCatchParameter"), node.block); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you should either use createUniqueName or createTempVariable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
} | ||
return visitEachChild(node, visitor, context); | ||
} | ||
|
||
/** | ||
* Visits a BinaryExpression that contains a destructuring assignment. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1807,8 +1807,8 @@ namespace ts { | |
|
||
export interface CatchClause extends Node { | ||
kind: SyntaxKind.CatchClause; | ||
parent?: TryStatement; | ||
variableDeclaration: VariableDeclaration; | ||
parent?: TryStatement; // We parse missing try statements | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this comment mean? This property is present to reduce costs when walking up the AST. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It wasn't apparent to me that the |
||
variableDeclaration?: VariableDeclaration; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✔️ |
||
block: Block; | ||
} | ||
|
||
|
@@ -4001,7 +4001,6 @@ namespace ts { | |
ContainsBindingPattern = 1 << 23, | ||
ContainsYield = 1 << 24, | ||
ContainsHoistedDeclarationOrCompletion = 1 << 25, | ||
|
||
ContainsDynamicImport = 1 << 26, | ||
|
||
// Please leave this as 1 << 29. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//// [emitter.ignoredCatchParameter.esnext.ts] | ||
function fn() { | ||
try {} catch {} | ||
} | ||
|
||
|
||
//// [emitter.ignoredCatchParameter.esnext.js] | ||
function fn() { | ||
try { } | ||
catch { } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
=== tests/cases/conformance/emitter/esnext/noCatchParameter/emitter.ignoredCatchParameter.esnext.ts === | ||
function fn() { | ||
>fn : Symbol(fn, Decl(emitter.ignoredCatchParameter.esnext.ts, 0, 0)) | ||
|
||
try {} catch {} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
=== tests/cases/conformance/emitter/esnext/noCatchParameter/emitter.ignoredCatchParameter.esnext.ts === | ||
function fn() { | ||
>fn : () => void | ||
|
||
try {} catch {} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
//// [tryStatements.ts] | ||
function fn() { | ||
try { | ||
try { } catch { } | ||
|
||
} catch (x) { | ||
var x: any; | ||
} | ||
try { } catch (x) { var x: any; } | ||
|
||
try { } finally { } | ||
|
||
try { }catch(z){ } finally { } | ||
try { } catch { } finally { } | ||
|
||
try { } catch(z) { } finally { } | ||
} | ||
|
||
//// [tryStatements.js] | ||
function fn() { | ||
try { | ||
} | ||
try { } | ||
catch (_ignoredCatchParameter) { } | ||
try { } | ||
catch (x) { | ||
var x; | ||
} | ||
try { } | ||
finally { } | ||
try { } | ||
catch (_ignoredCatchParameter) { } | ||
finally { } | ||
try { } | ||
catch (z) { } | ||
finally { } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is unnecessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.