Skip to content

Commit

Permalink
fix(compiler): optional second parameter for wire decorator (#193)
Browse files Browse the repository at this point in the history
close #181
  • Loading branch information
yungcheng authored Apr 12, 2018
1 parent 1ec2048 commit 5eb02f0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export default class Test {
}
Test.wire = {
wiredProp: {
adapter: getFoo,
params: {
key1: "prop1"
},
static: {
key2: ["fixed", 'array']
},
adapter: getFoo
}
}
};`
}
Expand All @@ -49,28 +49,28 @@ export default class Test {
}
Test.wire = {
wiredProp: {
adapter: getFoo,
params: {
key1: "prop",
key2: "prop"
},
static: {
key3: "fixed",
key4: ["fixed", 'array']
},
adapter: getFoo
}
}
};`
}
});

pluginTest('decorator expects 2 parameters', `
pluginTest('decorator expects wire adapter as first parameter', `
import { wire } from 'engine';
export default class Test {
@wire() wiredProp;
}
`, {
error: {
message: 'test.js: @wire(<adapterId>, <adapterConfig>) expects 2 parameters.',
message: 'test.js: @wire expects an adapter as first parameter. @wire(adapter: WireAdapter, config?: any).',
loc: {
line: 2,
column: 4,
Expand All @@ -95,14 +95,37 @@ export default class Test {
}
Test.wire = {
wiredProp: {
adapter: getFoo,
params: {},
static: {},
adapter: getFoo
static: {}
}
};`
}
});

pluginTest('decorator expects an optional config object as second parameter', `
import { wire } from 'engine';
import { getFoo } from 'data-service';
export default class Test {
@wire(getFoo) wiredProp;
}
`, {
output: {
code: `import { getFoo } from 'data-service';
export default class Test {
constructor() {
this.wiredProp = void 0;
}
}
Test.wire = {
wiredProp: {
adapter: getFoo
}
};`
}
});

pluginTest('decorator expects an imported identifier as first parameter', `
import { wire } from 'engine';
const ID = "adapterId"
Expand Down Expand Up @@ -210,22 +233,22 @@ export default class Test {
}
Test.wire = {
wired1: {
adapter: getFoo,
params: {
key1: "prop1"
},
static: {
key2: ["fixed"]
},
adapter: getFoo
}
},
wired2: {
adapter: getFoo,
params: {
key1: "prop1"
},
static: {
key2: ["array"]
},
adapter: getFoo
}
}
};`
}
Expand All @@ -249,13 +272,13 @@ export default class Test {
}
Test.wire = {
wiredMethod: {
adapter: getFoo,
params: {
key1: "prop1"
},
static: {
key2: ["fixed"]
},
adapter: getFoo,
method: 1
}
};`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,7 @@ function getWiredParams(t, wireConfig) {

function buildWireConfigValue(t, wiredValues) {
return t.objectExpression(wiredValues.map(wiredValue => {
const wireConfig = [
t.objectProperty(
t.identifier('params'),
t.objectExpression(wiredValue.params)
),
t.objectProperty(
t.identifier('static'),
t.objectExpression(wiredValue.static)
)
];

const wireConfig = [];
if (wiredValue.adapter) {
wireConfig.push(
t.objectProperty(
Expand All @@ -50,6 +40,24 @@ function buildWireConfigValue(t, wiredValues) {
)
}

if (wiredValue.params) {
wireConfig.push(
t.objectProperty(
t.identifier('params'),
t.objectExpression(wiredValue.params)
)
);
}

if (wiredValue.static) {
wireConfig.push(
t.objectProperty(
t.identifier('static'),
t.objectExpression(wiredValue.static)
)
)
}

if (wiredValue.isClassMethod) {
wireConfig.push(
t.objectProperty(
Expand Down Expand Up @@ -98,9 +106,12 @@ module.exports = function transform(t, klass, decorators) {

const wiredValue = {
propertyName,
isClassMethod,
static: getWiredStatic(config),
params: getWiredParams(t, config),
isClassMethod
}

if (config) {
wiredValue.static = getWiredStatic(config);
wiredValue.params = getWiredParams(t, config);
}

if (id.isIdentifier()) {
Expand All @@ -110,13 +121,18 @@ module.exports = function transform(t, klass, decorators) {
}
}

metadata.push({
const wireMetadata = {
name: wiredValue.propertyName,
adapter: wiredValue.adapter,
params: getWiredParamMetadata(wiredValue.params),
static: getWiredStaticMetadata(wiredValue.static),
type: isClassMethod ? 'method' : 'property'
});
};

if (config) {
wireMetadata.static = getWiredStaticMetadata(wiredValue.static);
wireMetadata.params = getWiredParamMetadata(wiredValue.params);
}

metadata.push(wireMetadata);

return wiredValue;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const { LWC_PACKAGE_EXPORTS: { WIRE_DECORATOR, TRACK_DECORATOR, API_DECORATOR }
function validateWireParameters(path) {
const [id, config] = path.get('expression.arguments');

if (!id || !config) {
if (!id) {
throw path.buildCodeFrameError(
`@wire(<adapterId>, <adapterConfig>) expects 2 parameters.`
`@wire expects an adapter as first parameter. @wire(adapter: WireAdapter, config?: any).`
);
}

Expand All @@ -22,7 +22,7 @@ function validateWireParameters(path) {
);
}

if (!config.isObjectExpression()) {
if (config && !config.isObjectExpression()) {
throw config.buildCodeFrameError(
`@wire expects a configuration object expression as second parameter.`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ Metadata.publicProps = {
Metadata.publicMethods = ["publicMethod"];
Metadata.wire = {
wiredProp: {
adapter: getTodo,
params: {},
static: {},
adapter: getTodo
static: {}
},
wiredMethod: {
adapter: getHello,
params: {
name: "publicProp"
},
static: {
fields: ['one', 'two']
},
adapter: getHello,
method: 1
}
};
Expand Down

0 comments on commit 5eb02f0

Please sign in to comment.