Skip to content

Commit

Permalink
chore: https://github.com/kufu/eslint-plugin-smarthr からファイルを移動 (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tokky0425 authored Dec 19, 2024
1 parent 979e93d commit c8e7de6
Show file tree
Hide file tree
Showing 112 changed files with 9,523 additions and 239 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

* @kufu/group-dev-tamatebako-reviewers
/packages/eslint-config-smarthr/ @kufu/group-dev-front-linter-reviewers
/packages/eslint-plugin-smarthr/ @kufu/group-dev-front-linter-reviewers
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@commitlint/cli": "^19.5.0",
"@commitlint/config-conventional": "^19.5.0",
"@types/jest": "^29.5.12",
"eslint": "^9.15.0",
"eslint": "9.15.0",
"eslint-config-smarthr": "8.0.0",
"husky": "^9.1.7",
"jest": "^29.7.0",
Expand Down
7 changes: 2 additions & 5 deletions packages/eslint-config-smarthr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
"registry": "https://registry.npmjs.org"
},
"devDependencies": {
"eslint": "9.15.0",
"vitest": "^2.1.8",
"@types/react": "^19.0.1",
"jest": "^29.7.0"
"@types/react": "^19.0.1"
},
"peerDependencies": {
"eslint": "^9.0.0",
Expand All @@ -51,7 +49,6 @@
"eslint-plugin-react-hooks": "^5.1.0",
"eslint-plugin-smarthr": "1.0.0",
"globals": "^15.13.0",
"typescript-eslint": "^8.18.0",
"vitest": "^2.1.8"
"typescript-eslint": "^8.18.0"
}
}
689 changes: 689 additions & 0 deletions packages/eslint-plugin-smarthr/CHANGELOG.md

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions packages/eslint-plugin-smarthr/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright 2024 SmartHR

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions packages/eslint-plugin-smarthr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# eslint-plugin-smarthr

- [a11y-anchor-has-href-attribute](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-anchor-has-href-attribute)
- [a11y-clickable-element-has-text](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-clickable-element-has-text)
- [a11y-delegate-element-has-role-presentation](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-delegate-element-has-role-presentation)
- [a11y-form-control-in-form](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-form-control-in-form)
- [a11y-heading-in-sectioning-content](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-heading-in-sectioning-content)
- [a11y-image-has-alt-attribute](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-image-has-alt-attribute)
- [a11y-input-has-name-attribute](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-input-has-name-attribute)
- [a11y-input-in-form-control](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-input-in-form-control)
- [a11y-numbered-text-within-ol](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-numbered-text-within-ol)
- [a11y-prohibit-input-maxlength-attribute](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-prohibit-input-maxlength-attribute)
- [a11y-prohibit-input-placeholder](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-prohibit-input-placeholder)
- [a11y-prohibit-sectioning-content-in-form](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-prohibit-sectioning-content-in-form)
- [a11y-prohibit-useless-sectioning-fragment](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-prohibit-useless-sectioning-fragment)
- [a11y-replace-unreadable-symbol](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-replace-unreadable-symbol)
- [a11y-required-layout-as-attribute](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-required-layout-as-attribute)
- [a11y-trigger-has-button](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/a11y-trigger-has-button)
- [best-practice-for-button-element](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-button-element)
- [best-practice-for-data-test-attribute](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-data-test-attribute)
- [best-practice-for-date](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-date)
- [best-practice-for-layouts](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-layouts)
- [best-practice-for-remote-trigger-dialog](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-remote-trigger-dialog)
- [design-system-guideline-prohibit-double-icons](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/design-system-guideline-prohibit-double-icons)
- [format-import-path](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/format-import-path)
- [format-translate-component](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/format-translate-component)
- [jsx-start-with-spread-attributes](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/jsx-start-with-spread-attributes)
- [no-import-other-domain](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/no-import-other-domain)
- [prohibit-export-array-type](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/prohibit-export-array-type)
- [prohibit-file-name](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/prohibit-file-name)
- [prohibit-import](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/prohibit-import)
- [prohibit-path-within-template-literal](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/prohibit-path-within-template-literal)
- [require-barrel-import](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/require-barrel-import)
- [require-declaration](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/require-declaration)
- [require-export](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/require-export)
- [require-import](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/require-import)
- [trim-props](https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/trim-props)
41 changes: 41 additions & 0 deletions packages/eslint-plugin-smarthr/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const fs = require('fs');
const path = require('path');

const rules = generateRulesMap();

/**
* @type {import('eslint').Linter.Config}
*/
module.exports = {
name: 'eslint-plugin-smarthr',
rules,
plugins: {
recommended: generateRecommendedConfig(rules),
}
}

function generateRulesMap() {
let rulesPath = path.join(__dirname, 'rules');
let dirs = fs.readdirSync(rulesPath);

let rulesMap = {};
for (let dir of dirs) {
rulesMap[path.parse(dir).name] = require(`./rules/${dir}`);
}
return rulesMap;
}

function generateRecommendedConfig(rules) {
let config = {
name: 'eslint-plugin-smarthr/recommended',
rules: {},
};

for (let ruleName of Object.keys(rules)) {
config.rules[`smarthr/${ruleName}`] = 'off';
}

return config;
}
5 changes: 5 additions & 0 deletions packages/eslint-plugin-smarthr/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
moduleFileExtensions: ['js'],
testMatch: ['**/test/**/*.+(js)'],
roots: ['<rootDir>/']
}
57 changes: 57 additions & 0 deletions packages/eslint-plugin-smarthr/libs/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const JSON5 = require('json5')
const path = require('path')
const fs = require('fs')

const replacePaths = (() => {
const cwd = process.cwd()
const eslintrc = (() => {
let file = `${cwd}/.eslintrc.js`
if (fs.existsSync(file)) {
return require(file)
}

file = `${cwd}/.eslintrc`

if (fs.existsSync(file)) {
return JSON5.parse(fs.readFileSync(file))
}

return {}
})()

const tsconfigFile = `${cwd}/${eslintrc.parserOptions?.project || 'tsconfig.json'}`

if (!fs.existsSync(tsconfigFile)) {
throw new Error(`${tsconfigFile} を設置してください`)
}

const { compilerOptions } = JSON5.parse(fs.readFileSync(tsconfigFile))

if (!compilerOptions || !compilerOptions.paths) {
throw new Error('tsconfig.json の compilerOptions.paths に `"@/*": ["any_path/*"]` 形式でフロントエンドのroot dir を指定してください')
}

const regexp = /\*$/
return Object.entries(compilerOptions.paths).reduce((prev, [key, values]) => {
return {
...prev,
[key.replace(regexp, '')]: values.map((v) => v.replace(regexp, '')),
}
}, {})
})()

const rootPath = (() => {
if (!replacePaths) {
return ''
}

const p = (replacePaths['@/'] || replacePaths['~/'] || [])[0]

if (!p) {
return ''
}

return path.resolve(`${process.cwd()}/${p.replace(/\/\*$/, '')}`)
})()

module.exports = { replacePaths, rootPath }
140 changes: 140 additions & 0 deletions packages/eslint-plugin-smarthr/libs/common_domain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
const path = require('path')
const fs = require('fs')
const { replacePaths, rootPath } = require('./common')

const BASE_SCHEMA_PROPERTIES = {
globalModuleDir: { type: 'array', items: { type: 'string' } },
domainModuleDir: { type: 'array', items: { type: 'string' }, default: [] },
domainConstituteDir: { type: 'array', items: { type: 'string' } },
}

const calculateDomainContext = (context) => {
if (!rootPath) {
throw new Error('tsconfig.json の compilerOptions.paths に `"@/*": ["any_path/*"]` 形式でフロントエンドのroot dir を指定してください')
}

const filename = context.getFilename()
const parentDir = (() => {
const dir = filename.split('/')
dir.pop()
return dir.join('/')
})()
const humanizeParentDir = parentDir.replace(new RegExp(`^${rootPath}/(.+)$`), '$1')

return {
option: context.options[0],
parentDir,
filename,
humanizeParentDir,
isTarget: humanizeParentDir !== parentDir,
}
}

const calculateDomainNode = (calclatedContext, node) => {
const importPath = node.source.value
const { option, parentDir, humanizeParentDir } = calclatedContext

let replacedPath = importPath

if (replacePaths) {
const exts = ['.ts', '.tsx', '.js', '.jsx', '']

replacedPath = Object.entries(replacePaths).reduce((prev, [key, values]) => {
if (replacedPath === prev) {
const regexp = new RegExp(`^${key}(.+)$`)
const matcher = prev.match(regexp)
if (matcher) {
return values.reduce((p, v) => {
if (prev === p) {
const id = p.replace(regexp, `${path.resolve(`${process.cwd()}/${v}`)}/$1`)

return exts.map((j) => `${id}${j}`).find((j) => fs.existsSync(j)) || p
}

return p
}, prev)
}
}

return prev
}, replacedPath)

if (replacedPath[0] === '.') {
replacedPath = path.resolve(`${parentDir}/${replacedPath}`)
}
if (!replacedPath.match(/\.[a-z0-9]+$/)) {
replacedPath = exts.map((j) => `${replacedPath}${j}`).find((j) => fs.existsSync(j)) || replacedPath
}

replacedPath = replacedPath.replace(/^(.+?)((\/index)?\.[a-z0-9]+|\/)$/, '$1')
}

const resolvedImportPath = replacedPath[0] === '/' ? replacedPath : ''

let isGlobalModuleImport = false
if (
!resolvedImportPath ||
option.globalModuleDir &&
option.globalModuleDir.some((global) =>
!!resolvedImportPath.match(new RegExp(`^${path.resolve(`${process.cwd()}/${global}`)}`))
)
) {
isGlobalModuleImport = true
}

const humanizeImportPath = resolvedImportPath.replace(new RegExp(`^${rootPath}/(.+)$`), '$1')
let dirs = humanizeParentDir.split('/')
let paths = humanizeImportPath.split('/')
let filteredDirs = dirs
let filteredPaths = paths

const deductedNames = []
const recursiveDeductionEq = () => {
if (dirs.length === 0 || paths.length === 0) {
return
}

if (dirs[0] === paths[0]) {
deductedNames.push(dirs.shift())
paths.shift()
recursiveDeductionEq()
}
}
recursiveDeductionEq()
filteredDirs = dirs
filteredPaths = paths

let isDomainConstitute = false

if (option.domainConstituteDir) {
const { domainConstituteDir } = option
isDomainConstitute =
!!deductedNames.find((d) => domainConstituteDir.includes(d)) || // 同一dirを削り、その中にconstitute dir があれば同一ドメイン
domainConstituteDir.includes(dirs[0]) && domainConstituteDir.includes(paths[0]) // 同一を削りきった先頭が両方constitute dirならば同一ドメイン

// HINT: 同一ドメイン内(例: workflows/index)で定形で利用されるディレクトリ名を省くことで
// ドメインの識別に利用される文字を抽出する
dirs = dirs.filter((k) => !option.domainConstituteDir.includes(k))
paths = paths.filter((k) => !option.domainConstituteDir.includes(k))
}

const isLowerImport = filteredDirs.length === 0 // 同一階層、もしくは下層からのimport
const isDomainImport = dirs.length === 0 || isDomainConstitute // 同一ドメイン内、もしくは同一階層・下層からのimport
const isModuleImport = paths.length > 0 && option.domainModuleDir.includes(paths[0]) // ドメイン内共通パーツ

return {
importPath,
humanizeImportPath,
resolvedImportPath,
filteredDirs,
filteredPaths,
dirs,
paths,
isGlobalModuleImport,
isModuleImport,
isDomainImport,
isLowerImport,
}
}

module.exports = { BASE_SCHEMA_PROPERTIES, calculateDomainContext, calculateDomainNode }
Loading

0 comments on commit c8e7de6

Please sign in to comment.