Skip to content

Commit 341fec4

Browse files
authored
feat(config): allow suppressing reserved public name warning (#6389)
* feat(config): allow suppressing reserved public name warning * chore(prettier): format prop decorator map call * test(config): cover suppressReservedPublicNameWarnings
1 parent d442307 commit 341fec4

8 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/compiler/config/test/validate-config.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ describe('validation', () => {
116116
});
117117
});
118118

119+
describe('suppressReservedPublicNameWarnings', () => {
120+
it.each([true, false])('sets suppressReservedPublicNameWarnings to %p when provided', (bool) => {
121+
userConfig.suppressReservedPublicNameWarnings = bool;
122+
const { config } = validateConfig(userConfig, bootstrapConfig);
123+
expect(config.suppressReservedPublicNameWarnings).toBe(bool);
124+
});
125+
126+
it('defaults suppressReservedPublicNameWarnings to false', () => {
127+
const { config } = validateConfig(userConfig, bootstrapConfig);
128+
expect(config.suppressReservedPublicNameWarnings).toBe(false);
129+
});
130+
});
131+
119132
describe('enableCache', () => {
120133
it('set enableCache true', () => {
121134
userConfig.enableCache = true;

src/compiler/config/validate-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export const validateConfig = (
205205
setBooleanConfig(validatedConfig, 'autoprefixCss', null, validatedConfig.buildEs5);
206206
setBooleanConfig(validatedConfig, 'validateTypes', null, !validatedConfig._isTesting);
207207
setBooleanConfig(validatedConfig, 'allowInlineScripts', null, true);
208+
setBooleanConfig(validatedConfig, 'suppressReservedPublicNameWarnings', null, false);
208209

209210
if (!isString(validatedConfig.taskQueue)) {
210211
validatedConfig.taskQueue = 'async';

src/compiler/transformers/decorators-to-static/convert-decorators.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ const visitClassDeclaration = (
119119
// parse member decorators (Prop, State, Listen, Event, Method, Element and Watch)
120120
if (decoratedMembers.length > 0) {
121121
propDecoratorsToStatic(
122+
config,
122123
diagnostics,
123124
decoratedMembers,
124125
typeChecker,

src/compiler/transformers/decorators-to-static/method-decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const parseMethodDecorator = (
8989
}
9090

9191
// Validate if the method name does not conflict with existing public names
92-
validatePublicName(diagnostics, methodName, '@Method()', 'method', method.name);
92+
validatePublicName(config, diagnostics, methodName, '@Method()', 'method', method.name);
9393

9494
const methodMeta: d.ComponentCompilerStaticMethod = {
9595
complexType: {

src/compiler/transformers/decorators-to-static/prop-decorator.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { getDecoratorParameters, isDecoratorNamed } from './decorator-utils';
3030
* @param decoratorName the name of the decorator to look for
3131
*/
3232
export const propDecoratorsToStatic = (
33+
config: d.ValidatedConfig,
3334
diagnostics: d.Diagnostic[],
3435
decoratedProps: ts.ClassElement[],
3536
typeChecker: ts.TypeChecker,
@@ -39,7 +40,7 @@ export const propDecoratorsToStatic = (
3940
): void => {
4041
const properties = decoratedProps
4142
.filter((prop) => ts.isPropertyDeclaration(prop) || ts.isGetAccessor(prop))
42-
.map((prop) => parsePropDecorator(diagnostics, typeChecker, program, prop, decoratorName, newMembers))
43+
.map((prop) => parsePropDecorator(config, diagnostics, typeChecker, program, prop, decoratorName, newMembers))
4344
.filter((prop): prop is ts.PropertyAssignment => prop != null);
4445

4546
if (properties.length > 0) {
@@ -59,6 +60,7 @@ export const propDecoratorsToStatic = (
5960
* @returns a property assignment expression to be added to the Stencil component's class
6061
*/
6162
const parsePropDecorator = (
63+
config: d.ValidatedConfig,
6264
diagnostics: d.Diagnostic[],
6365
typeChecker: ts.TypeChecker,
6466
program: ts.Program,
@@ -88,7 +90,7 @@ const parsePropDecorator = (
8890
warn.messageText = `The @Prop() name "${propName}" looks like an event. Please use the "@Event()" decorator to expose events instead, not properties or methods.`;
8991
augmentDiagnosticWithNode(warn, prop.name);
9092
} else {
91-
validatePublicName(diagnostics, propName, '@Prop()', 'prop', prop.name);
93+
validatePublicName(config, diagnostics, propName, '@Prop()', 'prop', prop.name);
9294
}
9395

9496
const symbol = typeChecker.getSymbolAtLocation(prop.name);

src/compiler/transformers/reserved-public-members.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ import type * as d from '../../declarations';
1414
* @param node the TypeScript AST node at which the class member is defined
1515
*/
1616
export const validatePublicName = (
17+
config: d.ValidatedConfig,
1718
diagnostics: d.Diagnostic[],
1819
memberName: string,
1920
decorator: string,
2021
memberType: string,
2122
node: ts.Node,
2223
): void => {
24+
if (config.suppressReservedPublicNameWarnings) {
25+
return;
26+
}
27+
2328
if (RESERVED_PUBLIC_MEMBERS.has(memberName.toLowerCase())) {
2429
const warn = buildWarn(diagnostics);
2530
warn.messageText = [

src/declarations/stencil-public-compiler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ export interface StencilConfig {
152152
* This behavior defaults to `true`, but may be opted-out of by setting this flag to `false`.
153153
*/
154154
transformAliasedImportPaths?: boolean;
155+
/**
156+
* When `true`, Stencil will suppress diagnostics which warn about public members using reserved names
157+
* (for example, decorating a method named `focus` with `@Method()`). Defaults to `false`.
158+
*/
159+
suppressReservedPublicNameWarnings?: boolean;
155160
/**
156161
* When `true`, we will validate a project's `package.json` based on the output target the user has designated
157162
* as `isPrimaryPackageOutputTarget: true` in their Stencil config.

src/testing/mocks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export function mockValidatedConfig(overrides: Partial<d.ValidatedConfig> = {}):
4545
rootDir,
4646
srcDir: '/src',
4747
srcIndexHtml: 'src/index.html',
48+
suppressReservedPublicNameWarnings: false,
4849
sys: createTestingSystem(),
4950
testing: {},
5051
transformAliasedImportPaths: true,
@@ -102,6 +103,7 @@ export function mockConfig(overrides: Partial<d.UnvalidatedConfig> = {}): d.Unva
102103
},
103104
rootDir,
104105
sourceMap: true,
106+
suppressReservedPublicNameWarnings: false,
105107
sys,
106108
testing: null,
107109
validateTypes: false,

0 commit comments

Comments
 (0)