diff --git a/packages/@lwc/babel-plugin-component/src/__tests__/wire-decorator.spec.js b/packages/@lwc/babel-plugin-component/src/__tests__/wire-decorator.spec.js index 437631a7d6..40b47d3c8f 100644 --- a/packages/@lwc/babel-plugin-component/src/__tests__/wire-decorator.spec.js +++ b/packages/@lwc/babel-plugin-component/src/__tests__/wire-decorator.spec.js @@ -101,6 +101,24 @@ describe('Transform property', () => { } ); + pluginTest( + 'decorator expects wire adapter to be imported', + ` + import { LightningElement, wire } from 'lwc'; + export default class PublicMethods extends LightningElement { + @wire(adapter) foo; + } + `, + { + error: { + message: 'Failed to resolve @wire adapter "adapter". Ensure it is imported', + loc: { + line: 2, + column: 4, + }, + }, + } + ); pluginTest( 'decorator expects wire adapter as first parameter', ` diff --git a/packages/@lwc/babel-plugin-component/src/decorators/wire/validate.js b/packages/@lwc/babel-plugin-component/src/decorators/wire/validate.js index a4eca1ea81..057adb8049 100644 --- a/packages/@lwc/babel-plugin-component/src/decorators/wire/validate.js +++ b/packages/@lwc/babel-plugin-component/src/decorators/wire/validate.js @@ -20,14 +20,25 @@ function validateWireParameters(path) { }); } - if (!id.isIdentifier()) { + const isIdentifier = id.isIdentifier(); + + if (!isIdentifier) { throw generateError(id, { errorInfo: DecoratorErrors.FUNCTION_IDENTIFIER_SHOULD_BE_FIRST_PARAMETER, }); } + // ensure wire adapter is imported + if (isIdentifier && !path.scope.getBinding(id.node.name)) { + throw generateError(id, { + errorInfo: DecoratorErrors.WIRE_ADAPTER_SHOULD_BE_IMPORTED, + messageArgs: [id.node.name], + }); + } + + // ensure wire adapter is a first parameter if ( - id.isIdentifier() && + isIdentifier && !path.scope.getBinding(id.node.name).path.isImportSpecifier() && !path.scope.getBinding(id.node.name).path.isImportDefaultSpecifier() ) { diff --git a/packages/@lwc/errors/src/compiler/error-info/index.ts b/packages/@lwc/errors/src/compiler/error-info/index.ts index c803c7a730..9552d6d576 100644 --- a/packages/@lwc/errors/src/compiler/error-info/index.ts +++ b/packages/@lwc/errors/src/compiler/error-info/index.ts @@ -6,7 +6,7 @@ */ /** * TODO: W-5678919 - implement script to determine the next available error code - * Next error code: 1118 + * Next error code: 1119 */ export * from './compiler'; diff --git a/packages/@lwc/errors/src/compiler/error-info/lwc-class.ts b/packages/@lwc/errors/src/compiler/error-info/lwc-class.ts index 3047838f28..f3e33f9945 100644 --- a/packages/@lwc/errors/src/compiler/error-info/lwc-class.ts +++ b/packages/@lwc/errors/src/compiler/error-info/lwc-class.ts @@ -197,4 +197,11 @@ export const DecoratorErrors = { level: DiagnosticLevel.Error, url: '', }, + + WIRE_ADAPTER_SHOULD_BE_IMPORTED: { + code: 1119, + message: 'Failed to resolve @wire adapter "{0}". Ensure it is imported.', + level: DiagnosticLevel.Error, + url: '', + }, };