|
1 | | -import { ESLintUtils } from '@typescript-eslint/utils' |
2 | | -import { relative } from 'path' |
3 | | - |
4 | | -const createRule = ESLintUtils.RuleCreator( |
5 | | - (name) => |
6 | | - `https://github.com/dev-five-git/devup/tree/main/packages/eslint-plugin/src/rules/${name}`, |
7 | | -) |
8 | | - |
9 | | -export const appPage = createRule({ |
10 | | - name: 'app-page', |
11 | | - defaultOptions: [], |
12 | | - meta: { |
13 | | - schema: [], |
14 | | - messages: { |
15 | | - pageOrLayoutComponentShouldDefaultExport: |
16 | | - '페이지나 레이아웃 컴포넌트는 반드시 `export default`로 내보내야 합니다.', |
17 | | - |
18 | | - nameOfPageOrLayoutComponentShouldHaveSuffix: |
19 | | - '페이지나 레이아웃 컴포넌트의 이름은 반드시 `Page`나 `Layout`으로 끝나야 합니다.', |
20 | | - pathParamsShouldExist: |
21 | | - '경로 변수를 사용할 수 있을 경우 `params`는 반드시 존재해야 합니다.', |
22 | | - }, |
23 | | - type: 'problem', |
24 | | - fixable: 'code', |
25 | | - docs: { |
26 | | - description: |
27 | | - 'required 페이지나 레이아웃 컴포넌트는 반드시 export default로 내보내야 합니다.', |
28 | | - }, |
29 | | - }, |
30 | | - create(context) { |
31 | | - const filename = relative(context.cwd, context.physicalFilename) |
32 | | - |
33 | | - if (!/src[/\\]app[/\\]/.test(filename)) return {} |
34 | | - const type = /page\.[j|t]sx?$/.test(filename) |
35 | | - ? 'page' |
36 | | - : /layout\.[j|t]sx?$/.test(filename) |
37 | | - ? 'layout' |
38 | | - : /404\.[j|t]sx?$/.test(filename) |
39 | | - ? '404' |
40 | | - : 'component' |
41 | | - if (type === 'component') return {} |
42 | | - |
43 | | - const functionSuffix = type === 'layout' ? 'Layout' : 'Page' |
44 | | - const pathParams = filename.match(/\[.*?]/g) ?? [] |
45 | | - const onlyLocale = pathParams.length === 1 && pathParams[0] === '[locale]' |
46 | | - // locale 만 있을 경우 무시합니다. |
47 | | - |
48 | | - let ok = false |
49 | | - return { |
50 | | - ExportDefaultDeclaration(defaultExport) { |
51 | | - ok = true |
52 | | - const declaration = defaultExport.declaration |
53 | | - if (declaration.type !== 'FunctionDeclaration') return |
54 | | - if ( |
55 | | - declaration.id?.name && |
56 | | - !declaration.id.name.endsWith(functionSuffix) |
57 | | - ) { |
58 | | - const func = declaration.id |
59 | | - context.report({ |
60 | | - node: func, |
61 | | - messageId: 'nameOfPageOrLayoutComponentShouldHaveSuffix', |
62 | | - fix(fixer) { |
63 | | - return fixer.replaceText(func, func.name + functionSuffix) |
64 | | - }, |
65 | | - }) |
66 | | - } |
67 | | - if ( |
68 | | - declaration.id && |
69 | | - pathParams.length && |
70 | | - declaration.params.length === 0 && |
71 | | - !onlyLocale |
72 | | - ) { |
73 | | - context.report({ |
74 | | - node: declaration, |
75 | | - messageId: 'pathParamsShouldExist', |
76 | | - fix(fixer) { |
77 | | - return fixer.replaceTextRange( |
78 | | - [ |
79 | | - declaration.id!.range[1], |
80 | | - declaration.returnType |
81 | | - ? declaration.returnType.range[0] |
82 | | - : declaration.body.range[0], |
83 | | - ], |
84 | | - `({params}:{params:Promise<{${pathParams |
85 | | - .map((param) => `${param.slice(1, -1)}:string`) |
86 | | - .join(';')}}>})`, |
87 | | - ) |
88 | | - }, |
89 | | - }) |
90 | | - } |
91 | | - return |
92 | | - }, |
93 | | - 'Program:exit'(program) { |
94 | | - // 소스코드가 비어있으면 자동생성 |
95 | | - if (ok) return |
96 | | - context.report({ |
97 | | - node: program, |
98 | | - messageId: 'pageOrLayoutComponentShouldDefaultExport', |
99 | | - fix(fixer) { |
100 | | - return fixer.insertTextAfter( |
101 | | - program, |
102 | | - `${context.sourceCode.text.trim().length ? '\n' : ''}export default function ` + |
103 | | - (type === '404' ? 'NotFoundPage' : functionSuffix) + |
104 | | - `(${ |
105 | | - pathParams.length |
106 | | - ? `{params}:{params:Promise<{${pathParams |
107 | | - .map((param) => `${param.slice(1, -1)}:string`) |
108 | | - .join(';')}}>}` |
109 | | - : '' |
110 | | - }){return <></>}`, |
111 | | - ) |
112 | | - }, |
113 | | - }) |
114 | | - }, |
115 | | - } |
116 | | - }, |
117 | | -}) |
| 1 | +import { ESLintUtils } from '@typescript-eslint/utils' |
| 2 | +import { relative } from 'path' |
| 3 | + |
| 4 | +const createRule = ESLintUtils.RuleCreator( |
| 5 | + (name) => |
| 6 | + `https://github.com/dev-five-git/devup/tree/main/packages/eslint-plugin/src/rules/${name}`, |
| 7 | +) |
| 8 | + |
| 9 | +export const appPage = createRule({ |
| 10 | + name: 'app-page', |
| 11 | + defaultOptions: [], |
| 12 | + meta: { |
| 13 | + schema: [], |
| 14 | + messages: { |
| 15 | + pageOrLayoutComponentShouldDefaultExport: |
| 16 | + 'Page or layout component must be exported with `export default`.', |
| 17 | + nameOfPageOrLayoutComponentShouldHaveSuffix: |
| 18 | + 'Page or layout component name must end with `Page` or `Layout`.', |
| 19 | + pathParamsShouldExist: |
| 20 | + '`params` must exist when path parameters are available.', |
| 21 | + }, |
| 22 | + type: 'problem', |
| 23 | + fixable: 'code', |
| 24 | + docs: { |
| 25 | + description: |
| 26 | + 'Require page or layout component to be exported with export default.', |
| 27 | + }, |
| 28 | + }, |
| 29 | + create(context) { |
| 30 | + const filename = relative(context.cwd, context.physicalFilename) |
| 31 | + |
| 32 | + if (!/src[/\\]app[/\\]/.test(filename)) return {} |
| 33 | + const type = /page\.[j|t]sx?$/.test(filename) |
| 34 | + ? 'page' |
| 35 | + : /layout\.[j|t]sx?$/.test(filename) |
| 36 | + ? 'layout' |
| 37 | + : /404\.[j|t]sx?$/.test(filename) |
| 38 | + ? '404' |
| 39 | + : 'component' |
| 40 | + if (type === 'component') return {} |
| 41 | + |
| 42 | + const functionSuffix = type === 'layout' ? 'Layout' : 'Page' |
| 43 | + const pathParams = filename.match(/\[.*?]/g) ?? [] |
| 44 | + // Ignore when only locale param exists |
| 45 | + const onlyLocale = pathParams.length === 1 && pathParams[0] === '[locale]' |
| 46 | + |
| 47 | + let ok = false |
| 48 | + return { |
| 49 | + ExportDefaultDeclaration(defaultExport) { |
| 50 | + ok = true |
| 51 | + const declaration = defaultExport.declaration |
| 52 | + if (declaration.type !== 'FunctionDeclaration') return |
| 53 | + if ( |
| 54 | + declaration.id?.name && |
| 55 | + !declaration.id.name.endsWith(functionSuffix) |
| 56 | + ) { |
| 57 | + const func = declaration.id |
| 58 | + context.report({ |
| 59 | + node: func, |
| 60 | + messageId: 'nameOfPageOrLayoutComponentShouldHaveSuffix', |
| 61 | + fix(fixer) { |
| 62 | + return fixer.replaceText(func, func.name + functionSuffix) |
| 63 | + }, |
| 64 | + }) |
| 65 | + } |
| 66 | + if ( |
| 67 | + declaration.id && |
| 68 | + pathParams.length && |
| 69 | + declaration.params.length === 0 && |
| 70 | + !onlyLocale |
| 71 | + ) { |
| 72 | + context.report({ |
| 73 | + node: declaration, |
| 74 | + messageId: 'pathParamsShouldExist', |
| 75 | + fix(fixer) { |
| 76 | + return fixer.replaceTextRange( |
| 77 | + [ |
| 78 | + declaration.id!.range[1], |
| 79 | + declaration.returnType |
| 80 | + ? declaration.returnType.range[0] |
| 81 | + : declaration.body.range[0], |
| 82 | + ], |
| 83 | + `({params}:{params:Promise<{${pathParams |
| 84 | + .map((param) => `${param.slice(1, -1)}:string`) |
| 85 | + .join(';')}}>})`, |
| 86 | + ) |
| 87 | + }, |
| 88 | + }) |
| 89 | + } |
| 90 | + return |
| 91 | + }, |
| 92 | + 'Program:exit'(program) { |
| 93 | + // Auto-generate if source code is empty |
| 94 | + if (ok) return |
| 95 | + context.report({ |
| 96 | + node: program, |
| 97 | + messageId: 'pageOrLayoutComponentShouldDefaultExport', |
| 98 | + fix(fixer) { |
| 99 | + return fixer.insertTextAfter( |
| 100 | + program, |
| 101 | + `${context.sourceCode.text.trim().length ? '\n' : ''}export default function ` + |
| 102 | + (type === '404' ? 'NotFoundPage' : functionSuffix) + |
| 103 | + `(${ |
| 104 | + pathParams.length |
| 105 | + ? `{params}:{params:Promise<{${pathParams |
| 106 | + .map((param) => `${param.slice(1, -1)}:string`) |
| 107 | + .join(';')}}>}` |
| 108 | + : '' |
| 109 | + }){return <></>}`, |
| 110 | + ) |
| 111 | + }, |
| 112 | + }) |
| 113 | + }, |
| 114 | + } |
| 115 | + }, |
| 116 | +}) |
0 commit comments