Skip to content

Commit 54e6c5f

Browse files
authored
Merge pull request #1329 from gusthoff/topic/infrastructure/yarn/refactoring/20260322/small_fixes
Frontend: various small fixes to Typescript code
2 parents 51682a3 + d34bdaa commit 54e6c5f

File tree

8 files changed

+56
-33
lines changed

8 files changed

+56
-33
lines changed

frontend/py_modules/code_projects/check_code_block.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def set_versions():
7171
gnat_version = run("gnat", "--version").partition('\n')[0]
7272
gnat_prove_version = run("gnatprove", "--version").partition('\n')[0]
7373
gprbuild_version = run("gprbuild", "--version").partition('\n')[0]
74-
except:
74+
except Exception:
7575
gcc_version = "<unknown>" if gcc_version is None else gcc_version
7676
gnat_version = "<unknown>" if gnat_version is None else gnat_version
7777
gnat_prove_version = "<unknown>" if gnat_prove_version is None else gnat_prove_version
@@ -153,7 +153,7 @@ def cleanup_project(language, project_filename, main_file):
153153

154154
try:
155155
ref_block_check = checks.BlockCheck.from_json_file()
156-
except:
156+
except Exception:
157157
pass
158158

159159
if ref_block_check is not None and not force_checks:

frontend/src/ts/areas.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ export class LabArea extends Area {
174174

175175
this.button.addEventListener('click', () => {
176176
this.button.classList.toggle('active');
177-
if (this.container.style.display == '' ||
178-
this.container.style.display == 'block') {
177+
if (this.container.style.display === '' ||
178+
this.container.style.display === 'block') {
179179
this.container.style.display = 'none';
180180
} else {
181181
this.container.style.display = 'block';

frontend/src/ts/download.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ export function getLanguages(files: ResourceList): string {
9191
* @returns {UnparsedSwitches} switches.
9292
*/
9393
export function getUnparsedSwitches(rawSwitches: string): UnparsedSwitches {
94-
const parsed = JSON.parse(rawSwitches);
94+
let parsed;
95+
try {
96+
parsed = JSON.parse(rawSwitches);
97+
} catch {
98+
throw new Error(`Failed to parse switches JSON: ${rawSwitches}`);
99+
}
95100
const switches: UnparsedSwitches = {Builder: [], Compiler: []};
96101
for (const k in switches) {
97102
if (k in parsed) {
@@ -131,7 +136,7 @@ export function parseSwitches(switches: UnparsedSwitches): ParsedSwitches {
131136
* @returns {Array<string>} the filenames of each potential 'main' file.
132137
*/
133138
export function findMains(files: ResourceList): Array<string> {
134-
if (files.length == 1) return [files[0].basename];
139+
if (files.length === 1) return [files[0].basename];
135140
const mains: Array<string> = [];
136141
const fileNames = new Set(files.map((f) => f.basename));
137142
for (const f of files) {
@@ -158,7 +163,7 @@ export function findMains(files: ResourceList): Array<string> {
158163
* file extension removed.
159164
*/
160165
export function getMain(files: ResourceList, main: string): string {
161-
if (main == '') {
166+
if (main === '') {
162167
const potentialMains = findMains(files);
163168
if (potentialMains.length == 1) {
164169
main = potentialMains[0];
@@ -168,7 +173,7 @@ export function getMain(files: ResourceList, main: string): string {
168173
}
169174
}
170175
main = main.split('.')[0];
171-
if (main == '') return '';
176+
if (main === '') return '';
172177
return `for Main use ("${main}");`;
173178
}
174179

frontend/src/ts/sandbox-redirect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export function sandboxRedirect(): void {
1515
const cookieValue = cookies.get(cookieName) as string;
1616
const cookieReferenceValue = "true";
1717

18-
if (cookieValue != cookieReferenceValue) {
18+
if (cookieValue !== cookieReferenceValue) {
1919
const passw = prompt("Enter site password:")
2020

21-
if (passw != "Ada") {
21+
if (passw !== "Ada") {
2222
const msg = 'You have reached learn-sandbox, the learn testing site. ' +
2323
'This is reserved for testers only. You will be directed to the main ' +
2424
'learn.adacore.com site after pressing OK.';

frontend/src/ts/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class ServerWorker {
8484

8585
/**
8686
* Delay function
87-
* @param {number} ms - Number of milliseconds to delay-+
87+
* @param {number} ms - Number of milliseconds to delay
8888
* @returns {Promise<unknown>} - A promise to await
8989
*/
9090
public static delay(ms: number): Promise<unknown> {

frontend/src/ts/strings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const RELOAD_CONFIRM_MSG =
1212

1313
export const DOWNLOAD_TOOLTIP = 'Download source files';
1414
export const DOWNLOAD_MAINTENANCE =
15-
'The download functionilty is currently undergoing maintenance';
15+
'The download functionality is currently undergoing maintenance';
1616
// const SETTINGS_TOOLTIP = 'Modify settings for this editor';
1717

1818
export const SETTINGS_TABBED_EDITOR_LABEL =

frontend/src/ts/widget.ts

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Widget {
7676
// Parse files
7777
const files = getElemsByClass(this.container, 'file');
7878
// Check to make sure we have files in the widget
79-
if (files.length == 0) {
79+
if (files.length === 0) {
8080
throw Error('Malformed widget: No files present.');
8181
}
8282
for (const file of files) {
@@ -148,7 +148,7 @@ class Widget {
148148

149149
// Get tabbed view status from cookie
150150
const cookieTabbedView = cookies.get('tabbed_view') as string;
151-
const tabbedView = (cookieTabbedView != 'false');
151+
const tabbedView = (cookieTabbedView !== 'false');
152152

153153
// attach handlers to the settings bar items
154154
const tabSetting =
@@ -184,7 +184,7 @@ class Widget {
184184
const themeSetting =
185185
this.getElem('settings-bar', 'theme-setting') as HTMLInputElement;
186186
// Set checkbox according to value from cookie
187-
themeSetting.checked = (cookieTheme == 'dark');
187+
themeSetting.checked = (cookieTheme === 'dark');
188188
this.setTheme(cookieTheme);
189189

190190
themeSetting.addEventListener('change', () => {
@@ -220,7 +220,13 @@ class Widget {
220220
dlButton.addEventListener('click', async () => {
221221
this.outputArea.reset();
222222
const files = this.collectResources();
223-
const switches = JSON.parse(this.container.dataset.switches as string);
223+
let switches;
224+
try {
225+
switches = JSON.parse(this.container.dataset.switches as string);
226+
} catch {
227+
this.outputArea.addError(Strings.INTERNAL_ERROR_MESSAGE);
228+
return;
229+
}
224230
const activeSwitches: UnparsedSwitches = {
225231
Builder: switches['Builder'],
226232
Compiler: this.getActiveCompilerSwitches()};
@@ -380,18 +386,22 @@ class Widget {
380386
'compiler-switch-help-info')[0];
381387
d.addEventListener('click', () => {
382388
if (! d.classList.contains('disabled')) {
383-
d.innerHTML = '';
389+
d.textContent = '';
384390
d.classList.add('disabled');
385391
}
386392
});
387393

388394
b.addEventListener('click', () => {
389-
d.innerHTML = '<b>' + switchName + '</b>: ' +
390-
b.title + '<br/>' +
391-
'<div class="compiler-switch-help-info-click-remove">(' +
392-
Strings.COMPILER_SWITCH_REMOVE_HELP_MESSAGE +
393-
')</div>';
394-
395+
d.textContent = '';
396+
const bold = document.createElement('b');
397+
bold.textContent = switchName;
398+
d.appendChild(bold);
399+
d.appendChild(document.createTextNode(': ' + b.title));
400+
d.appendChild(document.createElement('br'));
401+
const helpDiv = document.createElement('div');
402+
helpDiv.classList.add('compiler-switch-help-info-click-remove');
403+
helpDiv.textContent = '(' + Strings.COMPILER_SWITCH_REMOVE_HELP_MESSAGE + ')';
404+
d.appendChild(helpDiv);
395405
d.classList.remove('disabled');
396406
});
397407
}
@@ -403,7 +413,7 @@ class Widget {
403413
*/
404414
private setTheme(themeStr : string) : void {
405415
let theme = EditorTheme.Light;
406-
if (themeStr == 'dark') {
416+
if (themeStr === 'dark') {
407417
theme = EditorTheme.Dark;
408418
}
409419

@@ -474,7 +484,14 @@ class Widget {
474484

475485
const files = this.collectResources();
476486

477-
const switches = JSON.parse(this.container.dataset.switches as string);
487+
let switches;
488+
try {
489+
switches = JSON.parse(this.container.dataset.switches as string);
490+
} catch {
491+
this.outputArea.addError(Strings.INTERNAL_ERROR_MESSAGE);
492+
this.outputArea.showSpinner(false);
493+
return;
494+
}
478495
switches['Compiler'] = this.getActiveCompilerSwitches();
479496

480497
const serverData: RunProgram.TSData = {
@@ -555,7 +572,7 @@ class Widget {
555572

556573
// Lines that contain a sloc are clickable:
557574
const cb = (): void => {
558-
if (window.getSelection()?.toString() == '') {
575+
if (window.getSelection()?.toString() === '') {
559576
view.header.scrollIntoView(true);
560577
view.header.click();
561578
// Jump to corresponding line
@@ -567,7 +584,7 @@ class Widget {
567584

568585
// If the message if of type info, addInfo
569586
// Otherwise, addMsg
570-
if (msgType == 'info') {
587+
if (msgType === 'info') {
571588
homeArea.addInfo(outMsg, cb);
572589
} else {
573590
homeArea.addMsg(outMsg, cb);
@@ -594,7 +611,7 @@ class Widget {
594611
}
595612

596613
if (data.completed) {
597-
if (data.status != 0) {
614+
if (data.status !== 0) {
598615
this.outputArea.addError(Strings.EXIT_STATUS_LABEL +
599616
': ' + data.status);
600617
}
@@ -716,13 +733,14 @@ export function widgetFactory(widgets: Array<HTMLDivElement>): void {
716733
console.error('Error:', error);
717734

718735
// clear the offending element to remove any processing that was done
719-
element.innerHTML = '';
736+
element.textContent = '';
720737

721738
// add an error message to the page in its place
722739
const errorDiv = document.createElement('div');
723-
errorDiv.innerHTML = '<p>An error has occured processing this widget.' +
724-
Strings.INTERNAL_ERROR_MESSAGE + '</p>';
725-
740+
const errorP = document.createElement('p');
741+
errorP.textContent = 'An error has occured processing this widget.' +
742+
Strings.INTERNAL_ERROR_MESSAGE;
743+
errorDiv.appendChild(errorP);
726744
element.appendChild(errorDiv);
727745
}
728746
}

frontend/tests/ts/dom-utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('getElemsByTag', () => {
5959
c.classList.add('diff-class');
6060
parent.appendChild(c);
6161

62-
it('should return an array with the elems that have the class', () => {
62+
it('should return an array with the elems that have the tag', () => {
6363
const elems = getElemsByTag(parent, 'button');
6464
expect(elems).to.have.length(2);
6565
});

0 commit comments

Comments
 (0)