Skip to content

Commit 3110f40

Browse files
authored
Merge pull request microsoft#12250 from Microsoft/streamlineDestructuring
Streamline destructuring
2 parents a7d97c0 + cd023b2 commit 3110f40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3227
-2818
lines changed

Jakefile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ task("importDefinitelyTypedTests", [importDefinitelyTypedTestsJs], function () {
640640

641641
// Local target to build the compiler and services
642642
var tscFile = path.join(builtLocalDirectory, compilerFilename);
643-
compileFile(tscFile, compilerSources, [builtLocalDirectory, copyright].concat(compilerSources), [copyright], /*useBuiltCompiler:*/ false);
643+
compileFile(tscFile, compilerSources, [builtLocalDirectory, copyright].concat(compilerSources), [copyright], /*useBuiltCompiler:*/ false, { noMapRoot: true });
644644

645645
var servicesFile = path.join(builtLocalDirectory, "typescriptServices.js");
646646
var servicesFileInBrowserTest = path.join(builtLocalDirectory, "typescriptServicesInBrowserTest.js");

src/compiler/binder.ts

+99-51
Large diffs are not rendered by default.

src/compiler/checker.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -3030,7 +3030,7 @@ namespace ts {
30303030
}
30313031

30323032
function isComputedNonLiteralName(name: PropertyName): boolean {
3033-
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteral((<ComputedPropertyName>name).expression.kind);
3033+
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteral((<ComputedPropertyName>name).expression);
30343034
}
30353035

30363036
function getRestType(source: Type, properties: PropertyName[], symbol: Symbol): Type {
@@ -3081,7 +3081,7 @@ namespace ts {
30813081
}
30823082
const literalMembers: PropertyName[] = [];
30833083
for (const element of pattern.elements) {
3084-
if (element.kind !== SyntaxKind.OmittedExpression && !(element as BindingElement).dotDotDotToken) {
3084+
if (!(element as BindingElement).dotDotDotToken) {
30853085
literalMembers.push(element.propertyName || element.name as Identifier);
30863086
}
30873087
}
@@ -8927,7 +8927,7 @@ namespace ts {
89278927
return type;
89288928
}
89298929

8930-
function getTypeOfDestructuredProperty(type: Type, name: Identifier | LiteralExpression | ComputedPropertyName) {
8930+
function getTypeOfDestructuredProperty(type: Type, name: PropertyName) {
89318931
const text = getTextOfPropertyName(name);
89328932
return getTypeOfPropertyOfType(type, text) ||
89338933
isNumericLiteralName(text) && getIndexTypeOfType(type, IndexKind.Number) ||
@@ -14221,9 +14221,7 @@ namespace ts {
1422114221
}
1422214222
}
1422314223
else if (property.kind === SyntaxKind.SpreadAssignment) {
14224-
if (property.expression.kind !== SyntaxKind.Identifier) {
14225-
error(property.expression, Diagnostics.An_object_rest_element_must_be_an_identifier);
14226-
}
14224+
checkReferenceExpression(property.expression, Diagnostics.The_target_of_an_object_rest_assignment_must_be_a_variable_or_a_property_access);
1422714225
}
1422814226
else {
1422914227
error(property, Diagnostics.Property_assignment_expected);

src/compiler/core.ts

+7
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,13 @@ namespace ts {
816816
}
817817
}
818818

819+
export function appendProperty<T>(map: Map<T>, key: string | number, value: T): Map<T> {
820+
if (key === undefined || value === undefined) return map;
821+
if (map === undefined) map = createMap<T>();
822+
map[key] = value;
823+
return map;
824+
}
825+
819826
export function assign<T1 extends MapLike<{}>, T2, T3>(t: T1, arg1: T2, arg2: T3): T1 & T2 & T3;
820827
export function assign<T1 extends MapLike<{}>, T2>(t: T1, arg1: T2): T1 & T2;
821828
export function assign<T1 extends MapLike<{}>>(t: T1, ...args: any[]): any;

src/compiler/diagnosticMessages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,7 @@
19911991
"category": "Error",
19921992
"code": 2700
19931993
},
1994-
"An object rest element must be an identifier.": {
1994+
"The target of an object rest assignment must be a variable or a property access.": {
19951995
"category": "Error",
19961996
"code": 2701
19971997
},

src/compiler/factory.ts

+273-499
Large diffs are not rendered by default.

src/compiler/parser.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ namespace ts {
11681168

11691169
function parsePropertyNameWorker(allowComputedPropertyNames: boolean): PropertyName {
11701170
if (token() === SyntaxKind.StringLiteral || token() === SyntaxKind.NumericLiteral) {
1171-
return parseLiteralNode(/*internName*/ true);
1171+
return <StringLiteral | NumericLiteral>parseLiteralNode(/*internName*/ true);
11721172
}
11731173
if (allowComputedPropertyNames && token() === SyntaxKind.OpenBracketToken) {
11741174
return parseComputedPropertyName();
@@ -5514,7 +5514,7 @@ namespace ts {
55145514
node.flags |= NodeFlags.GlobalAugmentation;
55155515
}
55165516
else {
5517-
node.name = parseLiteralNode(/*internName*/ true);
5517+
node.name = <StringLiteral>parseLiteralNode(/*internName*/ true);
55185518
}
55195519

55205520
if (token() === SyntaxKind.OpenBraceToken) {

src/compiler/transformer.ts

+48-24
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,29 @@ namespace ts {
154154
* @param transforms An array of Transformers.
155155
*/
156156
export function transformFiles(resolver: EmitResolver, host: EmitHost, sourceFiles: SourceFile[], transformers: Transformer[]): TransformationResult {
157-
const lexicalEnvironmentVariableDeclarationsStack: VariableDeclaration[][] = [];
158-
const lexicalEnvironmentFunctionDeclarationsStack: FunctionDeclaration[][] = [];
159157
const enabledSyntaxKindFeatures = new Array<SyntaxKindFeatureFlags>(SyntaxKind.Count);
160158

159+
let lexicalEnvironmentDisabled = false;
160+
161161
let lexicalEnvironmentStackOffset = 0;
162-
let hoistedVariableDeclarations: VariableDeclaration[];
163-
let hoistedFunctionDeclarations: FunctionDeclaration[];
164-
let lexicalEnvironmentDisabled: boolean;
162+
let lexicalEnvironmentVariableDeclarations: VariableDeclaration[];
163+
let lexicalEnvironmentFunctionDeclarations: FunctionDeclaration[];
164+
let lexicalEnvironmentVariableDeclarationsStack: VariableDeclaration[][] = [];
165+
let lexicalEnvironmentFunctionDeclarationsStack: FunctionDeclaration[][] = [];
166+
let lexicalEnvironmentSuspended = false;
165167

166168
// The transformation context is provided to each transformer as part of transformer
167169
// initialization.
168170
const context: TransformationContext = {
169171
getCompilerOptions: () => host.getCompilerOptions(),
170172
getEmitResolver: () => resolver,
171173
getEmitHost: () => host,
172-
hoistVariableDeclaration,
173-
hoistFunctionDeclaration,
174174
startLexicalEnvironment,
175+
suspendLexicalEnvironment,
176+
resumeLexicalEnvironment,
175177
endLexicalEnvironment,
178+
hoistVariableDeclaration,
179+
hoistFunctionDeclaration,
176180
onSubstituteNode: (_emitContext, node) => node,
177181
enableSubstitution,
178182
isSubstitutionEnabled,
@@ -285,11 +289,11 @@ namespace ts {
285289
function hoistVariableDeclaration(name: Identifier): void {
286290
Debug.assert(!lexicalEnvironmentDisabled, "Cannot modify the lexical environment during the print phase.");
287291
const decl = createVariableDeclaration(name);
288-
if (!hoistedVariableDeclarations) {
289-
hoistedVariableDeclarations = [decl];
292+
if (!lexicalEnvironmentVariableDeclarations) {
293+
lexicalEnvironmentVariableDeclarations = [decl];
290294
}
291295
else {
292-
hoistedVariableDeclarations.push(decl);
296+
lexicalEnvironmentVariableDeclarations.push(decl);
293297
}
294298
}
295299

@@ -298,11 +302,11 @@ namespace ts {
298302
*/
299303
function hoistFunctionDeclaration(func: FunctionDeclaration): void {
300304
Debug.assert(!lexicalEnvironmentDisabled, "Cannot modify the lexical environment during the print phase.");
301-
if (!hoistedFunctionDeclarations) {
302-
hoistedFunctionDeclarations = [func];
305+
if (!lexicalEnvironmentFunctionDeclarations) {
306+
lexicalEnvironmentFunctionDeclarations = [func];
303307
}
304308
else {
305-
hoistedFunctionDeclarations.push(func);
309+
lexicalEnvironmentFunctionDeclarations.push(func);
306310
}
307311
}
308312

@@ -312,16 +316,31 @@ namespace ts {
312316
*/
313317
function startLexicalEnvironment(): void {
314318
Debug.assert(!lexicalEnvironmentDisabled, "Cannot start a lexical environment during the print phase.");
319+
Debug.assert(!lexicalEnvironmentSuspended, "Lexical environment is suspended.");
315320

316321
// Save the current lexical environment. Rather than resizing the array we adjust the
317322
// stack size variable. This allows us to reuse existing array slots we've
318323
// already allocated between transformations to avoid allocation and GC overhead during
319324
// transformation.
320-
lexicalEnvironmentVariableDeclarationsStack[lexicalEnvironmentStackOffset] = hoistedVariableDeclarations;
321-
lexicalEnvironmentFunctionDeclarationsStack[lexicalEnvironmentStackOffset] = hoistedFunctionDeclarations;
325+
lexicalEnvironmentVariableDeclarationsStack[lexicalEnvironmentStackOffset] = lexicalEnvironmentVariableDeclarations;
326+
lexicalEnvironmentFunctionDeclarationsStack[lexicalEnvironmentStackOffset] = lexicalEnvironmentFunctionDeclarations;
322327
lexicalEnvironmentStackOffset++;
323-
hoistedVariableDeclarations = undefined;
324-
hoistedFunctionDeclarations = undefined;
328+
lexicalEnvironmentVariableDeclarations = undefined;
329+
lexicalEnvironmentFunctionDeclarations = undefined;
330+
}
331+
332+
/** Suspends the current lexical environment, usually after visiting a parameter list. */
333+
function suspendLexicalEnvironment(): void {
334+
Debug.assert(!lexicalEnvironmentDisabled, "Cannot suspend a lexical environment during the print phase.");
335+
Debug.assert(!lexicalEnvironmentSuspended, "Lexical environment is already suspended.");
336+
lexicalEnvironmentSuspended = true;
337+
}
338+
339+
/** Resumes a suspended lexical environment, usually before visiting a function body. */
340+
function resumeLexicalEnvironment(): void {
341+
Debug.assert(!lexicalEnvironmentDisabled, "Cannot resume a lexical environment during the print phase.");
342+
Debug.assert(lexicalEnvironmentSuspended, "Lexical environment is not suspended suspended.");
343+
lexicalEnvironmentSuspended = false;
325344
}
326345

327346
/**
@@ -330,17 +349,18 @@ namespace ts {
330349
*/
331350
function endLexicalEnvironment(): Statement[] {
332351
Debug.assert(!lexicalEnvironmentDisabled, "Cannot end a lexical environment during the print phase.");
352+
Debug.assert(!lexicalEnvironmentSuspended, "Lexical environment is suspended.");
333353

334354
let statements: Statement[];
335-
if (hoistedVariableDeclarations || hoistedFunctionDeclarations) {
336-
if (hoistedFunctionDeclarations) {
337-
statements = [...hoistedFunctionDeclarations];
355+
if (lexicalEnvironmentVariableDeclarations || lexicalEnvironmentFunctionDeclarations) {
356+
if (lexicalEnvironmentFunctionDeclarations) {
357+
statements = [...lexicalEnvironmentFunctionDeclarations];
338358
}
339359

340-
if (hoistedVariableDeclarations) {
360+
if (lexicalEnvironmentVariableDeclarations) {
341361
const statement = createVariableStatement(
342362
/*modifiers*/ undefined,
343-
createVariableDeclarationList(hoistedVariableDeclarations)
363+
createVariableDeclarationList(lexicalEnvironmentVariableDeclarations)
344364
);
345365

346366
if (!statements) {
@@ -354,8 +374,12 @@ namespace ts {
354374

355375
// Restore the previous lexical environment.
356376
lexicalEnvironmentStackOffset--;
357-
hoistedVariableDeclarations = lexicalEnvironmentVariableDeclarationsStack[lexicalEnvironmentStackOffset];
358-
hoistedFunctionDeclarations = lexicalEnvironmentFunctionDeclarationsStack[lexicalEnvironmentStackOffset];
377+
lexicalEnvironmentVariableDeclarations = lexicalEnvironmentVariableDeclarationsStack[lexicalEnvironmentStackOffset];
378+
lexicalEnvironmentFunctionDeclarations = lexicalEnvironmentFunctionDeclarationsStack[lexicalEnvironmentStackOffset];
379+
if (lexicalEnvironmentStackOffset === 0) {
380+
lexicalEnvironmentVariableDeclarationsStack = [];
381+
lexicalEnvironmentFunctionDeclarationsStack = [];
382+
}
359383
return statements;
360384
}
361385
}

0 commit comments

Comments
 (0)