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
Support multiple static blocks #12738
Changes from all commits
2f91ac2
3566ab7
9665ad6
db76c8e
e002f53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,30 +30,28 @@ export default declare(({ types: t, template, assertVersion }) => { | |
const { scope } = path; | ||
const classBody = path.get("body"); | ||
const privateNames = new Set(); | ||
let staticBlockPath; | ||
for (const path of classBody.get("body")) { | ||
const body = classBody.get("body"); | ||
for (const path of body) { | ||
if (path.isPrivate()) { | ||
privateNames.add(path.get("key.id").node.name); | ||
} else if (path.isStaticBlock()) { | ||
staticBlockPath = path; | ||
} | ||
} | ||
if (!staticBlockPath) { | ||
return; | ||
for (const path of body) { | ||
if (!path.isStaticBlock()) continue; | ||
const staticBlockPrivateId = generateUid(scope, privateNames); | ||
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. The inserted unique staticBlockPrivateId may accidentally shadow private id defined on upper levels: class C {
static #_;
constructor() {
class D {
static {
C.#_ = 42;
}
}
}
} The injected 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. Why don't we just use the filename + source location to generate a private name? 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. That does not really solve the mentioned issue, just makes it way less likely to happen. I don't think this is a blocker and we can address that in another PR. 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. We should probably consider tracking private identifiers in 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. Or we provide a new |
||
privateNames.add(staticBlockPrivateId); | ||
const staticBlockRef = t.privateName( | ||
t.identifier(staticBlockPrivateId), | ||
); | ||
path.replaceWith( | ||
t.classPrivateProperty( | ||
staticBlockRef, | ||
template.expression.ast`(() => { ${path.node.body} })()`, | ||
[], | ||
/* static */ true, | ||
), | ||
); | ||
} | ||
const staticBlockRef = t.privateName( | ||
t.identifier(generateUid(scope, privateNames)), | ||
); | ||
classBody.pushContainer( | ||
"body", | ||
t.classPrivateProperty( | ||
staticBlockRef, | ||
template.expression.ast`(() => { ${staticBlockPath.node.body} })()`, | ||
[], | ||
/* static */ true, | ||
), | ||
); | ||
staticBlockPath.remove(); | ||
}, | ||
}, | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class Foo { | ||
static bar = 42; | ||
static { | ||
this.foo = Foo.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo { | ||
static bar = 42; | ||
static { | ||
this.foo = Foo.bar; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Foo { | ||
static bar = 42; | ||
static #_ = (() => { | ||
this.foo = Foo.bar; | ||
})(); | ||
} | ||
|
||
expect(Foo.foo).toBe(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class Foo { | ||
static bar = 42; | ||
static { | ||
this.foo = this.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class Foo { | ||
static bar = 42; | ||
static { | ||
this.foo = this.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
class Foo { | ||
static #bar = 21; | ||
static { | ||
this.foo = this.#bar + this.qux; | ||
this.foo = this.#bar; | ||
this.qux1 = this.qux; | ||
} | ||
static qux = 21; | ||
static { | ||
this.qux2 = this.qux; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); | ||
expect(Foo.foo).toBe(21); | ||
expect(Foo.qux1).toBe(undefined); | ||
expect(Foo.qux2).toBe(21); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Foo { | ||
static #bar = 21; | ||
static { | ||
this.foo = this.#bar; | ||
this.qux1 = this.qux; | ||
} | ||
static qux = 21; | ||
static { | ||
this.qux2 = this.qux; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Foo { | ||
static #bar = 21; | ||
static #_ = (() => { | ||
this.foo = this.#bar; | ||
this.qux1 = this.qux; | ||
})(); | ||
static qux = 21; | ||
static #_2 = (() => { | ||
this.qux2 = this.qux; | ||
})(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Base { | ||
constructor() { | ||
this.Foo = class { | ||
static { | ||
this.foo = new.target; | ||
} | ||
} | ||
} | ||
} | ||
expect((new Base).Foo.foo).toBe(undefined); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Base { | ||
constructor() { | ||
this.Foo = class { | ||
static { | ||
this.foo = new.target; | ||
} | ||
} | ||
} | ||
} | ||
expect((new Base).Foo.foo).toBe(undefined); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Base { | ||
constructor() { | ||
this.Foo = class { | ||
static #_ = (() => { | ||
this.foo = new.target; | ||
})(); | ||
}; | ||
} | ||
|
||
} | ||
|
||
expect(new Base().Foo.foo).toBe(undefined); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Base { | ||
constructor() { | ||
this.Foo = class { | ||
static { | ||
this.foo = new.target; | ||
} | ||
} | ||
} | ||
} | ||
expect((new Base).Foo.foo).toBe(undefined); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Base { | ||
constructor() { | ||
this.Foo = class { | ||
static { | ||
this.foo = new.target; | ||
} | ||
} | ||
} | ||
} | ||
expect((new Base).Foo.foo).toBe(undefined); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class Foo { | ||
static bar = 42; | ||
static { | ||
this.foo = Foo.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class Foo { | ||
static bar = 42; | ||
static { | ||
this.foo = Foo.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class Foo { | ||
static bar = 42; | ||
static { | ||
this.foo = this.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class Foo { | ||
static bar = 42; | ||
static { | ||
this.foo = this.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
class Foo { | ||
static #bar = 21; | ||
static { | ||
this.foo = this.#bar + this.qux; | ||
this.foo = this.#bar; | ||
this.qux1 = this.qux; | ||
} | ||
static qux = 21; | ||
static { | ||
this.qux2 = this.qux; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); | ||
expect(Foo.foo).toBe(21); | ||
expect(Foo.qux1).toBe(undefined); | ||
expect(Foo.qux2).toBe(21); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Foo { | ||
static #bar = 21; | ||
static { | ||
this.foo = this.#bar; | ||
this.qux1 = this.qux; | ||
} | ||
static qux = 21; | ||
static { | ||
this.qux2 = this.qux; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var _bar = babelHelpers.classPrivateFieldLooseKey("bar"); | ||
|
||
var _ = babelHelpers.classPrivateFieldLooseKey("_"); | ||
|
||
var _2 = babelHelpers.classPrivateFieldLooseKey("_2"); | ||
|
||
class Foo {} | ||
|
||
Object.defineProperty(Foo, _bar, { | ||
writable: true, | ||
value: 21 | ||
}); | ||
Object.defineProperty(Foo, _, { | ||
writable: true, | ||
value: (() => { | ||
Foo.foo = babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar]; | ||
Foo.qux1 = Foo.qux; | ||
})() | ||
}); | ||
Foo.qux = 21; | ||
Object.defineProperty(Foo, _2, { | ||
writable: true, | ||
value: (() => { | ||
Foo.qux2 = Foo.qux; | ||
})() | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Base { | ||
constructor() { | ||
this.Foo = class { | ||
static { | ||
this.foo = new.target; | ||
} | ||
} | ||
} | ||
} | ||
expect((new Base).Foo.foo).toBe(undefined); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Base { | ||
constructor() { | ||
this.Foo = class { | ||
static { | ||
// fixme: new.target should be undefined after transformed | ||
this.foo = new.target; | ||
} | ||
} | ||
} | ||
} | ||
expect((new Base).Foo.foo).toBe(undefined); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class Foo { | ||
static bar = 42; | ||
static { | ||
this.foo = Foo.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class Foo { | ||
static bar = 42; | ||
static { | ||
this.foo = Foo.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class Foo { | ||
static bar = 42; | ||
static { | ||
this.foo = this.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
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.
The parser types are not exported, so we are free to refactor here.