Skip to content

Instantly share code, notes, and snippets.

@jgable
Forked from ide/requiresToImports.js
Last active June 1, 2017 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgable/c7a4d0485495e22c0a9ef36cd44ed5d2 to your computer and use it in GitHub Desktop.
Save jgable/c7a4d0485495e22c0a9ef36cd44ed5d2 to your computer and use it in GitHub Desktop.
Converts commonJS requires to es6 imports
/**
### Regular component require statement
```
const Token = require('v2/core/components/tokenizer/token');
-> import Token from 'v2/core/components/tokenizer/token';
```
### Destructured require statement
```
const {OverlayTrigger, Tooltip} = require('react-bootstrap');
-> import {OverlayTrigger, Tooltip} from 'react-bootstrap';
```
### Regular require with member expression
```
const identity = require('underscore').identity;
-> import {identity} from 'underscore';
```
### Destructured require with member expression
```
const {
PERFORMANCE_QUESTION,
PREPARATION_QUESTION,
SATISFACTION_QUESTION,
PLANNING_QUESTION,
} = require('v2/core/constants/studentAssessmentReflection').REFLECTION_QUESTIONS
-> import {REFLECTION_QUESTIONS} from 'v2/core/constants/studentAssessmentReflection';
-> const {
PERFORMANCE_QUESTION,
PREPARATION_QUESTION,
SATISFACTION_QUESTION,
PLANNING_QUESTION,
} = REFLECTION_QUESTIONS;
```
*/
'use strict';
module.exports = function(fileInfo, api) {
var j = api.jscodeshift;
var root = j(fileInfo.source);
// This converts straight forward requires like ; const thing = require('thing');
root
.find(j.VariableDeclaration, {
declarations: [{
type: 'VariableDeclarator',
init: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'require',
},
},
}],
})
.filter(isTopLevel)
.forEach(function(path) {
const dec = path.value.declarations[0];
const id = dec.id;
const source = dec.init.arguments[0];
const comments = path.value.comments;
const loc = path.value.loc;
path.replace(
j.importDeclaration(
[{
type: 'ImportDefaultSpecifier',
id
}],
source
)
);
path.value.loc = loc;
path.value.comments = comments;
});
// This handles more complicated cases like; const {A, B} = require('stuff').THINGS;
root
.find(j.VariableDeclaration, {
declarations: [{
type: 'VariableDeclarator',
init: {
type: 'MemberExpression',
object: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'require'
},
},
},
}],
})
.filter(isTopLevel)
.forEach(function(path) {
const dec = path.value.declarations[0];
const name = dec.id;
const source = dec.init.object.arguments[0];
const id = dec.init.property;
const comments = path.value.comments;
const loc = path.value.loc;
let spec = {
type: 'ImportSpecifier',
id,
}
if (name.name !== id.name && name.type !== 'ObjectPattern') {
spec['name'] = name;
}
if (name.type === 'ObjectPattern') {
// Handle destructured member expression with two statements
path.insertAfter(j.variableDeclaration('const',[
j.variableDeclarator(name, id),
]));
}
path.replace(j.importDeclaration([spec], source));
path.value.loc = loc;
path.value.comments = comments;
});
return root.toSource();
};
function isTopLevel(path) {
return !path.parentPath.parentPath.parentPath.parentPath;
}
// Regular component require statement; should be
// import Token from 'core/components/tokenizer/token';
const Token = require('core/components/tokenizer/token');
// Destructured require statement; should be
// import {OverlayTrigger, Tooltip} from 'react-bootstrap';
const {OverlayTrigger, Tooltip} = require('react-bootstrap');
// Regular require with member expression; should be
// import {identity} from 'underscore';
const identity = require('underscore').identity;
// Destructured require with member expression; should be
// import {REFLECTION_QUESTIONS} from 'core/constants/reflection';
// const {
// PERFORMANCE_QUESTION,
// PREPARATION_QUESTION,
// SATISFACTION_QUESTION,
// PLANNING_QUESTION,
// } = REFLECTION_QUESTIONS;
const {
PERFORMANCE_QUESTION,
PREPARATION_QUESTION,
SATISFACTION_QUESTION,
PLANNING_QUESTION,
} = require('core/constants/reflection').REFLECTION_QUESTIONS;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment