Skip to content

Commit 3e12572

Browse files
authored
feat: format command (#41)
* feat: format command * updates * add playground * add prettier config option * updates * update api docs
1 parent 3332f6b commit 3e12572

26 files changed

+1001
-136
lines changed

api.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55
- [Function](#function)
66
- [annotate](#annotate)
77
- [compile](#compile)
8+
- [format](#format)
89
- [TypeAlias](#typealias)
910
- [AnnotateOptions](#annotateoptions)
11+
- [FormatOptions](#formatoptions)
1012
- [Enum](#enum)
1113
- [AnnotateWarningCodes](#annotatewarningcodes)
1214
- [CompileErrorCodes](#compileerrorcodes)
1315
- [Interface](#interface)
1416
- [CompileOptions](#compileoptions)
1517
- [SFCParseError](#sfcparseerror)
18+
- [Variable](#variable)
19+
- [DEFAULT_PRETTIER_OPTIONS](#default_prettier_options)
1620
- [Class](#class)
21+
- [FormatLangNotFoundError](#formatlangnotfounderror)
1722
- [SFCAnnotateError](#sfcannotateerror)
1823

1924
## Function
@@ -64,6 +69,31 @@ declare function compile(source: string, output: string, options?: CompileOption
6469

6570
This functoin is **asyncronous** function. If you want to get about error details, use the handler of [CompileOptions](#compileoptions) and [CompileErrorCodes](#compileerrorcodes)
6671

72+
### format
73+
74+
Format the Vue SFC block
75+
76+
**Signature:**
77+
```typescript
78+
declare function format(source: string, filepath: string, options?: FormatOptions): string;
79+
```
80+
81+
#### Parameters
82+
83+
| Parameter | Type | Description |
84+
| --- | --- | --- |
85+
| source | string | The source code of the Vue SFC |
86+
| filepath | string | The file path of the Vue SFC |
87+
| options | FormatOptions | The [options](#formatoptions) of the format function |
88+
89+
#### Returns
90+
91+
The formatted source code of the Vue SFC
92+
93+
#### Remarks
94+
95+
Currently, only i18n custom blocks supporting
96+
6797

6898
## TypeAlias
6999

@@ -81,6 +111,17 @@ declare type AnnotateOptions = {
81111
};
82112
```
83113

114+
### FormatOptions
115+
116+
Format options of [format](#format) function
117+
118+
**Signature:**
119+
```typescript
120+
declare type FormatOptions = {
121+
prettier?: Options;
122+
};
123+
```
124+
84125

85126
## Enum
86127

@@ -214,8 +255,68 @@ filepath: string;
214255

215256

216257

258+
## Variable
259+
260+
### DEFAULT_PRETTIER_OPTIONS
261+
262+
The default prettier options for formatting the content of `i18n` custom blocks
263+
264+
**Signature:**
265+
```typescript
266+
DEFAULT_PRETTIER_OPTIONS: {
267+
printWidth: number;
268+
tabWidth: number;
269+
jsonRecursiveSort: boolean;
270+
plugins: string[];
271+
}
272+
```
273+
274+
217275
## Class
218276

277+
### FormatLangNotFoundError
278+
279+
Fortmat lang fwnot found error
280+
281+
**Signature:**
282+
```typescript
283+
declare class FormatLangNotFoundError extends Error
284+
```
285+
286+
### Remarks
287+
288+
The error that not specified `lang` attribute in `i18n` custom block
289+
290+
291+
#### Constructor
292+
293+
Constructor
294+
295+
**Signature:**
296+
```typescript
297+
constructor(message: string, filepath: string);
298+
```
299+
300+
*Parameters*
301+
302+
| Parameter | Type | Description |
303+
| --- | --- | --- |
304+
| message | string | The error message |
305+
| filepath | string | The filepath of the target file at formatting processing |
306+
307+
308+
#### Properties
309+
310+
##### filepath
311+
312+
The filepath of the target file at formatting processing
313+
314+
**Signature:**
315+
```typescript
316+
filepath: string;
317+
```
318+
319+
219320
### SFCAnnotateError
220321
221322
The Annocation error

locales/en.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"Usage: $0 <command> [options]": "Usage: $0 <command> [options]",
3+
"dry run mode": "dry run mode",
34
"compile the i18n resources": "compile the i18n resources",
45
"the i18n resource source path": "the i18n resource source path",
56
"the compiled i18n resource output path": "the compiled i18n resource output path",
@@ -16,7 +17,6 @@
1617
"the attributes to annotate": "the attributes to annotate",
1718
"'--type' is not supported except for 'custom-block'": "'--type' is not supported except for 'custom-block'",
1819
"if you don't specify some files at the end of the command, the '—-source' option is required": "if you don't specify some files at the end of the command, the '—-source' option is required",
19-
"there is a parse error in {filename}": "there is a parse error in {filename}",
2020
"Unsupported '{type}' block content type: {actual}": "Unsupported '{type}' block content type: {actual}",
2121
"Detected lang mismatch in block `src` ('{src}') and block content ('{content}')": "Detected lang mismatch in block `src` ('{src}') and block content ('{content}')",
2222
"Detected lang mismatch in `lang` option ('{lang}') and block content ('{content}')": "Detected lang mismatch in `lang` option ('{lang}') and block content ('{content}')",
@@ -27,5 +27,16 @@
2727
"{count} warnings": "{count} warnings",
2828
"{count} forces": "{count} forces",
2929
"{count} ignores": "{count} ignores",
30-
"{count} errors": "{count} errors"
30+
"{count} errors": "{count} errors",
31+
"`lang` attr not found": "`lang` attr not found",
32+
"format for single-file components": "format for single-file components",
33+
"the format type": "the format type",
34+
"the config file path of prettier": "the config file path of prettier",
35+
"target files without formatting": "target files without formatting",
36+
"formatted": "formatted",
37+
"no change": "no change",
38+
"{count} formattable files": "{count} formattable files",
39+
"{count} formatted files": "{count} formatted files",
40+
"{count} no change files": "{count} no change files",
41+
"{count} error files": "{count} error files"
3142
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232
"@intlify/shared": "^9.1.9",
3333
"@vue/compiler-sfc": "^3.2.20",
3434
"chalk": "^4.1.0",
35+
"cosmiconfig": "^7.0.1",
3536
"debug": "^4.3.2",
3637
"diff": "^5.0.0",
3738
"fast-glob": "^3.2.7",
3839
"jsonc-eslint-parser": "^1.4.1",
3940
"pathe": "^0.2.0",
41+
"prettier": "^2.4.1",
42+
"prettier-plugin-sort-json": "^0.0.2",
4043
"yaml-eslint-parser": "^0.4.1",
4144
"yargs": "^17.2.0"
4245
},
@@ -52,6 +55,7 @@
5255
"@types/glob": "^7.1.3",
5356
"@types/mocha": "^9.0.0",
5457
"@types/node": "^16.10.3",
58+
"@types/prettier": "^2.4.1",
5559
"@types/sinon": "^10.0.4",
5660
"@types/yargs": "^17.0.3",
5761
"@typescript-eslint/eslint-plugin": "^5.0.0",
@@ -67,7 +71,6 @@
6771
"npm-run-all": "^4.1.5",
6872
"nyc": "^15.1.0",
6973
"opener": "^1.5.1",
70-
"prettier": "^2.3.2",
7174
"secretlint": "^3.3.0",
7275
"shipjs": "^0.24.0",
7376
"sinon": "^11.1.2",

playground/format/BasicFormat.vue

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<template>
2+
<p>This is BasicFormat</p>
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'BasicFormat'
8+
}
9+
</script>
10+
11+
<style scoped>
12+
#app {
13+
color: red;
14+
}
15+
</style>
16+
17+
<i18n lang="json">
18+
{
19+
"en": {
20+
"title": "The application"
21+
},
22+
"ja": {
23+
"title": "アプリケーション"
24+
}
25+
}
26+
</i18n>

playground/format/HasNested.vue

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<template>
2+
<p>This is Nested resources</p>
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'App'
8+
}
9+
</script>
10+
11+
<i18n lang="json" global>
12+
{
13+
"en": {
14+
"title": "The application",
15+
"menu": {
16+
"sub2": "this is sub2 placeholder",
17+
"sub1": {
18+
"menu1": "menu 1",
19+
"menu2": {
20+
"menu2": "this is sub menu2",
21+
"menu1": "this is sub menu1",
22+
"1menu": "this is sub 1menu"
23+
}
24+
},
25+
"sub3": "this is sub3 placeholder",
26+
"sub4": {
27+
"menu10": "menu 10",
28+
"menu1": {
29+
"menu1": "this is sub menu1",
30+
"menu2": "this is sub menu2"
31+
},
32+
"0menu": "this is sub 0menu"
33+
}
34+
},
35+
"footer": "this is footer placeholder"
36+
}
37+
}
38+
</i18n>
39+
40+
<style scoped>
41+
#app {
42+
color: red;
43+
}
44+
</style>

playground/format/MultiFormat.vue

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<template>
2+
<p>This is MultiFormat</p>
3+
</template>
4+
5+
<i18n lang="json">
6+
{
7+
"en": {
8+
"title": "The application"
9+
},
10+
"ja": {
11+
"title": "アプリケーション"
12+
}
13+
}
14+
</i18n>
15+
16+
<script>
17+
export default {
18+
name: 'MultiFormat'
19+
}
20+
</script>
21+
22+
<i18n lang="yaml">
23+
en:
24+
login: "login"
25+
ja:
26+
login: "ログイン"
27+
</i18n>
28+
29+
<style scoped>
30+
#app {
31+
color: red;
32+
}
33+
</style>
34+
35+
<i18n lang="json5">{en:
36+
{ login: "login" },
37+
ja: {
38+
login: "ログイン" }}
39+
</i18n>

playground/format/Oneline.vue

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<p>This is Oneline</p>
3+
</template>
4+
5+
<script>
6+
export default { name: 'App' }
7+
</script>
8+
9+
<style scoped>
10+
#app { color: red; }
11+
</style>
12+
13+
<i18n lang="json">{ "en": { "title": "The application" } }</i18n>

0 commit comments

Comments
 (0)