New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve plugins typings (Part 2) #14639
Merged
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b48b46d
exponentiation-operator
JLHwung 951254b
duplicate-keys
JLHwung 4003fa3
computed-properties
JLHwung f5165b2
replace-supers
JLHwung 60d5844
block-scoping
JLHwung d49cc57
block-scoped-functions
JLHwung 2b4bb62
syntax-typescript
JLHwung bc18c01
record-and-tuple
JLHwung 28eadbb
private-property-in-object
JLHwung 043a289
partial-application
JLHwung e70bca5
json-strings
JLHwung e0fc2b4
function-sent
JLHwung 75e7ed1
function-bind
JLHwung ae3b18b
class-static-block
JLHwung 4eeaa37
async-generator-functions
JLHwung ab68f2d
Update packages/babel-plugin-transform-block-scoping/src/index.ts
JLHwung 0054372
address review comments
JLHwung 3b0c873
helper-replace-supers
JLHwung File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import { template, types as t } from "@babel/core"; | ||
import { template, types as t, type PluginPass } from "@babel/core"; | ||
import type { Scope } from "@babel/traverse"; | ||
|
||
export interface Options { | ||
loose?: boolean; | ||
} | ||
|
||
type PropertyInfo = { | ||
scope: Scope; | ||
objId: t.Identifier; | ||
body: t.Statement[]; | ||
computedProps: t.ObjectMember[]; | ||
initPropExpression: t.ObjectExpression; | ||
getMutatorId: () => t.Identifier; | ||
state: PluginPass; | ||
}; | ||
|
||
export default declare((api, options: Options) => { | ||
api.assertVersion(7); | ||
|
||
|
@@ -15,12 +26,12 @@ export default declare((api, options: Options) => { | |
? pushComputedPropsLoose | ||
: pushComputedPropsSpec; | ||
|
||
const buildMutatorMapAssign = template(` | ||
const buildMutatorMapAssign = template.statements(` | ||
MUTATOR_MAP_REF[KEY] = MUTATOR_MAP_REF[KEY] || {}; | ||
MUTATOR_MAP_REF[KEY].KIND = VALUE; | ||
`); | ||
|
||
function getValue(prop) { | ||
function getValue(prop: t.ObjectMember) { | ||
if (t.isObjectProperty(prop)) { | ||
return prop.value; | ||
} else if (t.isObjectMethod(prop)) { | ||
|
@@ -34,28 +45,31 @@ export default declare((api, options: Options) => { | |
} | ||
} | ||
|
||
function pushAssign(objId, prop, body) { | ||
if (prop.kind === "get" && prop.kind === "set") { | ||
pushMutatorDefine(objId, prop); | ||
} else { | ||
body.push( | ||
t.expressionStatement( | ||
t.assignmentExpression( | ||
"=", | ||
t.memberExpression( | ||
t.cloneNode(objId), | ||
prop.key, | ||
prop.computed || t.isLiteral(prop.key), | ||
), | ||
// @ts-expect-error todo(flow->ts): double-check type error | ||
getValue(prop), | ||
function pushAssign( | ||
objId: t.Identifier, | ||
prop: t.ObjectMember, | ||
body: t.Statement[], | ||
) { | ||
body.push( | ||
t.expressionStatement( | ||
t.assignmentExpression( | ||
"=", | ||
t.memberExpression( | ||
t.cloneNode(objId), | ||
prop.key, | ||
prop.computed || t.isLiteral(prop.key), | ||
), | ||
// @ts-expect-error todo(flow->ts): double-check type error | ||
getValue(prop), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: what is the error here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
), | ||
); | ||
} | ||
), | ||
); | ||
} | ||
|
||
function pushMutatorDefine({ body, getMutatorId, scope }, prop) { | ||
function pushMutatorDefine( | ||
{ body, getMutatorId, scope }: PropertyInfo, | ||
prop: t.ObjectMethod, | ||
) { | ||
let key = | ||
!prop.computed && t.isIdentifier(prop.key) | ||
? t.stringLiteral(prop.key.name) | ||
|
@@ -70,47 +84,56 @@ export default declare((api, options: Options) => { | |
} | ||
|
||
body.push( | ||
...(buildMutatorMapAssign({ | ||
...buildMutatorMapAssign({ | ||
MUTATOR_MAP_REF: getMutatorId(), | ||
KEY: t.cloneNode(key), | ||
VALUE: getValue(prop), | ||
KIND: t.identifier(prop.kind), | ||
}) as t.Statement[]), | ||
}), | ||
); | ||
} | ||
|
||
function pushComputedPropsLoose(info) { | ||
function pushComputedPropsLoose(info: PropertyInfo) { | ||
for (const prop of info.computedProps) { | ||
if (prop.kind === "get" || prop.kind === "set") { | ||
if ( | ||
t.isObjectMethod(prop) && | ||
(prop.kind === "get" || prop.kind === "set") | ||
) { | ||
pushMutatorDefine(info, prop); | ||
} else { | ||
pushAssign(t.cloneNode(info.objId), prop, info.body); | ||
} | ||
} | ||
} | ||
|
||
function pushComputedPropsSpec(info) { | ||
function pushComputedPropsSpec(info: PropertyInfo) { | ||
const { objId, body, computedProps, state } = info; | ||
|
||
for (const prop of computedProps) { | ||
const key = t.toComputedKey(prop); | ||
// PrivateName must not be in ObjectExpression | ||
const key = t.toComputedKey(prop) as t.Expression; | ||
|
||
if (prop.kind === "get" || prop.kind === "set") { | ||
if ( | ||
t.isObjectMethod(prop) && | ||
(prop.kind === "get" || prop.kind === "set") | ||
) { | ||
pushMutatorDefine(info, prop); | ||
} else { | ||
// the value of ObjectProperty in ObjectExpression must be an expression | ||
const value = getValue(prop) as t.Expression; | ||
if (computedProps.length === 1) { | ||
return t.callExpression(state.addHelper("defineProperty"), [ | ||
info.initPropExpression, | ||
key, | ||
getValue(prop), | ||
value, | ||
]); | ||
} else { | ||
body.push( | ||
t.expressionStatement( | ||
t.callExpression(state.addHelper("defineProperty"), [ | ||
t.cloneNode(objId), | ||
key, | ||
getValue(prop), | ||
value, | ||
]), | ||
), | ||
); | ||
|
@@ -137,12 +160,14 @@ export default declare((api, options: Options) => { | |
// put all getters/setters into the first object expression as well as all initialisers up | ||
// to the first computed property | ||
|
||
const initProps = []; | ||
const computedProps = []; | ||
const initProps: t.ObjectMember[] = []; | ||
const computedProps: t.ObjectMember[] = []; | ||
let foundComputed = false; | ||
|
||
for (const prop of node.properties) { | ||
// @ts-ignore SpreadElement must not have computed property | ||
if (t.isSpreadElement(prop)) { | ||
continue; | ||
} | ||
if (prop.computed) { | ||
foundComputed = true; | ||
} | ||
|
@@ -164,7 +189,7 @@ export default declare((api, options: Options) => { | |
]), | ||
); | ||
|
||
let mutatorRef; | ||
let mutatorRef: t.Identifier; | ||
|
||
const getMutatorId = function () { | ||
if (!mutatorRef) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for reviewers: moved to
pushComputedPropsLoose
.