-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref : add custom eslint rules for page/layout.tsx files
- Loading branch information
Showing
25 changed files
with
230 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
extends: 'next/core-web-vitals', | ||
plugins: [ | ||
'unused-imports', | ||
'custom-rules', | ||
], | ||
rules: { | ||
'comma-dangle': ['error', 'always-multiline'], | ||
'object-curly-spacing': ['warn', 'always'], | ||
'import/consistent-type-specifier-style': ['error', 'prefer-top-level'], | ||
'import/order': [ | ||
'error', | ||
{ | ||
'newlines-between': 'always', | ||
groups: [ | ||
'builtin', | ||
'external', | ||
'internal', | ||
['parent', 'sibling', 'index'], | ||
'type', | ||
], | ||
pathGroups: [ | ||
{ | ||
pattern: 'react', | ||
group: 'builtin', | ||
position: 'after', | ||
}, | ||
{ | ||
pattern: 'react-icons/**', | ||
group: 'builtin', | ||
position: 'after', | ||
}, | ||
{ | ||
pattern: 'next/**', | ||
group: 'builtin', | ||
position: 'before', | ||
}, | ||
], | ||
pathGroupsExcludedImportTypes: ['react', 'react-icons/**', 'next/**'], | ||
alphabetize: { | ||
order: 'asc', | ||
caseInsensitive: false, | ||
}, | ||
}, | ||
], | ||
indent: ['error', 'tab', { SwitchCase: 1 }], | ||
'no-multi-spaces': ['error', { ignoreEOLComments: true }], | ||
'no-multiple-empty-lines': ['error', { max: 1, maxEOF: 1 }], | ||
quotes: ['error', 'single'], | ||
semi: ['error', 'always'], | ||
'unused-imports/no-unused-imports': 'error', | ||
|
||
'custom-rules/page-export-naming': 'error', | ||
'custom-rules/layout-export-naming': 'error', | ||
}, | ||
}; | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const plugin = { | ||
rules: { | ||
'page-export-naming': require('./rules/page-export-naming.cjs'), | ||
'layout-export-naming': require('./rules/layout-export-naming.cjs'), | ||
}, | ||
}; | ||
|
||
module.exports = plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "eslint-plugin-custom-rules", | ||
"type":"module", | ||
"version": "1.0.0", | ||
"main": "eslint-plugin-custom-rules.cjs", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"description": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const { sep } = require('path'); | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Enforce the default export names to ends with "Layout" in layout.tsx files', | ||
category: 'Best Practices', | ||
recommended: true, | ||
}, | ||
messages: { | ||
invalidExportName: 'Default export name must ends with "Layout"', | ||
invalidExportType: 'Default export must be a function', | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
/** | ||
* @param {import('eslint').Rule.RuleContext} context | ||
*/ | ||
create(context) { | ||
const filename = context.filename.split(sep).pop(); | ||
if (filename !== 'layout.tsx') return {}; | ||
|
||
return { | ||
ExportDefaultDeclaration(node) { | ||
if (node.declaration.type === 'FunctionDeclaration' || (node.declaration.type === 'ArrowFunctionExpression' && node.declaration.id)) { | ||
/** | ||
* @type {string | undefined} | ||
*/ | ||
const functionName = node.declaration.id && node.declaration.id.name; | ||
|
||
if (functionName && !functionName.endsWith('Layout')) { | ||
context.report({ | ||
node, | ||
messageId: 'invalidExportName', | ||
fix(fixed) { | ||
const newName = `${functionName}Layout`; | ||
const start = node.declaration.id.range[0]; | ||
const end = node.declaration.id.range[1]; | ||
return fixed.replaceTextRange([start, end], newName); | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
if (node.declaration.type === 'Identifier') { | ||
context.report({ | ||
node, | ||
messageId: 'invalidExportType', | ||
}); | ||
} | ||
}, | ||
}; | ||
|
||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const { sep } = require('path'); | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Enforce the default export names to ends with "Page" in .tsx files', | ||
category: 'Best Practices', | ||
recommended: true, | ||
}, | ||
messages: { | ||
invalidExportName: 'Default export name must ends with "Page"', | ||
invalidExportType: 'Default export must be a function', | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
/** | ||
* @param {import('eslint').Rule.RuleContext} context | ||
*/ | ||
create(context) { | ||
const filename = context.filename.split(sep).pop(); | ||
if (filename !== 'page.tsx') return {}; | ||
|
||
return { | ||
ExportDefaultDeclaration(node) { | ||
if (node.declaration.type === 'FunctionDeclaration' || (node.declaration.type === 'ArrowFunctionExpression' && node.declaration.id)) { | ||
/** | ||
* @type {string | undefined} | ||
*/ | ||
const functionName = node.declaration.id && node.declaration.id.name; | ||
|
||
if (functionName && !functionName.endsWith('Page')) { | ||
context.report({ | ||
node, | ||
messageId: 'invalidExportName', | ||
fix(fixed) { | ||
const newName = `${functionName}Page`; | ||
const start = node.declaration.id.range[0]; | ||
const end = node.declaration.id.range[1]; | ||
return fixed.replaceTextRange([start, end], newName); | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
if (node.declaration.type === 'Identifier') { | ||
context.report({ | ||
node, | ||
messageId: 'invalidExportType', | ||
}); | ||
} | ||
}, | ||
}; | ||
|
||
}, | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.