Skip to content

Commit e074690

Browse files
vaadin-botsissbrueckerweb-padawan
authored
refactor: add utility for issuing warnings once (#10015) (#10019)
* refactor: add utility for issuing warnings once * Update packages/component-base/src/warnings.js --------- Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com> Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
1 parent 2b06ba6 commit e074690

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2000 - 2025 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
7+
/**
8+
* Issues a warning in the browser console if it has not been issued before.
9+
*/
10+
export declare function issueWarning(warning: string): void;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2000 - 2025 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
7+
const issuedWarnings = new Set();
8+
9+
/**
10+
* Issues a warning in the browser console if it has not been issued before.
11+
* @param {string} warning
12+
*/
13+
export function issueWarning(warning) {
14+
if (issuedWarnings.has(warning)) {
15+
return;
16+
}
17+
18+
issuedWarnings.add(warning);
19+
console.warn(warning);
20+
}
21+
22+
/**
23+
* Clears all issued warnings. Only intended for testing purposes.
24+
*/
25+
export function clearWarnings() {
26+
issuedWarnings.clear();
27+
}

packages/form-layout/src/vaadin-form-item-mixin.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
*/
66
import { addValueToAttribute, removeValueFromAttribute } from '@vaadin/component-base/src/dom-utils.js';
77
import { generateUniqueId } from '@vaadin/component-base/src/unique-id-utils.js';
8-
9-
let spacingDeprecationNotified = false;
10-
let labelWidthDeprecationNotified = false;
11-
let labelSpacingDeprecationNotified = false;
8+
import { issueWarning } from '@vaadin/component-base/src/warnings.js';
129

1310
/**
1411
* @polymerMixin
@@ -58,25 +55,22 @@ export const FormItemMixin = (superClass) =>
5855
const labelWidth = computedStyle.getPropertyValue('--vaadin-form-item-label-width');
5956
const labelSpacing = computedStyle.getPropertyValue('--vaadin-form-item-label-spacing');
6057

61-
if (!spacingDeprecationNotified && spacing !== '' && parseInt(spacing) !== 0) {
62-
console.warn(
58+
if (spacing !== '' && parseInt(spacing) !== 0) {
59+
issueWarning(
6360
'`--vaadin-form-item-row-spacing` is deprecated since 24.7. Use `--vaadin-form-layout-row-spacing` on <vaadin-form-layout> instead.',
6461
);
65-
spacingDeprecationNotified = true;
6662
}
6763

68-
if (!labelWidthDeprecationNotified && labelWidth !== '' && parseInt(labelWidth) !== 0) {
69-
console.warn(
64+
if (labelWidth !== '' && parseInt(labelWidth) !== 0) {
65+
issueWarning(
7066
'`--vaadin-form-item-label-width` is deprecated since 24.7. Use `--vaadin-form-layout-label-width` on <vaadin-form-layout> instead.',
7167
);
72-
labelWidthDeprecationNotified = true;
7368
}
7469

75-
if (!labelSpacingDeprecationNotified && labelSpacing !== '' && parseInt(labelSpacing) !== 0) {
76-
console.warn(
70+
if (labelSpacing !== '' && parseInt(labelSpacing) !== 0) {
71+
issueWarning(
7772
'`--vaadin-form-item-label-spacing` is deprecated since 24.7. Use `--vaadin-form-layout-label-spacing` on <vaadin-form-layout> instead.',
7873
);
79-
labelSpacingDeprecationNotified = true;
8074
}
8175
}
8276

@@ -185,7 +179,7 @@ export const FormItemMixin = (superClass) =>
185179

186180
const fieldNodes = this.$.contentSlot.assignedElements();
187181
if (fieldNodes.length > 1) {
188-
console.warn(
182+
issueWarning(
189183
`WARNING: Since Vaadin 23, placing multiple fields directly to a <vaadin-form-item> is deprecated.
190184
Please wrap fields with a <vaadin-custom-field> instead.`,
191185
);

packages/form-layout/test/form-item.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import sinon from 'sinon';
44
import '../src/vaadin-form-item.js';
55
import '@vaadin/custom-field/src/vaadin-custom-field.js';
66
import '@vaadin/text-field/src/vaadin-text-field.js';
7+
import { clearWarnings } from '@vaadin/component-base/src/warnings.js';
78

89
describe('form-item', () => {
910
let item, label, input;
@@ -536,6 +537,7 @@ describe('form-item', () => {
536537

537538
afterEach(() => {
538539
stub.restore();
540+
clearWarnings();
539541
});
540542

541543
it('should not warn when a single field is placed to an item', async () => {

0 commit comments

Comments
 (0)