Skip to content

Commit 213c123

Browse files
Merge pull request #140 from Microsoft/addComment
Add section about emit comment
2 parents 3c7967d + 2e3a109 commit 213c123

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

FAQ.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@
6868
- [Why is a file in the `exclude` list still picked up by the compiler?](#why-is-a-file-in-the-exclude-list-still-picked-up-by-the-compiler)
6969
- [How can I specify an `include`?](#how-can-i-specify-an-include)
7070
- [Why am I getting the `error TS5055: Cannot write file 'xxx.js' because it would overwrite input file.` when using JavaScript files?](#why-am-i-getting-the-error-ts5055-cannot-write-file-xxxjs-because-it-would-overwrite-input-file-when-using-javascript-files)
71+
- [Comments](#comments)
72+
- [Why some comments are not preserved in emitted JavaScript even when `--removeComments` is not specified?](#why-some-comments-are-not-preserved-in-emitted-javascript-even-when---removecomments-is-not-specified)
73+
- [Why Copyright comments are removed when `--removeComments` is true?](#why-copyright-comments-are-removed-when---removecomments-is-true)
7174
- [Glossary and Terms in this FAQ](#glossary-and-terms-in-this-faq)
7275
- [Dogs, Cats, and Animals, Oh My](#dogs-cats-and-animals-oh-my)
7376
- ["Substitutability"](#substitutability)
77+
- [Trailing, leading, and detached comments](#trailing-leading-and-detached-comments)
7478
- [GitHub Process Questions](#github-process-questions)
7579
- [What do the labels on these issues mean?](#what-do-the-labels-on-these-issues-mean)
7680
- [I disagree with the outcome of this suggestion](#i-disagree-with-the-outcome-of-this-suggestion)
@@ -1327,6 +1331,43 @@ If you don't want JavaScript files included in your project at all, simply set t
13271331
If you do want to include and compile these JavaScript files, set the `outDir` option or `outFile` option to direct the emitted files elsewhere, so they won't conflict with your source files;
13281332
If you just want to include the JavaScript files for editing and don't need to compile, set the `noEmit` compiler option to `true` to skip the emitting check.
13291333
1334+
## Comments
1335+
1336+
### Why some comments are not preserved in emitted JavaScript even when `--removeComments` is not specified?
1337+
1338+
TypeScript compiler uses a position of a node in the abstract syntax tree to retrieve its comments during emit.
1339+
Because, the compiler does not store all tokens into the tree, some comments may be missed in an output JavaScript file.
1340+
For example, we do not store following tokens into the tree `,`, `{`, `}`, `(`, `)`.
1341+
Therefore, trailing comments or leading comments of such token cannot be retrieved during emit.
1342+
At the moment, there is not an easy method to preserve such comments without storing those tokens.
1343+
Doing so, however, can significantly increase the tree size and potentially have performance impact.
1344+
1345+
Some cases where TypeScript compiler will not be able to preserve your comments:
1346+
1347+
```ts
1348+
/* comment */
1349+
<div>
1350+
{/* comment will not be emitted */}
1351+
</div>
1352+
1353+
var x = {
1354+
prop1: 1, // won't get emit because we can't retrieve this comment
1355+
prop2: 2 // will be emit
1356+
}
1357+
1358+
function foo() /* this comment can't be preserved */ { }
1359+
```
1360+
1361+
1362+
### Why Copyright comments are removed when `--removeComments` is true?
1363+
1364+
TypeScript compiler will preserve copyright comment regardless of `--removeComments`.
1365+
For a comment to be considered a copyright comment, it must have following characteristics:
1366+
1367+
- a top-of-file comment following by empty line, separating it from the first statement.
1368+
- begin with `/*!`
1369+
1370+
13301371
-------------------------------------------------------------------------------------
13311372
13321373
# Glossary and Terms in this FAQ
@@ -1361,6 +1402,23 @@ We also commonly say that `X` is *assignable to* `Y` (these terms have slightly
13611402
13621403
In other words, if I ask for a `fork`, a `spork` is an acceptable *substitute* because it has the same functions and properties of a `fork` (three prongs and a handle).
13631404
1405+
### Trailing, leading, and detached comments
1406+
TypeScript classifies comments into three different types:
1407+
1408+
- Leading comment : a comment before a node followed by newline.
1409+
- Trailing comment : a comment after a node and in the same line as the node.
1410+
- Detached comment : a comment that is not part of any node such as copyright comment.
1411+
```ts
1412+
/*! Top-of-file copyright comment is a detached comment*/
1413+
1414+
/* Leading comments of the function AST node*/
1415+
function foo /*trailing comments of the function name, "foo", AST node*/ () {
1416+
/* Detached comment*/
1417+
1418+
let x = 10;
1419+
}
1420+
```
1421+
13641422
----------------------------------------------------------------------------------------
13651423
13661424
# GitHub Process Questions

0 commit comments

Comments
 (0)