standard-tsx 0.0.3 → 0.0.5
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/HISTORY.md +7 -0
- package/package.json +1 -1
- package/src/eslint.config.js +53 -11
- package/src/env-restore.js +0 -41
package/HISTORY.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## 0.0.5
|
|
2
|
+
* [Update .gitignore handling (#12)](https://github.com/extremeheat/standard-tsx/commit/29b2286623bf5145ec6c8b3d112136f9d0b7ccac) (thanks @extremeheat)
|
|
3
|
+
|
|
4
|
+
## 0.0.4
|
|
5
|
+
* [Fix tsconfig lookup (#9)](https://github.com/extremeheat/standard-tsx/commit/4f28fc3798d52a35d86126c29a7e5d3d341b1578) (thanks @extremeheat)
|
|
6
|
+
* [Fix .gitignore handling in eslint.config.js](https://github.com/extremeheat/standard-tsx/commit/a8d88a03742b02b7138d9b653026eb33a74ce829) (thanks @extremeheat)
|
|
7
|
+
|
|
1
8
|
## 0.0.3
|
|
2
9
|
* [Update README.md](https://github.com/extremeheat/standard-tsx/commit/0f6de2eac2d90cec95ad49ef887cfca79e6e35d1) (thanks @extremeheat)
|
|
3
10
|
* [Update eslint.config.js - glob: ignore node_modules](https://github.com/extremeheat/standard-tsx/commit/7f5f6735c0baebc1923ef86d12cbd00a1db22032) (thanks @extremeheat)
|
package/package.json
CHANGED
package/src/eslint.config.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// ./eslint.config.js
|
|
2
|
+
const path = require('node:path')
|
|
3
|
+
const fs = require('node:fs')
|
|
4
|
+
|
|
2
5
|
const globals = require('globals')
|
|
3
6
|
const pluginJs = require('@eslint/js').configs
|
|
4
7
|
const tseslint = require('typescript-eslint')
|
|
@@ -9,12 +12,16 @@ const promisePlugin = require('eslint-plugin-promise')
|
|
|
9
12
|
const eslintEnvRestorePlugin = require('eslint-plugin-eslint-env-restore')
|
|
10
13
|
const reactPlugin = require('eslint-plugin-react')
|
|
11
14
|
const reactHooksPlugin = require('eslint-plugin-react-hooks')
|
|
12
|
-
|
|
13
|
-
const { includeIgnoreFile } = require('@eslint/compat')
|
|
14
|
-
const path = require('node:path')
|
|
15
15
|
const glob = require('glob')
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
// Find all .gitignore files in the project (upto 2 levels up for monorepo support)
|
|
18
|
+
const gitignoreFiles = glob.sync('**/.gitignore', { cwd: process.cwd() })
|
|
19
|
+
if (gitignoreFiles.length === 0) {
|
|
20
|
+
gitignoreFiles.push(...glob.sync('**/.gitignore', { cwd: path.join(process.cwd(), '..') }).map(file => path.join('..', file)))
|
|
21
|
+
if (gitignoreFiles.length === 0) {
|
|
22
|
+
gitignoreFiles.push(...glob.sync('**/.gitignore', { cwd: path.join(process.cwd(), '../..') }).map(file => path.join('..', '..', file)))
|
|
23
|
+
}
|
|
24
|
+
}
|
|
18
25
|
|
|
19
26
|
// Sort by directory depth (root to leaves) to match Git's override behavior
|
|
20
27
|
const sortedGitignoreFiles = gitignoreFiles.sort((a, b) => {
|
|
@@ -23,14 +30,47 @@ const sortedGitignoreFiles = gitignoreFiles.sort((a, b) => {
|
|
|
23
30
|
return depthA - depthB
|
|
24
31
|
})
|
|
25
32
|
|
|
26
|
-
//
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
// Merge ignore patterns from all .gitignore files
|
|
34
|
+
const ignorePatterns = []
|
|
35
|
+
for (const file of sortedGitignoreFiles) {
|
|
36
|
+
const dir = path.dirname(file)
|
|
37
|
+
const content = fs.readFileSync(file, 'utf8')
|
|
38
|
+
const lines = content.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('#'))
|
|
39
|
+
for (const line of lines) {
|
|
40
|
+
let rule = path.join(dir, line).replaceAll('\\', '/')
|
|
41
|
+
if (rule.startsWith('..//')) continue // a rule intended for a parent directory
|
|
42
|
+
// rules that don't start with slash apply to all directories
|
|
43
|
+
rule = rule.replaceAll('../', '')
|
|
44
|
+
// mimic .gitignore behavior as ESlint is a bit different and doesn't recursively apply rules
|
|
45
|
+
if (!rule.startsWith('/') || !rule.startsWith('./') || !rule.startsWith('*')) rule = '**/' + rule
|
|
46
|
+
// Restore ordering of ! to front after above transformation
|
|
47
|
+
if (line.startsWith('!')) rule = '!' + rule.replaceAll('!', '')
|
|
48
|
+
ignorePatterns.push(rule)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function findNearestTsconfigDir (startDir = process.cwd()) {
|
|
53
|
+
let currentDir = path.resolve(startDir)
|
|
54
|
+
|
|
55
|
+
while (true) {
|
|
56
|
+
const tsconfigPath = path.join(currentDir, 'tsconfig.json')
|
|
57
|
+
if (fs.existsSync(tsconfigPath)) {
|
|
58
|
+
return currentDir
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const parentDir = path.dirname(currentDir)
|
|
62
|
+
if (parentDir === currentDir) {
|
|
63
|
+
// Reached filesystem root
|
|
64
|
+
break
|
|
65
|
+
}
|
|
66
|
+
currentDir = parentDir
|
|
67
|
+
}
|
|
68
|
+
return null
|
|
69
|
+
}
|
|
70
|
+
const tsconfigRootDir = findNearestTsconfigDir()
|
|
31
71
|
|
|
32
72
|
module.exports = [
|
|
33
|
-
|
|
73
|
+
{ ignores: ignorePatterns },
|
|
34
74
|
{
|
|
35
75
|
languageOptions: {
|
|
36
76
|
globals: {
|
|
@@ -102,7 +142,8 @@ module.exports = [
|
|
|
102
142
|
languageOptions: {
|
|
103
143
|
parser: tseslint.parser,
|
|
104
144
|
parserOptions: {
|
|
105
|
-
project: './tsconfig.json'
|
|
145
|
+
project: './tsconfig.json',
|
|
146
|
+
tsconfigRootDir
|
|
106
147
|
},
|
|
107
148
|
globals: {
|
|
108
149
|
...globals.node
|
|
@@ -130,6 +171,7 @@ module.exports = [
|
|
|
130
171
|
parser: tseslint.parser,
|
|
131
172
|
parserOptions: {
|
|
132
173
|
project: './tsconfig.json',
|
|
174
|
+
tsconfigRootDir,
|
|
133
175
|
ecmaFeatures: {
|
|
134
176
|
jsx: true
|
|
135
177
|
}
|
package/src/env-restore.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
// ./env-restore.js
|
|
2
|
-
const envGlobals = require('globals')
|
|
3
|
-
|
|
4
|
-
function extractEslintEnv (text) {
|
|
5
|
-
const firstLine = text.split('\n')[0].trim()
|
|
6
|
-
if (firstLine.startsWith('/* eslint-env ') && firstLine.endsWith('*/')) {
|
|
7
|
-
const envPart = firstLine.slice(14, -2).trim()
|
|
8
|
-
return envPart.split(',').map(env => env.trim())
|
|
9
|
-
}
|
|
10
|
-
return []
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function mapEnvsToGlobals (envs) {
|
|
14
|
-
const globals = {}
|
|
15
|
-
envs.forEach(env => {
|
|
16
|
-
if (envGlobals[env]) {
|
|
17
|
-
Object.assign(globals, envGlobals[env])
|
|
18
|
-
}
|
|
19
|
-
})
|
|
20
|
-
return globals
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
module.exports = {
|
|
24
|
-
processors: {
|
|
25
|
-
js: {
|
|
26
|
-
preprocess (text, filename) {
|
|
27
|
-
const envs = extractEslintEnv(text)
|
|
28
|
-
if (envs.length === 0) return [text]
|
|
29
|
-
|
|
30
|
-
const globals = mapEnvsToGlobals(envs)
|
|
31
|
-
const globalNames = Object.keys(globals).join(', ')
|
|
32
|
-
const injectedComment = `/* global ${globalNames} */ ${text}`
|
|
33
|
-
return [injectedComment]
|
|
34
|
-
},
|
|
35
|
-
postprocess (messages) {
|
|
36
|
-
return messages[0]
|
|
37
|
-
},
|
|
38
|
-
supportsAutofix: true
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|