Skip to content

Commit 0edd505

Browse files
authored
fix: handle cases where sourceContent is missing (#44)
1 parent 28855e9 commit 0edd505

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

src/generateError.ts

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@ export async function generateError({
3030
rootPath: string;
3131
exclude?: CheckSyntaxExclude;
3232
}): Promise<ECMASyntaxError | null> {
33+
const relativeOutputPath = filepath.replace(rootPath, '');
3334
let error = await tryGenerateErrorFromSourceMap({
3435
err,
35-
filepath,
36-
rootPath,
36+
code,
37+
outputFilepath: filepath,
38+
relativeOutputPath,
3739
});
3840

3941
if (!error) {
40-
const path = filepath.replace(rootPath, '');
4142
error = new ECMASyntaxError(err.message, {
4243
source: {
43-
path,
44+
path: relativeOutputPath,
4445
line: err.loc.line,
4546
column: err.loc.column,
4647
code: displayCodePointer(code, err.pos),
@@ -75,14 +76,16 @@ export function makeCodeFrame(lines: string[], highlightIndex: number) {
7576

7677
async function tryGenerateErrorFromSourceMap({
7778
err,
78-
filepath,
79-
rootPath,
79+
code,
80+
outputFilepath,
81+
relativeOutputPath,
8082
}: {
8183
err: AcornParseError;
82-
filepath: string;
83-
rootPath: string;
84+
code: string;
85+
outputFilepath: string;
86+
relativeOutputPath: string;
8487
}): Promise<ECMASyntaxError | null> {
85-
const sourceMapPath = `${filepath}.map`;
88+
const sourceMapPath = `${outputFilepath}.map`;
8689

8790
if (!fs.existsSync(sourceMapPath)) {
8891
return null;
@@ -91,37 +94,49 @@ async function tryGenerateErrorFromSourceMap({
9194
try {
9295
const sourcemap = await fs.promises.readFile(sourceMapPath, 'utf-8');
9396
const consumer = await new SourceMapConsumer(sourcemap);
94-
const sm = consumer.originalPositionFor({
97+
const mappedPosition = consumer.originalPositionFor({
9598
line: err.loc.line,
9699
column: err.loc.column,
97100
});
98-
if (!sm.source) {
101+
102+
if (!mappedPosition.source) {
99103
return null;
100104
}
101-
const { sources } = consumer;
102-
103-
const smIndex = sources.indexOf(sm.source);
104105

105-
const smContent: string = JSON.parse(sourcemap)?.sourcesContent?.[smIndex];
106-
107-
if (!smContent) {
108-
return null;
106+
const { sources } = consumer;
107+
const sourceIndex = sources.indexOf(mappedPosition.source);
108+
const sourceContent: string | null =
109+
JSON.parse(sourcemap).sourcesContent?.[sourceIndex];
110+
const sourcePath = mappedPosition.source.replace(/webpack:\/\/(tmp)?/g, '');
111+
112+
if (!sourceContent) {
113+
return new ECMASyntaxError(err.message, {
114+
source: {
115+
path: sourcePath,
116+
line: mappedPosition.line ?? 0,
117+
column: mappedPosition.column ?? 0,
118+
code: displayCodePointer(code, err.pos),
119+
},
120+
output: {
121+
path: relativeOutputPath,
122+
line: err.loc.line,
123+
column: err.loc.column,
124+
},
125+
});
109126
}
110127

111-
const path = sm.source.replace(/webpack:\/\/(tmp)?/g, '');
112-
const relativeFilepath = filepath.replace(rootPath, '');
113-
const rawLines = smContent.split(/\r?\n/g);
114-
const highlightLine = (sm.line ?? 1) - 1;
128+
const rawLines = sourceContent.split(/\r?\n/g);
129+
const highlightLine = (mappedPosition.line ?? 1) - 1;
115130

116131
return new ECMASyntaxError(err.message, {
117132
source: {
118-
path,
119-
line: sm.line ?? 0,
120-
column: sm.column ?? 0,
133+
path: sourcePath,
134+
line: mappedPosition.line ?? 0,
135+
column: mappedPosition.column ?? 0,
121136
code: makeCodeFrame(rawLines, highlightLine),
122137
},
123138
output: {
124-
path: relativeFilepath,
139+
path: relativeOutputPath,
125140
line: err.loc.line,
126141
column: err.loc.column,
127142
},

0 commit comments

Comments
 (0)