Skip to content

Commit

Permalink
Fixed const variables not properly marked const
Browse files Browse the repository at this point in the history
Resolves #1866
  • Loading branch information
Gerrit0 committed Feb 10, 2022
1 parent 33d164b commit 038f2a8
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 67 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

### Bug Fixes

- Fixed `const` variables not properly marked as `const`, #1866.

## v0.22.11 (2022-01-18)

### Features
Expand Down
23 changes: 10 additions & 13 deletions src/lib/converter/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,19 +930,6 @@ function convertVariableAsFunction(
exportSymbol
);
setModifiers(symbol, accessDeclaration, reflection);
// Does anyone care about this? I doubt it...
if (
declaration &&
hasAllFlags(symbol.flags, ts.SymbolFlags.BlockScopedVariable)
) {
reflection.setFlag(
ReflectionFlag.Const,
hasAllFlags(
(declaration || symbol.valueDeclaration).parent.flags,
ts.NodeFlags.Const
)
);
}

context.finalizeDeclarationReflection(reflection, symbol, exportSymbol);

Expand Down Expand Up @@ -1066,6 +1053,16 @@ function setModifiers(
hasAllFlags(modifiers, ts.ModifierFlags.Abstract)
);

if (
reflection.kindOf(ReflectionKind.Variable) &&
hasAllFlags(symbol.flags, ts.SymbolFlags.BlockScopedVariable)
) {
reflection.setFlag(
ReflectionFlag.Const,
hasAllFlags(declaration.parent.flags, ts.NodeFlags.Const)
);
}

// ReflectionFlag.Static happens when constructing the reflection.
// We don't have sufficient information here to determine if it ought to be static.
}
6 changes: 4 additions & 2 deletions src/test/converter/class/specs-with-lump-categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@
"kind": 32,
"kindString": "Variable",
"flags": {
"isPrivate": true
"isPrivate": true,
"isConst": true
},
"comment": {
"shortText": "A variable that is made private via comment."
Expand All @@ -205,7 +206,8 @@
"kind": 32,
"kindString": "Variable",
"flags": {
"isProtected": true
"isProtected": true,
"isConst": true
},
"comment": {
"shortText": "A variable that is made protected via comment."
Expand Down
6 changes: 4 additions & 2 deletions src/test/converter/class/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@
"kind": 32,
"kindString": "Variable",
"flags": {
"isPrivate": true
"isPrivate": true,
"isConst": true
},
"comment": {
"shortText": "A variable that is made private via comment."
Expand All @@ -205,7 +206,8 @@
"kind": 32,
"kindString": "Variable",
"flags": {
"isProtected": true
"isProtected": true,
"isConst": true
},
"comment": {
"shortText": "A variable that is made protected via comment."
Expand Down
8 changes: 6 additions & 2 deletions src/test/converter/declaration/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@
"name": "x",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "intrinsic",
"name": "number"
Expand Down Expand Up @@ -206,7 +208,9 @@
"name": "ActionSet",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "reference",
"qualifiedName": "ts.server.ActionSet",
Expand Down
28 changes: 21 additions & 7 deletions src/test/converter/exports/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@
"name": "Member",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "literal",
"value": 1
Expand All @@ -117,7 +119,9 @@
"name": "Module",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "reference",
"id": 29,
Expand All @@ -130,7 +134,9 @@
"name": "TypedMember",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "reference",
"id": 29,
Expand All @@ -143,7 +149,9 @@
"name": "TypedModule",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "reference",
"id": 29,
Expand Down Expand Up @@ -356,7 +364,9 @@
"name": "default",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "literal",
"value": 5
Expand Down Expand Up @@ -386,7 +396,9 @@
"name": "x",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "literal",
"value": 5
Expand Down Expand Up @@ -511,7 +523,9 @@
"name": "a",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"comment": {
"shortText": "A simple named export that will be exported from export.ts"
},
Expand Down
8 changes: 2 additions & 6 deletions src/test/converter/function/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,7 @@
"name": "all",
"kind": 64,
"kindString": "Function",
"flags": {
"isConst": true
},
"flags": {},
"signatures": [
{
"id": 100,
Expand Down Expand Up @@ -1544,9 +1542,7 @@
"name": "variableFunction",
"kind": 64,
"kindString": "Function",
"flags": {
"isConst": true
},
"flags": {},
"signatures": [
{
"id": 84,
Expand Down
12 changes: 5 additions & 7 deletions src/test/converter/js/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
"name": "export=",
"kind": 64,
"kindString": "Function",
"flags": {
"isConst": true
},
"flags": {},
"signatures": [
{
"id": 3,
Expand Down Expand Up @@ -536,7 +534,9 @@
"name": "ColumnType",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"comment": {},
"type": {
"type": "reflection",
Expand Down Expand Up @@ -591,9 +591,7 @@
"name": "usedFoo",
"kind": 64,
"kindString": "Function",
"flags": {
"isConst": true
},
"flags": {},
"signatures": [
{
"id": 8,
Expand Down
12 changes: 3 additions & 9 deletions src/test/converter/mixin/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1053,9 +1053,7 @@
"name": "Mixin1Func",
"kind": 64,
"kindString": "Function",
"flags": {
"isConst": true
},
"flags": {},
"signatures": [
{
"id": 20,
Expand Down Expand Up @@ -1158,9 +1156,7 @@
"name": "Mixin2",
"kind": 64,
"kindString": "Function",
"flags": {
"isConst": true
},
"flags": {},
"signatures": [
{
"id": 34,
Expand Down Expand Up @@ -1273,9 +1269,7 @@
"name": "Mixin3",
"kind": 64,
"kindString": "Function",
"flags": {
"isConst": true
},
"flags": {},
"signatures": [
{
"id": 52,
Expand Down
44 changes: 33 additions & 11 deletions src/test/converter/types/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@
"name": "BigIntLiteralType",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "literal",
"value": {
Expand All @@ -95,7 +97,9 @@
"name": "NegativeBigIntLiteralType",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "literal",
"value": {
Expand All @@ -110,7 +114,9 @@
"name": "negativeOne",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "literal",
"value": -1
Expand All @@ -122,7 +128,9 @@
"name": "numArray",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "array",
"elementType": {
Expand Down Expand Up @@ -661,7 +669,9 @@
"name": "x",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "literal",
"value": 1
Expand Down Expand Up @@ -843,7 +853,9 @@
"name": "leadingRest",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "tuple",
"elements": [
Expand All @@ -870,7 +882,9 @@
"name": "namedTuple",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "tuple",
"elements": [
Expand Down Expand Up @@ -901,7 +915,9 @@
"name": "withOptionalElements",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "tuple",
"elements": [
Expand Down Expand Up @@ -932,7 +948,9 @@
"name": "withRestType",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "tuple",
"elements": [
Expand All @@ -959,7 +977,9 @@
"name": "withRestTypeNames",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "tuple",
"elements": [
Expand Down Expand Up @@ -1114,7 +1134,9 @@
"name": "a",
"kind": 32,
"kindString": "Variable",
"flags": {},
"flags": {
"isConst": true
},
"type": {
"type": "typeOperator",
"operator": "unique",
Expand Down
Loading

0 comments on commit 038f2a8

Please sign in to comment.