Skip to content

Class static block #9

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

Merged
merged 8 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,7 @@ namespace ts {
case SyntaxKind.ConstructSignature:
case SyntaxKind.IndexSignature:
case SyntaxKind.ConstructorType:
case SyntaxKind.ClassStaticBlockDeclaration:
return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals | ContainerFlags.IsFunctionLike;

case SyntaxKind.FunctionExpression:
Expand Down Expand Up @@ -1864,7 +1865,7 @@ namespace ts {
// By not creating a new block-scoped-container here, we ensure that both 'var x'
// and 'let x' go into the Function-container's locals, and we do get a collision
// conflict.
return isFunctionLike(node.parent) ? ContainerFlags.None : ContainerFlags.IsBlockScopedContainer;
return isFunctionLike(node.parent) || isClassStaticBlockDeclaration(node.parent) ? ContainerFlags.None : ContainerFlags.IsBlockScopedContainer;
}

return ContainerFlags.None;
Expand Down Expand Up @@ -1926,6 +1927,7 @@ namespace ts {
case SyntaxKind.JSDocFunctionType:
case SyntaxKind.JSDocTypedefTag:
case SyntaxKind.JSDocCallbackTag:
case SyntaxKind.ClassStaticBlockDeclaration:
case SyntaxKind.TypeAliasDeclaration:
case SyntaxKind.MappedType:
// All the children of these container types are never visible through another
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37928,6 +37928,8 @@ namespace ts {
return checkMappedType(<MappedTypeNode>node);
case SyntaxKind.FunctionDeclaration:
return checkFunctionDeclaration(<FunctionDeclaration>node);
case SyntaxKind.ClassStaticBlockDeclaration:
return forEachChild(node, checkSourceElement);
case SyntaxKind.Block:
case SyntaxKind.ModuleBlock:
return checkBlock(<Block>node);
Expand Down Expand Up @@ -40948,7 +40950,7 @@ namespace ts {
function checkGrammarBreakOrContinueStatement(node: BreakOrContinueStatement): boolean {
let current: Node = node;
while (current) {
if (isFunctionLike(current)) {
if (isFunctionLikeOrClassStaticBlockDeclaration(current)) {
return grammarErrorOnNode(node, Diagnostics.Jump_target_cannot_cross_function_boundary);
}

Expand Down
13 changes: 13 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,8 @@ namespace ts {
return emitMethodSignature(<MethodSignature>node);
case SyntaxKind.MethodDeclaration:
return emitMethodDeclaration(<MethodDeclaration>node);
case SyntaxKind.ClassStaticBlockDeclaration:
return emitClassStaticBlockDeclaration(<ClassStaticBlockDeclaration>node);
case SyntaxKind.Constructor:
return emitConstructor(<ConstructorDeclaration>node);
case SyntaxKind.GetAccessor:
Expand Down Expand Up @@ -2008,6 +2010,11 @@ namespace ts {
emitSignatureAndBody(node, emitSignatureHead);
}

function emitClassStaticBlockDeclaration(node: ClassStaticBlockDeclaration) {
emit(node.staticToken);
emitBlockFunctionBody(node.body);
}

function emitConstructor(node: ConstructorDeclaration) {
emitModifiers(node, node.modifiers);
writeKeyword("constructor");
Expand Down Expand Up @@ -5770,6 +5777,12 @@ namespace ts {
visitTypeNode(node.type),
visitFunctionBody(node.body));

case SyntaxKind.ClassStaticBlockDeclaration:
Debug.type<ClassStaticBlockDeclaration>(node);
return factory.updateClassStaticBlockDeclaration(node,
visit(node.staticToken, isStaticModifier),
visitFunctionBody(node.body));

case SyntaxKind.Constructor:
Debug.type<ConstructorDeclaration>(node);
return factory.updateConstructorDeclaration(node,
Expand Down
32 changes: 32 additions & 0 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ namespace ts {
updateConstructSignature,
createIndexSignature,
updateIndexSignature,
createClassStaticBlockDeclaration,
updateClassStaticBlockDeclaration,
createTemplateLiteralTypeSpan,
updateTemplateLiteralTypeSpan,
createKeywordTypeNode,
Expand Down Expand Up @@ -1610,6 +1612,36 @@ namespace ts {
: node;
}

// @api
function createClassStaticBlockDeclaration(
staticToken: Token<SyntaxKind.StaticKeyword>,
body: Block
): ClassStaticBlockDeclaration {
const node = createBaseGenericNamedDeclaration<ClassStaticBlockDeclaration>(
SyntaxKind.ClassStaticBlockDeclaration,
/*decorators*/ undefined,
/*modifiers*/ undefined,
/*name*/ undefined,
/*typeParameters*/ undefined
);
node.staticToken = staticToken;
node.body = body;
node.transformFlags = TransformFlags.ContainsESNext;
return node;
}

// @api
function updateClassStaticBlockDeclaration(
node: ClassStaticBlockDeclaration,
staticToken: Token<SyntaxKind.StaticKeyword>,
body: Block
): ClassStaticBlockDeclaration {
return node.staticToken !== staticToken
|| node.body !== body
? update(createClassStaticBlockDeclaration(staticToken, body), node)
: node;
}

// @api
function createTemplateLiteralTypeSpan(type: TypeNode, literal: TemplateMiddle | TemplateTail) {
const node = createBaseNode<TemplateLiteralTypeSpan>(SyntaxKind.TemplateLiteralTypeSpan);
Expand Down
32 changes: 32 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ namespace ts {
visitNode(cbNode, (<FunctionLikeDeclaration>node).type) ||
visitNode(cbNode, (<ArrowFunction>node).equalsGreaterThanToken) ||
visitNode(cbNode, (<FunctionLikeDeclaration>node).body);
case SyntaxKind.ClassStaticBlockDeclaration:
return visitNode(cbNode, (<ClassStaticBlockDeclaration>node).staticToken) ||
visitNode(cbNode, (<ClassStaticBlockDeclaration>node).body);
case SyntaxKind.TypeReference:
return visitNode(cbNode, (<TypeReferenceNode>node).typeName) ||
visitNodes(cbNode, cbNodes, (<TypeReferenceNode>node).typeArguments);
Expand Down Expand Up @@ -6561,6 +6564,28 @@ namespace ts {
return false;
}

function parseClassStaticBlockDeclaration(): ClassStaticBlockDeclaration {
const pos = getNodePos();
const staticKeyworkd = parseExpectedToken(SyntaxKind.StaticKeyword);
const body = parseClassStaticBlockBodyBlock();
return finishNode(factory.createClassStaticBlockDeclaration(staticKeyworkd, body), pos);
}

function parseClassStaticBlockBodyBlock() {
const savedYieldContext = inYieldContext();
setYieldContext(false);

const savedAwaitContext = inAwaitContext();
setAwaitContext(false);

const block = parseBlock(/*ignoreMissingOpenBrace*/ false);

setAwaitContext(savedAwaitContext);
setYieldContext(savedYieldContext);

return block;
}

function parseDecoratorExpression() {
if (inAwaitContext() && token() === SyntaxKind.AwaitKeyword) {
// `@await` is is disallowed in an [Await] context, but can cause parsing to go off the rails
Expand Down Expand Up @@ -6645,6 +6670,9 @@ namespace ts {
nextToken();
return finishNode(factory.createSemicolonClassElement(), pos);
}
if (token() === SyntaxKind.StaticKeyword && lookAhead(nextTokenIsOpenBrace)) {
return parseClassStaticBlockDeclaration();
}

const hasJSDoc = hasPrecedingJSDocComment();
const decorators = parseDecorators();
Expand Down Expand Up @@ -6910,6 +6938,10 @@ namespace ts {
return nextToken() === SyntaxKind.OpenParenToken;
}

function nextTokenIsOpenBrace() {
return nextToken() === SyntaxKind.OpenBraceToken;
}

function nextTokenIsSlash() {
return nextToken() === SyntaxKind.SlashToken;
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ namespace ts {
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.ClassStaticBlockDeclaration:
// Fallback to the default visit behavior.
return visitorWorker(node);

Expand Down
9 changes: 9 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ namespace ts {
SetAccessor,
CallSignature,
ConstructSignature,
ClassStaticBlockDeclaration,
IndexSignature,
// Type
TypePredicate,
Expand Down Expand Up @@ -1502,6 +1503,12 @@ namespace ts {
readonly type: TypeNode;
}

export interface ClassStaticBlockDeclaration extends ClassElement {
readonly kind: SyntaxKind.ClassStaticBlockDeclaration;
readonly staticToken: Token<SyntaxKind.StaticKeyword>;
readonly body: Block;
}

export interface TypeNode extends Node {
_typeNodeBrand: any;
}
Expand Down Expand Up @@ -6929,6 +6936,8 @@ namespace ts {
updateIndexSignature(node: IndexSignatureDeclaration, decorators: readonly Decorator[] | undefined, modifiers: readonly Modifier[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode): IndexSignatureDeclaration;
createTemplateLiteralTypeSpan(type: TypeNode, literal: TemplateMiddle | TemplateTail): TemplateLiteralTypeSpan;
updateTemplateLiteralTypeSpan(node: TemplateLiteralTypeSpan, type: TypeNode, literal: TemplateMiddle | TemplateTail): TemplateLiteralTypeSpan;
createClassStaticBlockDeclaration(staticToken: Token<SyntaxKind.StaticKeyword>, body: Block): ClassStaticBlockDeclaration;
updateClassStaticBlockDeclaration(node: ClassStaticBlockDeclaration, staticToken: Token<SyntaxKind.StaticKeyword>, body: Block): ClassStaticBlockDeclaration;

//
// Types
Expand Down
1 change: 1 addition & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,7 @@ namespace ts {
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.ClassStaticBlockDeclaration:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertySignature:
case SyntaxKind.MethodDeclaration:
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,14 @@ namespace ts {
return !!node && isFunctionLikeKind(node.kind);
}

export function isClassStaticBlockDeclaration(node: Node): node is ClassStaticBlockDeclaration {
return node.kind === SyntaxKind.ClassStaticBlockDeclaration;
}

export function isFunctionLikeOrClassStaticBlockDeclaration(node: Node | undefined): node is (SignatureDeclaration | ClassStaticBlockDeclaration) {
return !!node && (isFunctionLikeKind(node.kind) || isClassStaticBlockDeclaration(node));
}

/* @internal */
export function isFunctionLikeDeclaration(node: Node): node is FunctionLikeDeclaration {
return node && isFunctionLikeDeclarationKind(node.kind);
Expand Down Expand Up @@ -1270,6 +1278,7 @@ namespace ts {
|| kind === SyntaxKind.GetAccessor
|| kind === SyntaxKind.SetAccessor
|| kind === SyntaxKind.IndexSignature
|| kind === SyntaxKind.ClassStaticBlockDeclaration
|| kind === SyntaxKind.SemicolonClassElement;
}

Expand Down
8 changes: 8 additions & 0 deletions src/compiler/visitorPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,14 @@ namespace ts {
visitParameterList(node.parameters, visitor, context, nodesVisitor),
visitFunctionBody(node.body!, visitor, context, nodeVisitor));

case SyntaxKind.ClassStaticBlockDeclaration:
Debug.type<ClassStaticBlockDeclaration>(node);
context.startLexicalEnvironment();
context.suspendLexicalEnvironment();
return factory.updateClassStaticBlockDeclaration(node,
nodeVisitor(node.staticToken, visitor, isStaticModifier),
visitFunctionBody(node.body, visitor, context, nodeVisitor));

case SyntaxKind.CallSignature:
Debug.type<CallSignatureDeclaration>(node);
return factory.updateCallSignature(node,
Expand Down
16 changes: 16 additions & 0 deletions tests/baselines/reference/classStaticBlock1(target=es2015).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [classStaticBlock1.ts]
const a = 2;

class C {
static {
const a = 1;

a;
}
}


//// [classStaticBlock1.js]
const a = 2;
class C {
}
16 changes: 16 additions & 0 deletions tests/baselines/reference/classStaticBlock1(target=es2015).symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock1.ts ===
const a = 2;
>a : Symbol(a, Decl(classStaticBlock1.ts, 0, 5))

class C {
>C : Symbol(C, Decl(classStaticBlock1.ts, 0, 12))

static {
const a = 1;
>a : Symbol(a, Decl(classStaticBlock1.ts, 4, 13))

a;
>a : Symbol(a, Decl(classStaticBlock1.ts, 4, 13))
}
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/classStaticBlock1(target=es2015).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock1.ts ===
const a = 2;
>a : 2
>2 : 2

class C {
>C : C

static {
const a = 1;
>a : 1
>1 : 1

a;
>a : 1
}
}

19 changes: 19 additions & 0 deletions tests/baselines/reference/classStaticBlock1(target=es5).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [classStaticBlock1.ts]
const a = 2;

class C {
static {
const a = 1;

a;
}
}


//// [classStaticBlock1.js]
var a = 2;
var C = /** @class */ (function () {
function C() {
}
return C;
}());
16 changes: 16 additions & 0 deletions tests/baselines/reference/classStaticBlock1(target=es5).symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock1.ts ===
const a = 2;
>a : Symbol(a, Decl(classStaticBlock1.ts, 0, 5))

class C {
>C : Symbol(C, Decl(classStaticBlock1.ts, 0, 12))

static {
const a = 1;
>a : Symbol(a, Decl(classStaticBlock1.ts, 4, 13))

a;
>a : Symbol(a, Decl(classStaticBlock1.ts, 4, 13))
}
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/classStaticBlock1(target=es5).types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/classes/classStaticBlock/classStaticBlock1.ts ===
const a = 2;
>a : 2
>2 : 2

class C {
>C : C

static {
const a = 1;
>a : 1
>1 : 1

a;
>a : 1
}
}

20 changes: 20 additions & 0 deletions tests/baselines/reference/classStaticBlock1(target=esnext).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [classStaticBlock1.ts]
const a = 2;

class C {
static {
const a = 1;

a;
}
}


//// [classStaticBlock1.js]
const a = 2;
class C {
static {
const a = 1;
a;
}
}
Loading