standard-tsx 0.0.5 → 0.0.6
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 +3 -0
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/eslint.config.js +14 -60
- package/src/utils.js +63 -0
package/HISTORY.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
## 0.0.6
|
|
2
|
+
* [Disable duplicate ts rules (#14)](https://github.com/extremeheat/standard-tsx/commit/625d956de1b1f98e57af040d7284d1fec0b93274) (thanks @extremeheat)
|
|
3
|
+
|
|
1
4
|
## 0.0.5
|
|
2
5
|
* [Update .gitignore handling (#12)](https://github.com/extremeheat/standard-tsx/commit/29b2286623bf5145ec6c8b3d112136f9d0b7ccac) (thanks @extremeheat)
|
|
3
6
|
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](https://gitpod.io/#https://github.com/extremeheat/standard-tsx)
|
|
5
5
|
|
|
6
6
|
A drop-in replacement for the `standard` JavaScript linter that supports both TypeScript and JavaScript code with and without JSX. This package does not
|
|
7
|
-
add any new rules. Instead, it applies standard's eslint config (https://github.com/standard/eslint-config-standard) to both `*.ts` and `*.js`/`.cjs`/`.mjs` files as well as `.tsx`/`.jsx` React files. This is on top of ESLint's default TypeScript reccomendations (which are mostly non-style related):
|
|
7
|
+
add any new JavaScript rules. Instead, it applies standard's eslint config (https://github.com/standard/eslint-config-standard) to both `*.ts` and `*.js`/`.cjs`/`.mjs` files as well as `.tsx`/`.jsx` React files. This is on top of ESLint's default TypeScript reccomendations (which are mostly non-style related) and add some `--fix`-able TypeScript rules to mirror standard's JavaScript behavior (semicolons, spacing):
|
|
8
8
|
|
|
9
9
|
>Recommended rules for code correctness that you can drop in without additional configuration. These rules are those whose reports are almost always for a bad practice and/or likely bug. recommended also disables core ESLint rules known to conflict with typescript-eslint rules or cause issues in TypeScript codebases.
|
|
10
10
|
|
package/package.json
CHANGED
package/src/eslint.config.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// ./eslint.config.js
|
|
2
|
-
const path = require('node:path')
|
|
3
|
-
const fs = require('node:fs')
|
|
4
|
-
|
|
5
1
|
const globals = require('globals')
|
|
6
2
|
const pluginJs = require('@eslint/js').configs
|
|
7
3
|
const tseslint = require('typescript-eslint')
|
|
@@ -12,62 +8,18 @@ const promisePlugin = require('eslint-plugin-promise')
|
|
|
12
8
|
const eslintEnvRestorePlugin = require('eslint-plugin-eslint-env-restore')
|
|
13
9
|
const reactPlugin = require('eslint-plugin-react')
|
|
14
10
|
const reactHooksPlugin = require('eslint-plugin-react-hooks')
|
|
15
|
-
const glob = require('glob')
|
|
16
|
-
|
|
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
|
-
}
|
|
25
|
-
|
|
26
|
-
// Sort by directory depth (root to leaves) to match Git's override behavior
|
|
27
|
-
const sortedGitignoreFiles = gitignoreFiles.sort((a, b) => {
|
|
28
|
-
const depthA = a.split(path.sep).length
|
|
29
|
-
const depthB = b.split(path.sep).length
|
|
30
|
-
return depthA - depthB
|
|
31
|
-
})
|
|
32
|
-
|
|
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
11
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
while (true) {
|
|
56
|
-
const tsconfigPath = path.join(currentDir, 'tsconfig.json')
|
|
57
|
-
if (fs.existsSync(tsconfigPath)) {
|
|
58
|
-
return currentDir
|
|
59
|
-
}
|
|
12
|
+
const { collectIgnores, findNearestTsconfigDir } = require('./utils.js')
|
|
13
|
+
const ignorePatterns = collectIgnores()
|
|
14
|
+
const tsconfigRootDir = findNearestTsconfigDir()
|
|
60
15
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
return null
|
|
16
|
+
const tsRules = {
|
|
17
|
+
// TODO: Need stylistic
|
|
18
|
+
// '@typescript-eslint/indent': ['error', 2],
|
|
19
|
+
// '@typescript-eslint/semi': ['error', 'never'],
|
|
20
|
+
// '@typescript-eslint/quotes': ['error', 'single'],
|
|
21
|
+
// '@typescript-eslint/type-annotation-spacing': ['error']
|
|
69
22
|
}
|
|
70
|
-
const tsconfigRootDir = findNearestTsconfigDir()
|
|
71
23
|
|
|
72
24
|
module.exports = [
|
|
73
25
|
{ ignores: ignorePatterns },
|
|
@@ -161,7 +113,8 @@ module.exports = [
|
|
|
161
113
|
...tseslint.configs.recommended.rules,
|
|
162
114
|
...standard.rules,
|
|
163
115
|
'no-unused-vars': ['error', { vars: 'local', args: 'none', caughtErrors: 'none', ignoreRestSiblings: true }],
|
|
164
|
-
'@typescript-eslint/no-unused-vars': ['error', { vars: 'local', args: 'none', caughtErrors: 'none', ignoreRestSiblings: true }]
|
|
116
|
+
// '@typescript-eslint/no-unused-vars': ['error', { vars: 'local', args: 'none', caughtErrors: 'none', ignoreRestSiblings: true }],
|
|
117
|
+
...tsRules
|
|
165
118
|
}
|
|
166
119
|
},
|
|
167
120
|
// TSX (Browser environment)
|
|
@@ -196,10 +149,11 @@ module.exports = [
|
|
|
196
149
|
...reactPlugin.configs.recommended.rules,
|
|
197
150
|
...reactHooksPlugin.configs.recommended.rules,
|
|
198
151
|
'no-unused-vars': ['error', { vars: 'local', args: 'none', caughtErrors: 'none', ignoreRestSiblings: true }],
|
|
199
|
-
'@typescript-eslint/no-unused-vars': ['error', { vars: 'local', args: 'none', caughtErrors: 'none', ignoreRestSiblings: true }],
|
|
152
|
+
// '@typescript-eslint/no-unused-vars': ['error', { vars: 'local', args: 'none', caughtErrors: 'none', ignoreRestSiblings: true }],
|
|
200
153
|
'react/jsx-uses-react': 'off',
|
|
201
154
|
'react/jsx-uses-vars': 'error',
|
|
202
|
-
'react/react-in-jsx-scope': 'off'
|
|
155
|
+
'react/react-in-jsx-scope': 'off',
|
|
156
|
+
...tsRules
|
|
203
157
|
}
|
|
204
158
|
}
|
|
205
159
|
]
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const path = require('node:path')
|
|
2
|
+
const fs = require('node:fs')
|
|
3
|
+
const glob = require('glob')
|
|
4
|
+
|
|
5
|
+
function collectIgnores () {
|
|
6
|
+
// Find all .gitignore files in the project (upto 2 levels up for monorepo support)
|
|
7
|
+
const gitignoreFiles = glob.sync('**/.gitignore', { cwd: process.cwd() })
|
|
8
|
+
if (gitignoreFiles.length === 0) {
|
|
9
|
+
gitignoreFiles.push(...glob.sync('**/.gitignore', { cwd: path.join(process.cwd(), '..') }).map(file => path.join('..', file)))
|
|
10
|
+
if (gitignoreFiles.length === 0) {
|
|
11
|
+
gitignoreFiles.push(...glob.sync('**/.gitignore', { cwd: path.join(process.cwd(), '../..') }).map(file => path.join('..', '..', file)))
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Sort by directory depth (root to leaves) to match Git's override behavior
|
|
16
|
+
const sortedGitignoreFiles = gitignoreFiles.sort((a, b) => {
|
|
17
|
+
const depthA = a.split(path.sep).length
|
|
18
|
+
const depthB = b.split(path.sep).length
|
|
19
|
+
return depthA - depthB
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
// Merge ignore patterns from all .gitignore files
|
|
23
|
+
const ignorePatterns = []
|
|
24
|
+
for (const file of sortedGitignoreFiles) {
|
|
25
|
+
const dir = path.dirname(file)
|
|
26
|
+
const content = fs.readFileSync(file, 'utf8')
|
|
27
|
+
const lines = content.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('#'))
|
|
28
|
+
for (const line of lines) {
|
|
29
|
+
let rule = path.join(dir, line).replaceAll('\\', '/')
|
|
30
|
+
if (rule.startsWith('..//')) continue // a rule intended for a parent directory
|
|
31
|
+
// rules that don't start with slash apply to all directories
|
|
32
|
+
rule = rule.replaceAll('../', '')
|
|
33
|
+
// mimic .gitignore behavior as ESlint is a bit different and doesn't recursively apply rules
|
|
34
|
+
if (!rule.startsWith('/') || !rule.startsWith('./') || !rule.startsWith('*')) rule = '**/' + rule
|
|
35
|
+
// Restore ordering of ! to front after above transformation
|
|
36
|
+
if (line.startsWith('!')) rule = '!' + rule.replaceAll('!', '')
|
|
37
|
+
ignorePatterns.push(rule)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return ignorePatterns
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function findNearestTsconfigDir (startDir = process.cwd()) {
|
|
45
|
+
let currentDir = path.resolve(startDir)
|
|
46
|
+
|
|
47
|
+
while (true) {
|
|
48
|
+
const tsconfigPath = path.join(currentDir, 'tsconfig.json')
|
|
49
|
+
if (fs.existsSync(tsconfigPath)) {
|
|
50
|
+
return currentDir
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const parentDir = path.dirname(currentDir)
|
|
54
|
+
if (parentDir === currentDir) {
|
|
55
|
+
// Reached filesystem root
|
|
56
|
+
break
|
|
57
|
+
}
|
|
58
|
+
currentDir = parentDir
|
|
59
|
+
}
|
|
60
|
+
return null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = { collectIgnores, findNearestTsconfigDir }
|