Skip to content

Commit 03d284d

Browse files
author
Andy Hanson
committed
Make flags non-nullable to reduce assertions
1 parent cd5c852 commit 03d284d

File tree

15 files changed

+143
-133
lines changed

15 files changed

+143
-133
lines changed

src/compiler/binder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ namespace ts {
18721872
}
18731873

18741874
function checkStrictModeNumericLiteral(node: NumericLiteral) {
1875-
if (inStrictMode && node.numericLiteralFlags! & TokenFlags.Octal) {
1875+
if (inStrictMode && node.numericLiteralFlags & TokenFlags.Octal) {
18761876
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode));
18771877
}
18781878
}
@@ -3507,7 +3507,7 @@ namespace ts {
35073507
break;
35083508

35093509
case SyntaxKind.NumericLiteral:
3510-
if ((<NumericLiteral>node).numericLiteralFlags! & TokenFlags.BinaryOrOctalSpecifier) {
3510+
if ((<NumericLiteral>node).numericLiteralFlags & TokenFlags.BinaryOrOctalSpecifier) {
35113511
transformFlags |= TransformFlags.AssertES2015;
35123512
}
35133513
break;

src/compiler/checker.ts

Lines changed: 82 additions & 82 deletions
Large diffs are not rendered by default.

src/compiler/factory.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ namespace ts {
138138

139139
/** Create a unique temporary variable. */
140140
export function createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined): Identifier;
141-
/* @internal */ export function createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes: boolean): Identifier; // tslint:disable-line unified-signatures
142-
export function createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes?: boolean): Identifier {
143-
const name = createIdentifier("");
141+
/* @internal */ export function createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes: boolean): GeneratedIdentifier; // tslint:disable-line unified-signatures
142+
export function createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes?: boolean): GeneratedIdentifier {
143+
const name = createIdentifier("") as GeneratedIdentifier;
144144
name.autoGenerateFlags = GeneratedIdentifierFlags.Auto;
145145
name.autoGenerateId = nextAutoGenerateId;
146146
nextAutoGenerateId++;
@@ -171,9 +171,11 @@ namespace ts {
171171
return name;
172172
}
173173

174+
/* @internal */ export function createOptimisticUniqueName(text: string): GeneratedIdentifier;
174175
/** Create a unique name based on the supplied text. */
175-
export function createOptimisticUniqueName(text: string): Identifier {
176-
const name = createIdentifier(text);
176+
export function createOptimisticUniqueName(text: string): Identifier;
177+
export function createOptimisticUniqueName(text: string): GeneratedIdentifier {
178+
const name = createIdentifier(text) as GeneratedIdentifier;
177179
name.autoGenerateFlags = GeneratedIdentifierFlags.Unique | GeneratedIdentifierFlags.Optimistic;
178180
name.autoGenerateId = nextAutoGenerateId;
179181
nextAutoGenerateId++;
@@ -183,7 +185,7 @@ namespace ts {
183185
/** Create a unique name based on the supplied text. This does not consider names injected by the transformer. */
184186
export function createFileLevelUniqueName(text: string): Identifier {
185187
const name = createOptimisticUniqueName(text);
186-
name.autoGenerateFlags! |= GeneratedIdentifierFlags.FileLevel;
188+
name.autoGenerateFlags |= GeneratedIdentifierFlags.FileLevel;
187189
return name;
188190
}
189191

@@ -1763,9 +1765,9 @@ namespace ts {
17631765
: node;
17641766
}
17651767

1766-
export function createVariableDeclarationList(declarations: ReadonlyArray<VariableDeclaration>, flags?: NodeFlags) {
1768+
export function createVariableDeclarationList(declarations: ReadonlyArray<VariableDeclaration>, flags = NodeFlags.None) {
17671769
const node = <VariableDeclarationList>createSynthesizedNode(SyntaxKind.VariableDeclarationList);
1768-
node.flags |= flags! & NodeFlags.BlockScoped;
1770+
node.flags |= flags & NodeFlags.BlockScoped;
17691771
node.declarations = createNodeArray(declarations);
17701772
return node;
17711773
}
@@ -1947,9 +1949,9 @@ namespace ts {
19471949
: node;
19481950
}
19491951

1950-
export function createModuleDeclaration(decorators: ReadonlyArray<Decorator> | undefined, modifiers: ReadonlyArray<Modifier> | undefined, name: ModuleName, body: ModuleBody | undefined, flags?: NodeFlags) {
1952+
export function createModuleDeclaration(decorators: ReadonlyArray<Decorator> | undefined, modifiers: ReadonlyArray<Modifier> | undefined, name: ModuleName, body: ModuleBody | undefined, flags = NodeFlags.None) {
19511953
const node = <ModuleDeclaration>createSynthesizedNode(SyntaxKind.ModuleDeclaration);
1952-
node.flags |= flags! & (NodeFlags.Namespace | NodeFlags.NestedNamespace | NodeFlags.GlobalAugmentation);
1954+
node.flags |= flags & (NodeFlags.Namespace | NodeFlags.NestedNamespace | NodeFlags.GlobalAugmentation);
19531955
node.decorators = asNodeArray(decorators);
19541956
node.modifiers = asNodeArray(modifiers);
19551957
node.name = name;
@@ -2505,7 +2507,7 @@ namespace ts {
25052507
/* @internal */
25062508
export function createEndOfDeclarationMarker(original: Node) {
25072509
const node = <EndOfDeclarationMarker>createSynthesizedNode(SyntaxKind.EndOfDeclarationMarker);
2508-
node.emitNode = {};
2510+
node.emitNode = {} as EmitNode;
25092511
node.original = original;
25102512
return node;
25112513
}
@@ -2517,7 +2519,7 @@ namespace ts {
25172519
/* @internal */
25182520
export function createMergeDeclarationMarker(original: Node) {
25192521
const node = <MergeDeclarationMarker>createSynthesizedNode(SyntaxKind.MergeDeclarationMarker);
2520-
node.emitNode = {};
2522+
node.emitNode = {} as EmitNode;
25212523
node.original = original;
25222524
return node;
25232525
}
@@ -2724,21 +2726,21 @@ namespace ts {
27242726
* various transient transformation properties.
27252727
*/
27262728
/* @internal */
2727-
export function getOrCreateEmitNode(node: Node) {
2729+
export function getOrCreateEmitNode(node: Node): EmitNode {
27282730
if (!node.emitNode) {
27292731
if (isParseTreeNode(node)) {
27302732
// To avoid holding onto transformation artifacts, we keep track of any
27312733
// parse tree node we are annotating. This allows us to clean them up after
27322734
// all transformations have completed.
27332735
if (node.kind === SyntaxKind.SourceFile) {
2734-
return node.emitNode = { annotatedNodes: [node] };
2736+
return node.emitNode = { annotatedNodes: [node] } as EmitNode;
27352737
}
27362738

27372739
const sourceFile = getSourceFileOfNode(node);
27382740
getOrCreateEmitNode(sourceFile).annotatedNodes!.push(node);
27392741
}
27402742

2741-
node.emitNode = {};
2743+
node.emitNode = {} as EmitNode;
27422744
}
27432745

27442746
return node.emitNode;
@@ -2766,7 +2768,7 @@ namespace ts {
27662768
/* @internal */
27672769
export function addEmitFlags<T extends Node>(node: T, emitFlags: EmitFlags) {
27682770
const emitNode = getOrCreateEmitNode(node);
2769-
emitNode.flags = emitNode.flags! | emitFlags;
2771+
emitNode.flags = emitNode.flags | emitFlags;
27702772
return node;
27712773
}
27722774

@@ -2994,7 +2996,7 @@ namespace ts {
29942996
helpers,
29952997
startsOnNewLine,
29962998
} = sourceEmitNode;
2997-
if (!destEmitNode) destEmitNode = {};
2999+
if (!destEmitNode) destEmitNode = {} as EmitNode;
29983000
// We are using `.slice()` here in case `destEmitNode.leadingComments` is pushed to later.
29993001
if (leadingComments) destEmitNode.leadingComments = addRange(leadingComments.slice(), destEmitNode.leadingComments);
30003002
if (trailingComments) destEmitNode.trailingComments = addRange(trailingComments.slice(), destEmitNode.trailingComments);
@@ -3061,7 +3063,7 @@ namespace ts {
30613063
: createElementAccess(target, memberName),
30623064
memberName
30633065
);
3064-
getOrCreateEmitNode(expression).flags! |= EmitFlags.NoNestedSourceMaps;
3066+
getOrCreateEmitNode(expression).flags |= EmitFlags.NoNestedSourceMaps;
30653067
return expression;
30663068
}
30673069
}
@@ -3705,13 +3707,13 @@ namespace ts {
37053707
return getName(node, allowComments, allowSourceMaps);
37063708
}
37073709

3708-
function getName(node: Declaration, allowComments?: boolean, allowSourceMaps?: boolean, emitFlags?: EmitFlags) {
3710+
function getName(node: Declaration, allowComments?: boolean, allowSourceMaps?: boolean, emitFlags: EmitFlags = 0) {
37093711
const nodeName = getNameOfDeclaration(node);
37103712
if (nodeName && isIdentifier(nodeName) && !isGeneratedIdentifier(nodeName)) {
37113713
const name = getMutableClone(nodeName);
3712-
emitFlags! |= getEmitFlags(nodeName);
3713-
if (!allowSourceMaps) emitFlags! |= EmitFlags.NoSourceMap;
3714-
if (!allowComments) emitFlags! |= EmitFlags.NoComments;
3714+
emitFlags |= getEmitFlags(nodeName);
3715+
if (!allowSourceMaps) emitFlags |= EmitFlags.NoSourceMap;
3716+
if (!allowComments) emitFlags |= EmitFlags.NoComments;
37153717
if (emitFlags) setEmitFlags(name, emitFlags);
37163718
return name;
37173719
}

src/compiler/sourcemap.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ namespace ts {
334334

335335
if (node) {
336336
const emitNode = node.emitNode;
337-
const emitFlags = emitNode && emitNode.flags;
337+
const emitFlags = emitNode && emitNode.flags || EmitFlags.None;
338338
const range = emitNode && emitNode.sourceMapRange;
339339
const { pos, end } = range || node;
340340
let source = range && range.source;
@@ -344,14 +344,14 @@ namespace ts {
344344
if (source) setSourceFile(source);
345345

346346
if (node.kind !== SyntaxKind.NotEmittedStatement
347-
&& (emitFlags! & EmitFlags.NoLeadingSourceMap) === 0
347+
&& (emitFlags & EmitFlags.NoLeadingSourceMap) === 0
348348
&& pos >= 0) {
349349
emitPos(skipSourceTrivia(pos));
350350
}
351351

352352
if (source) setSourceFile(oldSource);
353353

354-
if (emitFlags! & EmitFlags.NoNestedSourceMaps) {
354+
if (emitFlags & EmitFlags.NoNestedSourceMaps) {
355355
disabled = true;
356356
emitCallback(hint, node);
357357
disabled = false;
@@ -363,7 +363,7 @@ namespace ts {
363363
if (source) setSourceFile(source);
364364

365365
if (node.kind !== SyntaxKind.NotEmittedStatement
366-
&& (emitFlags! & EmitFlags.NoTrailingSourceMap) === 0
366+
&& (emitFlags & EmitFlags.NoTrailingSourceMap) === 0
367367
&& end >= 0) {
368368
emitPos(end);
369369
}
@@ -386,18 +386,18 @@ namespace ts {
386386
}
387387

388388
const emitNode = node && node.emitNode;
389-
const emitFlags = emitNode && emitNode.flags;
389+
const emitFlags = emitNode && emitNode.flags || EmitFlags.None;
390390
const range = emitNode && emitNode.tokenSourceMapRanges && emitNode.tokenSourceMapRanges[token];
391391

392392
tokenPos = skipSourceTrivia(range ? range.pos : tokenPos);
393-
if ((emitFlags! & EmitFlags.NoTokenLeadingSourceMaps) === 0 && tokenPos >= 0) {
393+
if ((emitFlags & EmitFlags.NoTokenLeadingSourceMaps) === 0 && tokenPos >= 0) {
394394
emitPos(tokenPos);
395395
}
396396

397397
tokenPos = emitCallback(token, writer, tokenPos);
398398

399399
if (range) tokenPos = range.end;
400-
if ((emitFlags! & EmitFlags.NoTokenTrailingSourceMaps) === 0 && tokenPos >= 0) {
400+
if ((emitFlags & EmitFlags.NoTokenTrailingSourceMaps) === 0 && tokenPos >= 0) {
401401
emitPos(tokenPos);
402402
}
403403

src/compiler/transformers/es2015.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3628,7 +3628,7 @@ namespace ts {
36283628
* @param node A string literal.
36293629
*/
36303630
function visitNumericLiteral(node: NumericLiteral) {
3631-
if (node.numericLiteralFlags! & TokenFlags.BinaryOrOctalSpecifier) {
3631+
if (node.numericLiteralFlags & TokenFlags.BinaryOrOctalSpecifier) {
36323632
return setTextRange(createNumericLiteral(node.text), node);
36333633
}
36343634
return node;

src/compiler/transformers/es2017.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ namespace ts {
649649
);
650650

651651
// Mark this node as originally an async function
652-
(generatorFunc.emitNode || (generatorFunc.emitNode = {})).flags! |= EmitFlags.AsyncFunctionBody | EmitFlags.ReuseTempVariableScope;
652+
(generatorFunc.emitNode || (generatorFunc.emitNode = {} as EmitNode)).flags |= EmitFlags.AsyncFunctionBody | EmitFlags.ReuseTempVariableScope;
653653

654654
return createCall(
655655
getHelperName("__awaiter"),

src/compiler/transformers/esnext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ namespace ts {
918918
context.requestEmitHelper(asyncGeneratorHelper);
919919

920920
// Mark this node as originally an async function
921-
(generatorFunc.emitNode || (generatorFunc.emitNode = {})).flags! |= EmitFlags.AsyncFunctionBody;
921+
(generatorFunc.emitNode || (generatorFunc.emitNode = {} as EmitNode)).flags |= EmitFlags.AsyncFunctionBody;
922922

923923
return createCall(
924924
getHelperName("__asyncGenerator"),

src/compiler/transformers/ts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ namespace ts {
897897
// record an alias as the class name is not in scope for statics.
898898
enableSubstitutionForClassAliases();
899899
const alias = getSynthesizedClone(temp);
900-
alias.autoGenerateFlags! &= ~GeneratedIdentifierFlags.ReservedInNestedScopes;
900+
alias.autoGenerateFlags &= ~GeneratedIdentifierFlags.ReservedInNestedScopes;
901901
classAliases[getOriginalNodeId(node)] = alias;
902902
}
903903

src/compiler/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ namespace ts {
15931593
export interface NumericLiteral extends LiteralExpression {
15941594
kind: SyntaxKind.NumericLiteral;
15951595
/* @internal */
1596-
numericLiteralFlags?: TokenFlags;
1596+
numericLiteralFlags: TokenFlags;
15971597
}
15981598

15991599
export interface TemplateHead extends LiteralLikeNode {
@@ -3543,7 +3543,7 @@ namespace ts {
35433543

35443544
/* @internal */
35453545
export interface NodeLinks {
3546-
flags?: NodeCheckFlags; // Set of flags specific to Node
3546+
flags: NodeCheckFlags; // Set of flags specific to Node
35473547
resolvedType?: Type; // Cached type of type node
35483548
resolvedSignature?: Signature; // Cached signature of signature node or call expression
35493549
resolvedSymbol?: Symbol; // Cached name resolution result
@@ -3553,7 +3553,7 @@ namespace ts {
35533553
isVisible?: boolean; // Is this node visible
35543554
containsArgumentsReference?: boolean; // Whether a function-like declaration contains an 'arguments' reference
35553555
hasReportedStatementInAmbientContext?: boolean; // Cache boolean if we report statements in ambient context
3556-
jsxFlags?: JsxFlags; // flags for knowing what kind of element/attributes we're dealing with
3556+
jsxFlags: JsxFlags; // flags for knowing what kind of element/attributes we're dealing with
35573557
resolvedJsxElementAttributesType?: Type; // resolved element attributes type of a JSX openinglike element
35583558
resolvedJsxElementAllAttributesType?: Type; // resolved all element attributes type of a JSX openinglike element
35593559
hasSuperCall?: boolean; // recorded result when we try to find super-call. We only try to find one if this flag is undefined, indicating that we haven't made an attempt.
@@ -4744,7 +4744,7 @@ namespace ts {
47444744
/* @internal */
47454745
export interface EmitNode {
47464746
annotatedNodes?: Node[]; // Tracks Parse-tree nodes with EmitNodes for eventual cleanup.
4747-
flags?: EmitFlags; // Flags that customize emit
4747+
flags: EmitFlags; // Flags that customize emit
47484748
leadingComments?: SynthesizedComment[]; // Synthesized leading comments
47494749
trailingComments?: SynthesizedComment[]; // Synthesized trailing comments
47504750
commentRange?: TextRange; // The text range to use when emitting leading or trailing comments
@@ -4757,6 +4757,7 @@ namespace ts {
47574757
}
47584758

47594759
export const enum EmitFlags {
4760+
None = 0,
47604761
SingleLine = 1 << 0, // The contents of this node should be emitted on a single line.
47614762
AdviseOnEmitNode = 1 << 1, // The printer should invoke the onEmitNode callback when printing this node.
47624763
NoSubstitution = 1 << 2, // Disables further substitution of an expression.

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ namespace ts {
360360
export function getLiteralText(node: LiteralLikeNode, sourceFile: SourceFile) {
361361
// If we don't need to downlevel and we can reach the original source text using
362362
// the node's parent reference, then simply get the text as it was originally written.
363-
if (!nodeIsSynthesized(node) && node.parent && !(isNumericLiteral(node) && node.numericLiteralFlags! & TokenFlags.ContainsSeparator)) {
363+
if (!nodeIsSynthesized(node) && node.parent && !(isNumericLiteral(node) && node.numericLiteralFlags & TokenFlags.ContainsSeparator)) {
364364
return getSourceTextOfNodeFromSourceFile(sourceFile, node);
365365
}
366366

0 commit comments

Comments
 (0)