Skip to content

Allow accessors in ambient classes and interfaces #32772

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

Closed
wants to merge 4 commits into from
Closed
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
10 changes: 10 additions & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,8 @@ namespace ts {
case SyntaxKind.MethodSignature:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.GetAccessorSignature:
case SyntaxKind.SetAccessorSignature:
case SyntaxKind.CallSignature:
case SyntaxKind.JSDocSignature:
case SyntaxKind.JSDocFunctionType:
Expand Down Expand Up @@ -1600,6 +1602,8 @@ namespace ts {
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.GetAccessorSignature:
case SyntaxKind.SetAccessorSignature:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
Expand Down Expand Up @@ -2236,8 +2240,10 @@ namespace ts {
case SyntaxKind.Constructor:
return declareSymbolAndAddToSymbolTable(<Declaration>node, SymbolFlags.Constructor, /*symbolExcludes:*/ SymbolFlags.None);
case SyntaxKind.GetAccessor:
case SyntaxKind.GetAccessorSignature:
return bindPropertyOrMethodOrAccessor(<Declaration>node, SymbolFlags.GetAccessor, SymbolFlags.GetAccessorExcludes);
case SyntaxKind.SetAccessor:
case SyntaxKind.SetAccessorSignature:
return bindPropertyOrMethodOrAccessor(<Declaration>node, SymbolFlags.SetAccessor, SymbolFlags.SetAccessorExcludes);
case SyntaxKind.FunctionType:
case SyntaxKind.JSDocFunctionType:
Expand Down Expand Up @@ -3750,6 +3756,8 @@ namespace ts {
case SyntaxKind.TypeParameter:
case SyntaxKind.PropertySignature:
case SyntaxKind.MethodSignature:
case SyntaxKind.GetAccessorSignature:
case SyntaxKind.SetAccessorSignature:
case SyntaxKind.CallSignature:
case SyntaxKind.ConstructSignature:
case SyntaxKind.IndexSignature:
Expand Down Expand Up @@ -3934,6 +3942,8 @@ namespace ts {
case SyntaxKind.TypeParameter:
case SyntaxKind.PropertySignature:
case SyntaxKind.MethodSignature:
case SyntaxKind.GetAccessorSignature:
case SyntaxKind.SetAccessorSignature:
case SyntaxKind.CallSignature:
case SyntaxKind.ConstructSignature:
case SyntaxKind.IndexSignature:
Expand Down
352 changes: 211 additions & 141 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,6 @@
"category": "Error",
"code": 1085
},
"An accessor cannot be declared in an ambient context.": {
"category": "Error",
"code": 1086
},
"'{0}' modifier cannot appear on a constructor declaration.": {
"category": "Error",
"code": 1089
Expand Down Expand Up @@ -3040,6 +3036,27 @@
"code": 4105
},

"Parameter type of setter '{0}' from exported interface has or is using name '{1}' from private module '{2}'.": {
"category": "Error",
"code": 4106
},
"Parameter type of setter '{0}' from exported interface has or is using private name '{1}'.": {
"category": "Error",
"code": 4107
},
"Return type of getter '{0}' from exported interface has or is using name '{1}' from external module {2} but cannot be named.": {
"category": "Error",
"code": 4108
},
"Return type of getter '{0}' from exported interface has or is using name '{1}' from private module '{2}'.": {
"category": "Error",
"code": 4109
},
"Return type of getter '{0}' from exported interface has or is using private name '{1}'.": {
"category": "Error",
"code": 4110
},

"The current host does not support the '{0}' option.": {
"category": "Error",
"code": 5001
Expand Down
17 changes: 17 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ namespace ts {
isLiteralConstDeclaration: notImplemented,
getJsxFactoryEntity: notImplemented,
getAllAccessorDeclarations: notImplemented,
getAllAccessorSignatures: notImplemented,
getSymbolOfExternalModuleSpecifier: notImplemented,
isBindingCapturedByNode: notImplemented,
};
Expand Down Expand Up @@ -1211,6 +1212,9 @@ namespace ts {
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
return emitAccessorDeclaration(<AccessorDeclaration>node);
case SyntaxKind.GetAccessorSignature:
case SyntaxKind.SetAccessorSignature:
return emitAccessorSignature(<AccessorSignature>node);
case SyntaxKind.CallSignature:
return emitCallSignature(<CallSignatureDeclaration>node);
case SyntaxKind.ConstructSignature:
Expand Down Expand Up @@ -1865,6 +1869,19 @@ namespace ts {
emitSignatureAndBody(node, emitSignatureHead);
}

function emitAccessorSignature(node: AccessorSignature) {
pushNameGenerationScope(node);
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
writeKeyword(node.kind === SyntaxKind.GetAccessorSignature ? "get" : "set");
writeSpace();
emit(node.name);
emitParameters(node, node.parameters);
emitTypeAnnotation(node.type);
writeTrailingSemicolon();
popNameGenerationScope(node);
}

function emitCallSignature(node: CallSignatureDeclaration) {
pushNameGenerationScope(node);
emitDecorators(node, node.decorators);
Expand Down
60 changes: 60 additions & 0 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,66 @@ namespace ts {
: node;
}

export function createGetAccessorSignature(
decorators: ReadonlyArray<Decorator> | undefined,
modifiers: ReadonlyArray<Modifier> | undefined,
name: string | PropertyName,
parameters: ReadonlyArray<ParameterDeclaration>,
type: TypeNode | undefined) {
const node = <GetAccessorSignature>createSynthesizedNode(SyntaxKind.GetAccessorSignature);
node.decorators = asNodeArray(decorators);
node.modifiers = asNodeArray(modifiers);
node.name = asName(name);
node.typeParameters = undefined;
node.parameters = createNodeArray(parameters);
node.type = type;
return node;
}

export function updateGetAccessorSignature(
node: GetAccessorSignature,
decorators: ReadonlyArray<Decorator> | undefined,
modifiers: ReadonlyArray<Modifier> | undefined,
name: PropertyName,
parameters: ReadonlyArray<ParameterDeclaration>,
type: TypeNode | undefined) {
return node.decorators !== decorators
|| node.modifiers !== modifiers
|| node.name !== name
|| node.parameters !== parameters
|| node.type !== type
? updateNode(createGetAccessorSignature(decorators, modifiers, name, parameters, type), node)
: node;
}

export function createSetAccessorSignature(
decorators: ReadonlyArray<Decorator> | undefined,
modifiers: ReadonlyArray<Modifier> | undefined,
name: string | PropertyName,
parameters: ReadonlyArray<ParameterDeclaration>) {
const node = <SetAccessorSignature>createSynthesizedNode(SyntaxKind.SetAccessorSignature);
node.decorators = asNodeArray(decorators);
node.modifiers = asNodeArray(modifiers);
node.name = asName(name);
node.typeParameters = undefined;
node.parameters = createNodeArray(parameters);
return node;
}

export function updateSetAccessorSignature(
node: SetAccessorSignature,
decorators: ReadonlyArray<Decorator> | undefined,
modifiers: ReadonlyArray<Modifier> | undefined,
name: PropertyName,
parameters: ReadonlyArray<ParameterDeclaration>) {
return node.decorators !== decorators
|| node.modifiers !== modifiers
|| node.name !== name
|| node.parameters !== parameters
? updateNode(createSetAccessorSignature(decorators, modifiers, name, parameters), node)
: node;
}

export function createCallSignature(typeParameters: ReadonlyArray<TypeParameterDeclaration> | undefined, parameters: ReadonlyArray<ParameterDeclaration>, type: TypeNode | undefined) {
return createSignatureDeclaration(SyntaxKind.CallSignature, typeParameters, parameters, type) as CallSignatureDeclaration;
}
Expand Down
35 changes: 31 additions & 4 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ namespace ts {
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.GetAccessorSignature:
case SyntaxKind.SetAccessorSignature:
case SyntaxKind.FunctionExpression:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ArrowFunction:
Expand Down Expand Up @@ -2006,6 +2008,8 @@ namespace ts {
switch (node.kind) {
case SyntaxKind.ConstructSignature:
case SyntaxKind.MethodSignature:
case SyntaxKind.GetAccessorSignature:
case SyntaxKind.SetAccessorSignature:
case SyntaxKind.IndexSignature:
case SyntaxKind.PropertySignature:
case SyntaxKind.CallSignature:
Expand Down Expand Up @@ -2702,6 +2706,14 @@ namespace ts {
return finishNode(node);
}

function parseAccessorSignature(node: AccessorSignature, kind: AccessorSignature["kind"]): AccessorSignature {
node.kind = kind;
node.name = parsePropertyName();
fillSignature(SyntaxKind.ColonToken, SignatureFlags.Type, node);
parseTypeMemberSemicolon();
return finishNode(node);
}

function parsePropertyOrMethodSignature(node: PropertySignature | MethodSignature): PropertySignature | MethodSignature {
node.name = parsePropertyName();
node.questionToken = parseOptionalToken(SyntaxKind.QuestionToken);
Expand Down Expand Up @@ -2730,10 +2742,10 @@ namespace ts {
if (token() === SyntaxKind.OpenParenToken || token() === SyntaxKind.LessThanToken) {
return true;
}
let idToken = false;
let idToken: SyntaxKind | undefined;
// Eat up all modifiers, but hold on to the last one in case it is actually an identifier
while (isModifierKind(token())) {
idToken = true;
idToken = token();
nextToken();
}
// Index signatures and computed property names are type members
Expand All @@ -2742,12 +2754,17 @@ namespace ts {
}
// Try to get the first property-like token following all modifiers
if (isLiteralPropertyName()) {
idToken = true;
idToken = token();
nextToken();
}
// If we were able to get any potential identifier, check that it is
// the start of a member declaration
if (idToken) {
if (idToken !== undefined) {
// If we have a non-keyword identifier, or if we have an accessor, then it's safe to parse.
if (!isKeyword(idToken) || idToken === SyntaxKind.SetKeyword || idToken === SyntaxKind.GetKeyword) {
return true;
}

return token() === SyntaxKind.OpenParenToken ||
token() === SyntaxKind.LessThanToken ||
token() === SyntaxKind.QuestionToken ||
Expand All @@ -2765,8 +2782,18 @@ namespace ts {
if (token() === SyntaxKind.NewKeyword && lookAhead(nextTokenIsOpenParenOrLessThan)) {
return parseSignatureMember(SyntaxKind.ConstructSignature);
}

const node = <TypeElement>createNodeWithJSDoc(SyntaxKind.Unknown);
node.modifiers = parseModifiers();

if (parseContextualModifier(SyntaxKind.GetKeyword)) {
return parseAccessorSignature(<AccessorSignature>node, SyntaxKind.GetAccessorSignature);
}

if (parseContextualModifier(SyntaxKind.SetKeyword)) {
return parseAccessorSignature(<AccessorSignature>node, SyntaxKind.SetAccessorSignature);
}

if (isIndexSignature()) {
return parseIndexSignatureDeclaration(<IndexSignatureDeclaration>node);
}
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,8 @@ namespace ts {
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.GetAccessorSignature:
case SyntaxKind.SetAccessorSignature:
case SyntaxKind.FunctionExpression:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ArrowFunction:
Expand Down
Loading