Skip to content

Split isConst into isVarConst and isEnumConst #25312

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
1 commit merged into from
Jun 29, 2018
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
6 changes: 3 additions & 3 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace ts {
return ModuleInstanceState.NonInstantiated;
// 2. const enum declarations
case SyntaxKind.EnumDeclaration:
if (isConst(node)) {
if (isEnumConst(node as EnumDeclaration)) {
return ModuleInstanceState.ConstEnumOnly;
}
break;
Expand Down Expand Up @@ -2611,7 +2611,7 @@ namespace ts {
}

function bindEnumDeclaration(node: EnumDeclaration) {
return isConst(node)
return isEnumConst(node)
? bindBlockScopedDeclaration(node, SymbolFlags.ConstEnum, SymbolFlags.ConstEnumExcludes)
: bindBlockScopedDeclaration(node, SymbolFlags.RegularEnum, SymbolFlags.RegularEnumExcludes);
}
Expand Down Expand Up @@ -2768,7 +2768,7 @@ namespace ts {
// report error on instantiated modules or const-enums only modules if preserveConstEnums is set
(node.kind === SyntaxKind.ModuleDeclaration && shouldReportErrorOnModuleDeclaration(<ModuleDeclaration>node)) ||
// report error on regular enums and const enums if preserveConstEnums is set
(node.kind === SyntaxKind.EnumDeclaration && (!isConstEnumDeclaration(node) || options.preserveConstEnums));
(isEnumDeclaration(node) && (!isEnumConst(node) || options.preserveConstEnums));

if (reportError) {
currentFlow = reportedUnreachableFlow;
Expand Down
25 changes: 13 additions & 12 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25169,7 +25169,7 @@ namespace ts {
}
// In ambient enum declarations that specify no const modifier, enum member declarations that omit
// a value are considered computed members (as opposed to having auto-incremented values).
if (member.parent.flags & NodeFlags.Ambient && !isConst(member.parent)) {
if (member.parent.flags & NodeFlags.Ambient && !isEnumConst(member.parent)) {
return undefined;
}
// If the member declaration specifies no value, the member is considered a constant enum member.
Expand All @@ -25185,7 +25185,7 @@ namespace ts {

function computeConstantValue(member: EnumMember): string | number | undefined {
const enumKind = getEnumKind(getSymbolOfNode(member.parent));
const isConstEnum = isConst(member.parent);
const isConstEnum = isEnumConst(member.parent);
const initializer = member.initializer!;
const value = enumKind === EnumKind.Literal && !isLiteralEnumMember(member) ? undefined : evaluate(initializer);
if (value !== undefined) {
Expand Down Expand Up @@ -25320,7 +25320,7 @@ namespace ts {

computeEnumMemberValues(node);

const enumIsConst = isConst(node);
const enumIsConst = isEnumConst(node);
if (compilerOptions.isolatedModules && enumIsConst && node.flags & NodeFlags.Ambient) {
error(node.name, Diagnostics.Ambient_const_enums_are_not_allowed_when_the_isolatedModules_flag_is_provided);
}
Expand All @@ -25337,7 +25337,7 @@ namespace ts {
if (enumSymbol.declarations.length > 1) {
// check that const is placed\omitted on all enum declarations
forEach(enumSymbol.declarations, decl => {
if (isConstEnumDeclaration(decl) !== enumIsConst) {
if (isEnumDeclaration(decl) && isEnumConst(decl) !== enumIsConst) {
error(getNameOfDeclaration(decl), Diagnostics.Enum_declarations_must_all_be_const_or_non_const);
}
});
Expand Down Expand Up @@ -27201,8 +27201,9 @@ namespace ts {
const symbol = getNodeLinks(node).resolvedSymbol;
if (symbol && (symbol.flags & SymbolFlags.EnumMember)) {
// inline property\index accesses only for const enums
if (isConstEnumDeclaration(symbol.valueDeclaration.parent)) {
return getEnumMemberValue(<EnumMember>symbol.valueDeclaration);
const member = symbol.valueDeclaration as EnumMember;
if (isEnumConst(member.parent)) {
return getEnumMemberValue(member);
}
}

Expand Down Expand Up @@ -27356,7 +27357,7 @@ namespace ts {
}

function isLiteralConstDeclaration(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration): boolean {
if (isConst(node)) {
if (isVariableDeclaration(node) && isVarConst(node)) {
const type = getTypeOfSymbol(getSymbolOfNode(node));
return !!(type.flags & TypeFlags.StringOrNumberLiteral && type.flags & TypeFlags.FreshLiteral);
}
Expand Down Expand Up @@ -28661,7 +28662,7 @@ namespace ts {
if (node.parent.parent.kind !== SyntaxKind.ForInStatement && node.parent.parent.kind !== SyntaxKind.ForOfStatement) {
if (node.flags & NodeFlags.Ambient) {
if (node.initializer) {
if (isConst(node) && !node.type) {
if (isVarConst(node) && !node.type) {
if (!isStringOrNumberLiteralExpression(node.initializer)) {
return grammarErrorOnNode(node.initializer, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal);
}
Expand All @@ -28672,7 +28673,7 @@ namespace ts {
return grammarErrorAtPos(node, node.initializer.pos - equalsTokenLength, equalsTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
}
}
if (node.initializer && !(isConst(node) && isStringOrNumberLiteralExpression(node.initializer))) {
if (node.initializer && !(isVarConst(node) && isStringOrNumberLiteralExpression(node.initializer))) {
// Error on equals token which immediate precedes the initializer
const equalsTokenLength = "=".length;
return grammarErrorAtPos(node, node.initializer.pos - equalsTokenLength, equalsTokenLength, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
Expand All @@ -28682,7 +28683,7 @@ namespace ts {
if (isBindingPattern(node.name) && !isBindingPattern(node.parent)) {
return grammarErrorOnNode(node, Diagnostics.A_destructuring_declaration_must_have_an_initializer);
}
if (isConst(node)) {
if (isVarConst(node)) {
return grammarErrorOnNode(node, Diagnostics.const_declarations_must_be_initialized);
}
}
Expand All @@ -28697,7 +28698,7 @@ namespace ts {
checkESModuleMarker(node.name);
}

const checkLetConstNames = (isLet(node) || isConst(node));
const checkLetConstNames = (isLet(node) || isVarConst(node));

// 1. LexicalDeclaration : LetOrConst BindingList ;
// It is a Syntax Error if the BoundNames of BindingList contains "let".
Expand Down Expand Up @@ -28777,7 +28778,7 @@ namespace ts {
if (isLet(node.declarationList)) {
return grammarErrorOnNode(node, Diagnostics.let_declarations_can_only_be_declared_inside_a_block);
}
else if (isConst(node.declarationList)) {
else if (isVarConst(node.declarationList)) {
return grammarErrorOnNode(node, Diagnostics.const_declarations_can_only_be_declared_inside_a_block);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1983,7 +1983,7 @@ namespace ts {
}

function emitVariableDeclarationList(node: VariableDeclarationList) {
writeKeyword(isLet(node) ? "let" : isConst(node) ? "const" : "var");
writeKeyword(isLet(node) ? "let" : isVarConst(node) ? "const" : "var");
writeSpace();
emitList(node, node.declarations, ListFormat.VariableDeclarationList);
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2602,7 +2602,7 @@ namespace ts {
* @param node The enum declaration node.
*/
function shouldEmitEnumDeclaration(node: EnumDeclaration) {
return !isConst(node)
return !isEnumConst(node)
|| compilerOptions.preserveConstEnums
|| compilerOptions.isolatedModules;
}
Expand Down
11 changes: 5 additions & 6 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,13 +897,12 @@ namespace ts {
return file.scriptKind === ScriptKind.JSON;
}

export function isConstEnumDeclaration(node: Node): boolean {
return node.kind === SyntaxKind.EnumDeclaration && isConst(node);
export function isEnumConst(node: EnumDeclaration): boolean {
return !!(getCombinedModifierFlags(node) & ModifierFlags.Const);
}

export function isConst(node: Node): boolean {
return !!(getCombinedNodeFlags(node) & NodeFlags.Const)
|| !!(isDeclaration(node) && getCombinedModifierFlags(node) & ModifierFlags.Const);
export function isVarConst(node: VariableDeclaration | VariableDeclarationList): boolean {
return !!(getCombinedNodeFlags(node) & NodeFlags.Const);
}

export function isLet(node: Node): boolean {
Expand Down Expand Up @@ -1184,7 +1183,7 @@ namespace ts {
}

export function isValidESSymbolDeclaration(node: Node): node is VariableDeclaration | PropertyDeclaration | SignatureDeclaration {
return isVariableDeclaration(node) ? isConst(node) && isIdentifier(node.name) && isVariableDeclarationInVariableStatement(node) :
return isVariableDeclaration(node) ? isVarConst(node) && isIdentifier(node.name) && isVariableDeclarationInVariableStatement(node) :
isPropertyDeclaration(node) ? hasReadonlyModifier(node) && hasStaticModifier(node) :
isPropertySignature(node) && hasReadonlyModifier(node);
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/symbolDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace ts.SymbolDisplay {
if (isFirstDeclarationOfSymbolParameter(symbol)) {
return ScriptElementKind.parameterElement;
}
else if (symbol.valueDeclaration && isConst(symbol.valueDeclaration)) {
else if (symbol.valueDeclaration && isVarConst(symbol.valueDeclaration as VariableDeclaration)) {
return ScriptElementKind.constElement;
}
else if (forEach(symbol.declarations, isLet)) {
Expand Down Expand Up @@ -298,7 +298,7 @@ namespace ts.SymbolDisplay {
}
if (symbolFlags & SymbolFlags.Enum) {
prefixNextMeaning();
if (forEach(symbol.declarations, isConstEnumDeclaration)) {
if (some(symbol.declarations, d => isEnumDeclaration(d) && isEnumConst(d))) {
displayParts.push(keywordPart(SyntaxKind.ConstKeyword));
displayParts.push(spacePart());
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ namespace ts {
}

function getKindOfVariableDeclaration(v: VariableDeclaration): ScriptElementKind {
return isConst(v)
return isVarConst(v)
? ScriptElementKind.constElement
: isLet(v)
? ScriptElementKind.letElement
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6168,8 +6168,8 @@ declare namespace ts {
function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpan;
function isExternalOrCommonJsModule(file: SourceFile): boolean;
function isJsonSourceFile(file: SourceFile): file is JsonSourceFile;
function isConstEnumDeclaration(node: Node): boolean;
function isConst(node: Node): boolean;
function isEnumConst(node: EnumDeclaration): boolean;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an internal-only change. (#24966)

function isVarConst(node: VariableDeclaration | VariableDeclarationList): boolean;
function isLet(node: Node): boolean;
function isSuperCall(n: Node): n is SuperCall;
function isImportCall(n: Node): n is ImportCall;
Expand Down