Skip to content

Commit

Permalink
convert custom types into maybeConverts
Browse files Browse the repository at this point in the history
convert `prop: MyMap` into `prop: type.maybeConvert(MyMap)`
  • Loading branch information
Mattchewone committed Jul 15, 2019
1 parent d1fe5ad commit a57894b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class Bar extends DefineObject {
name: 'string',
items: {
enumerable: false,
type: List,
type: type.maybeConvert(List),

get default() {
return [];
}
},
list: {
type: List,
type: type.maybeConvert(List),

get default() {
return new List();
Expand Down Expand Up @@ -77,13 +77,13 @@ class Baz extends DefineObject {
},
items: {
enumerable: false,
type: List,
type: type.maybeConvert(List),
default () {
return 'World!'
}
},
list: {
type: List,
type: type.maybeConvert(List),
default: List
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function transformer(file, api) {
return props.forEach(prop => {
const { nestedProp, propConversions, defaultLiteral } = prop.value.properties
.reduce((acc, path) => {
if (path.value.type === 'Literal' && path.key.name === 'type') {
if ((path.value.type === 'Literal' || path.value.type === 'Identifier') && path.key.name.toLowerCase() === 'type') {
acc.nestedProp = path;
}
if (path.value.type === 'Identifier' && path.key.name === 'Default') {
Expand All @@ -29,9 +29,9 @@ function transformer(file, api) {

// Convert "types" to maybeConverts
if (nestedProp) {
const type = nestedProp.value.value;
debug(`Converting property ${type} -> ${typeConversions[type]}`);
nestedProp.value = typeConversions[type];
const type = nestedProp.value.value || nestedProp.value.name;
debug(`Converting property ${type}.`);
nestedProp.value = typeConversions(type);
}

// Check for Type to be converted
Expand Down
2 changes: 1 addition & 1 deletion src/templates/can-property-definitions/shorthands-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CustomScroll extends StacheDefineElement {
static get define() {
return {
scrolled: 'boolean',
elem: 'string',
elem: MyMap,
something: {
type: 'string',
get (lastSet, resolve) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CustomScroll extends StacheDefineElement {
static get define() {
return {
scrolled: type.maybeConvert(Boolean),
elem: type.maybeConvert(String),
elem: type.maybeConvert(MyMap),
something: {
type: 'string',
get (lastSet, resolve) {
Expand Down
26 changes: 21 additions & 5 deletions src/templates/can-property-definitions/shorthands.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,33 @@ function transformer(file, api) {
return fileTransform(file, function (source) {
const root = j(source);

return find(root, 'Literal', function (props) {
// Update `prop: 'string'`
// into `prop: type.maybeConvert(String)`
find(root, 'Literal', function (props) {
return props.forEach(prop => {
const type = prop.value.value;
debug(`Converting ${type} -> ${typeConversions[type]}`);
prop.value = typeConversions[type];
debug(`Converting ${type}`);
prop.value = typeConversions(type);

// Add the import
addImport(j, root, { importName: 'type', hasSiblings: ['DefineArray', 'DefineObject', 'StacheDefineElement'] });
});
})
.toSource();
});

// Update `prop: MyMap`
// into `prop: type.maybeConvert(MyMapp)`
find(root, 'Identifier', function (props) {
return props.forEach(prop => {
const type = prop.value.name;
debug(`Converting ${type}`);
prop.value = typeConversions(type);

// Add the import
addImport(j, root, { importName: 'type', hasSiblings: ['DefineArray', 'DefineObject', 'StacheDefineElement'] });
});
});

return root.toSource();
});
}

Expand Down
16 changes: 15 additions & 1 deletion src/utils/typeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const maybeConvert = (val) => j.callExpression(
[j.identifier(val)]
);

export const typeConversions = {
const conversions = {
'string': maybeConvert('String'),
'number': maybeConvert('Number'),
'date': maybeConvert('Date'),
Expand All @@ -19,6 +19,20 @@ export const typeConversions = {
)
};

// Convert known types and unknown types into type.maybeConvert's
export const typeConversions = (type) => {
// Known primitive types
if (conversions[type]) {
return conversions[type];
} else {
// Unkown's should be converted
// prop: MyMap
// converts into
// prop: type.maybeConvert(MyMap)
return maybeConvert(type);
}
};

export const find = (root, type, cb = () => {}) => {
return root
.find(j.MethodDefinition, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class Bar extends DefineObject {
name: 'string',
items: {
enumerable: false,
type: List,
type: type.maybeConvert(List),

get default() {
return [];
}
},
list: {
type: List,
type: type.maybeConvert(List),

get default() {
return new List();
Expand Down Expand Up @@ -77,13 +77,13 @@ class Baz extends DefineObject {
},
items: {
enumerable: false,
type: List,
type: type.maybeConvert(List),
default () {
return 'World!'
}
},
list: {
type: List,
type: type.maybeConvert(List),
default: List
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CustomScroll extends StacheDefineElement {
static get define() {
return {
scrolled: 'boolean',
elem: 'string',
elem: MyMap,
something: {
type: 'string',
get (lastSet, resolve) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CustomScroll extends StacheDefineElement {
static get define() {
return {
scrolled: type.maybeConvert(Boolean),
elem: type.maybeConvert(String),
elem: type.maybeConvert(MyMap),
something: {
type: 'string',
get (lastSet, resolve) {
Expand Down

0 comments on commit a57894b

Please sign in to comment.