Skip to content

Commit

Permalink
test(errors): validate error object shapes and unique codes @W-148412…
Browse files Browse the repository at this point in the history
…23 (#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]>
  • Loading branch information
wjhsf and ekashida authored Jan 22, 2024
1 parent ebc8c32 commit 69a6766
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
50 changes: 50 additions & 0 deletions packages/@lwc/errors/src/compiler/__tests__/error-info.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import * as errorInfo from '../error-info';

// All exported objects are maps of label/error info, except for GENERIC_COMPILER_ERROR,
// which is a top-level error info object
const { GENERIC_COMPILER_ERROR, ...errors } = errorInfo;

const errorInfoMatcher = {
code: expect.any(Number),
message: expect.any(String),
url: expect.any(String),
// Technically not *any* number, but jest doesn't have oneOf
level: expect.any(Number),
};

it('GENERIC_COMPILER_ERROR should be an error info object', () => {
expect(GENERIC_COMPILER_ERROR).toEqual(errorInfoMatcher);
});

describe.each(Object.entries(errors))('%s errors', (_key, map) => {
it('labels should all be UPPER_SNAKE_CASE', () => {
Object.keys(map).forEach((label) => {
expect(label).toMatch(/^[A-Z]+(?:_[A-Z]+?)*?$/);
});
});
it.each(Object.entries(map))('%s should be an error info object', (_label, info) => {
expect(info).toEqual(errorInfoMatcher);
});
});

it('error codes are unique', () => {
// Map of error codes to the errors that use them
const seen = new Map([[GENERIC_COMPILER_ERROR.code, ['GENERIC_COMPILER_ERROR']]]);
Object.entries(errors).forEach(([key, map]) => {
Object.entries(map).forEach(([label, info]) => {
const path = `${key}.${label}`;
const prev = seen.get(info.code) ?? [];
seen.set(info.code, [...prev, path]);
});
});
// This assertion prints errors that use the same code for easier debugging
for (const arr of seen.values()) {
expect(arr).toHaveLength(1);
}
});
8 changes: 4 additions & 4 deletions packages/@lwc/errors/src/compiler/error-info/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
* Copyright (c) 2018, salesforce.com, inc.
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { DiagnosticLevel } from '../../shared/types';

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

export const GENERIC_COMPILER_ERROR = {
code: 1001,
message: 'Unexpected compilation error: {0}',
level: DiagnosticLevel.Error,
url: '',
};

export const CompilerValidationErrors = {
Expand Down
7 changes: 3 additions & 4 deletions packages/@lwc/errors/src/compiler/error-info/lwc-class.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/*
* Copyright (c) 2018, salesforce.com, inc.
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { DiagnosticLevel } from '../../shared/types';

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

export const LWCClassErrors = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/*
* Copyright (c) 2018, salesforce.com, inc.
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { DiagnosticLevel } from '../../shared/types';

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

export const TemplateErrors = {
Expand Down Expand Up @@ -640,6 +639,7 @@ export const ParserDiagnostics = {
message:
'Invalid lwc:spread usage on element "{0}". The directive binding must be an expression.',
level: DiagnosticLevel.Error,
url: '',
},

LWC_REF_INVALID_ELEMENT: {
Expand Down

0 comments on commit 69a6766

Please sign in to comment.