Skip to content

Commit

Permalink
Include standard webpack start/end locations in emitted errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzodallavecchia committed Feb 10, 2021
1 parent 91206c4 commit bbbae2f
Show file tree
Hide file tree
Showing 47 changed files with 117 additions and 62 deletions.
15 changes: 14 additions & 1 deletion src/interfaces.ts
Expand Up @@ -13,12 +13,25 @@ export interface ErrorInfo {
context: string;
}

export type FileLocation = { line: number; character: number };
export type FileLocation = {
/** 1-based */
line: number;
/** 1-based */
character: number;
};

export type WebpackSourcePosition = {
/** 1-based */
line: number;
/** 0-based */
column?: number;
};
export interface WebpackError {
module?: any;
file?: string;
message: string;
loc?: { start: WebpackSourcePosition; end?: WebpackSourcePosition };
/* ts-loader extra properties */
location?: FileLocation;
loaderSource: string;
}
Expand Down
64 changes: 53 additions & 11 deletions src/utils.ts
Expand Up @@ -7,6 +7,7 @@ import * as typescript from 'typescript';
import constants = require('./constants');
import {
ErrorInfo,
FileLocation,
FilePathKey,
LoaderOptions,
ResolvedModule,
Expand All @@ -15,6 +16,7 @@ import {
TSInstance,
WebpackError,
WebpackModule,
WebpackSourcePosition,
} from './interfaces';
import { getInputFileNameFromOutput } from './instances';
/**
Expand Down Expand Up @@ -75,10 +77,10 @@ export function formatErrors(
})
.map<WebpackError>(diagnostic => {
const file = diagnostic.file;
const position =
file === undefined
? undefined
: file.getLineAndCharacterOfPosition(diagnostic.start!);
const { start, end } =
file === undefined || diagnostic.start === undefined
? { start: undefined, end: undefined }
: getFileLocations(file, diagnostic.start, diagnostic.length);
const errorInfo: ErrorInfo = {
code: diagnostic.code,
severity: compiler.DiagnosticCategory[
Expand All @@ -89,8 +91,8 @@ export function formatErrors(
constants.EOL
),
file: file === undefined ? '' : path.normalize(file.fileName),
line: position === undefined ? 0 : position.line + 1,
character: position === undefined ? 0 : position.character + 1,
line: start === undefined ? 0 : start.line,
character: start === undefined ? 0 : start.character,
context,
};

Expand All @@ -103,15 +105,35 @@ export function formatErrors(
loaderOptions,
message,
merge.file === undefined ? errorInfo.file : merge.file,
position === undefined
? undefined
: { line: errorInfo.line, character: errorInfo.character }
start,
end
);

return Object.assign(error, merge) as WebpackError;
});
}

function getFileLocations(
file: typescript.SourceFile,
position: number,
length = 0
) {
const startLC = file.getLineAndCharacterOfPosition(position);
const start: FileLocation = {
line: startLC.line + 1,
character: startLC.character + 1,
};
const endLC =
length > 0
? file.getLineAndCharacterOfPosition(position + length)
: undefined;
const end: FileLocation | undefined =
endLC === undefined
? undefined
: { line: endLC.line + 1, character: endLC.character + 1 };
return { start, end };
}

export function fsReadFile(
fileName: string,
encoding: string | undefined = 'utf8'
Expand All @@ -128,16 +150,36 @@ export function makeError(
loaderOptions: LoaderOptions,
message: string,
file: string | undefined,
location?: { line: number; character: number }
location?: FileLocation,
endLocation?: FileLocation
): WebpackError {
return {
message,
location,
file,
loc:
location === undefined
? undefined
: makeWebpackLocation(location, endLocation),
location,
loaderSource: tsLoaderSource(loaderOptions),
};
}

function makeWebpackLocation(
location: FileLocation,
endLocation?: FileLocation
) {
const start: WebpackSourcePosition = {
line: location.line,
column: location.character - 1,
};
const end: WebpackSourcePosition | undefined =
endLocation === undefined
? undefined
: { line: endLocation.line, column: endLocation.character - 1 };
return { start, end };
}

export function tsLoaderSource(loaderOptions: LoaderOptions) {
return `ts-loader-${loaderOptions.instance}`;
}
Expand Down
Expand Up @@ -5,6 +5,6 @@ Entrypoint main = bundle.js
[./common/components/myComponent.ts] 46 bytes {main} [built]

ERROR in app.ts
./app.ts
./app.ts 2:30-55
[tsl] ERROR in app.ts(2,31)
 TS2307: Cannot find module 'components/myComponent2' or its corresponding type declarations.
Expand Up @@ -5,6 +5,6 @@ Entrypoint main = bundle.js
[./common/components/myComponent.ts] 45 bytes {main} [built]

ERROR in app.ts
./app.ts
./app.ts 2:30-55
[tsl] ERROR in app.ts(2,31)
 TS2307: Cannot find module 'components/myComponent2' or its corresponding type declarations.
Expand Up @@ -6,6 +6,6 @@ Entrypoint main = bundle.js
[./src/index.js] 207 bytes {main} [built]

ERROR in src/error2.js
./src/error2.js
./src/error2.js 4:9-12
[tsl] ERROR in src/error2.js(4,10)
 TS2339: Property 'bar' does not exist on type 'Class2'.
Expand Up @@ -6,6 +6,6 @@ Entrypoint main = bundle.js
[./submodule/submodule.ts] 149 bytes {main}

ERROR in app.ts
./app.ts
./app.ts 3:12-24
[tsl] ERROR in app.ts(3,13)
 TS2551: Property 'doSomething2' does not exist on type 'typeof externalLib'. Did you mean 'doSomething'?
2 changes: 1 addition & 1 deletion test/comparison-tests/colors/expectedOutput-4.1/output.txt
Expand Up @@ -13,6 +13,6 @@ You may need an additional loader to handle the result of these loaders.
|

ERROR in app.ts
./app.ts
./app.ts 1:6-8
[tsl] ERROR in app.ts(1,7)
TS1005: ',' expected.
Expand Up @@ -13,6 +13,6 @@ You may need an additional loader to handle the result of these loaders.
|

ERROR in app.ts
./app.ts
./app.ts 1:6-8
[tsl] ERROR in app.ts(1,7)
TS1005: ',' expected.
Expand Up @@ -4,6 +4,6 @@ Entrypoint main = bundle.js
[./app.ts] 41 bytes {main} [built] [1 error]

ERROR in app.ts
./app.ts
./app.ts 2:6-11
[tsl] ERROR in app.ts(2,7)
 TS2339: Property 'sayHi' does not exist on type 'typeof Hello'.
Expand Up @@ -5,11 +5,11 @@ Entrypoint main = bundle.js
[./dep.ts] 59 bytes {main} [built] [1 error]

ERROR in app.ts
./app.ts
./app.ts 5:6-17
[tsl] ERROR in app.ts(5,7)
 TS2339: Property 'doSomething' does not exist on type 'typeof Thing'.

ERROR in dep.ts
./dep.ts
./dep.ts 1:6-17
[tsl] ERROR in dep.ts(1,7)
 TS2339: Property 'doSomething' does not exist on type 'typeof Thing'.
Expand Up @@ -6,11 +6,11 @@ Entrypoint main = bundle.js
[./dep2.ts] 76 bytes {main} [built]

ERROR in app.ts
./app.ts
./app.ts 4:5-7
[tsl] ERROR in app.ts(4,6)
 TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

ERROR in app.ts
./app.ts
./app.ts 5:5-7
[tsl] ERROR in app.ts(5,6)
 TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
Expand Up @@ -6,6 +6,6 @@ Entrypoint main = bundle.js
[./dep2.ts] 76 bytes {main}

ERROR in app.ts
./app.ts
./app.ts 5:5-7
[tsl] ERROR in app.ts(5,6)
 TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
Expand Up @@ -5,5 +5,5 @@ Entrypoint main = bundle.js
[./common/components/myComponent.ts] 46 bytes {main} [built]

ERROR in app.ts
./app.ts
./app.ts 2:30-55
Does not compute.... code: 2307,severity: error,content: Cannot find module 'components/myComponent2' or its corresponding type declarations.,file: app.ts,line: 2,character: 31,context: .test/errorFormatter
2 changes: 1 addition & 1 deletion test/comparison-tests/errors/expectedOutput-4.1/output.txt
Expand Up @@ -13,6 +13,6 @@ You may need an additional loader to handle the result of these loaders.
|

ERROR in app.ts
./app.ts
./app.ts 1:6-8
[tsl] ERROR in app.ts(1,7)
 TS1005: ',' expected.
Expand Up @@ -4,7 +4,7 @@ Entrypoint main = bundle.js
[./app.ts] 220 bytes {main} [built] [failed] [2 errors]

ERROR in app.ts
./app.ts
./app.ts 1:6-8
[tsl] ERROR in app.ts(1,7)
 TS1005: ',' expected.

Expand Down
2 changes: 1 addition & 1 deletion test/comparison-tests/es3/expectedOutput-4.1/output.txt
Expand Up @@ -4,6 +4,6 @@ Entrypoint main = bundle.js
[./app.ts] 29 bytes {main} [built] [1 error]

ERROR in app.ts
./app.ts
./app.ts 1:6-7
[tsl] ERROR in app.ts(1,7)
 TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
Expand Up @@ -4,6 +4,6 @@ Entrypoint main = bundle.js
[./app.ts] 278 bytes {main} [built] [1 error]

ERROR in app.ts
./app.ts
./app.ts 9:4-5
[tsl] ERROR in app.ts(9,5)
 TS2322: Type 'string' is not assignable to type 'Number'.
Expand Up @@ -4,6 +4,6 @@ Entrypoint main = bundle.js
[./app.ts] 70 bytes {main} [built] [1 error]

ERROR in app.ts
./app.ts
./app.ts 4:0-7
[tsl] ERROR in app.ts(4,1)
 TS2322: Type 'string' is not assignable to type 'boolean'.
2 changes: 1 addition & 1 deletion test/comparison-tests/nolib/expectedOutput-4.1/output.txt
Expand Up @@ -32,6 +32,6 @@ ERROR in tsconfig.json
 TS2318: Cannot find global type 'RegExp'.

ERROR in app.ts
./app.ts
./app.ts 1:0-8
[tsl] ERROR in app.ts(1,1)
 TS2304: Cannot find name 'parseInt'.
Expand Up @@ -6,6 +6,6 @@ Entrypoint main = bundle.js
[./submodule/submodule.ts] 149 bytes {main}

ERROR in app.ts
./app.ts
./app.ts 3:12-24
[tsl] ERROR in app.ts(3,13)
 TS2551: Property 'doSomething2' does not exist on type 'typeof externalLib'. Did you mean 'doSomething'?
Expand Up @@ -4,6 +4,6 @@ Entrypoint main = bundle.js
[0] ./app.ts 27 bytes {0} [built] [1 error]

ERROR in app.ts
./app.ts
./app.ts 4:0-1
[tsl] ERROR in app.ts(4,1)
 TS2322: Type 'string' is not assignable to type 'number'.
Expand Up @@ -8,6 +8,6 @@ Entrypoint main = bundle.js
[./app.ts] 202 bytes {main} [built]

ERROR in common/index.ts
../common/index.ts
../common/index.ts 2:2-12
[tsl] ERROR in common/index.ts(2,3)
 TS2322: Type 'number' is not assignable to type 'string'.
Expand Up @@ -8,6 +8,6 @@ Entrypoint main = bundle.js
[./app.ts] 202 bytes {main} [built]

ERROR in utils/index.ts
../utils/index.ts
../utils/index.ts 5:35-50
[tsl] ERROR in utils/index.ts(5,36)
 TS2322: Type 'string' is not assignable to type 'number'.
Expand Up @@ -9,6 +9,6 @@ Entrypoint main = bundle.js
[./lib/index.ts] 119 bytes {main} [built]

ERROR in app.ts
./app.ts
./app.ts 3:45-49
[tsl] ERROR in app.ts(3,46)
 TS2339: Property 'four' does not exist on type '{ one: number; two: number; three: number; }'.
Expand Up @@ -11,6 +11,6 @@ Entrypoint main = bundle.js
[./lib/index.ts] 119 bytes {main} [built]

ERROR in app.ts
./app.ts
./app.ts 3:45-49
[tsl] ERROR in app.ts(3,46)
 TS2339: Property 'four' does not exist on type '{ one: number; two: number; three: number; }'.
Expand Up @@ -9,6 +9,6 @@ Entrypoint main = bundle.js
[./lib/index.ts] 119 bytes {main} [built]

ERROR in app.ts
./app.ts
./app.ts 3:45-49
[tsl] ERROR in app.ts(3,46)
 TS2339: Property 'four' does not exist on type '{ one: number; two: number; three: number; }'.
Expand Up @@ -14,6 +14,6 @@ Error: TypeScript emitted no output for lib/index.ts. The most common cause for
@ ./app.ts 3:12-28

ERROR in lib/index.ts
./lib/index.ts
./lib/index.ts 6:6-7
[tsl] ERROR in lib/index.ts(6,7)
 TS2322: Type 'number' is not assignable to type 'string'.
Expand Up @@ -16,6 +16,6 @@ Error: TypeScript emitted no output for lib/index.ts. The most common cause for
@ ./app.ts 3:12-28

ERROR in lib/index.ts
./lib/index.ts
./lib/index.ts 6:6-7
[tsl] ERROR in lib/index.ts(6,7)
 TS2322: Type 'number' is not assignable to type 'string'.

0 comments on commit bbbae2f

Please sign in to comment.