Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/create-beast/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,10 @@ function normalizePackageName(name: string): string {
.trim()
.toLowerCase()
.replace(/[^a-z0-9._-]+/gu, "-")
.replace(/^[._-]+|[._-]+$/gu, "")
.slice(0, 214);
return normalized || DEFAULT_DIRECTORY;
.replace(/^[._-]+/u, "");
let end = normalized.length;
while (end > 0 && "._-".includes(normalized[end - 1]!)) end -= 1;
return normalized.slice(0, Math.min(end, 214)) || DEFAULT_DIRECTORY;
}

function takeFlag(args: string[], name: string): boolean {
Expand Down
18 changes: 14 additions & 4 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ export function mapGeneratedError(
offset += original.column;
const position = { offset, line: original.line, column: original.column + 1 };
const message = octaneError.diagnostic?.message === undefined
? error.message
.replace(/^[^\n]*?:\d+:\d+:\s*(?=\[OCTANE_)/u, "")
.replace(/\s*\((?:[^()\n]*:)?\d+:\d+\)\s*$/u, "")
? stripMessageLocation(error.message
.replace(/^[^\n]*?:\d+:\d+:\s*(?=\[OCTANE_)/u, ""))
: `${octaneError.diagnostic.code ?? "OCTANE"}: ${octaneError.diagnostic.message}`;
return new BeastCompileError({
code: "BEAST9001_OCTANE",
Expand All @@ -118,8 +117,19 @@ export function mapGeneratedError(
});
}

function stripMessageLocation(message: string): string {
const trimmed = message.trimEnd();
const start = trimmed.lastIndexOf("(");
return start >= 0 && /^\((?:[^()\n]*:)?\d+:\d+\)$/u.test(trimmed.slice(start))
? trimmed.slice(0, start).trimEnd()
: message;
}

function messageLocation(message: string): { line: number; column: number } | undefined {
const match = /\((?:[^()\n]*:)?(\d+):(\d+)\)\s*$/u.exec(message);
const trimmed = message.trimEnd();
const start = trimmed.lastIndexOf("(");
if (start < 0) return undefined;
const match = /^\((?:[^()\n]*:)?(\d+):(\d+)\)$/u.exec(trimmed.slice(start));
if (match === null) return undefined;
return { line: Number(match[1]), column: Math.max(0, Number(match[2]) - 1) };
}
Expand Down
11 changes: 11 additions & 0 deletions tests/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,17 @@ describe('BTSX to TSRX', () => {
})

describe('error locations', () => {
test('mapping long diagnostic whitespace preserves text and strips only a location suffix', () => {
const source = 'p Ready\n'
const result = compileBeastResult(source, { filename: 'a.btsx' })
for (const suffix of [' (a.tsrx:1:1) ', ' (1:1)\t', ' (not a location)']) {
const text = 'Invalid' + ' '.repeat(100_000) + 'value'
const error = Object.assign(new Error(text + suffix), { loc: { line: 1, column: 0 } })
const mapped = mapGeneratedError(error, result.map, source, 'a.btsx') as BeastCompileError
expect(mapped.diagnostic.message).toBe(text + (suffix.includes('not a location') ? suffix : ''))
}
})

test('compile errors carry file:line:column in the message', () => {
expect(() => compileBeast('div\n <p>x</p>\n', { filename: 'a.btsx' })).toThrow(/a\.btsx:2:3 - BEAST1101/u)
})
Expand Down
Loading