Skip to content

Commit e4d7e9d

Browse files
committed
fix: Do not autofix global assignments
1 parent fba878c commit e4d7e9d

23 files changed

+424
-169
lines changed

src/linter/ui5Types/SourceFileLinter.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getPropertyAssignmentsInObjectLiteralExpression,
1414
findClassMember,
1515
isClassMethod,
16+
isGlobalAssignment,
1617
} from "./utils/utils.js";
1718
import {taskStart} from "../../utils/perf.js";
1819
import {getPositionsForNode} from "../../utils/nodePosition.js";
@@ -1607,15 +1608,17 @@ export default class SourceFileLinter {
16071608
!((ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) &&
16081609
this.isAllowedPropertyAccess(node))) {
16091610
const namespace = this.extractNamespace((node as ts.PropertyAccessExpression));
1610-
const {moduleName, exportName, propertyAccess} = this.getImportFromGlobal(namespace);
1611+
1612+
const fixable = ts.isCallExpression(node) || !isGlobalAssignment(node);
1613+
let fixHints = {};
1614+
if (fixable) {
1615+
fixHints = this.getImportFromGlobal(namespace);
1616+
}
1617+
16111618
this.#reporter.addMessage(MESSAGE.NO_GLOBALS, {
16121619
variableName: symbol.getName(),
16131620
namespace,
1614-
fixHints: {
1615-
moduleName,
1616-
exportName,
1617-
propertyAccess, // TODO: Provide end position instead?
1618-
},
1621+
fixHints,
16191622
}, node);
16201623
}
16211624
}

src/linter/ui5Types/utils/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,18 @@ export function resolveUniqueName(inputName: string, existingIdentifiers?: Set<s
195195

196196
return getUniqueName(Array.from(existingIdentifiers ?? []), name ?? inputName, "/");
197197
}
198+
199+
export function isGlobalAssignment(node: ts.AccessExpression): boolean {
200+
let currentNode: ts.Node | undefined = node;
201+
while (ts.isPropertyAccessExpression(currentNode) || ts.isElementAccessExpression(currentNode)) {
202+
if (!currentNode.parent) {
203+
return false;
204+
}
205+
if (ts.isBinaryExpression(currentNode.parent)) {
206+
return currentNode.parent.left === currentNode &&
207+
currentNode.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken;
208+
}
209+
currentNode = currentNode.parent;
210+
}
211+
return false;
212+
}

test/e2e/snapshots/compare-ui5lint-fix-snapshots.ts.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,63 +92,63 @@ Generated by [AVA](https://avajs.dev).
9292
},
9393
{
9494
column: 4,
95-
line: 21,
95+
line: 25,
9696
message: 'Access of global variable \'sap\' (sap.ui.view)',
9797
ruleId: 'no-globals',
9898
severity: 2,
9999
},
100100
{
101101
column: 11,
102-
line: 21,
102+
line: 25,
103103
message: 'Call to deprecated function \'view\' (sap.ui.view)',
104104
ruleId: 'no-deprecated-api',
105105
severity: 2,
106106
},
107107
{
108108
column: 29,
109-
line: 27,
109+
line: 31,
110110
message: 'Call to deprecated function \'attachTap\' of class \'Button\'',
111111
ruleId: 'no-deprecated-api',
112112
severity: 2,
113113
},
114114
{
115115
column: 39,
116-
line: 30,
116+
line: 34,
117117
message: 'Call to deprecated function \'attachTap\' of class \'Button\'',
118118
ruleId: 'no-deprecated-api',
119119
severity: 2,
120120
},
121121
{
122122
column: 15,
123-
line: 38,
123+
line: 42,
124124
message: 'Call to deprecated function \'getBlocked\' (testButton.getBlocked)',
125125
ruleId: 'no-deprecated-api',
126126
severity: 2,
127127
},
128128
{
129129
column: 16,
130-
line: 41,
130+
line: 45,
131131
message: 'Call to deprecated function \'attachTap\' of class \'Button\'',
132132
ruleId: 'no-deprecated-api',
133133
severity: 2,
134134
},
135135
{
136136
column: 25,
137-
line: 47,
137+
line: 51,
138138
message: 'Call to deprecated function \'prop\' of class \'UI5Element\'',
139139
ruleId: 'no-deprecated-api',
140140
severity: 2,
141141
},
142142
{
143143
column: 35,
144-
line: 48,
144+
line: 52,
145145
message: 'Call to deprecated function \'prop\' of class \'UI5Element\'',
146146
ruleId: 'no-deprecated-api',
147147
severity: 2,
148148
},
149149
{
150150
column: 28,
151-
line: 57,
151+
line: 61,
152152
message: 'Call to deprecated function \'attachTap\' of class \'Button\'',
153153
ruleId: 'no-deprecated-api',
154154
severity: 2,
@@ -1743,7 +1743,7 @@ Generated by [AVA](https://avajs.dev).
17431743
> Snapshot 40
17441744
17451745
`Main.controller.js:␊
1746-
sap.ui.define(["./BaseController", "sap/m/MessageBox", "sap/m/Button", "sap/ui/core/library", "sap/m/library"], function (BaseController, MessageBox, Button, coreLibrary, mLibrary) {␊
1746+
sap.ui.define(["./BaseController", "sap/m/MessageBox", "sap/m/Button", "sap/ui/unified/FileUploader", "sap/ui/core/library", "sap/m/library"], function (BaseController, MessageBox, Button, FileUploader, coreLibrary, mLibrary) {␊
17471747
"use strict";␊
17481748
17491749
const MainController = BaseController.extend("com.ui5.troublesome.app.controller.Main", {␊
@@ -1760,9 +1760,13 @@ Generated by [AVA](https://avajs.dev).
17601760
const button3 = new Button({␊
17611761
text: "Hello"␊
17621762
});␊
1763-
coreLibrary.ValueState.Success;␊
1763+
const fileUploader = new FileUploader({␊
1764+
valueState: coreLibrary.ValueState.Success␊
1765+
});␊
17641766
const core = coreLibrary;␊
1765-
core.ValueState.Success;␊
1767+
const fileUploader2 = new FileUploader({␊
1768+
valueState: core.ValueState.Success␊
1769+
});␊
17661770
sap.ui.view("myView");␊
17671771
mLibrary.URLHelper.triggerSms();␊
17681772
},␊
41 Bytes
Binary file not shown.

test/e2e/snapshots/compare-ui5lint-snapshots.ts.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Generated by [AVA](https://avajs.dev).
6565
warningCount: 0,
6666
},
6767
{
68-
errorCount: 17,
68+
errorCount: 19,
6969
fatalErrorCount: 0,
7070
filePath: 'webapp/controller/Main.controller.js',
7171
messages: [
@@ -105,85 +105,99 @@ Generated by [AVA](https://avajs.dev).
105105
severity: 2,
106106
},
107107
{
108-
column: 4,
108+
column: 29,
109109
line: 18,
110-
message: 'Access of global variable \'sap\' (sap.ui.core.ValueState.Success)',
110+
message: 'Access of global variable \'sap\' (sap.ui.unified.FileUploader)',
111111
ruleId: 'no-globals',
112112
severity: 2,
113113
},
114114
{
115115
column: 17,
116116
line: 19,
117+
message: 'Access of global variable \'sap\' (sap.ui.core.ValueState.Success)',
118+
ruleId: 'no-globals',
119+
severity: 2,
120+
},
121+
{
122+
column: 17,
123+
line: 21,
117124
message: 'Access of global variable \'sap\' (sap.ui.core)',
118125
ruleId: 'no-globals',
119126
severity: 2,
120127
},
128+
{
129+
column: 30,
130+
line: 22,
131+
message: 'Access of global variable \'sap\' (sap.ui.unified.FileUploader)',
132+
ruleId: 'no-globals',
133+
severity: 2,
134+
},
121135
{
122136
column: 4,
123-
line: 21,
137+
line: 25,
124138
message: 'Access of global variable \'sap\' (sap.ui.view)',
125139
ruleId: 'no-globals',
126140
severity: 2,
127141
},
128142
{
129143
column: 11,
130-
line: 21,
144+
line: 25,
131145
message: 'Call to deprecated function \'view\' (sap.ui.view)',
132146
ruleId: 'no-deprecated-api',
133147
severity: 2,
134148
},
135149
{
136150
column: 4,
137-
line: 22,
151+
line: 26,
138152
message: 'Access of global variable \'sap\' (sap.m.URLHelper.triggerSms)',
139153
ruleId: 'no-globals',
140154
severity: 2,
141155
},
142156
{
143157
column: 29,
144-
line: 27,
158+
line: 31,
145159
message: 'Call to deprecated function \'attachTap\' of class \'Button\'',
146160
ruleId: 'no-deprecated-api',
147161
severity: 2,
148162
},
149163
{
150164
column: 39,
151-
line: 30,
165+
line: 34,
152166
message: 'Call to deprecated function \'attachTap\' of class \'Button\'',
153167
ruleId: 'no-deprecated-api',
154168
severity: 2,
155169
},
156170
{
157171
column: 15,
158-
line: 38,
172+
line: 42,
159173
message: 'Call to deprecated function \'getBlocked\' (testButton.getBlocked)',
160174
ruleId: 'no-deprecated-api',
161175
severity: 2,
162176
},
163177
{
164178
column: 16,
165-
line: 41,
179+
line: 45,
166180
message: 'Call to deprecated function \'attachTap\' of class \'Button\'',
167181
ruleId: 'no-deprecated-api',
168182
severity: 2,
169183
},
170184
{
171185
column: 25,
172-
line: 47,
186+
line: 51,
173187
message: 'Call to deprecated function \'prop\' of class \'UI5Element\'',
174188
ruleId: 'no-deprecated-api',
175189
severity: 2,
176190
},
177191
{
178192
column: 35,
179-
line: 48,
193+
line: 52,
180194
message: 'Call to deprecated function \'prop\' of class \'UI5Element\'',
181195
ruleId: 'no-deprecated-api',
182196
severity: 2,
183197
},
184198
{
185199
column: 28,
186-
line: 57,
200+
line: 61,
187201
message: 'Call to deprecated function \'attachTap\' of class \'Button\'',
188202
ruleId: 'no-deprecated-api',
189203
severity: 2,
117 Bytes
Binary file not shown.

test/fixtures/autofix/GlobalsExistingDefineTooManyArgs.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ sap.ui.define([], function () {
77
const button3 = new window.sap.m.Button({
88
text: "Hello",
99
});
10-
sap.ui.core.ValueState.Success;
10+
const fileUploader = new sap.ui.unified.FileUploader({
11+
valueState: sap.ui.core.ValueState.Success
12+
});
1113
const core = sap.ui.core;
12-
core.ValueState.Success;
14+
const fileUploader2 = new sap.ui.unified.FileUploader({
15+
valueState: core.ValueState.Success
16+
});
1317
sap.ui.view("myView");
1418
sap.m.URLHelper.triggerSms();
1519
}, true, true, true);

test/fixtures/autofix/GlobalsExistingDefineWithDeps.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ sap.ui.define(["sap/m/Button", "sap/m/library", "sap/m/Avatar", "sap/m/ComboBox"
1212
const button3 = new window.sap.m.Button({
1313
text: "Hello"
1414
});
15-
sap.ui.core.ValueState.Success;
15+
const fileUploader = new sap.ui.unified.FileUploader({
16+
valueState: sap.ui.core.ValueState.Success
17+
});
1618
const core = sap.ui.core;
17-
core.ValueState.Success;
19+
const fileUploader2 = new sap.ui.unified.FileUploader({
20+
valueState: core.ValueState.Success
21+
});
1822
sap.ui.view("myView");
1923
sap.m.URLHelper.triggerSms();
2024

test/fixtures/autofix/GlobalsExistingDefineWithDeps_ArrowFunction.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ sap.ui.define(["sap/m/Button", "sap/m/Avatar", "sap/m/ComboBox"], (ButtonRenamed
1010
const button3 = new window.sap.m.Button({
1111
text: "Hello"
1212
});
13-
sap.ui.core.ValueState.Success;
13+
const fileUploader = new sap.ui.unified.FileUploader({
14+
valueState: sap.ui.core.ValueState.Success
15+
});
1416
const core = sap.ui.core;
15-
core.ValueState.Success;
17+
const fileUploader2 = new sap.ui.unified.FileUploader({
18+
valueState: core.ValueState.Success
19+
});
1620
sap.ui.view("myView");
1721
sap.m.URLHelper.triggerSms();
18-
22+
1923
sap.ui.require(["sap/m/Dialog", "sap/m/MessageToast", "sap/f/library"], function(Dialog, MessageToast, fLib) {
2024
sap.f.AvatarType.Icon;
2125
fLib.AvatarType.Image;

test/fixtures/autofix/GlobalsExistingDefineWithDeps_FunctionExpression.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ const factoryFn = function(ButtonRenamed) {
1010
const button3 = new window.sap.m.Button({
1111
text: "Hello"
1212
});
13-
sap.ui.core.ValueState.Success;
13+
const fileUploader = new sap.ui.unified.FileUploader({
14+
valueState: sap.ui.core.ValueState.Success
15+
});
1416
const core = sap.ui.core;
15-
core.ValueState.Success;
17+
const fileUploader2 = new sap.ui.unified.FileUploader({
18+
valueState: core.ValueState.Success
19+
});
1620
sap.ui.view("myView");
1721
sap.m.URLHelper.triggerSms();
18-
22+
1923
sap.ui.require(["sap/m/Dialog", "sap/m/MessageToast", "sap/f/library"], function(Dialog, MessageToast, fLib) {
2024
sap.f.AvatarType.Icon;
2125
fLib.AvatarType.Image;

0 commit comments

Comments
 (0)