Skip to content
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

Fix positioned keywords rule tests #113

Merged
merged 4 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions src/rules/v1/common/PredefinedFilenames.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)
};
Expand Down
135 changes: 121 additions & 14 deletions test/rules/v1/js/PositionedKeywords.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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(.*[@,.]/)'
}
]
};
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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`,
Expand All @@ -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,
Expand All @@ -140,7 +139,7 @@ describe('invoke function', () => {
`+const {`,
`+ method2`,
`+ method3`,
`+} = require('@/helpers/methods2')`,
`+} = require('./helpers/methods2')`,
`+const method1 = require('@/helpers/methods');`
]
}
Expand All @@ -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,
Expand All @@ -164,7 +163,7 @@ describe('invoke function', () => {
`+const {`,
`+ method2`,
`+ method3`,
`+} = require('@/helpers/methods2')`,
`+} = require('./helpers/methods2')`,
`+`,
`+const method4 = require('package2');`
]
Expand All @@ -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);
});
});
55 changes: 27 additions & 28 deletions test/rules/v1/vue/PositionedKeywords.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<script>',
regex: /<script>/
},
BOF: false
},
regex: /import.*(?:from|{)/,
maxLineBreaks: 0,
enforced: true,
breakOnFirstOccurence: false,
Expand All @@ -34,30 +27,36 @@ const importKeywordCustomConfig = {
startsWith: '} from'
}
}
],
order: [
{
name: 'packages',
regex: /from(?!.*[@,.]\/)/
},
{
name: 'others',
regex: /from.*[@,.]\//
}
]
};

const importKeywordBOFConfig = {
name: 'import',
regex: /import.*/,
const importKeywordCustomConfig = {
...importKeywordBaseConfig,
position: {
custom: null,
BOF: true
},
maxLineBreaks: 0,
enforced: true,
breakOnFirstOccurence: false,
countDifferentCodeAsLineBreak: false,
multiLineOptions: [
{
indicator: {
notIncludes: 'from'
},
limiter: {
startsWith: '} from'
}
BOF: false,
custom: {
name: '<script>',
regex: '<script>'
}
]
}
};

const importKeywordBOFConfig = {
...importKeywordBaseConfig,
position: {
BOF: true,
custom: null
}
};

const requireKeywordConfig = (position, override = null) => {
Expand Down