forked from babel/minify
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented plugin-transform-consecutive-attribute-defs-plugin.
This plugin inlines consecutive attribute definitions. Updated variable name. Resolved indirect references (and added unit tests). Added support for SequenceExpressions and computed properties. Lint refactored, added array & set support renamed things Pass per preset the babili plugins renamed interfaces added to preset added array property-assignment-based collapse Fixed a bug where we would override initialized values. Added set support for arrays. added support for string indices for arrays refactored exports => classes lint refactor refactor refactor removed weird ; file handled edge cases addressind CR comments added verification added better computed property check fixed preset test
- Loading branch information
Showing
14 changed files
with
917 additions
and
15 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
packages/babel-plugin-transform-inline-consecutive-adds/.npmignore
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,4 @@ | ||
src | ||
__tests__ | ||
node_modules | ||
*.log |
66 changes: 66 additions & 0 deletions
66
packages/babel-plugin-transform-inline-consecutive-adds/README.md
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,66 @@ | ||
# babel-plugin-transform-inline-consecutive-adds | ||
|
||
This plugin inlines consecutive property assignments, array pushes, etc. | ||
|
||
## Example | ||
|
||
**In** | ||
|
||
```javascript | ||
const foo = {}; | ||
foo.a = 42; | ||
foo.b = ["hi"]; | ||
foo.c = bar(); | ||
foo.d = "str"; | ||
|
||
... | ||
const bar = []; | ||
bar.push(1); | ||
bar.push(2); | ||
``` | ||
|
||
**Out** | ||
|
||
```javascript | ||
const foo = { | ||
a: 42, | ||
b: ["hi"], | ||
c: bar(), | ||
d: "str" | ||
}; | ||
|
||
... | ||
const bar = [1, 2]; | ||
``` | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install babel-plugin-transform-inline-consecutive-adds | ||
``` | ||
|
||
## Usage | ||
|
||
### Via `.babelrc` (Recommended) | ||
|
||
**.babelrc** | ||
|
||
```json | ||
{ | ||
"plugins": ["transform-inline-consecutive-adds"] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
$ babel --plugins transform-inline-consecutive-adds script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("babel-core").transform("code", { | ||
plugins: ["transform-inline-consecutive-adds"] | ||
}); | ||
``` |
309 changes: 309 additions & 0 deletions
309
...gin-transform-inline-consecutive-adds/__tests__/transform-inline-consecutive-adds-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,309 @@ | ||
jest.autoMockOff(); | ||
|
||
const babel = require("babel-core"); | ||
const unpad = require("../../../utils/unpad"); | ||
const plugin = require("../src/index"); | ||
|
||
function transform(code) { | ||
return babel.transform(code, { | ||
plugins: [plugin], | ||
}).code; | ||
} | ||
|
||
describe("transform-inline-consecutive-adds-plugin", () => { | ||
it("should collapse simple consecutive", () => { | ||
const source = unpad(` | ||
const foo = { | ||
z: 3.0 | ||
}; | ||
foo.a = 42; | ||
foo.b = ["hi"]; | ||
foo.c = bar(); | ||
foo.d = "str"; | ||
`); | ||
const expected = unpad(` | ||
const foo = { | ||
z: 3.0, | ||
a: 42, | ||
b: ["hi"], | ||
c: bar(), | ||
d: "str" | ||
}; | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should collapse only up to last ExpressionStatement", () => { | ||
const source = unpad(` | ||
const foo = {}; | ||
foo.a = 42; | ||
console.log(foo); | ||
`); | ||
const expected = unpad(` | ||
const foo = { | ||
a: 42 | ||
}; | ||
console.log(foo); | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should not collapse if lval is nested MemberExpression", () => { | ||
const source = unpad(` | ||
const foo = {}; | ||
foo.bar.a = 42; | ||
`); | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should not collapse if lval has wrong name", () => { | ||
const source = unpad(` | ||
const foo = {}; | ||
bar.a = 42; | ||
`); | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should not collapse if has direct dependency issues", () => { | ||
const source = unpad(` | ||
const foo = {}; | ||
foo.a = function () { | ||
console.log(3); | ||
}; | ||
foo.b = foo.a(); | ||
`); | ||
const expected = unpad(` | ||
const foo = { | ||
a: function () { | ||
console.log(3); | ||
} | ||
}; | ||
foo.b = foo.a(); | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should not collapse if has indirect dependency issues", () => { | ||
const source = unpad(` | ||
var foo = {}; | ||
foo.a = 4; | ||
foo.b = cat(); | ||
function cat() { | ||
return bar(); | ||
} | ||
function bar() { | ||
console.log(foo); | ||
return 0; | ||
} | ||
`); | ||
const expected = unpad(` | ||
var foo = { | ||
a: 4 | ||
}; | ||
foo.b = cat(); | ||
function cat() { | ||
return bar(); | ||
} | ||
function bar() { | ||
console.log(foo); | ||
return 0; | ||
} | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
|
||
it("should not collapse if has indirect, nested dependency issues", () => { | ||
const source = unpad(` | ||
var foo = {}; | ||
foo.a = 4; | ||
foo.b = cat(); | ||
function cat() { | ||
return bar(); | ||
function bar() { | ||
console.log(foo); | ||
return 0; | ||
} | ||
} | ||
`); | ||
const expected = unpad(` | ||
var foo = { | ||
a: 4 | ||
}; | ||
foo.b = cat(); | ||
function cat() { | ||
return bar(); | ||
function bar() { | ||
console.log(foo); | ||
return 0; | ||
} | ||
} | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should collapse computed properties if they are literals", () => { | ||
const source = unpad(` | ||
var foo = {}; | ||
foo["a"] = 0; | ||
foo[4] = 1; | ||
`); | ||
const expected = unpad(` | ||
var foo = { | ||
"a": 0, | ||
4: 1 | ||
}; | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should not collapse computed properties otherwise", () => { | ||
const source = unpad(` | ||
var foo = {}; | ||
foo[global] = 0; | ||
`); | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should not collapse computed properties with dependency issues", () => { | ||
const source = unpad(` | ||
var foo = {}; | ||
foo[bar()] = 0; | ||
function bar() { | ||
console.log(foo); | ||
return 0; | ||
} | ||
`); | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should collapse statements with multiple assignments", () => { | ||
const source = unpad(` | ||
var foo = {}; | ||
foo.a = 0, foo.b = 2; | ||
`); | ||
const expected = unpad(` | ||
var foo = { | ||
a: 0, | ||
b: 2 | ||
}; | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should not collapse statements with multiple assignments and dependency issues", () => { | ||
const source = unpad(` | ||
var foo = {}; | ||
foo.a = 0, foo.b = bar(); | ||
function bar() { | ||
console.log(foo); | ||
return 0; | ||
} | ||
`); | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should collapse statements for arrays", () => { | ||
const source = unpad(` | ||
var foo = [1, 2]; | ||
foo.push(3, 4), foo.push(5); | ||
foo.push(6); | ||
`); | ||
const expected = unpad(` | ||
var foo = [1, 2, 3, 4, 5, 6]; | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should collapse statements for sets", () => { | ||
const source = unpad(` | ||
var foo = new Set(); | ||
foo.add(1), foo.add(2); | ||
foo.add(3); | ||
`); | ||
const expected = unpad(` | ||
var foo = new Set([1, 2, 3]); | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should collapse statements for array-initialized sets", () => { | ||
const source = unpad(` | ||
var foo = new Set([1, 2]); | ||
foo.add(3); | ||
`); | ||
const expected = unpad(` | ||
var foo = new Set([1, 2, 3]); | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should collapse array property assignments", () => { | ||
const source = unpad(` | ||
var foo = []; | ||
foo[5] = 'blah'; | ||
foo[3] = 'blah'; | ||
foo[7] = 'blah'; | ||
`); | ||
const expected = unpad(` | ||
var foo = [,,, 'blah',, 'blah',, 'blah']; | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should not collapse array property assignments if long", () => { | ||
const source = unpad(` | ||
var foo = []; | ||
foo[10] = 'blah'; | ||
`); | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should not collapse array property assignments if override initial", () => { | ||
const source = unpad(` | ||
var foo = [1, 2, 3]; | ||
foo[2] = 'blah'; | ||
`); | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should not collapse array property assignments if override dynamic", () => { | ||
const source = unpad(` | ||
var foo = [1, 2]; | ||
foo[2] = 'blah'; | ||
foo[2] = 'ok'; | ||
`); | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should not collapse array property assignments if index is float", () => { | ||
const source = unpad(` | ||
var foo = []; | ||
foo[2.1] = 'blah'; | ||
`); | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should not collapse array property assignments if index is non-int as string", () => { | ||
const source = unpad(` | ||
var foo = []; | ||
foo['2.1'] = 'blah'; | ||
`); | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should collapse array property assignments if index is int as string", () => { | ||
const source = unpad(` | ||
var foo = []; | ||
foo['2'] = 'blah'; | ||
`); | ||
const expected = unpad(` | ||
var foo = [,, 'blah']; | ||
`); | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/babel-plugin-transform-inline-consecutive-adds/package.json
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,16 @@ | ||
{ | ||
"name": "babel-plugin-transform-inline-consecutive-adds", | ||
"version": "0.0.1", | ||
"description": "This plugin inlines consecutive property assignments, array pushes, etc.", | ||
"homepage": "https://github.com/babel/babili#readme", | ||
"repository": "https://github.com/babel/babili/tree/master/packages/babel-plugin-transform-inline-consecutive-adds", | ||
"bugs": "https://github.com/babel/babili/issues", | ||
"author": "shinew", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
"babel-plugin" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
Oops, something went wrong.