eslint-plugin-smarthr 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [0.1.2](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.1.1...v0.1.2) (2022-03-09)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * require-barrel-import修正(barrelファイルが複数存在する場合、一番親に当たるファイルを検知する) ([#14](https://github.com/kufu/eslint-plugin-smarthr/issues/14)) ([87a6724](https://github.com/kufu/eslint-plugin-smarthr/commit/87a67240f31c9408faad6784741bbf6a2f7ef47b))
11
+
5
12
  ### [0.1.1](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.1.0...v0.1.1) (2022-03-08)
6
13
 
7
14
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-smarthr",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "author": "SmartHR",
5
5
  "license": "MIT",
6
6
  "description": "A sharable ESLint plugin for SmartHR",
@@ -43,6 +43,7 @@ const calculateReplacedImportPath = (source) => {
43
43
  return prev
44
44
  }, source)
45
45
  }
46
+ const TARGET_EXTS = ['ts', 'tsx', 'js', 'jsx']
46
47
 
47
48
  module.exports = {
48
49
  meta: {
@@ -55,11 +56,6 @@ module.exports = {
55
56
  create(context) {
56
57
  const filename = context.getFilename()
57
58
 
58
- // HINT: indexファイルがある == barrelであるとする
59
- if (filename.match(/\/index\.(js|ts)x?$/)) {
60
- return {}
61
- }
62
-
63
59
  const dir = (() => {
64
60
  const d = filename.split('/')
65
61
  d.pop()
@@ -82,39 +78,40 @@ module.exports = {
82
78
  }
83
79
 
84
80
  const sources = sourceValue.split('/')
85
- let joinedSources = sourceValue
86
- let ext = undefined
81
+
82
+ // HINT: directoryの場合、indexファイルからimportしていることは自明であるため、一階層上からチェックする
83
+ if (fs.existsSync(sourceValue) && fs.statSync(sourceValue).isDirectory()) {
84
+ sources.pop()
85
+ sourceValue = sources.join('/')
86
+ }
87
+
88
+ let barrel = undefined
87
89
 
88
90
  while (sources.length > 0) {
89
91
  // HINT: 以下の場合は即終了
90
92
  // - import元以下のimportだった場合
91
93
  // - rootまで捜索した場合
92
- if (dir === joinedSources || dir === rootPath) {
93
- return
94
- }
95
-
96
- ext = ['ts', 'tsx', 'js', 'jsx'].find((e) => fs.existsSync(`${sources.join('/')}/index.${e}`))
97
-
98
- if (ext) {
94
+ if (
95
+ dir === rootPath ||
96
+ dir.match(new RegExp(`^${sourceValue}`))
97
+ ) {
99
98
  break
100
99
  }
101
100
 
101
+ barrel = TARGET_EXTS.map((e) => `${sourceValue}/index.${e}`).find((p) => fs.existsSync(p)) || barrel
102
+
102
103
  sources.pop()
103
- joinedSources = sources.join('/')
104
+ sourceValue = sources.join('/')
104
105
  }
105
106
 
106
- if (
107
- joinedSources &&
108
- sourceValue !== joinedSources &&
109
- !dir.match(new RegExp(`^${joinedSources}/`))
110
- ) {
111
- const replacedSources = calculateReplacedImportPath(joinedSources)
107
+ if (barrel) {
108
+ barrel = calculateReplacedImportPath(barrel)
112
109
 
113
110
  context.report({
114
111
  node,
115
112
  messageId: 'require-barrel-import',
116
113
  data: {
117
- message: `${replacedSources}からimportするか、${replacedSources}/index.${ext}を削除してください`,
114
+ message: `${barrel.replace(/\/index\.(ts|js)x?$/, '')} からimportするか、${barrel} を削除してください`,
118
115
  },
119
116
  });
120
117
  }