diff --git a/.husky/commit-msg b/.husky/commit-msg old mode 100644 new mode 100755 diff --git a/.husky/pre-commit b/.husky/pre-commit old mode 100644 new mode 100755 diff --git a/.travis.yml b/.travis.yml index 4803664..370d9b1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: node_js node_js: - - 'stable' + - 17 before_script: - npm prune jobs: diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cb76a7..6559ac0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [1.0.1](https://github.com/ridvanaltun/commitlint-plugin-selective-scope/compare/v1.0.0...v1.0.1) (2022-12-22) + + +### Bug Fixes + +* correctly check for RegExp ([af6d426](https://github.com/ridvanaltun/commitlint-plugin-selective-scope/commit/af6d426cd08a3c7bbbc0a47f4e8ace13210ad8f0)) + # 1.0.0 (2021-09-30) diff --git a/__tests__/plugin.test.js b/__tests__/plugin.test.js index 08df168..c5cd94b 100644 --- a/__tests__/plugin.test.js +++ b/__tests__/plugin.test.js @@ -1,4 +1,15 @@ const commitlintPluginSelectiveScope = require('../index') +const commitlintPluginSelectiveScopeResolver = + commitlintPluginSelectiveScope.rules['selective-scope'] +// Wrap the resolver so we don't have to pass in "always" +const commitlintPluginSelectiveScopeResolverWrapped = (ctx, rules) => + commitlintPluginSelectiveScopeResolver(ctx, 'always', rules) + +const TEST_RULES = { + feat: [/^frontend\/[^/]+$/, 'backend'], + perf: [], + ci: [null, 'codebuild', 'jenkins'] +} describe('commitlintPluginSelectiveScope', () => { it('should return a valid config', () => { @@ -7,4 +18,111 @@ describe('commitlintPluginSelectiveScope', () => { Object.keys(commitlintPluginSelectiveScope.rules).length ).toBeGreaterThan(0) }) + + it('should not enforce scope if the type does not appear in the rules', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'frontend/web', type: 'fix' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'anything', type: 'fix' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: null, type: 'fix' }, + TEST_RULES + )[0] + ).toBe(true) + }) + + it('should not allow scope if the type appears in the rules with an empty array', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: null, type: 'perf' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'something', type: 'perf' }, + TEST_RULES + )[0] + ).toBe(false) + }) + + describe('should only allow scopes defined if the type appears in the rule with a non-empty array', () => { + it('should properly match a string literal', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'backend', type: 'feat' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'test', type: 'feat' }, + TEST_RULES + )[0] + ).toBe(false) + }) + + it('should properly match a RegExp', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'frontend/web', type: 'feat' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'frontend', type: 'feat' }, + TEST_RULES + )[0] + ).toBe(false) + }) + }) + + describe('optional scope', () => { + it('should allow scope to be optional if the type appears in the rules with a null in the array', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: null, type: 'ci' }, + TEST_RULES + )[0] + ).toBe(true) + }) + + it('should match scope if provided', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'codebuild', type: 'ci' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'jenkins', type: 'ci' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'github', type: 'ci' }, + TEST_RULES + )[0] + ).toBe(false) + }) + }) }) diff --git a/index.js b/index.js index 1e11218..25a54d2 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,7 @@ module.exports = { allowedScopes.findIndex(s => { if ( typeof ctx.scope === 'string' && - Object.prototype.toString.call() === '[object RegExp]' + Object.prototype.toString.call(s) === '[object RegExp]' ) { return ctx.scope.match(s) } diff --git a/package-lock.json b/package-lock.json index 939dc64..7f6f318 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "commitlint-plugin-selective-scope", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "commitlint-plugin-selective-scope", - "version": "1.0.0", + "version": "1.0.1", "license": "MIT", "devDependencies": { "@commitlint/cli": "^13.2.0", diff --git a/package.json b/package.json index d9e037a..22d361f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "commitlint-plugin-selective-scope", - "version": "1.0.0", + "version": "1.0.1", "description": "Limit scopes per type with regexp and plain text", "main": "index.js", "scripts": {