Skip to content

Commit 1110fa7

Browse files
feat(format-query-block): add option overridePrettierOption (#104)
* refactor(utils): add type PrettierSupportOptionName close #60 * refactor: add PrettierOptions * refactor: refactor type * feat: support vueIndentScriptAndStyle * chore: rename get-code-wrap-indent-info * test: update test get-code-wrap-indent-info * feat(format-query-block): add option overridePrettierOption * chore: change eslint settings
1 parent a5d1ff2 commit 1110fa7

File tree

32 files changed

+545
-380
lines changed

32 files changed

+545
-380
lines changed

.eslintrc.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22
module.exports = {
33
parserOptions: {
44
ecmaVersion: 2017,
5-
project: "tsconfig.json",
65
sourceType: "module",
76
},
7+
parser: "@typescript-eslint/parser",
8+
env: {
9+
node: true,
10+
},
811

9-
extends: ["plugin:@mysticatea/es2017", "plugin:@mysticatea/+eslint-plugin"],
12+
extends: [
13+
"eslint:recommended",
14+
"plugin:@typescript-eslint/recommended",
15+
"prettier",
16+
],
17+
plugins: ["prettier"],
1018
rules: {
11-
"@mysticatea/prettier": [
12-
"error",
13-
{
14-
tabWidth: 2,
15-
},
16-
],
19+
"prettier/prettier": "error",
1720
"func-style": ["error", "expression"],
21+
"@typescript-eslint/explicit-module-boundary-types": "off",
22+
"@typescript-eslint/ban-types": "off",
23+
"@typescript-eslint/no-explicit-any": "off",
1824
},
1925
};

docs/rules/format-query-block.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description: Enforce consistent format style in query block ex) `<page-query>`,`
1010

1111
- This rule checks the consistency of a code in `<page-query>` and `<static-query>` tags.
1212
- This rule's formatter is [Prettier](https://prettier.io). [Parser is `graphql`](https://prettier.io/docs/en/options.html#parser)
13-
- If you use `.prettierrc`, this rule is check that file. Consider option `tabWidth` and `useTabs`.
13+
- If you use `.prettierrc`, this rule follow Prettier's option at `.prettierrc`. This rule checks the option `useTabs`, `tabWidth`, and `vueIndentScriptAndStyle`.
1414

1515
## :book: Rule Details
1616

@@ -44,4 +44,21 @@ description: Enforce consistent format style in query block ex) `<page-query>`,`
4444

4545
## :wrench: Options
4646

47-
Nothing.
47+
```json
48+
{
49+
"gridsome/format-query-block": [
50+
"warn",
51+
{
52+
"overridePrettierOption": {
53+
"tabWidth": 4,
54+
"vueIndentScriptAndStyle": true
55+
}
56+
}
57+
]
58+
}
59+
```
60+
61+
- `overridePrettierOption` (`{ tabWidth: number; useTabs: boolean; vueIndentScriptAndStyle: boolean; }`) ... This option can override Prettier's option. Default is `{}`
62+
- [tabWidth](https://prettier.io/docs/en/options.html#tab-width)
63+
- [useTabs](https://prettier.io/docs/en/options.html#tabs)
64+
- [vueIndentScriptAndStyle](https://prettier.io/docs/en/options.html#vue-files-script-and-style-tags-indentation)

lib/rules/format-query-block.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ import {
1010
getPrettierDefaultOption,
1111
getPrettierRcOption,
1212
getMergedPrettierOption,
13-
getIndentInfo,
13+
getCodeWrapIndentInfo,
1414
NodeNames,
15+
OverridePrettierOption,
1516
} from "../utils";
1617

1718
import prettier from "prettier";
1819

1920
const prettierParser = "graphql";
2021
const LINES = /[^\r\n\u2028\u2029]+(?:$|\r\n|[\r\n\u2028\u2029])|\s/gu;
2122

22-
type Options = {};
23+
type Options = {
24+
overridePrettierOption?: OverridePrettierOption;
25+
};
2326

2427
const defaultOptions: [Options] = [{}];
2528

@@ -38,10 +41,26 @@ export = createRule<[Options], MessageIds>({
3841
messages: {
3942
formatQueryBlock: "{{ name }} code format is incorrect",
4043
},
41-
schema: [],
44+
schema: [
45+
{
46+
type: "object",
47+
properties: {
48+
overridePrettierOption: {
49+
type: "object",
50+
items: [
51+
{ tabWidth: "number" },
52+
{ useTabs: "boolean" },
53+
{ vueIndentScriptAndStyle: "boolean" },
54+
],
55+
},
56+
},
57+
},
58+
],
4259
},
4360
defaultOptions,
4461
create(context) {
62+
const overridePrettierOption = context.options[0]?.overridePrettierOption;
63+
4564
const sourceCode = context.getSourceCode();
4665
const filePath = context.getFilename();
4766

@@ -50,8 +69,11 @@ export = createRule<[Options], MessageIds>({
5069
getPrettierRcOption(filePath)
5170
);
5271

53-
const { baseIndent, indentChar } = getIndentInfo(mergedPrettierOption);
54-
const indent = indentChar.repeat(baseIndent);
72+
const { indentRepeatTime, indentChar } = getCodeWrapIndentInfo(
73+
mergedPrettierOption,
74+
overridePrettierOption
75+
);
76+
const indent = indentChar.repeat(indentRepeatTime);
5577

5678
return {
5779
Program(node: AST.ESLintProgram) {
@@ -76,15 +98,13 @@ export = createRule<[Options], MessageIds>({
7698

7799
const code = sourceCode.text.slice(...codeRange);
78100

79-
const prettierConfig = getPrettierRcOption(filePath);
101+
const prettierConfig = {
102+
...getPrettierRcOption(filePath),
103+
...overridePrettierOption,
104+
};
80105

81106
const formattedCode = prettier
82-
.format(
83-
code,
84-
Object.assign({}, prettierConfig, {
85-
parser: prettierParser,
86-
})
87-
)
107+
.format(code, { ...prettierConfig, parser: prettierParser })
88108
.trim();
89109

90110
let lines = formattedCode.match(LINES);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import prettier from "prettier";
2+
3+
import { MergedPrettierOption } from "./get-prettier-option";
4+
import { PrettierSupportOptionName } from "./types";
5+
6+
const indent = {
7+
space: " ",
8+
tab: "\t",
9+
none: "",
10+
};
11+
12+
export type OverridePrettierOption =
13+
| {
14+
tabWidth?: number;
15+
useTabs?: boolean;
16+
vueIndentScriptAndStyle?: boolean;
17+
}
18+
| undefined;
19+
20+
type PrettierOptions = {
21+
[key in PrettierSupportOptionName]: prettier.SupportOption["default"];
22+
};
23+
24+
/**
25+
* Get indent information
26+
*/
27+
export const getCodeWrapIndentInfo = (
28+
mergedPrettierOptions: MergedPrettierOption,
29+
eslintOption?: OverridePrettierOption
30+
) => {
31+
const defaultInfo = {
32+
indentRepeatTime: 0,
33+
indentChar: indent.space,
34+
};
35+
36+
const prettierOptions = mergedPrettierOptions as PrettierOptions;
37+
38+
const tabWidth = eslintOption?.tabWidth || prettierOptions.tabWidth;
39+
const useTabs = eslintOption?.useTabs ?? prettierOptions.useTabs;
40+
const vueIndentScriptAndStyle =
41+
eslintOption?.vueIndentScriptAndStyle ??
42+
prettierOptions.vueIndentScriptAndStyle;
43+
44+
const result = {
45+
indentRepeatTime: tabWidth as number,
46+
indentChar: defaultInfo.indentChar,
47+
};
48+
49+
if (useTabs) {
50+
result.indentRepeatTime = 1;
51+
result.indentChar = indent.tab;
52+
}
53+
54+
if (!vueIndentScriptAndStyle) {
55+
result.indentRepeatTime = 0;
56+
result.indentChar = indent.none;
57+
}
58+
59+
return { ...defaultInfo, ...result };
60+
};

lib/utils/get-indent-info.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/utils/get-prettier-option.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
import prettier from "prettier";
22

3+
type PrettierOptions = {
4+
[key: string]: prettier.SupportOption["default"];
5+
};
6+
37
/**
48
* Get Prettier's default option
59
*/
610
export const getPrettierDefaultOption = prettier
711
.getSupportInfo()
8-
.options.reduce((acc, option) => {
12+
.options.reduce<PrettierOptions>((acc, option) => {
913
acc[option.name] = option.default;
1014
return acc;
11-
}, {} as any);
15+
}, {});
1216

1317
// /**
1418
// * Get option from .prettierrc
1519
// */
1620
export const getPrettierRcOption = (filePath: string) =>
1721
prettier.resolveConfig.sync(filePath, {
1822
editorconfig: true,
19-
});
23+
}) as PrettierOptions | null;
2024

2125
// /**
2226
// * Get option merged getPrettierDefaultOption and getPrettierRcOption
2327
// */
2428
export const getMergedPrettierOption = (
2529
prettierDefaultOption: typeof getPrettierDefaultOption,
2630
prettierRcOption: ReturnType<typeof getPrettierRcOption>
27-
) => ({ ...prettierDefaultOption, ...prettierRcOption });
31+
) => ({
32+
...prettierDefaultOption,
33+
...prettierRcOption,
34+
});
2835

2936
export type MergedPrettierOption = ReturnType<typeof getMergedPrettierOption>;

lib/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export * from "./parser-services";
66
// Prettier
77
// ====================================================
88
export * from "./get-prettier-option";
9-
export * from "./get-indent-info";
9+
export * from "./get-code-wrap-indent-info";
1010

1111
// ====================================================
1212
// AST

lib/utils/types/Prettier.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @see https://prettier.io/docs/en/options.html
3+
*/
4+
type PrettierDefaultOptionName =
5+
| "arrowParens"
6+
| "bracketSpacing"
7+
| "cursorOffset"
8+
| "endOfLine"
9+
| "filepath"
10+
| "htmlWhitespaceSensitivity"
11+
| "insertPragma"
12+
| "jsxBracketSameLine"
13+
| "jsxSingleQuote"
14+
| "parser"
15+
| "pluginSearchDirs"
16+
| "plugins"
17+
| "printWidth"
18+
| "proseWrap"
19+
| "quoteProps"
20+
| "rangeEnd"
21+
| "rangeStart"
22+
| "requirePragma"
23+
| "semi"
24+
| "singleQuote"
25+
| "tabWidth"
26+
| "trailingComma"
27+
| "useTabs"
28+
| "vueIndentScriptAndStyle";
29+
30+
/**
31+
* Note: If eslint-plugin-gridsome supoprts prettier's plugin, define type like PrettierDefaultOptionName. Then, merge type at this type.
32+
*/
33+
export type PrettierSupportOptionName = PrettierDefaultOptionName;

lib/utils/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./Node";
22
export * from "./Rule";
33
export * from "./Parser";
4+
export * from "./Prettier";

0 commit comments

Comments
 (0)