Skip to content

Commit

Permalink
fix(amdTranspiler): Fix substiute for sap.ui.require errbacks
Browse files Browse the repository at this point in the history
If the error callback is a function expression without a name, generate
a name since the generated function definition must have one.
  • Loading branch information
RandomByte committed Apr 12, 2024
1 parent bf0cefd commit c52b53f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function transformAsyncRequireCall(
errbackDeclaration = nodeFactory.createFunctionDeclaration(
errback.modifiers,
errback.asteriskToken,
errback.name,
errback.name ?? nodeFactory.createUniqueName("extracted_require_errback"),
errback.typeParameters,
errback.parameters,
errback.type,
Expand Down
4 changes: 2 additions & 2 deletions src/linter/ui5Types/amdTranspiler/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ export default function transpileAmdToEsm(fileName: string, content: string, str
throw err;
}
if (err instanceof UnsupportedModuleError) {
log.verbose(`Failed to transform module: ${err.message}`);
log.verbose(`Failed to transform module ${fileName}: ${err.message}`);
if (err.stack && log.isLevelEnabled("verbose")) {
log.verbose(`Stack trace:`);
log.verbose(err.stack);
}
return {source: content, map: ""};
} else if (err instanceof Error && err.message.startsWith("Debug Failure")) {
// We probably failed to create a valid AST
log.verbose(`AST transformation failed for module: ${err.message}`);
log.verbose(`AST transformation failed for module ${fileName}: ${err.message}`);
if (err.stack && log.isLevelEnabled("verbose")) {
log.verbose(`Stack trace:`);
log.verbose(err.stack);
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/transpiler/amd/Require_Errback_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sap.ui.require(["my/module"], (Module) => {
return Module;
}, function (err) {
console.log(err);
});

var err = function (err) {
console.log(err);
};

sap.ui.require(["my/module"], (Module) => {
return Module;
}, err);
38 changes: 37 additions & 1 deletion test/lib/linter/amdTranspiler/snapshots/transpiler.ts.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Snapshot report for `test/lib/linter/js/transpiler.ts`
# Snapshot report for `test/lib/linter/amdTranspiler/transpiler.ts`

The actual snapshot is saved in `transpiler.ts.snap`.

Expand Down Expand Up @@ -1069,6 +1069,42 @@ Generated by [AVA](https://avajs.dev).
version: 3,
}

## Transpile Require_Errback_2.js

> Snapshot 1
`import Module from "my/module";␊
import Module from "my/module";␊
((Module) => {␊
return Module;␊
})(Module);␊
var err = function (err) {␊
console.log(err);␊
};␊
((Module) => {␊
return Module;␊
})(Module);␊
function extracted_require_errback_1(err) {␊
console.log(err);␊
}␊
function extracted_require_errback_2(err) {␊
console.log(err);␊
}␊
//# sourceMappingURL=Require_Errback_2.js.map`

> Snapshot 2
{
file: 'Require_Errback_2.js',
mappings: 'OAA+B,MAAM,MAArB,WAAW;OAUI,MAAM,MAArB,WAAW;AAVG,CAAA,CAAC,MAAM,EAAE,EAAE;IACxC,OAAO,MAAM,CAAC;AACf,CAAC,CAAA,CAF8B,MAAM,EAIlC;AAEH,IAAI,GAAG,GAAG,UAAU,GAAG;IACtB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAClB,CAAC,CAAC;AAE4B,CAAA,CAAC,MAAM,EAAE,EAAE;IACxC,OAAO,MAAM,CAAC;AACf,CAAC,CAAA,CAF8B,MAAM,EAE7B;qCAVK,GAAG;IACf,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAClB,CAAC;qCAEmB,GAAG;IACtB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAClB,CAAC',
names: [],
sourceRoot: '',
sources: [
'Require_Errback_2.js',
],
version: 3,
}

## Transpile Require_Import.js

> Snapshot 1
Expand Down
Binary file modified test/lib/linter/amdTranspiler/snapshots/transpiler.ts.snap
Binary file not shown.

0 comments on commit c52b53f

Please sign in to comment.