Skip to content

Commit e624bab

Browse files
committed
fix uncaught parse errors
1 parent 06c5fdc commit e624bab

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

src/data/report.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,12 @@ export function activate(context: vscode.ExtensionContext, disposables?: Array<v
174174
let needRun = false
175175
for (const file of files) {
176176
let existing = knownPkgFiles.get(file.fsPath)
177-
const cacheKey = pkgJSONSrcToStableStringKey(file.str)
177+
let cacheKey;
178+
try {
179+
cacheKey = pkgJSONSrcToStableStringKey(file.str)
180+
} catch {
181+
continue
182+
}
178183
if (!existing) {
179184
needRun = true
180185
existing = {

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ export async function activate(context: ExtensionContext) {
8585
)
8686
function normalizeReportAndLocations(report: SocketReport, doc: Parameters<typeof parseExternals>[0]) {
8787
const externals = parseExternals(doc)
88+
if (!externals) {
89+
return
90+
}
8891
const issuesForSource = radixMergeReportIssues(report)
8992
let ranges: Record<string, Array<vscode.Range>> = Object.create(null);
9093
let prioritizedRanges: Record<string, { range: vscode.Range, prioritize: boolean }> = Object.create(null)

src/ui/javascript-file.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ export function activate(
143143
if (!socketReport) return
144144
let hovers: Array<vscode.Hover> = []
145145
const externals = parseExternals(doc)
146+
if (!externals) {
147+
return
148+
}
146149
const issues = radixMergeReportIssues(socketReport)
147150
for (const {name, range} of externals) {
148151
const pkgIssues = issues.get(name)
@@ -269,7 +272,11 @@ ${issues.sort((a, b) => sortIssues({
269272
e.setDecorations(errorDecoration, errorDecorations);
270273
e.setDecorations(warningDecoration, warningDecorations);
271274

272-
for (const {name, range} of parseExternals(e.document)) {
275+
const externals = parseExternals(e.document)
276+
if (!externals) {
277+
return
278+
}
279+
for (const {name, range} of externals) {
273280
if (isBuiltin(name)) {
274281
const deco: vscode.DecorationOptions = {
275282
range,

src/ui/parse-externals.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,29 @@ function getPackageNameFromVersionRange(name: string): string {
3131
name.split('@', 2)
3232
).join('@');
3333
}
34-
export function parseExternals(doc: Pick<vscode.TextDocument, 'getText' | 'languageId' | 'fileName'>): Iterable<ExternalRef> {
34+
export function parseExternals(doc: Pick<vscode.TextDocument, 'getText' | 'languageId' | 'fileName'>): Iterable<ExternalRef> | null {
3535
const src = doc.getText();
3636
const results: Array<ExternalRef> = []
3737
if (SUPPORTED_LANGUAGE_IDS.includes(doc.languageId)) {
38-
const ast = parser.parse(
39-
src,
40-
{
41-
allowAwaitOutsideFunction: true,
42-
allowImportExportEverywhere: true,
43-
allowReturnOutsideFunction: true,
44-
errorRecovery: true,
45-
plugins: [
46-
'jsx',
47-
'typescript',
48-
'decorators'
49-
],
50-
}
51-
)
38+
let ast
39+
try {
40+
ast = parser.parse(
41+
src,
42+
{
43+
allowAwaitOutsideFunction: true,
44+
allowImportExportEverywhere: true,
45+
allowReturnOutsideFunction: true,
46+
errorRecovery: true,
47+
plugins: [
48+
'jsx',
49+
'typescript',
50+
'decorators'
51+
],
52+
}
53+
)
54+
} catch {
55+
return null
56+
}
5257
function addResult(node: astTypes.namedTypes.Node, specifier: string) {
5358
if (/^[\.\/]/u.test(specifier)) {
5459
return

0 commit comments

Comments
 (0)