UNPKG

commonjs-to-es-module-codemod

Version:

jscodeshift codemod that convert CommonJS(require/exports) to ES Modules(import/export) for JavaScript/TypeScript

3 lines (2 loc) 6.88 kB
function e(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function r(r,t){var n;if("undefined"==typeof Symbol||null==r[Symbol.iterator]){if(Array.isArray(r)||(n=function(r,t){if(r){if("string"==typeof r)return e(r,t);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?e(r,t):void 0}}(r))||t&&r&&"number"==typeof r.length){n&&(r=n);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(n=r[Symbol.iterator]()).next.bind(n)}var t=function(){function e(e,r){this.prefix=e.path+":",this.silent=r.silent,this.verbose=r.verbose}var r=e.prototype;return r.log=function(){var e;!this.silent&&this.verbose>0&&(e=console).log.apply(e,["[LOG]",this.prefix].concat([].slice.call(arguments)))},r.warn=function(){var e;this.silent||(e=console).warn.apply(e,["[WARNING]",this.prefix].concat([].slice.call(arguments)))},r.error=function(){var e;this.silent||(e=console).error.apply(e,["[ERROR]",this.prefix].concat([].slice.call(arguments)))},r.lines=function(e){return e.loc?e.loc.start.line===e.loc.end.line?"(line "+e.loc.start.line+")":"(lines "+e.loc.start.line+" to "+e.loc.end.line+")":""},e}(),n=function(e,r){return e.Program.check(r.parent.value)};function o(e,o,i){var a=o.jscodeshift,l=new t(e,i),s=a(e.source).find(a.VariableDeclaration,{declarations:[{init:{type:"CallExpression",callee:{name:"require"}}}]}).filter(function(e){return n(a,e)});return l.log(s.length+" nodes will be transformed"),s.replaceWith(function(t){for(var n,o=[],i=[],s=r(t.node.declarations);!(n=s()).done;){var c=n.value,u=null!==c.init&&"CallExpression"===c.init.type&&"require"===c.init.callee.name;if(u&&void 0!==c.init.property)if("Identifier"===c.id.type){var p,f,m=c.init.arguments.shift();if(c.init.arguments.length)return l.error(l.lines(c)+" too many arguments.Aborting transformation"),e.source;if(!a.Literal.check(m))return l.error(l.lines(c)+" bad argument.Expecting a string literal, got "+a(m).toSource()+"`. Aborting transformation"),e.source;"Identifier"===(null==c||null==(p=c.init)?void 0:p.property.type)&&l.log("Unknown declaration",c);var d=a.importSpecifier(c.init.property,null==c||null==(f=c.init)?void 0:f.property);i.push(a.importDeclaration([d],m))}else"ObjectPattern"===c.id.type&&l.log("Does not support pattern",c);else if(u){if("Identifier"===c.id.type){var g=a.importDefaultSpecifier(c.id),h=c.init.arguments.shift();if(c.init.arguments.length)return l.error(l.lines(c)+" too many arguments.Aborting transformation"),e.source;if(!a.Literal.check(h))return l.error(l.lines(c)+" bad argument.Expecting a string literal, got "+a(h).toSource()+"`. Aborting transformation"),e.source;i.push(a.importDeclaration([g],h))}else if("ObjectPattern"===c.id.type){var b=c.id.properties.map(function(e){var r=a.identifier(e.key.name),t=a.identifier(e.value.name);return a.importSpecifier(r,t)}),v=c.init.arguments.shift();if(c.init.arguments.length)return l.error(l.lines(c)+" too many arguments.Aborting transformation"),e.source;if(!a.Literal.check(v))return l.error(l.lines(c)+" bad argument.Expecting a string literal, got "+a(v).toSource()+"`. Aborting transformation"),e.source;i.push(a.importDeclaration(b,v))}}else o.push(c)}return i.length>0&&(i[0].comments=t.node.comments),o.length>0?(l.warn(l.lines(t.node)+" introduced leftover"),[].concat(i,[a.variableDeclaration(t.node.kind,o)])):i}).toSource()}function i(e,o,i){var a=o.jscodeshift,l=new t(e,i),s=a(e.source).find(a.VariableDeclaration,{declarations:[{init:{type:"MemberExpression",object:{type:"CallExpression",callee:{name:"require"}}}}]}).filter(function(e){return n(a,e)});return l.log(s.length+" nodes will be transformed"),s.replaceWith(function(t){for(var n,o=[],i=[],s=r(t.node.declarations);!(n=s()).done;){var c=n.value;if(null!==c.init&&"MemberExpression"===c.init.type&&"CallExpression"===c.init.object.type&&"require"===c.init.object.callee.name&&void 0!==c.init.property)if("Identifier"===c.id.type){var u,p=c.init.object.arguments.shift();if(c.init.object.arguments.length)return l.error(l.lines(c)+" too many arguments.Aborting transformation"),e.source;if(!a.Literal.check(p))return l.error(l.lines(c)+" bad argument.Expecting a string literal, got "+a(p).toSource()+"`. Aborting transformation"),e.source;if("Identifier"!==(null==c||null==(u=c.init)?void 0:u.property.type))return l.log("Unknown declaration",c),e.source;var f=a.importSpecifier(c.init.property,c.id);i.push(a.importDeclaration([f],p))}else"ObjectPattern"===c.id.type&&l.log("Does not support pattern",c);else o.push(c)}return i.length>0&&(i[0].comments=t.node.comments),o.length>0?(l.warn(l.lines(t.node)+" introduced leftover"),[].concat(i,[a.variableDeclaration(t.node.kind,o)])):i}).toSource()}function a(e,r,o){var i=r.jscodeshift,a=new t(e,o),l=i(e.source).find(i.ExpressionStatement,{expression:{left:{object:{name:"module"},property:{name:"exports"}},operator:"="}}).filter(function(e){return n(i,e)});return l.length>1?(a.error("There should not be more than one `module.exports` declaration in a file. Aborting transformation"),e.source):(a.log(l.length+" nodes will be transformed"),l.replaceWith(function(e){var r=i.exportDefaultDeclaration(e.node.expression.right);return r.comments=e.node.comments,r}).toSource())}function l(e,r,o){var i=r.jscodeshift,a=function(e){return n(i,e)},l=new t(e,o),s=i(e.source),c=s.find(i.ExpressionStatement,{expression:{left:{object:{object:{name:"module"},property:{name:"exports"}}},operator:"="}}).filter(a),u=s.find(i.ExpressionStatement,{expression:{left:{object:{name:"exports"}},operator:"="}}).filter(a);l.log(c.length+u.length+" nodes will be transformed");var p=function(e){var r=e.node,t=r.expression.left.property,n=r.expression.right;if("Identifier"===t.type&&"Identifier"===n.type){var o=i.exportNamedDeclaration(null,[i.exportSpecifier.from({exported:t,local:n})]);return o.comments=r.comments,o}var a=i.variableDeclaration("const",[i.variableDeclarator(t,n)]),l=i.exportNamedDeclaration(a);return l.comments=r.comments,l};return u.replaceWith(p),c.replaceWith(p),s.toSource()}function s(e,r,o){var i=r.jscodeshift,a=new t(e,o),l=i(e.source).find(i.ExpressionStatement,{expression:{callee:{name:"require"}}}).filter(function(e){return n(i,e)});return a.log(l.length+" nodes will be transformed"),l.replaceWith(function(e){var r=e.node.expression.arguments.pop(),t=i.importDeclaration([],r);return t.comments=e.node.comments,t}).toSource()}module.exports=function(e,r,t){return[a,i,s,o,l].reduce(function(e,n){return n({source:e},r,t)},e.source)}; //# sourceMappingURL=index.js.map