Skip to content

Commit 69a6766

Browse files
wjhsfekashida
andauthored
test(errors): validate error object shapes and unique codes @W-14841223 (#3949)
* test(errors): validate error object shapes and unique codes * fix(typo): fix regex Co-authored-by: Eugene Kashida <[email protected]> * chore: add missing copyright headers --------- Co-authored-by: Eugene Kashida <[email protected]>
1 parent ebc8c32 commit 69a6766

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2024, Salesforce, Inc.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: MIT
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6+
*/
7+
import * as errorInfo from '../error-info';
8+
9+
// All exported objects are maps of label/error info, except for GENERIC_COMPILER_ERROR,
10+
// which is a top-level error info object
11+
const { GENERIC_COMPILER_ERROR, ...errors } = errorInfo;
12+
13+
const errorInfoMatcher = {
14+
code: expect.any(Number),
15+
message: expect.any(String),
16+
url: expect.any(String),
17+
// Technically not *any* number, but jest doesn't have oneOf
18+
level: expect.any(Number),
19+
};
20+
21+
it('GENERIC_COMPILER_ERROR should be an error info object', () => {
22+
expect(GENERIC_COMPILER_ERROR).toEqual(errorInfoMatcher);
23+
});
24+
25+
describe.each(Object.entries(errors))('%s errors', (_key, map) => {
26+
it('labels should all be UPPER_SNAKE_CASE', () => {
27+
Object.keys(map).forEach((label) => {
28+
expect(label).toMatch(/^[A-Z]+(?:_[A-Z]+?)*?$/);
29+
});
30+
});
31+
it.each(Object.entries(map))('%s should be an error info object', (_label, info) => {
32+
expect(info).toEqual(errorInfoMatcher);
33+
});
34+
});
35+
36+
it('error codes are unique', () => {
37+
// Map of error codes to the errors that use them
38+
const seen = new Map([[GENERIC_COMPILER_ERROR.code, ['GENERIC_COMPILER_ERROR']]]);
39+
Object.entries(errors).forEach(([key, map]) => {
40+
Object.entries(map).forEach(([label, info]) => {
41+
const path = `${key}.${label}`;
42+
const prev = seen.get(info.code) ?? [];
43+
seen.set(info.code, [...prev, path]);
44+
});
45+
});
46+
// This assertion prints errors that use the same code for easier debugging
47+
for (const arr of seen.values()) {
48+
expect(arr).toHaveLength(1);
49+
}
50+
});

packages/@lwc/errors/src/compiler/error-info/compiler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/*
2-
* Copyright (c) 2018, salesforce.com, inc.
2+
* Copyright (c) 2024, Salesforce, Inc.
33
* All rights reserved.
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
77
import { DiagnosticLevel } from '../../shared/types';
88

9-
/**
10-
* TODO [W-5678919]: implement script to determine the next available error code
11-
* In the meantime, reference and the update the value at src/compiler/error-info/index.ts
9+
/*
10+
* For the next available error code, reference (and update!) the value in ./index.ts
1211
*/
1312

1413
export const GENERIC_COMPILER_ERROR = {
1514
code: 1001,
1615
message: 'Unexpected compilation error: {0}',
1716
level: DiagnosticLevel.Error,
17+
url: '',
1818
};
1919

2020
export const CompilerValidationErrors = {

packages/@lwc/errors/src/compiler/error-info/lwc-class.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/*
2-
* Copyright (c) 2018, salesforce.com, inc.
2+
* Copyright (c) 2024, Salesforce, Inc.
33
* All rights reserved.
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
77
import { DiagnosticLevel } from '../../shared/types';
88

9-
/**
10-
* TODO [W-5678919]: implement script to determine the next available error code
11-
* In the meantime, reference and the update the value at src/compiler/error-info/index.ts
9+
/*
10+
* For the next available error code, reference (and update!) the value in ./index.ts
1211
*/
1312

1413
export const LWCClassErrors = {

packages/@lwc/errors/src/compiler/error-info/template-transform.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/*
2-
* Copyright (c) 2018, salesforce.com, inc.
2+
* Copyright (c) 2024, Salesforce, Inc.
33
* All rights reserved.
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
77
import { DiagnosticLevel } from '../../shared/types';
88

9-
/**
10-
* TODO [W-5678919]: implement script to determine the next available error code
11-
* In the meantime, reference and the update the value at src/compiler/error-info/index.ts
9+
/*
10+
* For the next available error code, reference (and update!) the value in ./index.ts
1211
*/
1312

1413
export const TemplateErrors = {
@@ -640,6 +639,7 @@ export const ParserDiagnostics = {
640639
message:
641640
'Invalid lwc:spread usage on element "{0}". The directive binding must be an expression.',
642641
level: DiagnosticLevel.Error,
642+
url: '',
643643
},
644644

645645
LWC_REF_INVALID_ELEMENT: {

0 commit comments

Comments
 (0)