Skip to content

Commit e9c2677

Browse files
Include standard webpack start/end locations in emitted errors (#1255)
1 parent 91206c4 commit e9c2677

File tree

49 files changed

+121
-63
lines changed
  • src
  • test/comparison-tests
    • aliasResolution/expectedOutput-4.1
    • allowJs-ts-check/expectedOutput-4.1
    • basic/expectedOutput-4.1/patch0
    • colors
    • declarationDeps/expectedOutput-4.1
    • declarationWatch/expectedOutput-4.1/patch0
    • dependencyErrors/expectedOutput-4.1
    • errorFormatter/expectedOutput-4.1
    • errors
    • es3/expectedOutput-4.1
    • ignoreDiagnostics/expectedOutput-4.1
    • importsWatch/expectedOutput-4.1/patch1
    • nolib/expectedOutput-4.1
    • onlyCompileBundledFiles/expectedOutput-4.1/patch0
    • production/expectedOutput-4.1
    • projectReferencesMultipleDifferentInstance/expectedOutput-4.1
    • projectReferencesNotBuilt_ErrorInProject/expectedOutput-4.1
    • projectReferencesNotBuilt_ErrorInProject_Composite_WatchApi/expectedOutput-4.1
    • projectReferencesNotBuilt_ErrorInProject_WatchApi/expectedOutput-4.1
    • projectReferencesNotBuilt_SemanticErrorInReference/expectedOutput-4.1
    • projectReferencesNotBuilt_SemanticErrorInReference_Composite_WatchApi/expectedOutput-4.1
    • projectReferencesNotBuilt_SemanticErrorInReference_WatchApi/expectedOutput-4.1
    • projectReferencesNotBuilt_SyntaxErrorInReference/expectedOutput-4.1
    • projectReferencesNotBuilt_SyntaxErrorInReference_Composite_WatchApi/expectedOutput-4.1
    • projectReferencesNotBuilt_SyntaxErrorInReference_WatchApi/expectedOutput-4.1
    • projectReferencesOutDirWithPackageJson/expectedOutput-4.1/patch4
    • projectReferencesOutDirWithPackageJsonAlreadyBuilt/expectedOutput-4.1/patch4
    • projectReferencesSymLinks/expectedOutput-4.1/patch0
    • projectReferencesSymLinksPreserve/expectedOutput-4.1/patch0
    • projectReferencesSymLinksPreserve_WatchApi/expectedOutput-4.1/patch0
    • projectReferencesWatch/expectedOutput-4.1
    • projectReferencesWatch_Composite_WatchApi/expectedOutput-4.1
    • projectReferencesWatch_WatchApi/expectedOutput-4.1
    • reportFiles/expectedOutput-4.1
    • simpleDependency/expectedOutput-4.1/patch0
    • typeSystemWatch/expectedOutput-4.1/patch0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+121
-63
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## v8.0.17
4+
* [Included correct webpack source location in emitted errors](https://github.com/TypeStrong/ts-loader/issues/1199) - thanks @lorenzodallavecchia
5+
36
## v8.0.16
47
* [Re-Fixed missing errors in watch mode in webpack5](https://github.com/TypeStrong/ts-loader/issues/1204) - thanks @appzuka
58

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-loader",
3-
"version": "8.0.16",
3+
"version": "8.0.17",
44
"description": "TypeScript loader for webpack",
55
"main": "index.js",
66
"types": "dist",

src/interfaces.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,25 @@ export interface ErrorInfo {
1313
context: string;
1414
}
1515

16-
export type FileLocation = { line: number; character: number };
16+
export type FileLocation = {
17+
/** 1-based */
18+
line: number;
19+
/** 1-based */
20+
character: number;
21+
};
1722

23+
export type WebpackSourcePosition = {
24+
/** 1-based */
25+
line: number;
26+
/** 0-based */
27+
column?: number;
28+
};
1829
export interface WebpackError {
1930
module?: any;
2031
file?: string;
2132
message: string;
33+
loc?: { start: WebpackSourcePosition; end?: WebpackSourcePosition };
34+
/* ts-loader extra properties */
2235
location?: FileLocation;
2336
loaderSource: string;
2437
}

src/utils.ts

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as typescript from 'typescript';
77
import constants = require('./constants');
88
import {
99
ErrorInfo,
10+
FileLocation,
1011
FilePathKey,
1112
LoaderOptions,
1213
ResolvedModule,
@@ -15,6 +16,7 @@ import {
1516
TSInstance,
1617
WebpackError,
1718
WebpackModule,
19+
WebpackSourcePosition,
1820
} from './interfaces';
1921
import { getInputFileNameFromOutput } from './instances';
2022
/**
@@ -75,10 +77,10 @@ export function formatErrors(
7577
})
7678
.map<WebpackError>(diagnostic => {
7779
const file = diagnostic.file;
78-
const position =
79-
file === undefined
80-
? undefined
81-
: file.getLineAndCharacterOfPosition(diagnostic.start!);
80+
const { start, end } =
81+
file === undefined || diagnostic.start === undefined
82+
? { start: undefined, end: undefined }
83+
: getFileLocations(file, diagnostic.start, diagnostic.length);
8284
const errorInfo: ErrorInfo = {
8385
code: diagnostic.code,
8486
severity: compiler.DiagnosticCategory[
@@ -89,8 +91,8 @@ export function formatErrors(
8991
constants.EOL
9092
),
9193
file: file === undefined ? '' : path.normalize(file.fileName),
92-
line: position === undefined ? 0 : position.line + 1,
93-
character: position === undefined ? 0 : position.character + 1,
94+
line: start === undefined ? 0 : start.line,
95+
character: start === undefined ? 0 : start.character,
9496
context,
9597
};
9698

@@ -103,15 +105,35 @@ export function formatErrors(
103105
loaderOptions,
104106
message,
105107
merge.file === undefined ? errorInfo.file : merge.file,
106-
position === undefined
107-
? undefined
108-
: { line: errorInfo.line, character: errorInfo.character }
108+
start,
109+
end
109110
);
110111

111112
return Object.assign(error, merge) as WebpackError;
112113
});
113114
}
114115

116+
function getFileLocations(
117+
file: typescript.SourceFile,
118+
position: number,
119+
length = 0
120+
) {
121+
const startLC = file.getLineAndCharacterOfPosition(position);
122+
const start: FileLocation = {
123+
line: startLC.line + 1,
124+
character: startLC.character + 1,
125+
};
126+
const endLC =
127+
length > 0
128+
? file.getLineAndCharacterOfPosition(position + length)
129+
: undefined;
130+
const end: FileLocation | undefined =
131+
endLC === undefined
132+
? undefined
133+
: { line: endLC.line + 1, character: endLC.character + 1 };
134+
return { start, end };
135+
}
136+
115137
export function fsReadFile(
116138
fileName: string,
117139
encoding: string | undefined = 'utf8'
@@ -128,16 +150,36 @@ export function makeError(
128150
loaderOptions: LoaderOptions,
129151
message: string,
130152
file: string | undefined,
131-
location?: { line: number; character: number }
153+
location?: FileLocation,
154+
endLocation?: FileLocation
132155
): WebpackError {
133156
return {
134157
message,
135-
location,
136158
file,
159+
loc:
160+
location === undefined
161+
? undefined
162+
: makeWebpackLocation(location, endLocation),
163+
location,
137164
loaderSource: tsLoaderSource(loaderOptions),
138165
};
139166
}
140167

168+
function makeWebpackLocation(
169+
location: FileLocation,
170+
endLocation?: FileLocation
171+
) {
172+
const start: WebpackSourcePosition = {
173+
line: location.line,
174+
column: location.character - 1,
175+
};
176+
const end: WebpackSourcePosition | undefined =
177+
endLocation === undefined
178+
? undefined
179+
: { line: endLocation.line, column: endLocation.character - 1 };
180+
return { start, end };
181+
}
182+
141183
export function tsLoaderSource(loaderOptions: LoaderOptions) {
142184
return `ts-loader-${loaderOptions.instance}`;
143185
}

test/comparison-tests/aliasResolution/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ Entrypoint main = bundle.js
55
[./common/components/myComponent.ts] 46 bytes {main} [built]
66

77
ERROR in app.ts
8-
./app.ts
8+
./app.ts 2:30-55
99
[tsl] ERROR in app.ts(2,31)
1010
 TS2307: Cannot find module 'components/myComponent2' or its corresponding type declarations.

test/comparison-tests/aliasResolution/expectedOutput-4.1/patch0/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ Entrypoint main = bundle.js
55
[./common/components/myComponent.ts] 45 bytes {main} [built]
66

77
ERROR in app.ts
8-
./app.ts
8+
./app.ts 2:30-55
99
[tsl] ERROR in app.ts(2,31)
1010
 TS2307: Cannot find module 'components/myComponent2' or its corresponding type declarations.

test/comparison-tests/allowJs-ts-check/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Entrypoint main = bundle.js
66
[./src/index.js] 207 bytes {main} [built]
77

88
ERROR in src/error2.js
9-
./src/error2.js
9+
./src/error2.js 4:9-12
1010
[tsl] ERROR in src/error2.js(4,10)
1111
 TS2339: Property 'bar' does not exist on type 'Class2'.

test/comparison-tests/basic/expectedOutput-4.1/patch0/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Entrypoint main = bundle.js
66
[./submodule/submodule.ts] 149 bytes {main}
77

88
ERROR in app.ts
9-
./app.ts
9+
./app.ts 3:12-24
1010
[tsl] ERROR in app.ts(3,13)
1111
 TS2551: Property 'doSomething2' does not exist on type 'typeof externalLib'. Did you mean 'doSomething'?

test/comparison-tests/colors/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ You may need an additional loader to handle the result of these loaders.
1313
|
1414

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

test/comparison-tests/colors/expectedOutput-transpile-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ You may need an additional loader to handle the result of these loaders.
1313
|
1414

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

test/comparison-tests/declarationDeps/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ Entrypoint main = bundle.js
44
[./app.ts] 41 bytes {main} [built] [1 error]
55

66
ERROR in app.ts
7-
./app.ts
7+
./app.ts 2:6-11
88
[tsl] ERROR in app.ts(2,7)
99
 TS2339: Property 'sayHi' does not exist on type 'typeof Hello'.

test/comparison-tests/declarationWatch/expectedOutput-4.1/patch0/output.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ Entrypoint main = bundle.js
55
[./dep.ts] 59 bytes {main} [built] [1 error]
66

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

1212
ERROR in dep.ts
13-
./dep.ts
13+
./dep.ts 1:6-17
1414
[tsl] ERROR in dep.ts(1,7)
1515
 TS2339: Property 'doSomething' does not exist on type 'typeof Thing'.

test/comparison-tests/dependencyErrors/expectedOutput-4.1/output.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Entrypoint main = bundle.js
66
[./dep2.ts] 76 bytes {main} [built]
77

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

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

test/comparison-tests/dependencyErrors/expectedOutput-4.1/patch0/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Entrypoint main = bundle.js
66
[./dep2.ts] 76 bytes {main}
77

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

test/comparison-tests/errorFormatter/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ Entrypoint main = bundle.js
55
[./common/components/myComponent.ts] 46 bytes {main} [built]
66

77
ERROR in app.ts
8-
./app.ts
8+
./app.ts 2:30-55
99
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

test/comparison-tests/errors/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ You may need an additional loader to handle the result of these loaders.
1313
|
1414

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

test/comparison-tests/errors/expectedOutput-transpile-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Entrypoint main = bundle.js
44
[./app.ts] 220 bytes {main} [built] [failed] [2 errors]
55

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

test/comparison-tests/es3/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ Entrypoint main = bundle.js
44
[./app.ts] 29 bytes {main} [built] [1 error]
55

66
ERROR in app.ts
7-
./app.ts
7+
./app.ts 1:6-7
88
[tsl] ERROR in app.ts(1,7)
99
 TS1056: Accessors are only available when targeting ECMAScript 5 and higher.

test/comparison-tests/ignoreDiagnostics/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ Entrypoint main = bundle.js
44
[./app.ts] 278 bytes {main} [built] [1 error]
55

66
ERROR in app.ts
7-
./app.ts
7+
./app.ts 9:4-5
88
[tsl] ERROR in app.ts(9,5)
99
 TS2322: Type 'string' is not assignable to type 'Number'.

test/comparison-tests/importsWatch/expectedOutput-4.1/patch1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ Entrypoint main = bundle.js
44
[./app.ts] 70 bytes {main} [built] [1 error]
55

66
ERROR in app.ts
7-
./app.ts
7+
./app.ts 4:0-7
88
[tsl] ERROR in app.ts(4,1)
99
 TS2322: Type 'string' is not assignable to type 'boolean'.

test/comparison-tests/nolib/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ ERROR in tsconfig.json
3232
 TS2318: Cannot find global type 'RegExp'.
3333

3434
ERROR in app.ts
35-
./app.ts
35+
./app.ts 1:0-8
3636
[tsl] ERROR in app.ts(1,1)
3737
 TS2304: Cannot find name 'parseInt'.

test/comparison-tests/onlyCompileBundledFiles/expectedOutput-4.1/patch0/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Entrypoint main = bundle.js
66
[./submodule/submodule.ts] 149 bytes {main}
77

88
ERROR in app.ts
9-
./app.ts
9+
./app.ts 3:12-24
1010
[tsl] ERROR in app.ts(3,13)
1111
 TS2551: Property 'doSomething2' does not exist on type 'typeof externalLib'. Did you mean 'doSomething'?

test/comparison-tests/production/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ Entrypoint main = bundle.js
44
[0] ./app.ts 27 bytes {0} [built] [1 error]
55

66
ERROR in app.ts
7-
./app.ts
7+
./app.ts 4:0-1
88
[tsl] ERROR in app.ts(4,1)
99
 TS2322: Type 'string' is not assignable to type 'number'.

test/comparison-tests/projectReferencesMultipleDifferentInstance/expectedOutput-4.1/patch3/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ Entrypoint main = bundle.js
88
[./app.ts] 202 bytes {main} [built]
99

1010
ERROR in common/index.ts
11-
../common/index.ts
11+
../common/index.ts 2:2-12
1212
[tsl] ERROR in common/index.ts(2,3)
1313
 TS2322: Type 'number' is not assignable to type 'string'.

test/comparison-tests/projectReferencesMultipleDifferentInstance/expectedOutput-4.1/patch5/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ Entrypoint main = bundle.js
88
[./app.ts] 202 bytes {main} [built]
99

1010
ERROR in utils/index.ts
11-
../utils/index.ts
11+
../utils/index.ts 5:35-50
1212
[tsl] ERROR in utils/index.ts(5,36)
1313
 TS2322: Type 'string' is not assignable to type 'number'.

test/comparison-tests/projectReferencesNotBuilt_ErrorInProject/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Entrypoint main = bundle.js
99
[./lib/index.ts] 119 bytes {main} [built]
1010

1111
ERROR in app.ts
12-
./app.ts
12+
./app.ts 3:45-49
1313
[tsl] ERROR in app.ts(3,46)
1414
 TS2339: Property 'four' does not exist on type '{ one: number; two: number; three: number; }'.

test/comparison-tests/projectReferencesNotBuilt_ErrorInProject_Composite_WatchApi/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ Entrypoint main = bundle.js
1111
[./lib/index.ts] 119 bytes {main} [built]
1212

1313
ERROR in app.ts
14-
./app.ts
14+
./app.ts 3:45-49
1515
[tsl] ERROR in app.ts(3,46)
1616
 TS2339: Property 'four' does not exist on type '{ one: number; two: number; three: number; }'.

test/comparison-tests/projectReferencesNotBuilt_ErrorInProject_WatchApi/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Entrypoint main = bundle.js
99
[./lib/index.ts] 119 bytes {main} [built]
1010

1111
ERROR in app.ts
12-
./app.ts
12+
./app.ts 3:45-49
1313
[tsl] ERROR in app.ts(3,46)
1414
 TS2339: Property 'four' does not exist on type '{ one: number; two: number; three: number; }'.

test/comparison-tests/projectReferencesNotBuilt_SemanticErrorInReference/expectedOutput-4.1/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ Error: TypeScript emitted no output for lib/index.ts. The most common cause for
1414
@ ./app.ts 3:12-28
1515

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

0 commit comments

Comments
 (0)