Skip to content

Commit

Permalink
Keep indentation in @component documentation comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fnune committed Jul 6, 2020
1 parent 6c8c4b8 commit e6a983c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/svelte2tsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"svelte-shims.d.ts"
],
"dependencies": {
"dedent-js": "^1.0.1",
"pascal-case": "^3.1.1"
}
}
8 changes: 6 additions & 2 deletions packages/svelte2tsx/src/svelte2tsx.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dedent from 'dedent-js';
import { pascalCase } from 'pascal-case';
import MagicString from 'magic-string';
import path from 'path';
Expand Down Expand Up @@ -792,12 +793,15 @@ function processInstanceScriptContent(str: MagicString, script: Node): InstanceS

function formatComponentDocumentation(contents?: string | null) {
if (!contents) return '';

if (!contents.includes('\n')) {
return `/** ${contents} */\n`;
}

const lines = contents.split('\n').map(line => ` *${line ? ` ${line}` : ''}`).join('\n');
const lines = dedent(contents)
.split('\n')
.map(line => ` *${line ? ` ${line}` : ''}`)
.join('\n');

return `/**\n${lines}\n */\n`;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<></>;function render() {
<>

<main>At least I am documented</main></>
return { props: {}, slots: {} }}

/**
* This component has indented multiline documentation:
*
* ```typescript
* type Type = 'type'
* ```
*
* An indented list:
* - One item
* - Two items
*
* The output should be indented properly!
*/
export default class Input {
$$prop_def = __sveltets_partial(render().props)
$$slot_def = render().slots
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
@component
This component has indented multiline documentation:
```typescript
type Type = 'type'
```
An indented list:
- One item
- Two items
The output should be indented properly!
-->

<main>At least I am documented</main>
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,11 @@ decamelize@^1.2.0:
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
integrity sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=

dedent-js@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/dedent-js/-/dedent-js-1.0.1.tgz#bee5fb7c9e727d85dffa24590d10ec1ab1255305"
integrity sha1-vuX7fJ5yfYXf+iRZDRDsGrElUwU=

deep-is@~0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"
Expand Down

0 comments on commit e6a983c

Please sign in to comment.