Skip to content

Commit

Permalink
Add support for formatting from closure compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoferbaxter committed Jan 14, 2020
1 parent abeac73 commit 847bca7
Show file tree
Hide file tree
Showing 59 changed files with 239 additions and 18 deletions.
13 changes: 5 additions & 8 deletions src/parsing/preserve-default-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { ExpressionStatement, AssignmentExpression, MemberExpression } from 'estree';
import { ExpressionStatement, AssignmentExpression } from 'estree';
import { ExportDetails, Range } from '../types';
import MagicString from 'magic-string';

Expand All @@ -26,14 +26,11 @@ export function PreserveDefault(
exportInline: boolean,
): boolean {
const assignmentExpression = ancestor.expression as AssignmentExpression;
const memberExpression = assignmentExpression.left as MemberExpression;
const [memberExpressionStart, memberExpressionEnd]: Range = memberExpression.range as Range;
const [leftStart]: Range = assignmentExpression.left.range as Range;
const [rightStart]: Range = assignmentExpression.right.range as Range;

source.overwrite(
memberExpressionStart,
memberExpressionEnd + assignmentExpression.operator.length,
'export default ',
);
// console.log(code.substring(leftStart, rightStart));
source.overwrite(leftStart, rightStart, 'export default ');

return false;
}
4 changes: 2 additions & 2 deletions src/parsing/preserve-named-constant-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function PreserveIdentifier(
const left = assignmentExpression.left;
const right = assignmentExpression.right;
const [ancestorStart, ancestorEnd]: Range = ancestor.range as Range;
const [leftStart] = left.range as Range;
const [rightStart, rightEnd]: Range = right.range as Range;

if (exportInline) {
Expand All @@ -80,10 +81,9 @@ function PreserveIdentifier(
} else if (exportDetails.source === null && 'name' in right) {
// This is a locally defined identifier with a name we can use.
exportDetails.local = right.name;
source.remove((left.range as Range)[0], rightEnd + 1);
source.remove(leftStart, ancestorEnd);
return true;
} else {
// exportDetails.local =
source.overwrite(
ancestorStart,
ancestorEnd,
Expand Down
4 changes: 3 additions & 1 deletion src/transformers/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ export default class ExportTransform extends Transform implements TransformInter
}

if (!exportIsLocal) {
source.remove((left.range as Range)[0], (expression.right.range as Range)[1] + 1);
const [leftStart] = left.range as Range;
const { 1: ancestorEnd } = ancestor.range as Range;
source.remove(leftStart, ancestorEnd);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/arrow-function/fixtures/multiple-arguments.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export var multipleArguments=(a, b) => console.log(a, b);
2 changes: 2 additions & 0 deletions test/arrow-function/fixtures/single-argument.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export var singleArgument=a => console.log(a);
5 changes: 5 additions & 0 deletions test/const-rename/fixtures/rename.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

let b = [3, 4], c = [5, 6];
export function yes(a) {
return 0 <= c.indexOf(a) && 0 <= b.indexOf(a);
};
5 changes: 5 additions & 0 deletions test/export-all/fixtures/all.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

var export1=1;
function export2(){
return 2;
};export{export1,export2}
2 changes: 2 additions & 0 deletions test/export-default/fixtures/array.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export default [];
2 changes: 2 additions & 0 deletions test/export-default/fixtures/arrow-function.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export default a => console.log(a);
10 changes: 10 additions & 0 deletions test/export-default/fixtures/class.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

class a {
constructor(b) {
this.name_ = b;
}
console() {
console.log(this.name_);
}
}
export default a;
5 changes: 5 additions & 0 deletions test/export-default/fixtures/function.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export default function(a) {
console.log(a);
console.log(1);
};
2 changes: 2 additions & 0 deletions test/export-default/fixtures/identifier.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export default 1;
10 changes: 10 additions & 0 deletions test/export-default/fixtures/named-class.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

class a {
constructor(b) {
this.name_ = b;
}
console() {
console.log(this.name_);
}
}
export default a;
5 changes: 5 additions & 0 deletions test/export-default/fixtures/named-function.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export default function(a) {
console.log(a);
console.log(1);
};
2 changes: 2 additions & 0 deletions test/export-default/fixtures/number.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export default 42;
2 changes: 2 additions & 0 deletions test/export-default/fixtures/object.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export default {key:"value"};
1 change: 1 addition & 0 deletions test/export-externals/fixtures/array.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export{exportedArray}from'./external.js';
1 change: 1 addition & 0 deletions test/export-externals/fixtures/class.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export{exportedClass}from'./external.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export{constFunction}from'./external.js';
1 change: 1 addition & 0 deletions test/export-externals/fixtures/const-number.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export{constNumber}from'./external.js';
1 change: 1 addition & 0 deletions test/export-externals/fixtures/default.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export{default as X}from'./external.js';
1 change: 1 addition & 0 deletions test/export-externals/fixtures/function.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export{exportedFunction}from'./external.js';
1 change: 1 addition & 0 deletions test/export-externals/fixtures/local.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export{exportedFunction as exF}from'./external.js';
2 changes: 2 additions & 0 deletions test/export-externals/fixtures/multiple-default.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export{default as Y}from'./external-default.js';export{default as X}from'./external.js';

1 change: 1 addition & 0 deletions test/export-externals/fixtures/object.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export{constObject}from'./external.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export{exportedFunction as a}from'y.js';
a("1");
2 changes: 2 additions & 0 deletions test/export-named/fixtures/identifier-alias.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

var a=1;export{a as bar}
2 changes: 2 additions & 0 deletions test/export-named/fixtures/identifier-default.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export default 1;
2 changes: 2 additions & 0 deletions test/export-named/fixtures/identifier.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export var foo=1;
17 changes: 17 additions & 0 deletions test/export-named/fixtures/multiple.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

class b {
constructor(a) {
this.name_ = a;
}
console() {
console.log(this.name_);
}
}

function bar(){
console.log(1);
};
function baz(a) {
console.log(a);
};
var foo=1;export{b as ExportedClass,bar,baz,foo}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

var b = Symbol.for("smth");
export var isSmth=a => a && !!a[b];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

var b = Symbol.for("smth");
export function isSmth(a) {
return a && !!a[b];
};
10 changes: 10 additions & 0 deletions test/export-variables/fixtures/class.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

class a {
constructor(b) {
this.name_ = b;
}
console() {
console.log(this.name_);
}
}
export var Exported=a;
4 changes: 4 additions & 0 deletions test/export-variables/fixtures/const-function.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function foo(a) {
console.log(a);
};
2 changes: 2 additions & 0 deletions test/export-variables/fixtures/const-number.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export var foo=1;
4 changes: 4 additions & 0 deletions test/export-variables/fixtures/function.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function foo(a) {
console.log(a);
};
4 changes: 4 additions & 0 deletions test/export-variables/fixtures/let-function.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function foo(a) {
console.log(a);
};
2 changes: 2 additions & 0 deletions test/export-variables/fixtures/let-identifier.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export var foo=1;
2 changes: 2 additions & 0 deletions test/export-variables/fixtures/let-number.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export var foo=1;
4 changes: 4 additions & 0 deletions test/export-variables/fixtures/var-function.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function foo(a) {
console.log(a);
};
2 changes: 2 additions & 0 deletions test/export-variables/fixtures/var-identifier.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export var foo=1;
2 changes: 2 additions & 0 deletions test/export-variables/fixtures/var-number.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export var foo=1;
7 changes: 7 additions & 0 deletions test/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const fs = require('fs');
const path = require('path');

const DEFAULT_CLOSURE_OPTIONS = { default: {} };
const PRETTY_PRINT_CLOSURE_OPTIONS = {
pretty: {
formatting: 'PRETTY_PRINT',
},
};
const ADVANCED_CLOSURE_OPTIONS = {
advanced: {
compilation_level: 'ADVANCED_OPTIMIZATIONS',
Expand All @@ -34,6 +39,7 @@ const ES5_STRICT_CLOSURE_OPTIONS = {
};
const defaultClosureFlags = {
...DEFAULT_CLOSURE_OPTIONS,
...PRETTY_PRINT_CLOSURE_OPTIONS,
...ADVANCED_CLOSURE_OPTIONS,
...ES5_STRICT_CLOSURE_OPTIONS,
};
Expand Down Expand Up @@ -155,6 +161,7 @@ function generator(

module.exports = {
DEFAULT_CLOSURE_OPTIONS,
PRETTY_PRINT_CLOSURE_OPTIONS,
ADVANCED_CLOSURE_OPTIONS,
ES5_STRICT_CLOSURE_OPTIONS,
ES_OUTPUT,
Expand Down
8 changes: 8 additions & 0 deletions test/iife/fixtures/iife-wrapped-safely.iife.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
var wrapper = function(a) {
a.greeting = function(a) {
document.body.innerHTML = "hello " + a;
};
return a;
}({});

9 changes: 7 additions & 2 deletions test/import/dynamic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
* limitations under the License.
*/

import {generator, DEFAULT_CLOSURE_OPTIONS, ES5_STRICT_CLOSURE_OPTIONS} from '../generator';
import {
generator,
DEFAULT_CLOSURE_OPTIONS,
PRETTY_PRINT_CLOSURE_OPTIONS,
ES5_STRICT_CLOSURE_OPTIONS,
} from '../generator';

generator('import', 'dynamic', true, undefined, {
...DEFAULT_CLOSURE_OPTIONS,
...PRETTY_PRINT_CLOSURE_OPTIONS,
...ES5_STRICT_CLOSURE_OPTIONS,
});

4 changes: 4 additions & 0 deletions test/import/fixtures/dynamic-imported-67216f69.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function handleImport(){
return 2;
};
4 changes: 4 additions & 0 deletions test/import/fixtures/dynamic.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export function exported(){
import("./dynamic-imported-67216f69.js").then(a => a.handleImport());
};
2 changes: 2 additions & 0 deletions test/import/fixtures/external.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import j from'lodash3';import{thing,thing2}from'lodash2';import _,{foo,bar}from'lodash';
console.log("lodash", _, foo, bar, thing, thing2, j);
5 changes: 5 additions & 0 deletions test/import/fixtures/flattened.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export function exported(a) {
console.log(a);
console.log(1);
};
10 changes: 10 additions & 0 deletions test/import/fixtures/utf8-common-38fdc940.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

function ɵɵbar(){
console.log("bar");
};
function baz(){
console.log("baz");
};
function ɵɵfoo(){
console.log("foo");
};export{ɵɵbar as a,baz as b,ɵɵfoo as ɵ}
2 changes: 2 additions & 0 deletions test/import/fixtures/utf8-lazy-d5bcdc27.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import{a as ɵɵbar}from'./utf8-common-38fdc940.js';
\u0275\u0275bar();
4 changes: 4 additions & 0 deletions test/import/fixtures/utf8.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import{ɵ as ɵɵfoo,b as baz}from'./utf8-common-38fdc940.js';
\u0275\u0275foo();
baz();
import("./utf8-lazy-d5bcdc27.js");
3 changes: 3 additions & 0 deletions test/import/utf8.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ import {
generator,
failureGenerator,
DEFAULT_CLOSURE_OPTIONS,
PRETTY_PRINT_CLOSURE_OPTIONS,
ADVANCED_CLOSURE_OPTIONS,
ES5_STRICT_CLOSURE_OPTIONS,
} from '../generator';

generator('import', 'utf8', true, undefined, {
...DEFAULT_CLOSURE_OPTIONS,
...PRETTY_PRINT_CLOSURE_OPTIONS,
...ES5_STRICT_CLOSURE_OPTIONS,
});

// Seperate the Advanced Compilation as we work on it.
generator('import', 'utf8', true, undefined, {
...ADVANCED_CLOSURE_OPTIONS,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

console.log({0:"value"});
6 changes: 6 additions & 0 deletions test/literal-computed-keys/fixtures/mixed-keys.esm.pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

console.log({0:"value", 1:"value", 2:"value2", 3:"value3", 4(a) {
console.log(a);
}, 5(a) {
console.log(a);
}});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

let a = ["put", "add", "delete", "clear"];
export function SpreadExpression(){
return {...a, get:() => console.log("get thing")};
};
20 changes: 15 additions & 5 deletions test/provided-externs/class.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,32 @@
* limitations under the License.
*/

import { generator, ESM_OUTPUT } from '../generator';
import {
generator,
DEFAULT_CLOSURE_OPTIONS,
PRETTY_PRINT_CLOSURE_OPTIONS,
ADVANCED_CLOSURE_OPTIONS,
ES5_STRICT_CLOSURE_OPTIONS,
} from '../generator';
const path = require('path');

const EXTERNS = path.resolve('test', 'provided-externs', 'fixtures', 'class.externs.js');

generator('provided-externs', 'class', false, [ESM_OUTPUT], {
generator('provided-externs', 'class', false, undefined, {
default: {
...DEFAULT_CLOSURE_OPTIONS.default,
externs: EXTERNS,
},
pretty: {
...PRETTY_PRINT_CLOSURE_OPTIONS.pretty,
externs: EXTERNS,
},
advanced: {
...ADVANCED_CLOSURE_OPTIONS.advanced,
externs: EXTERNS,
compilation_level: 'ADVANCED_OPTIMIZATIONS',
language_out: 'ECMASCRIPT_2015',
},
es5: {
...ES5_STRICT_CLOSURE_OPTIONS.es5,
externs: EXTERNS,
language_out: 'ECMASCRIPT5_STRICT',
},
});
Loading

0 comments on commit 847bca7

Please sign in to comment.