Skip to content

Commit 5861315

Browse files
committed
fix(#5): add getFilePath method to get file path
1 parent 319221e commit 5861315

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

lib/rules/filename-naming-convention.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
*/
55
'use strict';
66

7-
const {
8-
getFilename,
9-
getBasename,
10-
getPathFromRepositoryRoot,
11-
toPosixPath,
12-
} = require('../utils/filename');
7+
const { getFilename, getBasename, getFilePath } = require('../utils/filename');
138
const {
149
checkSettings,
1510
namingPatternValidator,
@@ -70,16 +65,13 @@ module.exports = {
7065
return;
7166
}
7267

73-
const pathFromRepositoryRoot = getPathFromRepositoryRoot(
74-
toPosixPath(context.getPhysicalFilename()),
75-
toPosixPath(context.getCwd())
76-
);
77-
const filename = getFilename(pathFromRepositoryRoot);
68+
const filenameWithPath = getFilePath(context);
69+
const filename = getFilename(filenameWithPath);
7870

7971
for (const [fexPattern, namingPattern] of Object.entries(rules)) {
8072
const matchResult =
8173
matchRule(
82-
pathFromRepositoryRoot,
74+
filenameWithPath,
8375
fexPattern,
8476
getBasename(filename, ignoreMiddleExtensions),
8577
namingPattern

lib/rules/folder-match-with-fex.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const {
88
getFolderPath,
99
getFilename,
10-
toPosixPath,
10+
getFilePath,
1111
} = require('../utils/filename');
1212
const { checkSettings, globPatternValidator } = require('../utils/settings');
1313
const { getDocUrl } = require('../utils/doc');
@@ -59,7 +59,7 @@ module.exports = {
5959
return;
6060
}
6161

62-
const filenameWithPath = toPosixPath(context.getPhysicalFilename());
62+
const filenameWithPath = getFilePath(context);
6363
const filename = getFilename(filenameWithPath);
6464
const folderPath = getFolderPath(filenameWithPath);
6565

lib/rules/folder-naming-convention.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99
getFolderPath,
1010
getSubPaths,
1111
getAllFolders,
12-
toPosixPath,
12+
getFilePath,
1313
} = require('../utils/filename');
1414
const {
1515
checkSettings,
@@ -63,7 +63,7 @@ module.exports = {
6363
return;
6464
}
6565

66-
const filenameWithPath = toPosixPath(context.getPhysicalFilename());
66+
const filenameWithPath = getFilePath(context);
6767
const folderPath = getFolderPath(filenameWithPath);
6868
const subPaths = getSubPaths(folderPath);
6969

lib/rules/no-index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'use strict';
66

77
const { getDocUrl } = require('../utils/doc');
8-
const { getFilename, getBasename, toPosixPath } = require('../utils/filename');
8+
const { getFilename, getBasename, getFilePath } = require('../utils/filename');
99

1010
/**
1111
* @type {import('eslint').Rule.RuleModule}
@@ -26,7 +26,7 @@ module.exports = {
2626
create(context) {
2727
return {
2828
Program: (node) => {
29-
const filenameWithPath = toPosixPath(context.getPhysicalFilename());
29+
const filenameWithPath = getFilePath(context);
3030
const filename = getFilename(filenameWithPath);
3131
const basename = getBasename(filename);
3232

lib/utils/filename.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,56 @@ const getSubPaths = (p) => {
6565

6666
/**
6767
* @type {string} path from repository root
68-
* @param {string} fullPath filename with full path in posix style
69-
* @param {string} repositoryRoot path of repository root in posix style
68+
* @param {string} fullPath filename with full path
69+
* @param {string} repositoryRoot path of repository root
7070
*/
7171
const getPathFromRepositoryRoot = (fullPath, repositoryRoot) =>
72-
fullPath.replace(path.join(repositoryRoot, path.posix.sep), '');
72+
fullPath.replace(path.join(repositoryRoot, path.sep), '');
7373

7474
/**
7575
* @type {string} file path in posix style
7676
* @param {string} p file path based on the operating system
7777
*/
7878
const toPosixPath = (p) => p.split(path.sep).join(path.posix.sep);
7979

80+
/**
81+
* @type {string} file path without drive letter on windows
82+
* @param {string} p file path on windows
83+
*/
84+
const removeDriveLetter = (p) => p.replace(/^[A-Za-z]:\\/, '');
85+
86+
/**
87+
* Callback for file path
88+
* @callback callback
89+
* @param {string} p file path
90+
*/
91+
/**
92+
* @type {callback} piped function
93+
* @param {callback[]} fns callback functions
94+
*/
95+
const pipe =
96+
(...fns) =>
97+
(x) =>
98+
fns.reduce((v, f) => f(v), x);
99+
100+
/**
101+
* @type {string} file path in posix style
102+
* @param {Rule.RuleContext} context rule eslint context
103+
*/
104+
const getFilePath = (context) => {
105+
const pathFromRoot = getPathFromRepositoryRoot(
106+
context.getPhysicalFilename(),
107+
context.getCwd()
108+
);
109+
110+
return pipe(removeDriveLetter, toPosixPath)(pathFromRoot);
111+
};
112+
80113
module.exports = {
81114
getFolderPath,
82115
getFilename,
83116
getBasename,
84117
getSubPaths,
85118
getAllFolders,
86-
getPathFromRepositoryRoot,
87-
toPosixPath,
119+
getFilePath,
88120
};

0 commit comments

Comments
 (0)