-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(un-assignment-merging): add new rule
un-assignment-merging
for…
… spliting chained assignment
- Loading branch information
Showing
5 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
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
77 changes: 77 additions & 0 deletions
77
packages/unminify/src/transformations/__tests__/un-assignment-merging.spec.ts
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,77 @@ | ||
import { defineInlineTest } from '@wakaru/test-utils' | ||
import transform from '../un-assignment-merging' | ||
|
||
const inlineTest = defineInlineTest(transform) | ||
|
||
inlineTest('chained assignment should be splitted', | ||
` | ||
exports.foo = exports.bar = exports.baz = 1; | ||
`, | ||
` | ||
exports.foo = 1; | ||
exports.bar = 1; | ||
exports.baz = 1; | ||
`, | ||
) | ||
|
||
inlineTest('chained assignment should be splitted - allowed', | ||
` | ||
a1 = a2 = 0; | ||
b1 = b2 = 0n; | ||
c1 = c2 = ''; | ||
d1 = d2 = true; | ||
e1 = e2 = null; | ||
f1 = f2 = undefined; | ||
g1 = g2 = foo; | ||
`, | ||
` | ||
a1 = 0; | ||
a2 = 0; | ||
b1 = 0n; | ||
b2 = 0n; | ||
c1 = ''; | ||
c2 = ''; | ||
d1 = true; | ||
d2 = true; | ||
e1 = null; | ||
e2 = null; | ||
f1 = undefined; | ||
f2 = undefined; | ||
g1 = foo; | ||
g2 = foo; | ||
`, | ||
) | ||
|
||
inlineTest('chained assignment should be splitted - not allowed', | ||
` | ||
a1 = a2 = \`template\${foo}\`; | ||
b1 = b2 = Symbol(); | ||
c1 = c2 = /regex/; | ||
d1 = d2 = foo.bar; | ||
f1 = f2 = fn(); | ||
`, | ||
` | ||
a1 = a2 = \`template\${foo}\`; | ||
b1 = b2 = Symbol(); | ||
c1 = c2 = /regex/; | ||
d1 = d2 = foo.bar; | ||
f1 = f2 = fn(); | ||
`, | ||
) | ||
|
||
inlineTest('chained assignment should be splitted - comments', | ||
` | ||
// before | ||
exports.foo = exports.bar = exports.baz = 1; | ||
// after | ||
`, | ||
` | ||
// before | ||
exports.foo = 1; | ||
exports.bar = 1; | ||
exports.baz = 1; | ||
// after | ||
`, | ||
) |
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
82 changes: 82 additions & 0 deletions
82
packages/unminify/src/transformations/un-assignment-merging.ts
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,82 @@ | ||
import { mergeComments } from '@wakaru/ast-utils' | ||
import { isSimpleValue } from '../utils/checker' | ||
import { replaceWithMultipleStatements } from '../utils/insert' | ||
import wrap from '../wrapAstTransformation' | ||
import type { ASTTransformation } from '../wrapAstTransformation' | ||
import type { AssignmentExpression } from 'jscodeshift' | ||
|
||
/** | ||
* Separate chained assignment into multiple statements. | ||
* This rule is only applied to simple value assignment to | ||
* avoid introducing behavior changes. | ||
* | ||
* Normally, this rule should assign the next variable to the | ||
* previous one, which is also how the code is executed. | ||
* | ||
* For example: | ||
* ```js | ||
* exports.foo = exports.bar = 1 | ||
* -> should be | ||
* exports.bar = 1 | ||
* exports.foo = exports.bar | ||
* ``` | ||
* | ||
* But instead, in this rule, it is assigned to the original value | ||
* to maximize the readability, and ease some edge cases that other | ||
* rules might hit on. | ||
* | ||
* @example | ||
* exports.foo = exports.bar = 1 | ||
* -> | ||
* exports.bar = 1 | ||
* exports.foo = 1 | ||
* | ||
* @example | ||
* foo = bar = baz = void 0 | ||
* -> | ||
* foo = void 0 | ||
* bar = void 0 | ||
* baz = void 0 | ||
*/ | ||
export const transformAST: ASTTransformation = (context) => { | ||
const { root, j } = context | ||
|
||
root | ||
.find(j.ExpressionStatement, { | ||
expression: { | ||
type: 'AssignmentExpression', | ||
operator: '=', | ||
right: { | ||
type: 'AssignmentExpression', | ||
operator: '=', | ||
}, | ||
}, | ||
}) | ||
.forEach((p) => { | ||
const { expression } = p.node | ||
|
||
let node = expression as AssignmentExpression | ||
const assignments: AssignmentExpression[] = [node] | ||
while (j.AssignmentExpression.check(node.right)) { | ||
node = node.right | ||
assignments.push(node) | ||
} | ||
|
||
if (assignments.length < 2) return | ||
|
||
const valueNode = node.right | ||
if ( | ||
j.Identifier.check(valueNode) | ||
|| isSimpleValue(j, valueNode) | ||
) { | ||
const replacements = assignments.map((assignment) => { | ||
return j.expressionStatement(j.assignmentExpression('=', assignment.left, valueNode)) | ||
}) | ||
mergeComments(replacements, p.node.comments) | ||
|
||
replaceWithMultipleStatements(j, p, replacements) | ||
} | ||
}) | ||
} | ||
|
||
export default wrap(transformAST) |
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