Skip to content

Commit

Permalink
Fix inline DefineMap.extend as a property (#153)
Browse files Browse the repository at this point in the history
* Fix inline DefineMap.extend as a property

* Separate export handling from property

* Handle parentPath value if undefined

* Use class expression
  • Loading branch information
cherifGsoul authored Jan 9, 2020
1 parent dfdb40e commit 587bec8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 7 deletions.
12 changes: 12 additions & 0 deletions build/transforms.json
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,18 @@
"type": "fixture",
"version": "6"
},
{
"input": "can-observable-object/observable-object-inline-input.js",
"outputPath": "can-observable-object/observable-object-inline-input.js",
"type": "fixture",
"version": "6"
},
{
"input": "can-observable-object/observable-object-inline-output.js",
"outputPath": "can-observable-object/observable-object-inline-output.js",
"type": "fixture",
"version": "6"
},
{
"input": "can-observable-object/observable-object-test.js",
"outputPath": "can-observable-object/observable-object-test.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const TodosList = DefineList.extend({
"#": DefineMap.extend({
id: "number",
name: "string"
})
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const TodosList = DefineList.extend({
"#": class extends ObservableObject {
static get props() {
return {
id: "number",
name: "string"
};
}
}
});
7 changes: 7 additions & 0 deletions src/templates/can-observable-object/observable-object-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ describe('can-observable-object/observable-object', function() {
utils.diffFiles(fn, inputPath, outputPath);
});

it('Converts inline DefineMap.extend to class extends ObservableObject', function() {
const fn = require(toTest.file);
const inputPath = `fixtures/version-6/${toTest.fileName.replace('.js', '-inline-input.js')}`;
const outputPath = `fixtures/version-6/${toTest.fileName.replace('.js', '-inline-output.js')}`;
utils.diffFiles(fn, inputPath, outputPath);
});

});
20 changes: 13 additions & 7 deletions src/utils/defineTransform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createClass, createMethod } from './classUtils';
import replaceRefs from './replaceRefs';

const transformInlineMap = (path) => path.value.arguments.length === 2 ? path.value.arguments[0].value : 'Model';

// can-define transform util
// used to transform can-define/map & can-define/list
export default function defineTransform ({
Expand Down Expand Up @@ -30,19 +32,19 @@ export default function defineTransform ({
let classPath;
let refUpdate;

var parentPathValueType = path.parentPath && path.parentPath.value && path.parentPath.value.type;

// Replace variable declarations with class def
if (path.parentPath && path.parentPath.value && path.parentPath.value.type === 'VariableDeclarator') {
if (parentPathValueType && (path.parentPath && path.parentPath.value && path.parentPath.value.type === 'VariableDeclarator')) {
varDeclaration = path.parentPath.value.id.name;
classPath = path.parentPath.parentPath.parentPath;
// Handle default exports
} else if (path.parentPath && path.parentPath.value && path.parentPath.value.type === 'ExportDefaultDeclaration') {
} else if (parentPathValueType && (path.parentPath && path.parentPath.value && path.parentPath.value.type === 'ExportDefaultDeclaration')) {
// If we have "default" export if the DefineMap or DefineList has two arguments, use the first as the name of the class
// fallback to using `Model` if not
varDeclaration = path.value.arguments.length === 2 ?
path.value.arguments[0].value :
'Model';
varDeclaration = transformInlineMap(path);
classPath = path;
} else if (path.parentPath && path.parentPath.value && path.parentPath.value.type === 'AssignmentExpression') {
} else if (parentPathValueType && (path.parentPath && path.parentPath.value && path.parentPath.value.type === 'AssignmentExpression')) {
classPath = path.parentPath.parentPath;
// Use either the first argument if there are more than one
// or use the expression ie. Message.List = DefineList {...}
Expand All @@ -56,6 +58,10 @@ export default function defineTransform ({
objectName: path.parentPath.value.left.object.name,
propertyName: path.parentPath.value.left.property.name
};
} else if (parentPathValueType && path.parentPath.value.type === 'Property') {
// Handle Define.extend as a property like '#': DefineMap.extend
// the class will be just class expression without a name
classPath = path;
}

let propDefinitionsArg = path.value.arguments.length === 1 ?
Expand All @@ -80,7 +86,7 @@ export default function defineTransform ({

const classDeclaration = createClass({
j,
className: varDeclaration,
className: varDeclaration ? varDeclaration : '',
body: [
createMethod({
j,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const TodosList = DefineList.extend({
"#": DefineMap.extend({
id: "number",
name: "string"
})
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const TodosList = DefineList.extend({
"#": class extends ObservableObject {
static get props() {
return {
id: "number",
name: "string"
};
}
}
});

0 comments on commit 587bec8

Please sign in to comment.