diff --git a/src/rules/v1/common/PredefinedFilenames.js b/src/rules/v1/common/PredefinedFilenames.js index 2c8e3e5..0018f6f 100644 --- a/src/rules/v1/common/PredefinedFilenames.js +++ b/src/rules/v1/common/PredefinedFilenames.js @@ -35,10 +35,6 @@ class PredefinedFilenamesRule extends BaseRule { let isFilenameMatched = false; const isPathWithAsterisk = path.endsWith('*'); - const parsedExpectedName = isString(expectedName) - ? new RegExp(expectedName) - : expectedName; - if (isPathWithAsterisk) { const fixedPath = path.slice(0, -1); @@ -52,9 +48,13 @@ class PredefinedFilenamesRule extends BaseRule { } const rawFilename = filename.split('/').pop(); - const isFilenameValid = rawFilename.match(parsedExpectedName); + const isFilenameValid = rawFilename.match(expectedName); if (isFilenameMatched && !isFilenameValid) { + const parsedExpectedName = isString(expectedName) + ? new RegExp(expectedName) + : expectedName; + return { body: this._getCommentBody(filename, parsedExpectedName) }; diff --git a/test/rules/v1/js/PositionedKeywords.test.js b/test/rules/v1/js/PositionedKeywords.test.js index 7944422..7d2249f 100644 --- a/test/rules/v1/js/PositionedKeywords.test.js +++ b/test/rules/v1/js/PositionedKeywords.test.js @@ -13,13 +13,13 @@ const PositionedKeywordsRule = require('src/rules/v1/common/PositionedKeywords') const requireKeywordBOFConfig = { name: 'require', - regex: /const.*(?:require|{)/, + regex: 'const.*(?:require|{)', position: { BOF: true, custom: null }, - enforced: true, maxLineBreaks: 0, + enforced: true, breakOnFirstOccurence: false, countDifferentCodeAsLineBreak: false, multiLineOptions: [ @@ -28,19 +28,18 @@ const requireKeywordBOFConfig = { notIncludes: 'require' }, limiter: { - startsWith: '} = require', - indentation: 'eq-indicator' + startsWith: '} = require' } } ], order: [ { name: 'packages', - regex: /require(?!.*@).*/ + regex: 'require(?!.*[@,.]/)' }, { - name: 'other', - regex: /require.*/ + name: 'others', + regex: 'require(.*[@,.]/)' } ] }; @@ -71,7 +70,7 @@ describe('invoke function', () => { * --------------------------------------------------- */ - it('returns empty array on valid require BOF position', () => { + it('returns empty array on valid require BOF position + order', () => { const positionedKeywordsRule = new PositionedKeywordsRule( patchronContext, validConfig, @@ -96,7 +95,7 @@ describe('invoke function', () => { expect(result).toEqual([]); }); - it('returns review on invalid require BOF position', () => { + it('returns review on invalid require BOF position + order', () => { const positionedKeywordsRule = new PositionedKeywordsRule( patchronContext, validConfig, @@ -106,7 +105,7 @@ describe('invoke function', () => { `@@ -10,13 +1,7 @@`, `-removed line`, `+some code`, - `+const method1 = require('@/helpers/methods');`, + `+const method1 = require('../helpers/methods');`, `-`, `+const {`, `+ method2`, @@ -127,7 +126,7 @@ describe('invoke function', () => { expect(result[1]).toHaveProperty('line', 8); }); - it('returns empty array on valid require BOF position (enforced)', () => { + it('returns empty array on valid require BOF position + order (enforced)', () => { const positionedKeywordsRule = new PositionedKeywordsRule( patchronContext, validConfig, @@ -140,7 +139,7 @@ describe('invoke function', () => { `+const {`, `+ method2`, `+ method3`, - `+} = require('@/helpers/methods2')`, + `+} = require('./helpers/methods2')`, `+const method1 = require('@/helpers/methods');` ] } @@ -151,7 +150,7 @@ describe('invoke function', () => { expect(result).toEqual([]); }); - it('returns review on invalid require BOF position (enforced)', () => { + it('returns review on invalid require BOF position + order (enforced)', () => { const positionedKeywordsRule = new PositionedKeywordsRule( patchronContext, validConfig, @@ -164,7 +163,7 @@ describe('invoke function', () => { `+const {`, `+ method2`, `+ method3`, - `+} = require('@/helpers/methods2')`, + `+} = require('./helpers/methods2')`, `+`, `+const method4 = require('package2');` ] @@ -180,4 +179,112 @@ describe('invoke function', () => { expect(result[1]).toHaveProperty('line', 11); }); + + it('returns empty array on valid require order (example1, enforced)', () => { + const positionedKeywordsRule = new PositionedKeywordsRule( + patchronContext, + validConfig, + { + ...file, + splitPatch: [ + `@@ -10,13 +5,7 @@`, + `+const method4 = require('package2');`, + `+const {`, + `+ method2`, + `+ method3`, + `+} = require('./helpers/methods2')`, + `+const method1 = require('@/helpers/methods');` + ] + } + ); + + const result = positionedKeywordsRule.invoke(); + + expect(result).toEqual([]); + }); + + it('returns review on invalid require order (example1, enforced)', () => { + const positionedKeywordsRule = new PositionedKeywordsRule( + patchronContext, + validConfig, + { + ...file, + splitPatch: [ + `@@ -10,13 +5,7 @@`, + `+const {`, + `+ method2`, + `+ method3`, + `+} = require('./helpers/methods2')`, + `+const method1 = require('@/helpers/methods');`, + `+const method4 = require('package2');` + ] + } + ); + + const result = positionedKeywordsRule.invoke(); + + expect(result).toHaveLength(1); + + expect(result[0]).toHaveProperty('line', 10); + }); + + it('returns empty array on valid require order (example2, enforced)', () => { + const positionedKeywordsRule = new PositionedKeywordsRule( + patchronContext, + validConfig, + { + ...file, + splitPatch: [ + `@@ -10,13 +5,7 @@`, + `+const {`, + `+ method5`, + `+ method6`, + `+} = require('package3')`, + `+const method4 = require('package2');`, + `+const method7 = require('package3/index');`, + `+const {`, + `+ method2`, + `+ method3`, + `+} = require('./helpers/methods2')`, + `+const method1 = require('@/helpers/methods');` + ] + } + ); + + const result = positionedKeywordsRule.invoke(); + + expect(result).toEqual([]); + }); + + it('returns review on invalid require order (example2, enforced)', () => { + const positionedKeywordsRule = new PositionedKeywordsRule( + patchronContext, + validConfig, + { + ...file, + splitPatch: [ + `@@ -10,13 +5,7 @@`, + `+const method7 = require('package3/index');`, + `+const {`, + `+ method2`, + `+ method3`, + `+} = require('./helpers/methods2')`, + `+const method1 = require('@/helpers/methods');`, + `+const {`, + `+ method5`, + `+ method6`, + `+} = require('package3')`, + `+const method4 = require('package2');` + ] + } + ); + + const result = positionedKeywordsRule.invoke(); + + expect(result).toHaveLength(2); + + expect(result[0]).toHaveProperty('line', 11); + + expect(result[1]).toHaveProperty('line', 15); + }); }); diff --git a/test/rules/v1/vue/PositionedKeywords.test.js b/test/rules/v1/vue/PositionedKeywords.test.js index 840d4ae..49efeb0 100644 --- a/test/rules/v1/vue/PositionedKeywords.test.js +++ b/test/rules/v1/vue/PositionedKeywords.test.js @@ -11,16 +11,9 @@ const setupPatchronContext = require('test/setupPatchronContext'); const initializeFile = require('test/rules/helpers/initializeFile'); const PositionedKeywordsRule = require('src/rules/v1/common/PositionedKeywords'); -const importKeywordCustomConfig = { +const importKeywordBaseConfig = { name: 'import', - regex: /import.*/, - position: { - custom: { - name: '