Skip to content

Commit 838b9b6

Browse files
Provide an experimental flag that allows us to emit declarations except for nodes marked with '@internal'.
1 parent 0ca0304 commit 838b9b6

File tree

10 files changed

+84
-5
lines changed

10 files changed

+84
-5
lines changed

src/compiler/commandLineParser.ts

+6
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ module ts {
134134
type: "boolean",
135135
description: Diagnostics.Suppress_noImplicitAny_errors_for_indexing_objects_lacking_index_signatures,
136136
},
137+
{
138+
name: "stripInternal",
139+
type: "boolean",
140+
description: Diagnostics.Do_not_emit_declarations_for_code_that_has_an_internal_annotation,
141+
experimental: true
142+
},
137143
{
138144
name: "target",
139145
shortName: "t",

src/compiler/diagnosticInformationMap.generated.ts

+1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ module ts {
433433
File_0_not_found: { code: 6053, category: DiagnosticCategory.Error, key: "File '{0}' not found." },
434434
File_0_must_have_extension_ts_or_d_ts: { code: 6054, category: DiagnosticCategory.Error, key: "File '{0}' must have extension '.ts' or '.d.ts'." },
435435
Suppress_noImplicitAny_errors_for_indexing_objects_lacking_index_signatures: { code: 6055, category: DiagnosticCategory.Message, key: "Suppress noImplicitAny errors for indexing objects lacking index signatures." },
436+
Do_not_emit_declarations_for_code_that_has_an_internal_annotation: { code: 6056, category: DiagnosticCategory.Message, key: "Do not emit declarations for code that has an '@internal' annotation." },
436437
Variable_0_implicitly_has_an_1_type: { code: 7005, category: DiagnosticCategory.Error, key: "Variable '{0}' implicitly has an '{1}' type." },
437438
Parameter_0_implicitly_has_an_1_type: { code: 7006, category: DiagnosticCategory.Error, key: "Parameter '{0}' implicitly has an '{1}' type." },
438439
Member_0_implicitly_has_an_1_type: { code: 7008, category: DiagnosticCategory.Error, key: "Member '{0}' implicitly has an '{1}' type." },

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,10 @@
17251725
"category": "Message",
17261726
"code": 6055
17271727
},
1728+
"Do not emit declarations for code that has an '@internal' annotation.": {
1729+
"category": "Message",
1730+
"code": 6056
1731+
},
17281732

17291733
"Variable '{0}' implicitly has an '{1}' type.": {
17301734
"category": "Error",

src/compiler/emitter.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ module ts {
355355
var reportedDeclarationError = false;
356356

357357
var emitJsDocComments = compilerOptions.removeComments ? function (declaration: Node) { } : writeJsDocComments;
358+
var emit = compilerOptions.stripInternal ? stripInternal : emitNode;
358359

359360
var aliasDeclarationEmitInfo: AliasDeclarationEmitInfo[] = [];
360361

@@ -383,7 +384,7 @@ module ts {
383384
});
384385
}
385386

386-
emitNode(root);
387+
emitSourceFile(root);
387388
}
388389
else {
389390
// Emit references corresponding to this file
@@ -405,7 +406,7 @@ module ts {
405406
});
406407
}
407408

408-
emitNode(sourceFile);
409+
emitSourceFile(sourceFile);
409410
}
410411
});
411412
}
@@ -417,6 +418,23 @@ module ts {
417418
referencePathsOutput,
418419
}
419420

421+
function hasInternalAnnotation(range: CommentRange) {
422+
var text = currentSourceFile.text;
423+
var comment = text.substring(range.pos, range.end);
424+
return comment.indexOf("@internal") >= 0;
425+
}
426+
427+
function stripInternal(node: Node) {
428+
if (node) {
429+
var leadingCommentRanges = getLeadingCommentRanges(currentSourceFile.text, node.pos);
430+
if (forEach(leadingCommentRanges, hasInternalAnnotation)) {
431+
return;
432+
}
433+
434+
emitNode(node);
435+
}
436+
}
437+
420438
function createAndSetNewTextWriterWithSymbolWriter(): EmitTextWriterWithSymbolWriter {
421439
var writer = <EmitTextWriterWithSymbolWriter>createTextWriter(newLine);
422440
writer.trackSymbol = trackSymbol;
@@ -522,7 +540,7 @@ module ts {
522540

523541
function emitLines(nodes: Node[]) {
524542
for (var i = 0, n = nodes.length; i < n; i++) {
525-
emitNode(nodes[i]);
543+
emit(nodes[i]);
526544
}
527545
}
528546

src/compiler/tsc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ module ts {
413413
output += getDiagnosticText(Diagnostics.Options_Colon) + sys.newLine;
414414

415415
// Sort our options by their names, (e.g. "--noImplicitAny" comes before "--watch")
416-
var optsList = optionDeclarations.slice();
416+
var optsList = filter(optionDeclarations.slice(), v => !v.experimental);
417417
optsList.sort((a, b) => compareValues<string>(a.name.toLowerCase(), b.name.toLowerCase()));
418418

419419
// We want our descriptions to align at the same column in our output,

src/compiler/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,7 @@ module ts {
14761476
target?: ScriptTarget;
14771477
version?: boolean;
14781478
watch?: boolean;
1479+
stripInternal?: boolean;
14791480
[option: string]: string | number | boolean;
14801481
}
14811482

@@ -1514,6 +1515,7 @@ module ts {
15141515
description?: DiagnosticMessage; // The message describing what the command line switch does
15151516
paramType?: DiagnosticMessage; // The name to be used for a non-boolean option's parameter
15161517
error?: DiagnosticMessage; // The error given when the argument does not fit a customized 'type'
1518+
experimental?: boolean;
15171519
}
15181520

15191521
export const enum CharacterCodes {

src/harness/harness.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,9 @@ module Harness {
10151015
options.removeComments = setting.value === 'false';
10161016
break;
10171017

1018+
case 'stripinternal':
1019+
options.stripInternal = !!setting.value;
1020+
10181021
case 'usecasesensitivefilenames':
10191022
useCaseSensitiveFileNames = setting.value === 'true';
10201023
break;
@@ -1454,7 +1457,7 @@ module Harness {
14541457
var optionRegex = /^[\/]{2}\s*@(\w+)\s*:\s*(\S*)/gm; // multiple matches on multiple lines
14551458

14561459
// List of allowed metadata names
1457-
var fileMetadataNames = ["filename", "comments", "declaration", "module", "nolib", "sourcemap", "target", "out", "outdir", "noemitonerror", "noimplicitany", "noresolve", "newline", "newlines", "emitbom", "errortruncation", "usecasesensitivefilenames", "preserveconstenums", "includebuiltfile", "suppressimplicitanyindexerrors"];
1460+
var fileMetadataNames = ["filename", "comments", "declaration", "module", "nolib", "sourcemap", "target", "out", "outdir", "noemitonerror", "noimplicitany", "noresolve", "newline", "newlines", "emitbom", "errortruncation", "usecasesensitivefilenames", "preserveconstenums", "includebuiltfile", "suppressimplicitanyindexerrors", "stripinternal"];
14581461

14591462
function extractCompilerSettings(content: string): CompilerSetting[] {
14601463

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [stripInternal1.ts]
2+
3+
class C {
4+
foo(): void { }
5+
// @internal
6+
bar(): void { }
7+
}
8+
9+
//// [stripInternal1.js]
10+
var C = (function () {
11+
function C() {
12+
}
13+
C.prototype.foo = function () {
14+
};
15+
// @internal
16+
C.prototype.bar = function () {
17+
};
18+
return C;
19+
})();
20+
21+
22+
//// [stripInternal1.d.ts]
23+
declare class C {
24+
foo(): void;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/stripInternal1.ts ===
2+
3+
class C {
4+
>C : C
5+
6+
foo(): void { }
7+
>foo : () => void
8+
9+
// @internal
10+
bar(): void { }
11+
>bar : () => void
12+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @declaration:true
2+
// @stripInternal:true
3+
4+
class C {
5+
foo(): void { }
6+
// @internal
7+
bar(): void { }
8+
}

0 commit comments

Comments
 (0)