Skip to content

Commit b9eb56c

Browse files
authored
Merge pull request #146 from jdesrosiers/fix-types-for-node16
Fix types for node16
2 parents 52f8e85 + 637b712 commit b9eb56c

File tree

10 files changed

+240
-98
lines changed

10 files changed

+240
-98
lines changed

.eslintrc.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,16 @@ module.exports = {
2727
// Uses `require.resolve` to support npm linked eslint-config
2828
extends: require.resolve('eslint-config-ostai'),
2929
root: true,
30-
rules
30+
rules,
31+
overrides: [
32+
{
33+
files: ['*.ts', '*.cts', '*.mts', '*.cjs', '*.mjs'],
34+
extends: ['plugin:@typescript-eslint/recommended'],
35+
parser: '@typescript-eslint/parser',
36+
plugins: ['@typescript-eslint'],
37+
rules: {
38+
'@typescript-eslint/no-unused-vars': 'off'
39+
}
40+
}
41+
]
3142
}

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
# build target
2-
/index.mjs
3-
41
# test
5-
test/ts/*.js
62
/.tap
73

84
# coverage

index.d.ts

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,66 +17,65 @@ interface PatternParams {
1717
mark?: string
1818
}
1919

20-
export interface Ignore {
21-
/**
22-
* Adds one or several rules to the current manager.
23-
* @param {string[]} patterns
24-
* @returns IgnoreBase
25-
*/
26-
add(
27-
patterns: string | Ignore | readonly (string | Ignore)[] | PatternParams
28-
): this
29-
30-
/**
31-
* Filters the given array of pathnames, and returns the filtered array.
32-
* NOTICE that each path here should be a relative path to the root of your repository.
33-
* @param paths the array of paths to be filtered.
34-
* @returns The filtered array of paths
35-
*/
36-
filter(pathnames: readonly Pathname[]): Pathname[]
20+
/**
21+
* Creates new ignore manager.
22+
*/
23+
declare function ignore(options?: ignore.Options): ignore.Ignore
24+
declare namespace ignore {
25+
interface Ignore {
26+
/**
27+
* Adds one or several rules to the current manager.
28+
* @param {string[]} patterns
29+
* @returns IgnoreBase
30+
*/
31+
add(
32+
patterns: string | Ignore | readonly (string | Ignore)[] | PatternParams
33+
): this
3734

38-
/**
39-
* Creates a filter function which could filter
40-
* an array of paths with Array.prototype.filter.
41-
*/
42-
createFilter(): (pathname: Pathname) => boolean
35+
/**
36+
* Filters the given array of pathnames, and returns the filtered array.
37+
* NOTICE that each path here should be a relative path to the root of your repository.
38+
* @param paths the array of paths to be filtered.
39+
* @returns The filtered array of paths
40+
*/
41+
filter(pathnames: readonly Pathname[]): Pathname[]
4342

44-
/**
45-
* Returns Boolean whether pathname should be ignored.
46-
* @param {string} pathname a path to check
47-
* @returns boolean
48-
*/
49-
ignores(pathname: Pathname): boolean
43+
/**
44+
* Creates a filter function which could filter
45+
* an array of paths with Array.prototype.filter.
46+
*/
47+
createFilter(): (pathname: Pathname) => boolean
5048

51-
/**
52-
* Returns whether pathname should be ignored or unignored
53-
* @param {string} pathname a path to check
54-
* @returns TestResult
55-
*/
56-
test(pathname: Pathname): TestResult
49+
/**
50+
* Returns Boolean whether pathname should be ignored.
51+
* @param {string} pathname a path to check
52+
* @returns boolean
53+
*/
54+
ignores(pathname: Pathname): boolean
5755

58-
/**
59-
* Debugs ignore rules and returns the checking result, which is
60-
* equivalent to `git check-ignore -v`.
61-
* @returns TestResult
62-
*/
63-
checkIgnore(pathname: Pathname): TestResult
64-
}
56+
/**
57+
* Returns whether pathname should be ignored or unignored
58+
* @param {string} pathname a path to check
59+
* @returns TestResult
60+
*/
61+
test(pathname: Pathname): TestResult
6562

66-
export interface Options {
67-
ignorecase?: boolean
68-
// For compatibility
69-
ignoreCase?: boolean
70-
allowRelativePaths?: boolean
71-
}
63+
/**
64+
* Debugs ignore rules and returns the checking result, which is
65+
* equivalent to `git check-ignore -v`.
66+
* @returns TestResult
67+
*/
68+
checkIgnore(pathname: Pathname): TestResult
69+
}
7270

73-
/**
74-
* Creates new ignore manager.
75-
*/
76-
declare function ignore(options?: Options): Ignore
77-
declare function isPathValid (pathname: string): boolean
71+
interface Options {
72+
ignorecase?: boolean
73+
// For compatibility
74+
ignoreCase?: boolean
75+
allowRelativePaths?: boolean
76+
}
7877

79-
export default ignore
80-
export {
81-
isPathValid
78+
function isPathValid(pathname: string): boolean
8279
}
80+
81+
export = ignore

index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,6 @@ const factory = options => new Ignore(options)
739739
const isPathValid = path =>
740740
checkPath(path && checkPath.convert(path), path, RETURN_FALSE)
741741

742-
factory.isPathValid = isPathValid
743-
744742

745743
// Windows
746744
// --------------------------------------------------------------
@@ -771,7 +769,5 @@ if (
771769

772770
// COMMONJS_EXPORTS ////////////////////////////////////////////////////////////
773771

774-
// Fixes typescript
775-
factory.default = factory
776-
777772
module.exports = factory
773+
module.exports.isPathValid = isPathValid

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,29 @@
33
"version": "7.0.0",
44
"description": "Ignore is a manager and filter for .gitignore rules, the one used by eslint, gitbook and many others.",
55
"main": "index.js",
6-
"module": "index.mjs",
76
"types": "index.d.ts",
87
"files": [
98
"legacy.js",
109
"index.js",
11-
"index.mjs",
1210
"index.d.ts",
1311
"LICENSE-MIT"
1412
],
1513
"scripts": {
1614
"prepublishOnly": "npm run build",
17-
"build": "babel -o legacy.js index.js && node ./scripts/build.js",
15+
"build": "babel -o legacy.js index.js",
1816
"test:lint": "eslint .",
19-
"test:tsc": "tsc ./test/ts/simple.ts --lib ES6",
20-
"test:tsc:16": "tsc ./test/ts/simple.ts --lib ES6 --moduleResolution Node16 --module Node16",
21-
"test:ts": "node ./test/ts/simple.js",
17+
"test:ts": "ts-node ./test/ts/simple.ts",
18+
"test:ts:16": "ts-node --compilerOptions '{\"moduleResolution\": \"Node16\", \"module\": \"Node16\"}' ./test/ts/simple.ts",
19+
"test:cjs:16": "ts-node --compilerOptions '{\"moduleResolution\": \"Node16\", \"module\": \"Node16\"}' ./test/ts/simple.cjs",
20+
"test:mjs:16": "ts-node --compilerOptions '{\"moduleResolution\": \"Node16\", \"module\": \"Node16\"}' ./test/ts/simple.mjs",
2221
"tap": "tap --reporter classic",
2322
"test:git": "npm run tap test/git-check-ignore.test.js",
2423
"test:ignore": "npm run tap test/ignore.test.js",
2524
"test:ignore:only": "IGNORE_ONLY_IGNORES=1 npm run tap test/ignore.test.js",
2625
"test:others": "npm run tap test/others.test.js",
2726
"test:cases": "npm run tap test/*.test.js -- --coverage",
2827
"test:no-coverage": "npm run tap test/*.test.js -- --no-check-coverage",
29-
"test:only": "npm run test:lint && npm run build && npm run test:tsc && npm run test:tsc:16 && npm run test:ts && npm run test:cases",
28+
"test:only": "npm run test:lint && npm run build && npm run test:ts && npm run test:ts:16 && npm run test:cjs:16 && npm run test:mjs:16 && npm run test:cases",
3029
"test": "npm run test:only",
3130
"test:win32": "IGNORE_TEST_WIN32=1 npm run test",
3231
"report": "tap --coverage-report=html",
@@ -60,6 +59,7 @@
6059
"@babel/cli": "^7.22.9",
6160
"@babel/core": "^7.22.9",
6261
"@babel/preset-env": "^7.22.9",
62+
"@typescript-eslint/eslint-plugin": "^8.19.1",
6363
"codecov": "^3.8.3",
6464
"debug": "^4.3.4",
6565
"eslint": "^8.46.0",
@@ -71,6 +71,7 @@
7171
"spawn-sync": "^2.0.0",
7272
"tap": "^16.3.9",
7373
"tmp": "0.2.3",
74+
"ts-node": "^10.9.2",
7475
"typescript": "^5.6.2"
7576
},
7677
"engines": {

scripts/build.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/ts/simple.cjs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const ignore = require('../..') // eslint-disable-line @typescript-eslint/no-require-imports
2+
const {isPathValid} = require('../..') // eslint-disable-line @typescript-eslint/no-require-imports
3+
4+
const equal = (actual, expect, message) => {
5+
if (actual !== expect) {
6+
throw new Error(`${message}, expect: ${expect}, actual: ${actual}`)
7+
}
8+
}
9+
10+
const paths = ['a', 'a/b', 'foo/bar']
11+
12+
let ig = ignore()
13+
14+
ig = ig.add('*')
15+
ig = ig.add(['!*/', '!foo/bar'])
16+
17+
const filter = ig.createFilter()
18+
paths.filter(filter)
19+
const passed = filter('a')
20+
equal(passed, false, 'filters a out')
21+
22+
const filtered_paths = ig.filter(paths)
23+
const ignores = ig.ignores('a')
24+
equal(ignores, true, 'ignores a')
25+
26+
let ig2 = ignore()
27+
28+
ig2 = ig2.add('# test ig.add(Ignore)')
29+
ig2 = ig2.add(ig)
30+
31+
let ig3 = ignore()
32+
ig3 = ig3.add('*.js')
33+
34+
let ig4 = ignore()
35+
ig4 = ig4.add('*.png')
36+
37+
ig2 = ig2.add([ig3, ig4])
38+
39+
const ig5 = ignore({
40+
ignorecase: false
41+
})
42+
43+
const isValid = isPathValid('./foo')
44+
equal(isValid, false, './foo is not valid')
45+
46+
const {
47+
ignored,
48+
unignored
49+
} = ig4.test('foo')
50+
51+
equal(ignored, false, 'not ignored')
52+
equal(unignored, false, 'not unignored')
53+
54+
// Filter an Readyonly array
55+
const readonlyPaths = ['a', 'a/b', 'foo/bar']
56+
ig.filter(readonlyPaths)
57+
58+
// Add an Readonly array of rules
59+
const ig6 = ignore()
60+
ig6.add([ig3, ig4])
61+
62+
// options.ignoreCase and options.allowRelativePaths
63+
ignore({
64+
ignoreCase: false,
65+
allowRelativePaths: true
66+
})
67+
68+
const ig7 = ignore()
69+
70+
ig7.add({pattern: 'foo/*', mark: '10'})
71+
const {
72+
ignored: ignored7,
73+
rule: ignoreRule7
74+
} = ig7.checkIgnore('foo/')
75+
76+
equal(ignored7, true, 'should ignore')
77+
equal(ignoreRule7.mark, '10', 'mark is 10')
78+
equal(ignoreRule7.pattern, 'foo/*', 'ignored by foo/*')

test/ts/simple.mjs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import ignore, {isPathValid} from '../../index.js' // eslint-disable-line import/extensions
2+
3+
const equal = (actual, expect, message) => {
4+
if (actual !== expect) {
5+
throw new Error(`${message}, expect: ${expect}, actual: ${actual}`)
6+
}
7+
}
8+
9+
const paths = ['a', 'a/b', 'foo/bar']
10+
11+
let ig = ignore()
12+
13+
ig = ig.add('*')
14+
ig = ig.add(['!*/', '!foo/bar'])
15+
16+
const filter = ig.createFilter()
17+
paths.filter(filter)
18+
const passed = filter('a')
19+
equal(passed, false, 'filters a out')
20+
21+
const filtered_paths = ig.filter(paths)
22+
const ignores = ig.ignores('a')
23+
equal(ignores, true, 'ignores a')
24+
25+
let ig2 = ignore()
26+
27+
ig2 = ig2.add('# test ig.add(Ignore)')
28+
ig2 = ig2.add(ig)
29+
30+
let ig3 = ignore()
31+
ig3 = ig3.add('*.js')
32+
33+
let ig4 = ignore()
34+
ig4 = ig4.add('*.png')
35+
36+
ig2 = ig2.add([ig3, ig4])
37+
38+
const ig5 = ignore({
39+
ignorecase: false
40+
})
41+
42+
const isValid = isPathValid('./foo')
43+
equal(isValid, false, './foo is not valid')
44+
45+
const {
46+
ignored,
47+
unignored
48+
} = ig4.test('foo')
49+
50+
equal(ignored, false, 'not ignored')
51+
equal(unignored, false, 'not unignored')
52+
53+
// Filter an Readyonly array
54+
const readonlyPaths = ['a', 'a/b', 'foo/bar']
55+
ig.filter(readonlyPaths)
56+
57+
// Add an Readonly array of rules
58+
const ig6 = ignore()
59+
ig6.add([ig3, ig4])
60+
61+
// options.ignoreCase and options.allowRelativePaths
62+
ignore({
63+
ignoreCase: false,
64+
allowRelativePaths: true
65+
})
66+
67+
const ig7 = ignore()
68+
69+
ig7.add({pattern: 'foo/*', mark: '10'})
70+
const {
71+
ignored: ignored7,
72+
rule: ignoreRule7
73+
} = ig7.checkIgnore('foo/')
74+
75+
equal(ignored7, true, 'should ignore')
76+
equal(ignoreRule7.mark, '10', 'mark is 10')
77+
equal(ignoreRule7.pattern, 'foo/*', 'ignored by foo/*')

0 commit comments

Comments
 (0)