Skip to content

Commit

Permalink
fix(babel-plugin-component): validate wire adapter import (#1096)
Browse files Browse the repository at this point in the history
* fix(babel-plugin-component): validate wire adapter import

* wip: add spacing and a comment
  • Loading branch information
apapko authored Mar 7, 2019
1 parent fa645e2 commit e3525d5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/@lwc/errors/src/compiler/error-info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 7 additions & 0 deletions packages/@lwc/errors/src/compiler/error-info/lwc-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
},
};

0 comments on commit e3525d5

Please sign in to comment.