Skip to content

Commit 3f3c720

Browse files
committed
fix(autofix): Prevent invalid delete expressions
Check for usage of "delete" keyword with the module export, which is not fixable as only an identifier would remain after the autofix, which would not be valid. Fixes: #668
1 parent 22bbfe3 commit 3f3c720

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

src/linter/ui5Types/fixHints/GlobalsFixHintsGenerator.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ export default class GlobalsFixHintsGenerator {
3232
return undefined;
3333
}
3434

35+
// Check for usage of "delete" keyword with the module export, which is not fixable
36+
// as only an identifier would remain after the autofix, which would not be valid.
37+
if (
38+
ts.isDeleteExpression(propertyAccessNode.parent) &&
39+
propertyAccessNode.parent.expression === propertyAccessNode
40+
) {
41+
return undefined;
42+
}
43+
3544
// Check whether the access is conditional / probing / lazy
3645
fixHints.conditional = isConditionalAccess(propertyAccessNode);
3746

test/fixtures/autofix/GlobalsConditionalAccess.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ sap.ui.define([], function() {
2727
console.log("Button prototype has someMethod");
2828
}
2929

30-
// // Should be replaced, as there is no lazy dependency to Button in this module
30+
// Should be replaced, as there is no lazy dependency to Button in this module
3131
const oButton = new sap.m.Button();
3232

3333
});

test/fixtures/autofix/GlobalsNotFixable.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ sap.ui.define([], function() {
1010
}
1111
};
1212

13+
// Delete can't be fixed in case the module export is deleted
14+
delete sap.ushell.Container;
15+
16+
// Deleting an inner property of a global module access can be fixed
17+
delete sap.ushell.Container.getService;
18+
1319
});

test/lib/autofix/snapshots/autofix.fixtures.ts.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Generated by [AVA](https://avajs.dev).
122122
console.log("Button prototype has someMethod");␊
123123
}␊
124124
125-
// // Should be replaced, as there is no lazy dependency to Button in this module␊
125+
// Should be replaced, as there is no lazy dependency to Button in this module␊
126126
const oButton = new Button();␊
127127
128128
});␊
@@ -2158,7 +2158,7 @@ Generated by [AVA](https://avajs.dev).
21582158
[
21592159
{
21602160
coverageInfo: [],
2161-
errorCount: 2,
2161+
errorCount: 3,
21622162
fatalErrorCount: 0,
21632163
filePath: 'GlobalsNotFixable.js',
21642164
messages: [
@@ -2178,11 +2178,42 @@ Generated by [AVA](https://avajs.dev).
21782178
ruleId: 'no-globals',
21792179
severity: 2,
21802180
},
2181+
{
2182+
column: 9,
2183+
line: 14,
2184+
message: 'Access of global variable \'sap\' (sap.ushell.Container)',
2185+
messageDetails: 'Do not use global variables to access UI5 modules or APIs. See Best Practices for Developers (https://ui5.sap.com/#/topic/28fcd55b04654977b63dacbee0552712)',
2186+
ruleId: 'no-globals',
2187+
severity: 2,
2188+
},
21812189
],
21822190
warningCount: 0,
21832191
},
21842192
]
21852193

2194+
> AutofixResult: /GlobalsNotFixable.js
2195+
2196+
`// Assignments to global variables should not be fixed/replaced␊
2197+
sap.ui.define(["sap/ushell/Container"], function(Container) {␊
2198+
2199+
// Declaring globals might appear in tests␊
2200+
window.sap.ushell = {};␊
2201+
// Assignments can be with or without window prefix␊
2202+
sap.ushell.Container = {␊
2203+
getService: function() {␊
2204+
return {};␊
2205+
}␊
2206+
};␊
2207+
2208+
// Delete can't be fixed in case the module export is deleted␊
2209+
delete sap.ushell.Container;␊
2210+
2211+
// Deleting an inner property of a global module access can be fixed␊
2212+
delete Container.getService;␊
2213+
2214+
});␊
2215+
`
2216+
21862217
## General: GlobalsOutsideDefine.js
21872218

21882219
> Snapshot 1
257 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)