Skip to content

Commit

Permalink
fix(compiler): log warning for missing name/namespace (#4825)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson authored Dec 16, 2024
1 parent c4218fc commit 5d1837b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024, salesforce.com, 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 { afterEach, beforeEach, expect, vi, test } from 'vitest';
import { transformSync } from '@babel/core';
import plugin from '../index';

let spy;

beforeEach(() => {
spy = vi.spyOn(console, 'warn');
});

afterEach(() => {
spy!.mockReset();
});

test('warns on missing name/namespace', () => {
const source = `
import { LightningElement } from 'lwc';
export default class extends LightningElement {};
`;

const { code } = transformSync(source, {
babelrc: false,
configFile: false,
filename: `foo.js`,
plugins: [
[
plugin,
{
namespace: '',
name: '',
},
],
],
})!;

// compilation works successfully
expect(code).toBeTypeOf('string');

expect(spy!).toHaveBeenCalledOnce();
expect(spy!).toHaveBeenCalledWith(
'The namespace and name should both be non-empty strings. You may get unexpected behavior at runtime. Found: namespace="" and name=""'
);
});
4 changes: 2 additions & 2 deletions packages/@lwc/babel-plugin-component/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export interface LwcBabelPluginOptions {
loader?: string;
strictSpecifier?: boolean;
};
namespace?: string;
name?: string;
namespace: string;
name: string;
instrumentation?: InstrumentationObject;
apiVersion?: number;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/@lwc/compiler/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ export interface DynamicImportConfig {
*/
export interface TransformOptions {
/** The name of the component. For example, the name in `<my-component>` is `"component"`. */
name?: string;
name: string;
/** The namespace of the component. For example, the namespace in `<my-component>` is `"my"`. */
namespace?: string;
namespace: string;
/** @deprecated Ignored by compiler. */
stylesheetConfig?: StylesheetConfig;
// TODO [#5031]: Unify dynamicImports and experimentalDynamicComponent options
Expand Down
11 changes: 11 additions & 0 deletions packages/@lwc/rollup-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,17 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
// a '/' regardless of Windows vs Unix, since it comes from the Rollup `id`
specifier?.split('/') ?? path.dirname(filename).split('/').slice(-2);

/* v8 ignore next */
if (!namespace || !name) {
// TODO [#4824]: Make this an error rather than a warning
this.warn(
'The component namespace and name could not be determined from the specifier ' +
JSON.stringify(specifier) +
' or filename ' +
JSON.stringify(filename)
);
}

const apiVersionToUse = getAPIVersionFromNumber(apiVersion);

const { code, map, warnings } = transformSync(src, filename, {
Expand Down
12 changes: 12 additions & 0 deletions packages/@lwc/shared/src/custom-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
* @returns component tag name
*/
export function generateCustomElementTagName(namespace: string = '', name: string = '') {
if (!namespace || !name) {
// TODO [#4824]: Make this an error rather than a warning
// eslint-disable-next-line no-console
console.warn(
'The namespace and name should both be non-empty strings. ' +
'You may get unexpected behavior at runtime. ' +
'Found: namespace=' +
JSON.stringify(namespace) +
' and name=' +
JSON.stringify(namespace)
);
}
const kebabCasedName = name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
return `${namespace}-${kebabCasedName}`;
}

0 comments on commit 5d1837b

Please sign in to comment.