Skip to content

Commit

Permalink
Add split variable declarations (#158)
Browse files Browse the repository at this point in the history
* Add split variable declarations

* Split variables just for DefineMap, DefineList and Component

* Add a test without DefineMap, DefineList and Component
  • Loading branch information
cherifGsoul authored Jan 23, 2020
1 parent 2c65b06 commit 893dd9d
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 0 deletions.
41 changes: 41 additions & 0 deletions build/transforms.json
Original file line number Diff line number Diff line change
Expand Up @@ -1944,5 +1944,46 @@
"version": "6"
}
]
},
{
"copy": [
{
"input": "variable-declaration/chained-declaration.js",
"outputPath": "variable-declaration/chained-declaration.js",
"type": "transform",
"version": "6",
"order": 1
},
{
"input": "variable-declaration/chained-declaration-input.js",
"outputPath": "variable-declaration/chained-declaration-input.js",
"type": "fixture",
"version": "6"
},
{
"input": "variable-declaration/chained-declaration-output.js",
"outputPath": "variable-declaration/chained-declaration-output.js",
"type": "fixture",
"version": "6"
},
{
"input": "variable-declaration/chained-declaration-simple-input.js",
"outputPath": "variable-declaration/chained-declaration-simple-input.js",
"type": "fixture",
"version": "6"
},
{
"input": "variable-declaration/chained-declaration-simple-output.js",
"outputPath": "variable-declaration/chained-declaration-simple-output.js",
"type": "fixture",
"version": "6"
},
{
"input": "variable-declaration/chained-declaration-test.js",
"outputPath": "variable-declaration/chained-declaration-test.js",
"type": "test",
"version": "6"
}
]
}
]
26 changes: 26 additions & 0 deletions src/templates/variable-declaration/chained-declaration-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const Singleton = DefineMap.extend({
foo: "number",
}),
singleton = new Singleton({
foo: 2
});

var Foo = DefineMap.extend({
foo: "number",
}),
foo = new Foo({
foo: 2
});

/**
* @prop
*/
var Bar = DefineMap.extend({
foo: "number",
}),
/**
* @prop
*/
Bar = new Bar({
foo: 2
});
29 changes: 29 additions & 0 deletions src/templates/variable-declaration/chained-declaration-output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const Singleton = DefineMap.extend({
foo: "number",
});

const singleton = new Singleton({
foo: 2
});

var Foo = DefineMap.extend({
foo: "number",
});

var foo = new Foo({
foo: 2
});

/**
* @prop
*/
var Bar = DefineMap.extend({
foo: "number",
});

/**
* @prop
*/
var Bar = new Bar({
foo: 2
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const foo = 'bar', baz = 'qux';

let anObj = {}, anArray = [1, 2, 3];

var anFn = function () {

},
aStr = 'qwerty';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const foo = 'bar', baz = 'qux';

let anObj = {}, anArray = [1, 2, 3];

var anFn = function () {

},
aStr = 'qwerty';
25 changes: 25 additions & 0 deletions src/templates/variable-declaration/chained-declaration-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require('mocha');
const utils = require('../../../../test/utils');
const transforms = require('../../../../');

const toTest = transforms.filter(function(transform) {
return transform.name === 'variable-declaration/chained-declaration.js';
})[0];

describe('variable-declaration/chained-declaration', function() {

it('splits chained var declaration', function() {
const fn = require(toTest.file);
const inputPath = `fixtures/version-6/${toTest.fileName.replace('.js', '-input.js')}`;
const outputPath = `fixtures/version-6/${toTest.fileName.replace('.js', '-output.js')}`;
utils.diffFiles(fn, inputPath, outputPath);
});

it('Doesn not split chained var declaration DefineMap, DefineList and Component are not defined', function() {
const fn = require(toTest.file);
const inputPath = `fixtures/version-6/${toTest.fileName.replace('.js', '-simple-input.js')}`;
const outputPath = `fixtures/version-6/${toTest.fileName.replace('.js', '-simple-output.js')}`;
utils.diffFiles(fn, inputPath, outputPath);
});

});
61 changes: 61 additions & 0 deletions src/templates/variable-declaration/chained-declaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import makeDebug from 'debug';
import fileTransform from '../../../utils/fileUtil';

export default function transformer(file, api) {
const debug = makeDebug(`can-migrate:variable-declaration/chained-declaration:${file.path}`);
const j = api.jscodeshift;

return fileTransform(file, function (source) {
if (
hasDeclarations({j, source, objectName: 'DefineMap'}) ||
hasDeclarations({j, source, objectName: 'DefineList'}) ||
hasDeclarations({j, source, objectName: 'Component'})
) {

// Filter declarations except the declaration inside "for" statement
const chainedDeclarations = j(source)
.find(j.VariableDeclaration)
.filter(variableDeclaration => (
variableDeclaration.value.declarations.length > 1
))
.filter(variableDeclaration => (
variableDeclaration.parent.value.type !== 'ForStatement'
));

return chainedDeclarations.forEach(chainedDeclaration => {
const kind = chainedDeclaration.value.kind;
debug(`Splitting chained ${kind} declaration`);
j(chainedDeclaration)
.replaceWith(chainedDeclaration.value.declarations.map((declaration, i) => {
const unchainedDeclaration =
j.variableDeclaration(kind, [declaration]);

if (i === 0) {
unchainedDeclaration.comments = chainedDeclaration.value.comments;
} else if (declaration.comments) {
unchainedDeclaration.comments = declaration.comments;
declaration.comments = null;
}

return unchainedDeclaration;
}));
}).toSource();
}
return source.toSource();
});
}

function hasDeclarations({j, source, objectName = 'DefineMap'} = {}) {
const declarations = j(source).find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: {
name: objectName
},
property: {
name: 'extend'
}
}
});
return declarations.length > 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const Singleton = DefineMap.extend({
foo: "number",
}),
singleton = new Singleton({
foo: 2
});

var Foo = DefineMap.extend({
foo: "number",
}),
foo = new Foo({
foo: 2
});

/**
* @prop
*/
var Bar = DefineMap.extend({
foo: "number",
}),
/**
* @prop
*/
Bar = new Bar({
foo: 2
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const Singleton = DefineMap.extend({
foo: "number",
});

const singleton = new Singleton({
foo: 2
});

var Foo = DefineMap.extend({
foo: "number",
});

var foo = new Foo({
foo: 2
});

/**
* @prop
*/
var Bar = DefineMap.extend({
foo: "number",
});

/**
* @prop
*/
var Bar = new Bar({
foo: 2
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const foo = 'bar', baz = 'qux';

let anObj = {}, anArray = [1, 2, 3];

var anFn = function () {

},
aStr = 'qwerty';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const foo = 'bar', baz = 'qux';

let anObj = {}, anArray = [1, 2, 3];

var anFn = function () {

},
aStr = 'qwerty';
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,4 @@ require('../lib/transforms/version-6/can-property-definitions/observable-default
require('../lib/transforms/version-6/can-property-definitions/observable-object-stache-element-sealed-test.js');
require('../lib/transforms/version-6/can-rest-model/map-list-test.js');
require('../lib/transforms/version-6/can-rest-model/map-list-shorthand-test.js');
require('../lib/transforms/version-6/variable-declaration/chained-declaration-test.js');

0 comments on commit 893dd9d

Please sign in to comment.