Skip to content

Commit db2e23c

Browse files
Addressed code review.
Signed-off-by: Titian Cernicova-Dragomir <[email protected]>
1 parent 97161e0 commit db2e23c

File tree

6 files changed

+57
-64
lines changed

6 files changed

+57
-64
lines changed

src/compiler/transformer.ts

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import {
44
Bundle,
55
chainBundle,
66
CompilerOptions,
7-
CoreEmitResolver,
87
createEmitHelperFactory,
98
CustomTransformer,
109
CustomTransformerFactory,
1110
CustomTransformers,
1211
Debug,
13-
Diagnostic,
1412
DiagnosticWithLocation,
1513
disposeEmitNodes,
1614
EmitFlags,
@@ -32,7 +30,6 @@ import {
3230
getUseDefineForClassFields,
3331
Identifier,
3432
isBundle,
35-
IsolatedTransformationContext,
3633
isSourceFile,
3734
LexicalEnvironmentFlags,
3835
map,
@@ -42,7 +39,6 @@ import {
4239
NodeFactory,
4340
NodeFlags,
4441
noop,
45-
notImplemented,
4642
NullTransformationContext,
4743
returnUndefined,
4844
ScriptTarget,
@@ -668,51 +664,22 @@ export function transformNodes<T extends Node>(resolver: EmitResolver | undefine
668664
}
669665
}
670666
}
667+
671668
/** @internal */
672-
export function createTransformationContext(kind: TransformationContextKind.NullContext): NullTransformationContext;
673-
/** @internal */
674-
export function createTransformationContext(
675-
kind: TransformationContextKind.IsolatedContext,
676-
options: CompilerOptions,
677-
diagnostics: Diagnostic[],
678-
resolver: CoreEmitResolver,
679-
): IsolatedTransformationContext;
680-
export function createTransformationContext(
681-
kind: TransformationContextKind.IsolatedContext | TransformationContextKind.NullContext,
682-
options: CompilerOptions = {},
683-
diagnostics?: Diagnostic[],
684-
resolver?: EmitResolver | CoreEmitResolver,
685-
host?: EmitHost,
686-
): NullTransformationContext | IsolatedTransformationContext | TransformationContext {
687-
return {
688-
kind,
689-
factory: factory, // eslint-disable-line object-shorthand
690-
getCompilerOptions: () => options,
691-
getEmitResolver: !resolver ? notImplemented : () => resolver,
692-
getEmitHost: !host ? notImplemented : () => host,
693-
getEmitHelperFactory: notImplemented,
694-
startLexicalEnvironment: noop,
695-
resumeLexicalEnvironment: noop,
696-
suspendLexicalEnvironment: noop,
697-
endLexicalEnvironment: returnUndefined,
698-
setLexicalEnvironmentFlags: noop,
699-
getLexicalEnvironmentFlags: () => 0,
700-
hoistVariableDeclaration: noop,
701-
hoistFunctionDeclaration: noop,
702-
addInitializationStatement: noop,
703-
startBlockScope: noop,
704-
endBlockScope: returnUndefined,
705-
addBlockScopedVariable: noop,
706-
requestEmitHelper: noop,
707-
readEmitHelpers: notImplemented,
708-
enableSubstitution: noop,
709-
enableEmitNotification: noop,
710-
isSubstitutionEnabled: notImplemented,
711-
isEmitNotificationEnabled: notImplemented,
712-
onSubstituteNode: noEmitSubstitution,
713-
onEmitNode: noEmitNotification,
714-
addDiagnostic: !diagnostics ? noop : (diag: Diagnostic) => diagnostics.push(diag),
715-
};
716-
}
717-
/** @internal */
718-
export const nullTransformationContext: NullTransformationContext = createTransformationContext(TransformationContextKind.NullContext);
669+
export const nullTransformationContext: NullTransformationContext = {
670+
kind: TransformationContextKind.NullContext,
671+
factory: factory, // eslint-disable-line object-shorthand
672+
getCompilerOptions: () => ({}),
673+
startLexicalEnvironment: noop,
674+
resumeLexicalEnvironment: noop,
675+
suspendLexicalEnvironment: noop,
676+
endLexicalEnvironment: returnUndefined,
677+
setLexicalEnvironmentFlags: noop,
678+
getLexicalEnvironmentFlags: () => 0,
679+
hoistVariableDeclaration: noop,
680+
hoistFunctionDeclaration: noop,
681+
addInitializationStatement: noop,
682+
startBlockScope: noop,
683+
endBlockScope: returnUndefined,
684+
addBlockScopedVariable: noop,
685+
};

src/compiler/transformers/declarations/transpileDeclaration.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
createPrinter,
88
createSourceMapGenerator,
99
createTextWriter,
10-
createTransformationContext,
1110
Debug,
1211
Diagnostic,
1312
EmitHost,
@@ -20,8 +19,10 @@ import {
2019
getRelativePathToDirectoryOrUrl,
2120
getRootLength,
2221
getSourceFilePathInNewDir,
22+
IsolatedTransformationContext,
2323
normalizePath,
2424
normalizeSlashes,
25+
nullTransformationContext,
2526
PrinterOptions,
2627
SourceFile,
2728
SourceMapGenerator,
@@ -47,7 +48,13 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
4748
};
4849
const emitResolver = createEmitDeclarationResolver(sourceFile);
4950
const diagnostics: Diagnostic[] = [];
50-
const transformationContext = createTransformationContext(TransformationContextKind.IsolatedContext, compilerOptions, diagnostics, emitResolver);
51+
const transformationContext: IsolatedTransformationContext = {
52+
...nullTransformationContext,
53+
kind: TransformationContextKind.IsolatedContext,
54+
getCompilerOptions: () => compilerOptions,
55+
addDiagnostic: diag => diagnostics.push(diag),
56+
getEmitResolver: () => emitResolver,
57+
};
5158
const transformer = transformDeclarations(transformationContext);
5259
const result = transformer(sourceFile);
5360

tests/baselines/reference/api/typescript.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8436,6 +8436,11 @@ declare namespace ts {
84368436
*/
84378437
replacePropertyName<T extends AccessorDeclaration | MethodDeclaration | MethodSignature | PropertyDeclaration | PropertySignature | PropertyAssignment>(node: T, name: T["name"]): T;
84388438
}
8439+
enum TransformationContextKind {
8440+
FullContext = 0,
8441+
IsolatedContext = 1,
8442+
NullContext = 2,
8443+
}
84398444
interface CoreTransformationContext {
84408445
readonly factory: NodeFactory;
84418446
/** Gets the compiler options supplied to the transformer. */
@@ -8454,6 +8459,7 @@ declare namespace ts {
84548459
hoistVariableDeclaration(node: Identifier): void;
84558460
}
84568461
interface TransformationContext extends CoreTransformationContext {
8462+
kind: TransformationContextKind.FullContext;
84578463
/** Records a request for a non-scoped emit helper in the current context. */
84588464
requestEmitHelper(helper: EmitHelper): void;
84598465
/** Gets and resets the requested non-scoped emit helpers. */

tests/baselines/reference/isolated-declarations/auto-fixed/diff/symbolDeclarationEmit12.d.ts.diff

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,30 @@
55
===================================================================
66
--- TSC declarations
77
+++ DTE declarations
8-
@@ -5,8 +5,9 @@
8+
@@ -5,33 +5,40 @@
99
interface I {
1010
}
1111
export class C {
1212
[Symbol.iterator]: I;
1313
+ [Symbol.toPrimitive](x: I): invalid;
1414
[Symbol.isConcatSpreadable](): I;
15-
get [Symbol.toPrimitive](): I;
15+
- get [Symbol.toPrimitive](): I;
16+
+ get [Symbol.toPrimitive](): invalid;
1617
set [Symbol.toPrimitive](x: I);
1718
}
18-
@@ -14,18 +15,21 @@
19+
export {};
1920
}
2021
//# sourceMappingURL=symbolDeclarationEmit12.d.ts.map
2122
/// [Errors] ////
2223

2324
+symbolDeclarationEmit12.ts(5,9): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
2425
symbolDeclarationEmit12.ts(9,13): error TS2300: Duplicate identifier '[Symbol.toPrimitive]'.
26+
+symbolDeclarationEmit12.ts(9,13): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
2527
symbolDeclarationEmit12.ts(10,13): error TS2300: Duplicate identifier '[Symbol.toPrimitive]'.
2628

2729

2830
-==== symbolDeclarationEmit12.ts (2 errors) ====
29-
+==== symbolDeclarationEmit12.ts (3 errors) ====
31+
+==== symbolDeclarationEmit12.ts (4 errors) ====
3032
module M {
3133
interface I { }
3234
export class C {
@@ -37,4 +39,12 @@
3739
[Symbol.isConcatSpreadable](): I {
3840
return undefined
3941
}
40-
get [Symbol.toPrimitive](): I { return undefined; }
42+
get [Symbol.toPrimitive]() { return undefined; }
43+
~~~~~~~~~~~~~~~~~~~~
44+
!!! error TS2300: Duplicate identifier '[Symbol.toPrimitive]'.
45+
+ ~~~~~~~~~~~~~~~~~~~~
46+
+!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
47+
set [Symbol.toPrimitive](x: I) { }
48+
~~~~~~~~~~~~~~~~~~~~
49+
!!! error TS2300: Duplicate identifier '[Symbol.toPrimitive]'.
50+
}

tests/baselines/reference/isolated-declarations/auto-fixed/dte/symbolDeclarationEmit12.d.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module M {
99
[Symbol.isConcatSpreadable](): I {
1010
return undefined
1111
}
12-
get [Symbol.toPrimitive](): I { return undefined; }
12+
get [Symbol.toPrimitive]() { return undefined; }
1313
set [Symbol.toPrimitive](x: I) { }
1414
}
1515
}
@@ -26,7 +26,7 @@ declare namespace M {
2626
[Symbol.iterator]: I;
2727
[Symbol.toPrimitive](x: I): invalid;
2828
[Symbol.isConcatSpreadable](): I;
29-
get [Symbol.toPrimitive](): I;
29+
get [Symbol.toPrimitive](): invalid;
3030
set [Symbol.toPrimitive](x: I);
3131
}
3232
export {};
@@ -36,10 +36,11 @@ declare namespace M {
3636

3737
symbolDeclarationEmit12.ts(5,9): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
3838
symbolDeclarationEmit12.ts(9,13): error TS2300: Duplicate identifier '[Symbol.toPrimitive]'.
39+
symbolDeclarationEmit12.ts(9,13): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
3940
symbolDeclarationEmit12.ts(10,13): error TS2300: Duplicate identifier '[Symbol.toPrimitive]'.
4041

4142

42-
==== symbolDeclarationEmit12.ts (3 errors) ====
43+
==== symbolDeclarationEmit12.ts (4 errors) ====
4344
module M {
4445
interface I { }
4546
export class C {
@@ -50,9 +51,11 @@ symbolDeclarationEmit12.ts(10,13): error TS2300: Duplicate identifier '[Symbol.t
5051
[Symbol.isConcatSpreadable](): I {
5152
return undefined
5253
}
53-
get [Symbol.toPrimitive](): I { return undefined; }
54+
get [Symbol.toPrimitive]() { return undefined; }
5455
~~~~~~~~~~~~~~~~~~~~
5556
!!! error TS2300: Duplicate identifier '[Symbol.toPrimitive]'.
57+
~~~~~~~~~~~~~~~~~~~~
58+
!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit.
5659
set [Symbol.toPrimitive](x: I) { }
5760
~~~~~~~~~~~~~~~~~~~~
5861
!!! error TS2300: Duplicate identifier '[Symbol.toPrimitive]'.

tests/baselines/reference/isolated-declarations/auto-fixed/tsc/symbolDeclarationEmit12.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module M {
99
[Symbol.isConcatSpreadable](): I {
1010
return undefined
1111
}
12-
get [Symbol.toPrimitive](): I { return undefined; }
12+
get [Symbol.toPrimitive]() { return undefined; }
1313
set [Symbol.toPrimitive](x: I) { }
1414
}
1515
}
@@ -46,7 +46,7 @@ symbolDeclarationEmit12.ts(10,13): error TS2300: Duplicate identifier '[Symbol.t
4646
[Symbol.isConcatSpreadable](): I {
4747
return undefined
4848
}
49-
get [Symbol.toPrimitive](): I { return undefined; }
49+
get [Symbol.toPrimitive]() { return undefined; }
5050
~~~~~~~~~~~~~~~~~~~~
5151
!!! error TS2300: Duplicate identifier '[Symbol.toPrimitive]'.
5252
set [Symbol.toPrimitive](x: I) { }

0 commit comments

Comments
 (0)