|
68 | 68 | - [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)
|
69 | 69 | - [How can I specify an `include`?](#how-can-i-specify-an-include)
|
70 | 70 | - [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) |
71 | 74 | - [Glossary and Terms in this FAQ](#glossary-and-terms-in-this-faq)
|
72 | 75 | - [Dogs, Cats, and Animals, Oh My](#dogs-cats-and-animals-oh-my)
|
73 | 76 | - ["Substitutability"](#substitutability)
|
| 77 | + - [Trailing, leading, and detached comments](#trailing-leading-and-detached-comments) |
74 | 78 | - [GitHub Process Questions](#github-process-questions)
|
75 | 79 | - [What do the labels on these issues mean?](#what-do-the-labels-on-these-issues-mean)
|
76 | 80 | - [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
|
1327 | 1331 | 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;
|
1328 | 1332 | 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.
|
1329 | 1333 |
|
| 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 | +
|
1330 | 1371 | -------------------------------------------------------------------------------------
|
1331 | 1372 |
|
1332 | 1373 | # Glossary and Terms in this FAQ
|
@@ -1361,6 +1402,23 @@ We also commonly say that `X` is *assignable to* `Y` (these terms have slightly
|
1361 | 1402 |
|
1362 | 1403 | 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).
|
1363 | 1404 |
|
| 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 | +
|
1364 | 1422 | ----------------------------------------------------------------------------------------
|
1365 | 1423 |
|
1366 | 1424 | # GitHub Process Questions
|
|
0 commit comments