-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add split variable declarations (#158)
* Add split variable declarations * Split variables just for DefineMap, DefineList and Component * Add a test without DefineMap, DefineList and Component
- Loading branch information
1 parent
2c65b06
commit 893dd9d
Showing
12 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/templates/variable-declaration/chained-declaration-input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/templates/variable-declaration/chained-declaration-output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
8 changes: 8 additions & 0 deletions
8
src/templates/variable-declaration/chained-declaration-simple-input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
8 changes: 8 additions & 0 deletions
8
src/templates/variable-declaration/chained-declaration-simple-output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/templates/variable-declaration/chained-declaration-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
26 changes: 26 additions & 0 deletions
26
test/fixtures/version-6/variable-declaration/chained-declaration-input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
test/fixtures/version-6/variable-declaration/chained-declaration-output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
8 changes: 8 additions & 0 deletions
8
test/fixtures/version-6/variable-declaration/chained-declaration-simple-input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
8 changes: 8 additions & 0 deletions
8
test/fixtures/version-6/variable-declaration/chained-declaration-simple-output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters