Skip to content

Commit

Permalink
Terse shorthand work start (#260)
Browse files Browse the repository at this point in the history
* Terse shorthand work start

* All tests pass now
  • Loading branch information
kristoferbaxter authored Dec 28, 2019
1 parent 539a800 commit b5b863d
Show file tree
Hide file tree
Showing 37 changed files with 74 additions and 39 deletions.
41 changes: 38 additions & 3 deletions src/transformers/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ import {
import { TransformSourceDescription, OutputOptions } from 'rollup';
import { NamedDeclaration, DefaultDeclaration } from './parsing-utilities';
import { isESMFormat } from '../options';
import { Transform, TransformInterface, ExportClosureMapping, ExportDetails } from '../types';
import {
Transform,
TransformInterface,
ExportClosureMapping,
ExportDetails,
Range,
} from '../types';
import MagicString from 'magic-string';
import { parse, walk } from '../acorn';

Expand Down Expand Up @@ -230,8 +236,37 @@ export default class ExportTransform extends Transform implements TransformInter
if (exportFromCurrentSource) {
const { object: leftObject } = ancestor.expression.left;
if (leftObject.range) {
const statement = inlineExport ? 'export var ' : 'var ';
source.overwrite(leftObject.range[0], leftObject.range[1] + 1, statement);
const { right } = ancestor.expression;
switch (right.type) {
case 'FunctionExpression':
if (right.params.length > 0) {
// FunctionExpression has parameters.
source.overwrite(
(leftObject.range as Range)[0],
(right.params[0].range as Range)[0],
`${inlineExport ? 'export ' : ''}function ${
exportDetails.exported
}(`,
);
} else {
source.overwrite(
(leftObject.range as Range)[0],
(right.body.range as Range)[0],
`${inlineExport ? 'export ' : ''}function ${
exportDetails.exported
}()`,
);
}
break;
default:
const statement = inlineExport ? 'export var ' : 'var ';
source.overwrite(
leftObject.range[0],
leftObject.range[1] + 1,
statement,
);
break;
}
}
if (exportDetails.local !== exportDetails.exported) {
exportDetails.local = exportDetails.exported;
Expand Down
2 changes: 1 addition & 1 deletion test/arrow-function/fixtures/multiple-arguments.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var multipleArguments=function(a,b){return console.log(a,b)};
export function multipleArguments(a,b){return console.log(a,b)};
2 changes: 1 addition & 1 deletion test/arrow-function/fixtures/single-argument.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var singleArgument=function(a){return console.log(a)};
export function singleArgument(a){return console.log(a)};
2 changes: 1 addition & 1 deletion test/const-rename/fixtures/rename.esm.advanced.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
let b=[3,4],c=[5,6];export var yes=function(a){return 0<=c.indexOf(a)&&0<=b.indexOf(a)};
let b=[3,4],c=[5,6];export function yes(a){return 0<=c.indexOf(a)&&0<=b.indexOf(a)};
2 changes: 1 addition & 1 deletion test/const-rename/fixtures/rename.esm.default.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
let b=[3,4],c=[5,6];export var yes=function(a){return 0<=c.indexOf(a)&&0<=b.indexOf(a)};
let b=[3,4],c=[5,6];export function yes(a){return 0<=c.indexOf(a)&&0<=b.indexOf(a)};
2 changes: 1 addition & 1 deletion test/const-rename/fixtures/rename.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var b=[3,4],c=[5,6];export var yes=function(a){return 0<=c.indexOf(a)&&0<=b.indexOf(a)};
var b=[3,4],c=[5,6];export function yes(a){return 0<=c.indexOf(a)&&0<=b.indexOf(a)};
2 changes: 1 addition & 1 deletion test/export-all/fixtures/all.esm.advanced.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var export1=1;var export2=function(){return 2};export{export1,export2};
var export1=1;function export2(){return 2};export{export1,export2};
2 changes: 1 addition & 1 deletion test/export-all/fixtures/all.esm.default.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var export1=1;var export2=function(){return 2};export{export1,export2};
var export1=1;function export2(){return 2};export{export1,export2};
2 changes: 1 addition & 1 deletion test/export-all/fixtures/all.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var export1=1;var export2=function(){return 2};export{export1,export2};
var export1=1;function export2(){return 2};export{export1,export2};
2 changes: 1 addition & 1 deletion test/export-named/fixtures/multiple.esm.advanced.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var bar=function(){console.log(1)};var foo=1;export{bar,foo};
function bar(){console.log(1)};var foo=1;export{bar,foo};
2 changes: 1 addition & 1 deletion test/export-named/fixtures/multiple.esm.default.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var bar=function(){console.log(1)};var foo=1;export{bar,foo};
function bar(){console.log(1)};var foo=1;export{bar,foo};
2 changes: 1 addition & 1 deletion test/export-named/fixtures/multiple.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var bar=function(){console.log(1)};var foo=1;export{bar,foo};
function bar(){console.log(1)};var foo=1;export{bar,foo};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
var b=Symbol.for("smth");export var isSmth=function(a){return a&&!!a[b]};
var b=Symbol.for("smth");export function isSmth(a){return a&&!!a[b]};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var b=Symbol.for("smth");export var isSmth=function(a){return a&&!!a[b]};
var b=Symbol.for("smth");export function isSmth(a){return a&&!!a[b]};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var b=Symbol.for("smth");export var isSmth=function(a){return a&&!!a[b]};
var b=Symbol.for("smth");export function isSmth(a){return a&&!!a[b]};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
2 changes: 1 addition & 1 deletion test/export-variables/fixtures/const-function.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
2 changes: 1 addition & 1 deletion test/export-variables/fixtures/function.esm.advanced.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
2 changes: 1 addition & 1 deletion test/export-variables/fixtures/function.esm.default.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
2 changes: 1 addition & 1 deletion test/export-variables/fixtures/function.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
2 changes: 1 addition & 1 deletion test/export-variables/fixtures/let-function.esm.default.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
2 changes: 1 addition & 1 deletion test/export-variables/fixtures/let-function.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
2 changes: 1 addition & 1 deletion test/export-variables/fixtures/var-function.esm.default.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
2 changes: 1 addition & 1 deletion test/export-variables/fixtures/var-function.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var foo=function(a){console.log(a)};
export function foo(a){console.log(a)};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var handleImport=function(){return 2};
export function handleImport(){return 2};
2 changes: 1 addition & 1 deletion test/import/fixtures/dynamic-imported-67216f69.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var handleImport=function(){return 2};
export function handleImport(){return 2};
2 changes: 1 addition & 1 deletion test/import/fixtures/dynamic-imported.esm.advanced.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var handleImport=function(){return 2};
export function handleImport(){return 2};
2 changes: 1 addition & 1 deletion test/import/fixtures/dynamic.esm.advanced.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var exported=function(){import("./dynamic-imported-67216f69.js").then((a)=>a.handleImport())};
export function exported(){import("./dynamic-imported-67216f69.js").then((a)=>a.handleImport())};
2 changes: 1 addition & 1 deletion test/import/fixtures/dynamic.esm.default.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var exported=function(){import("./dynamic-imported-67216f69.js").then(a=>a.handleImport())};
export function exported(){import("./dynamic-imported-67216f69.js").then(a=>a.handleImport())};
2 changes: 1 addition & 1 deletion test/import/fixtures/dynamic.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var exported=function(){import("./dynamic-imported-67216f69.js").then(function(a){return a.handleImport()})};
export function exported(){import("./dynamic-imported-67216f69.js").then(function(a){return a.handleImport()})};
2 changes: 1 addition & 1 deletion test/import/fixtures/flattened.esm.advanced.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var exported=function(a){console.log(a);console.log(1)};
export function exported(a){console.log(a);console.log(1)};
2 changes: 1 addition & 1 deletion test/import/fixtures/flattened.esm.default.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var exported=function(a){console.log(a);console.log(1)};
export function exported(a){console.log(a);console.log(1)};
2 changes: 1 addition & 1 deletion test/import/fixtures/flattened.esm.es5.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export var exported=function(a){console.log(a);console.log(1)};
export function exported(a){console.log(a);console.log(1)};

0 comments on commit b5b863d

Please sign in to comment.