Skip to content

Modernize #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
.DS_Store
npm-debug.log
coverage
.nyc_output
74 changes: 74 additions & 0 deletions cjs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cjs/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions esm/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions esm/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const {anymatch} = require('./cjs')
module.exports = anymatch
20 changes: 0 additions & 20 deletions index.d.ts

This file was deleted.

3 changes: 3 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {anymatch} from './esm'
export * from './esm'
export default anymatch
58 changes: 21 additions & 37 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
'use strict';

Object.defineProperty(exports, "__esModule", { value: true });
import picomatch, { PicomatchOptions } from 'picomatch';
import normalizePath from 'normalize-path';

const picomatch = require('picomatch');
const normalizePath = require('normalize-path');

/**
* @typedef {(testString: string) => boolean} AnymatchFn
* @typedef {string|RegExp|AnymatchFn} AnymatchPattern
* @typedef {AnymatchPattern|AnymatchPattern[]} AnymatchMatcher
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we keep these jsdoc comments instead of turning this file into typescript? TS supports using jsdoc as typings so we don't need to convert to benefit from typing.

Copy link
Author

@OrbintSoft OrbintSoft Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsdoc needs to be maintained, also index.d.ts that I removed. If you forget to update typings you risk issues like this: #40

Also typescript with noImplicitAny does a strict typing check assuring all types are correctly defined.

There are many other advantages of using typescript, like:

  • having multiple targets as output, supporting both commonjs and ecmascipt modules
  • using latest ecmascript features and targeting es5 keeping compatibility with nodejs versions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the tradeoffs. I suppose I framed the reversion as a question, but I actually meant it as "this won't land without undoing the TS change".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you mean you want keep the code in typescript, but use jsdoc instead of using declaring type.
Sure it can be done, but I don't see the advantage apart from reverting 3 lines change.
Typescript syntax is much more flexible and it has no much sense to keep jsodoc in a typescript project, anyway let me know what you prefer, if you want I'll revert it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We subscribe to https://dev.to/thepassle/using-typescript-without-compilation-3ko4 - so please put the jsdoc typings back.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read the article, personally I am totally against it, but anyway I don't want waste time discussing about. I hope one day they will standardize typings in ecmascript https://github.com/tc39/proposal-type-annotations.
For now I updated the code as you requested to use jsdoc instead of typescript, I hope you like it.

My goal was just to fix typings and make this library more maintainable with esm module support.

I did also some fixes to package.json to keep old nodeJs version compatibility and export correctly esm and cjs files.
Not sure anyway if still works with node 8, that's a pretty old node version.

declare type AnymatchFn = (testString: string) => boolean
declare type AnymatchPattern = string|RegExp|AnymatchFn
declare type AnymatchMatcher = AnymatchPattern|AnymatchPattern[]

const BANG = '!';
const DEFAULT_OPTIONS = {returnIndex: false};
const arrify = (item) => Array.isArray(item) ? item : [item];

/**
* @param {AnymatchPattern} matcher
* @param {object} options
* @returns {AnymatchFn}
*/
const createPattern = (matcher, options) => {
type returnIndexOptions = {returnIndex?: boolean}
const DEFAULT_OPTIONS: returnIndexOptions = {returnIndex: false};
const arrify = (item: unknown) => Array.isArray(item) ? item : [item];


const createPattern = (matcher: AnymatchPattern, options: object): AnymatchFn => {
if (typeof matcher === 'function') {
return matcher;
}
Expand All @@ -30,17 +25,10 @@ const createPattern = (matcher, options) => {
if (matcher instanceof RegExp) {
return (string) => matcher.test(string);
}
return (string) => false;
return () => false;
};

/**
* @param {Array<Function>} patterns
* @param {Array<Function>} negPatterns
* @param {String|Array} args
* @param {Boolean} returnIndex
* @returns {boolean|number}
*/
const matchPatterns = (patterns, negPatterns, args, returnIndex) => {
const matchPatterns = (patterns: Array<Function>, negPatterns: Array<Function>, args: string|Array<unknown>, returnIndex: boolean): boolean|number => {
const isList = Array.isArray(args);
const _path = isList ? args[0] : args;
if (!isList && typeof _path !== 'string') {
Expand All @@ -67,18 +55,15 @@ const matchPatterns = (patterns, negPatterns, args, returnIndex) => {
return returnIndex ? -1 : false;
};

/**
* @param {AnymatchMatcher} matchers
* @param {Array|string} testString
* @param {object} options
* @returns {boolean|number|Function}
*/
const anymatch = (matchers, testString, options = DEFAULT_OPTIONS) => {

type anymatchFn = (testString: string, ri: boolean) => anymatchFn|boolean|number

export const anymatch = (matchers: AnymatchMatcher, testString: Array<unknown>|string, options: PicomatchOptions | returnIndexOptions | boolean = DEFAULT_OPTIONS): boolean|number|anymatchFn => {
if (matchers == null) {
throw new TypeError('anymatch: specify first argument');
}
const opts = typeof options === 'boolean' ? {returnIndex: options} : options;
const returnIndex = opts.returnIndex || false;
const opts : PicomatchOptions & returnIndexOptions = typeof options === 'boolean' ? {returnIndex: options} : options as PicomatchOptions & returnIndexOptions;
const returnIndex: boolean = opts.returnIndex || false;

// Early cache for matchers.
const mtchers = arrify(matchers);
Expand All @@ -100,5 +85,4 @@ const anymatch = (matchers, testString, options = DEFAULT_OPTIONS) => {
return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
};

anymatch.default = anymatch;
module.exports = anymatch;
export default anymatch
24 changes: 17 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"name": "anymatch",
"version": "3.1.3",
"version": "4.0.0-dev.1",
"description": "Matches strings against configurable strings, globs, regular expressions, and/or functions",
"files": [
"index.js",
"index.d.ts"
"index.cjs",
"index.mjs",
"index.ts",
"cjs/index.js",
"cjs/index.js.map",
"mjs/index.js",
"mjs/index.js.map"
],
"dependencies": {
"normalize-path": "^3.0.0",
"picomatch": "^2.0.4"
"picomatch": "^2.3.1",
"typescript": "^4.9.5"
},
"author": {
"name": "Elan Shanker",
Expand Down Expand Up @@ -36,11 +42,15 @@
],
"scripts": {
"test": "nyc mocha",
"mocha": "mocha"
"mocha": "mocha",
"build": "tsc -b && tsc -p tsconfig.esm.json"
},
"devDependencies": {
"mocha": "^6.1.3",
"nyc": "^14.0.0"
"@types/node": "^18.15.0",
"@types/normalize-path": "^3.0.0",
"@types/picomatch": "^2.3.0",
"mocha": "^10.2.0",
"nyc": "^15.1.0"
},
"engines": {
"node": ">= 8"
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "ES6",
"outDir": "./esm",
},
"extends": "./tsconfig.json",
}
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"alwaysStrict": true,
"esModuleInterop": true,
"module": "CommonJS",
"moduleResolution": "node16",
"noImplicitAny": true,
"outDir": "./cjs",
"preserveConstEnums": true,
"removeComments": true,
"sourceMap": true,
},
"files": [
"index.ts",
],
}