Skip to content

Commit

Permalink
Merge pull request #146 from jdesrosiers/fix-types-for-node16
Browse files Browse the repository at this point in the history
Fix types for node16
  • Loading branch information
kaelzhang authored Jan 13, 2025
2 parents 52f8e85 + 637b712 commit b9eb56c
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 98 deletions.
13 changes: 12 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,16 @@ module.exports = {
// Uses `require.resolve` to support npm linked eslint-config
extends: require.resolve('eslint-config-ostai'),
root: true,
rules
rules,
overrides: [
{
files: ['*.ts', '*.cts', '*.mts', '*.cjs', '*.mjs'],
extends: ['plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/no-unused-vars': 'off'
}
}
]
}
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# build target
/index.mjs

# test
test/ts/*.js
/.tap

# coverage
Expand Down
109 changes: 54 additions & 55 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,66 +17,65 @@ interface PatternParams {
mark?: string
}

export interface Ignore {
/**
* Adds one or several rules to the current manager.
* @param {string[]} patterns
* @returns IgnoreBase
*/
add(
patterns: string | Ignore | readonly (string | Ignore)[] | PatternParams
): this

/**
* Filters the given array of pathnames, and returns the filtered array.
* NOTICE that each path here should be a relative path to the root of your repository.
* @param paths the array of paths to be filtered.
* @returns The filtered array of paths
*/
filter(pathnames: readonly Pathname[]): Pathname[]
/**
* Creates new ignore manager.
*/
declare function ignore(options?: ignore.Options): ignore.Ignore
declare namespace ignore {
interface Ignore {
/**
* Adds one or several rules to the current manager.
* @param {string[]} patterns
* @returns IgnoreBase
*/
add(
patterns: string | Ignore | readonly (string | Ignore)[] | PatternParams
): this

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

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

/**
* Returns whether pathname should be ignored or unignored
* @param {string} pathname a path to check
* @returns TestResult
*/
test(pathname: Pathname): TestResult
/**
* Returns Boolean whether pathname should be ignored.
* @param {string} pathname a path to check
* @returns boolean
*/
ignores(pathname: Pathname): boolean

/**
* Debugs ignore rules and returns the checking result, which is
* equivalent to `git check-ignore -v`.
* @returns TestResult
*/
checkIgnore(pathname: Pathname): TestResult
}
/**
* Returns whether pathname should be ignored or unignored
* @param {string} pathname a path to check
* @returns TestResult
*/
test(pathname: Pathname): TestResult

export interface Options {
ignorecase?: boolean
// For compatibility
ignoreCase?: boolean
allowRelativePaths?: boolean
}
/**
* Debugs ignore rules and returns the checking result, which is
* equivalent to `git check-ignore -v`.
* @returns TestResult
*/
checkIgnore(pathname: Pathname): TestResult
}

/**
* Creates new ignore manager.
*/
declare function ignore(options?: Options): Ignore
declare function isPathValid (pathname: string): boolean
interface Options {
ignorecase?: boolean
// For compatibility
ignoreCase?: boolean
allowRelativePaths?: boolean
}

export default ignore
export {
isPathValid
function isPathValid(pathname: string): boolean
}

export = ignore
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,6 @@ const factory = options => new Ignore(options)
const isPathValid = path =>
checkPath(path && checkPath.convert(path), path, RETURN_FALSE)

factory.isPathValid = isPathValid


// Windows
// --------------------------------------------------------------
Expand Down Expand Up @@ -771,7 +769,5 @@ if (

// COMMONJS_EXPORTS ////////////////////////////////////////////////////////////

// Fixes typescript
factory.default = factory

module.exports = factory
module.exports.isPathValid = isPathValid
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@
"version": "7.0.0",
"description": "Ignore is a manager and filter for .gitignore rules, the one used by eslint, gitbook and many others.",
"main": "index.js",
"module": "index.mjs",
"types": "index.d.ts",
"files": [
"legacy.js",
"index.js",
"index.mjs",
"index.d.ts",
"LICENSE-MIT"
],
"scripts": {
"prepublishOnly": "npm run build",
"build": "babel -o legacy.js index.js && node ./scripts/build.js",
"build": "babel -o legacy.js index.js",
"test:lint": "eslint .",
"test:tsc": "tsc ./test/ts/simple.ts --lib ES6",
"test:tsc:16": "tsc ./test/ts/simple.ts --lib ES6 --moduleResolution Node16 --module Node16",
"test:ts": "node ./test/ts/simple.js",
"test:ts": "ts-node ./test/ts/simple.ts",
"test:ts:16": "ts-node --compilerOptions '{\"moduleResolution\": \"Node16\", \"module\": \"Node16\"}' ./test/ts/simple.ts",
"test:cjs:16": "ts-node --compilerOptions '{\"moduleResolution\": \"Node16\", \"module\": \"Node16\"}' ./test/ts/simple.cjs",
"test:mjs:16": "ts-node --compilerOptions '{\"moduleResolution\": \"Node16\", \"module\": \"Node16\"}' ./test/ts/simple.mjs",
"tap": "tap --reporter classic",
"test:git": "npm run tap test/git-check-ignore.test.js",
"test:ignore": "npm run tap test/ignore.test.js",
"test:ignore:only": "IGNORE_ONLY_IGNORES=1 npm run tap test/ignore.test.js",
"test:others": "npm run tap test/others.test.js",
"test:cases": "npm run tap test/*.test.js -- --coverage",
"test:no-coverage": "npm run tap test/*.test.js -- --no-check-coverage",
"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",
"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",
"test": "npm run test:only",
"test:win32": "IGNORE_TEST_WIN32=1 npm run test",
"report": "tap --coverage-report=html",
Expand Down Expand Up @@ -60,6 +59,7 @@
"@babel/cli": "^7.22.9",
"@babel/core": "^7.22.9",
"@babel/preset-env": "^7.22.9",
"@typescript-eslint/eslint-plugin": "^8.19.1",
"codecov": "^3.8.3",
"debug": "^4.3.4",
"eslint": "^8.46.0",
Expand All @@ -71,6 +71,7 @@
"spawn-sync": "^2.0.0",
"tap": "^16.3.9",
"tmp": "0.2.3",
"ts-node": "^10.9.2",
"typescript": "^5.6.2"
},
"engines": {
Expand Down
18 changes: 0 additions & 18 deletions scripts/build.js

This file was deleted.

78 changes: 78 additions & 0 deletions test/ts/simple.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const ignore = require('../..') // eslint-disable-line @typescript-eslint/no-require-imports
const {isPathValid} = require('../..') // eslint-disable-line @typescript-eslint/no-require-imports

const equal = (actual, expect, message) => {
if (actual !== expect) {
throw new Error(`${message}, expect: ${expect}, actual: ${actual}`)
}
}

const paths = ['a', 'a/b', 'foo/bar']

let ig = ignore()

ig = ig.add('*')
ig = ig.add(['!*/', '!foo/bar'])

const filter = ig.createFilter()
paths.filter(filter)
const passed = filter('a')
equal(passed, false, 'filters a out')

const filtered_paths = ig.filter(paths)
const ignores = ig.ignores('a')
equal(ignores, true, 'ignores a')

let ig2 = ignore()

ig2 = ig2.add('# test ig.add(Ignore)')
ig2 = ig2.add(ig)

let ig3 = ignore()
ig3 = ig3.add('*.js')

let ig4 = ignore()
ig4 = ig4.add('*.png')

ig2 = ig2.add([ig3, ig4])

const ig5 = ignore({
ignorecase: false
})

const isValid = isPathValid('./foo')
equal(isValid, false, './foo is not valid')

const {
ignored,
unignored
} = ig4.test('foo')

equal(ignored, false, 'not ignored')
equal(unignored, false, 'not unignored')

// Filter an Readyonly array
const readonlyPaths = ['a', 'a/b', 'foo/bar']
ig.filter(readonlyPaths)

// Add an Readonly array of rules
const ig6 = ignore()
ig6.add([ig3, ig4])

// options.ignoreCase and options.allowRelativePaths
ignore({
ignoreCase: false,
allowRelativePaths: true
})

const ig7 = ignore()

ig7.add({pattern: 'foo/*', mark: '10'})
const {
ignored: ignored7,
rule: ignoreRule7
} = ig7.checkIgnore('foo/')

equal(ignored7, true, 'should ignore')
equal(ignoreRule7.mark, '10', 'mark is 10')
equal(ignoreRule7.pattern, 'foo/*', 'ignored by foo/*')
77 changes: 77 additions & 0 deletions test/ts/simple.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import ignore, {isPathValid} from '../../index.js' // eslint-disable-line import/extensions

const equal = (actual, expect, message) => {
if (actual !== expect) {
throw new Error(`${message}, expect: ${expect}, actual: ${actual}`)
}
}

const paths = ['a', 'a/b', 'foo/bar']

let ig = ignore()

ig = ig.add('*')
ig = ig.add(['!*/', '!foo/bar'])

const filter = ig.createFilter()
paths.filter(filter)
const passed = filter('a')
equal(passed, false, 'filters a out')

const filtered_paths = ig.filter(paths)
const ignores = ig.ignores('a')
equal(ignores, true, 'ignores a')

let ig2 = ignore()

ig2 = ig2.add('# test ig.add(Ignore)')
ig2 = ig2.add(ig)

let ig3 = ignore()
ig3 = ig3.add('*.js')

let ig4 = ignore()
ig4 = ig4.add('*.png')

ig2 = ig2.add([ig3, ig4])

const ig5 = ignore({
ignorecase: false
})

const isValid = isPathValid('./foo')
equal(isValid, false, './foo is not valid')

const {
ignored,
unignored
} = ig4.test('foo')

equal(ignored, false, 'not ignored')
equal(unignored, false, 'not unignored')

// Filter an Readyonly array
const readonlyPaths = ['a', 'a/b', 'foo/bar']
ig.filter(readonlyPaths)

// Add an Readonly array of rules
const ig6 = ignore()
ig6.add([ig3, ig4])

// options.ignoreCase and options.allowRelativePaths
ignore({
ignoreCase: false,
allowRelativePaths: true
})

const ig7 = ignore()

ig7.add({pattern: 'foo/*', mark: '10'})
const {
ignored: ignored7,
rule: ignoreRule7
} = ig7.checkIgnore('foo/')

equal(ignored7, true, 'should ignore')
equal(ignoreRule7.mark, '10', 'mark is 10')
equal(ignoreRule7.pattern, 'foo/*', 'ignored by foo/*')
Loading

0 comments on commit b9eb56c

Please sign in to comment.