-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented try-catch support (#133)
Implemented visitors for visitTryStatement, visitThrowStatement, visitUnknownKeyword and visitAnyKeyword
- Loading branch information
Showing
4 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {connect, disconnect, rewriteRun, typeScript} from '../testHarness'; | ||
|
||
describe('throw mapping', () => { | ||
beforeAll(() => connect()); | ||
afterAll(() => disconnect()); | ||
|
||
test('simple throw', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
throw new Error("Cannot divide by zero!"); | ||
`) | ||
); | ||
}); | ||
|
||
test('simple throw with comments', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
/*a*/ throw /*b*/ new /*c*/ Error/*d*/(/*e*/'Cannot divide by zero!'/*f*/)/*g*/; | ||
`) | ||
); | ||
}); | ||
|
||
test('re-throwing', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
function riskyOperation() { | ||
try { | ||
throw new Error("An error occurred during risky operation."); | ||
} catch (error) { | ||
console.error("Logging Error:", (error as Error).message); | ||
throw error; // Re-throw the error to be handled at a higher level | ||
} | ||
} | ||
`) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import {connect, disconnect, rewriteRun, typeScript} from '../testHarness'; | ||
|
||
describe('try-catch mapping', () => { | ||
beforeAll(() => connect()); | ||
afterAll(() => disconnect()); | ||
|
||
test('try-catch empty', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
try { | ||
} catch (error) { | ||
} | ||
`) | ||
); | ||
}); | ||
|
||
test('try-finally empty', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
try { | ||
} finally { | ||
} | ||
`) | ||
); | ||
}); | ||
|
||
test('try-catch-finally empty', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
try { | ||
} catch (error) { | ||
} finally { | ||
} | ||
`) | ||
); | ||
}); | ||
|
||
test('try-catch-finally empty comments', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
/*a*/ try /*b*/ { | ||
} /*c*/ catch /*d*/ ( /*e*/ error /*f*/) { /*g*/ | ||
} /*h*/ finally /*i*/ { /*j*/ | ||
} /*k*/ | ||
`) | ||
); | ||
}); | ||
|
||
test('try-catch with typed unknown error', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
try { | ||
// error | ||
} catch (error: unknown) { | ||
// handel error | ||
} | ||
`) | ||
); | ||
}); | ||
|
||
test('try-catch with typed any error', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
try { | ||
// error | ||
} catch (error: any) { | ||
// handel error | ||
} | ||
`) | ||
); | ||
}); | ||
|
||
test('try-catch with typed error and comments', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
try { | ||
// error | ||
} catch (/*a*/ error /*b*/: /*c*/ unknown /*d*/) { | ||
// handel error | ||
} | ||
`) | ||
); | ||
}); | ||
|
||
test('try-catch-finally with body', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
try { | ||
// Try to parse JSON string | ||
const data = JSON.parse('{ "name": "Alice" }'); | ||
console.log(data.name); // Output: "Alice" | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
console.error("Caught an error:", error.message); | ||
} else { | ||
console.error("An unknown error occurred"); | ||
} | ||
} finally { | ||
console.log("Parsing attempt finished."); | ||
} | ||
`) | ||
); | ||
}); | ||
|
||
test('try-catch-finally with throw', () => { | ||
rewriteRun( | ||
//language=typescript | ||
typeScript(` | ||
try { | ||
throw new Error("Failed to perform database operation."); | ||
} catch (error) { | ||
console.error("Database Error:", (error as Error).message); | ||
} finally { | ||
console.log("Clean."); | ||
} | ||
`) | ||
); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters