Skip to content

Commit

Permalink
Handle inline define-map static properties transformation (#157)
Browse files Browse the repository at this point in the history
* handle inline define-map static properties transformation

* Remove unused code

* Add only seal to static properties when it is true

* Seal observable objects

* Cleaning

* Just remove static properties
  • Loading branch information
cherifGsoul authored Jan 23, 2020
1 parent 893dd9d commit 9ae13c2
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 12 deletions.
12 changes: 12 additions & 0 deletions build/transforms.json
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,18 @@
"type": "fixture",
"version": "6"
},
{
"input": "can-observable-object/observable-object-static-properties-input.js",
"outputPath": "can-observable-object/observable-object-static-properties-input.js",
"type": "fixture",
"version": "6"
},
{
"input": "can-observable-object/observable-object-static-properties-output.js",
"outputPath": "can-observable-object/observable-object-static-properties-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,11 @@
const Todo = DefineMap.extend('Foo', { bar: 'Bar', seal: true}, {
foo: "number"
});

const MyTodo = DefineMap.extend({ bar: 'Bar', seal: false}, {
foo: "number"
});

const MyOtherTodo = DefineMap.extend('Foo', {
foo: "number"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Todo extends ObservableObject {
static get props() {
return {
foo: "number"
};
}
}

class MyTodo extends ObservableObject {
static get props() {
return {
foo: "number"
};
}
}

class MyOtherTodo extends ObservableObject {
static get props() {
return {
foo: "number"
};
}
}
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 @@ -29,4 +29,11 @@ describe('can-observable-object/observable-object', function() {
utils.diffFiles(fn, inputPath, outputPath);
});

it('Handles DefineMap static propeties to ObservableObject static properties', function() {
const fn = require(toTest.file);
const inputPath = `fixtures/version-6/${toTest.fileName.replace('.js', '-static-properties-input.js')}`;
const outputPath = `fixtures/version-6/${toTest.fileName.replace('.js', '-static-properties-output.js')}`;
utils.diffFiles(fn, inputPath, outputPath);
});

});
35 changes: 23 additions & 12 deletions src/utils/defineTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ export default function defineTransform ({
classPath = path;
}

let propDefinitionsArg = path.value.arguments.length === 1 ?
path.value.arguments[0] :
path.value.arguments[1];
let propDefinitionsArg;

if (path.value.arguments.length === 3) {
// Handle DefineMap.extend('Foo', {//staticProps}, {protoProps})
propDefinitionsArg = path.value.arguments[2];
} else if (path.value.arguments.length === 2) {
// Handle DefineMap.extend({//staticProps}, {protoProps})
propDefinitionsArg = path.value.arguments[1];
} else if (path.value.arguments.length === 1) {
// Handle DefineMap.extend({protoProps})
propDefinitionsArg = path.value.arguments[0];
}

// Check if we have an existing varDeclaration
// if so let's create a new name to prevent clashing
Expand All @@ -84,18 +93,20 @@ export default function defineTransform ({

debug(`Replacing ${varDeclaration} with ${extendedClassName} class`);

let body = [
createMethod({
j,
method: false, // Want this to be a getter
name: 'props',
blockStatement: [j.returnStatement(propDefinitionsArg)],
isStatic: true
})
];

const classDeclaration = createClass({
j,
className: varDeclaration ? varDeclaration : '',
body: [
createMethod({
j,
method: false, // Want this to be a getter
name: 'props',
blockStatement: [j.returnStatement(propDefinitionsArg)],
isStatic: true
})
],
body: body,
extendedClassName
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const Todo = DefineMap.extend('Foo', { bar: 'Bar', seal: true}, {
foo: "number"
});

const MyTodo = DefineMap.extend({ bar: 'Bar', seal: false}, {
foo: "number"
});

const MyOtherTodo = DefineMap.extend('Foo', {
foo: "number"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Todo extends ObservableObject {
static get props() {
return {
foo: "number"
};
}
}

class MyTodo extends ObservableObject {
static get props() {
return {
foo: "number"
};
}
}

class MyOtherTodo extends ObservableObject {
static get props() {
return {
foo: "number"
};
}
}

0 comments on commit 9ae13c2

Please sign in to comment.