Files
compiler-explorer/static/panes/stack-usage-view.ts
Matt Godbolt 03d20c5fde Move assert.ts and rison.ts to shared/ directory (#8246)
## Summary
Moves `static/assert.ts` and `static/rison.ts` to `shared/` directory to
make them available to both frontend and backend code without browser
dependencies. Updates all import paths across the codebase (~47 files).

## Motivation
This refactoring eliminates browser dependencies in these utilities,
allowing them to be imported by Node.js contexts (like Cypress test
files) without causing module load failures. This is a prerequisite for
upcoming Cypress test improvements.

## Changes
- Move `static/assert.ts` → `shared/assert.ts`
- Move `static/rison.ts` → `shared/rison.ts`  
- Update `biome.json` to allow `hasOwnProperty` in `shared/` directory
- Update all imports across `static/`, `lib/`, and `test/` directories
(47 files changed)

## Benefits
- No functional changes, purely a code reorganization
- Makes these utilities accessible to both frontend and backend without
circular dependencies
- Enables future Cypress improvements that require these utilities in
Node.js context
- All tests pass ✓ (699 tests)

## Test Plan
- [x] TypeScript compilation passes
- [x] Linting passes
- [x] All unit tests pass (699 tests)
- [x] Pre-commit hooks pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-04 10:58:11 -06:00

200 lines
7.9 KiB
TypeScript

// Copyright (c) 2023, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import {Container} from 'golden-layout';
import $ from 'jquery';
import * as monaco from 'monaco-editor';
import _ from 'underscore';
import {unwrap} from '../../shared/assert.js';
import {CompilationResult} from '../../types/compilation/compilation.interfaces.js';
import {CompilerInfo} from '../../types/compiler.interfaces.js';
import {Hub} from '../hub.js';
import {extendConfig} from '../monaco-config.js';
import {SentryCapture} from '../sentry.js';
import {MonacoPaneState} from './pane.interfaces.js';
import {MonacoPane} from './pane.js';
import {StackUsageState, suCodeEntry} from './stack-usage-view.interfaces.js';
type SuClass = 'None' | 'static' | 'dynamic' | 'dynamic,bounded';
type SuViewLine = {
text: string;
srcLine: number;
suClass: SuClass;
};
export class StackUsage extends MonacoPane<monaco.editor.IStandaloneCodeEditor, StackUsageState> {
// Note: bool | undef here instead of just bool because of an issue with field initialization order
isCompilerSupported?: boolean;
constructor(hub: Hub, container: Container, state: StackUsageState & MonacoPaneState) {
super(hub, container, state);
if (state.suOutput) {
this.showStackUsageResults(state.suOutput, state.source);
}
this.eventHub.emit('stackUsageViewOpened', this.compilerInfo.compilerId);
}
override getInitialHTML(): string {
return $('#stackusage').html();
}
override createEditor(editorRoot: HTMLElement): void {
this.editor = monaco.editor.create(
editorRoot,
extendConfig({
language: 'plaintext',
readOnly: true,
glyphMargin: true,
}),
);
}
override registerCallbacks() {
this.eventHub.emit('requestSettings');
this.eventHub.emit('findCompilers');
this.container.on('shown', this.resize, this);
const cursorSelectionThrottledFunction = _.throttle(this.onDidChangeCursorSelection.bind(this), 500);
this.editor.onDidChangeCursorSelection(e => {
cursorSelectionThrottledFunction(e);
});
}
override onCompileResult(id: number, compiler: CompilerInfo, result: CompilationResult) {
if (this.compilerInfo.compilerId !== id || !this.isCompilerSupported) return;
const source = unwrap(result.source);
this.editor.setValue(source);
if (result.stackUsageOutput) {
this.showStackUsageResults(result.stackUsageOutput, source);
}
// TODO: This is inelegant again. Previously took advantage of fourth argument for the compileResult event.
const lang = compiler.lang === 'c++' ? 'cpp' : compiler.lang;
const model = this.editor.getModel();
if (model != null && this.getCurrentEditorLanguage() !== lang) {
monaco.editor.setModelLanguage(model, lang);
}
if (!this.isAwaitingInitialResults) {
if (this.selection) {
this.editor.setSelection(this.selection);
this.editor.revealLinesInCenter(this.selection.startLineNumber, this.selection.endLineNumber);
}
this.isAwaitingInitialResults = true;
}
}
// Monaco language id of the current editor
getCurrentEditorLanguage() {
return this.editor.getModel()?.getLanguageId();
}
override getDefaultPaneName() {
return 'Stack Usage Viewer';
}
override getPrintName() {
return '<Unimplemented>';
}
showStackUsageResults(suEntries: suCodeEntry[], source: string) {
const splitLines = (text: string): string[] => {
if (!text) return [];
const result = text.split(/\r?\n/);
if (result.length > 0 && result[result.length - 1] === '') return result.slice(0, -1);
return result;
};
const srcLines: string[] = source ? splitLines(source) : [];
const srcAsSuLines: SuViewLine[] = srcLines.map((line, i) => ({text: line, srcLine: i, suClass: 'None'}));
const groupedResults = _.groupBy(suEntries, x => x.DebugLoc.Line);
const resLines = [...srcAsSuLines];
for (const [key, value] of Object.entries(groupedResults)) {
const origLineNum = Number(key);
const curLineNum = resLines.findIndex(line => line.srcLine === origLineNum);
const contents = value.map(rem => ({
text: rem.displayString,
srcLine: -1,
suClass: rem.Qualifier,
}));
resLines.splice(curLineNum, 0, ...contents);
}
const newText: string = resLines.reduce((accText, curSrcLine) => {
return accText + (curSrcLine.suClass === 'None' ? curSrcLine.text : ' ') + '\n';
}, '');
this.editor.setValue(newText);
const suDecorations: monaco.editor.IModelDeltaDecoration[] = [];
resLines.forEach((line, lineNum) => {
if (!line.suClass) {
// Shouldn't be possible, temp SentryCapture here to investigate
// https://compiler-explorer.sentry.io/issues/5374209222/
SentryCapture(
{comp: this.compilerInfo.compilerId, code: srcLines},
'StackUsageView: line.suClass is undefined',
);
return;
}
if (line.suClass !== 'None') {
suDecorations.push({
range: new monaco.Range(lineNum + 1, 1, lineNum + 1, Number.POSITIVE_INFINITY),
options: {
isWholeLine: true,
after: {
content: line.text,
},
inlineClassName: 'stack-usage.' + line.suClass.replace(',', '_'),
},
});
}
});
this.editorDecorations.set(suDecorations);
}
override onCompiler(id: number, compiler: CompilerInfo | null) {
if (id === this.compilerInfo.compilerId) {
this.compilerInfo.compilerName = compiler ? compiler.name : '';
this.updateTitle();
this.isCompilerSupported = compiler ? compiler.supportsStackUsageOutput : false;
if (!this.isCompilerSupported) {
this.editor.setValue('<Stack usage output is not supported for this compiler>');
}
}
}
// Don't do anything for this pane
override sendPrintData() {}
override close() {
this.eventHub.unsubscribe();
this.eventHub.emit('stackUsageViewClosed', this.compilerInfo.compilerId);
this.editor.dispose();
}
}