Skip to content

Commit 319221e

Browse files
committed
fix(#5): convert all kinds of file path to posix style
1 parent 558c188 commit 319221e

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

lib/rules/filename-naming-convention.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
getFilename,
99
getBasename,
1010
getPathFromRepositoryRoot,
11+
toPosixPath,
1112
} = require('../utils/filename');
1213
const {
1314
checkSettings,
@@ -70,8 +71,8 @@ module.exports = {
7071
}
7172

7273
const pathFromRepositoryRoot = getPathFromRepositoryRoot(
73-
context.getPhysicalFilename(),
74-
context.getCwd()
74+
toPosixPath(context.getPhysicalFilename()),
75+
toPosixPath(context.getCwd())
7576
);
7677
const filename = getFilename(pathFromRepositoryRoot);
7778

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

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

7-
const { getFolderPath, getFilename } = require('../utils/filename');
7+
const {
8+
getFolderPath,
9+
getFilename,
10+
toPosixPath,
11+
} = require('../utils/filename');
812
const { checkSettings, globPatternValidator } = require('../utils/settings');
913
const { getDocUrl } = require('../utils/doc');
1014
const { matchRule } = require('../utils/match');
@@ -55,7 +59,7 @@ module.exports = {
5559
return;
5660
}
5761

58-
const filenameWithPath = context.getPhysicalFilename();
62+
const filenameWithPath = toPosixPath(context.getPhysicalFilename());
5963
const filename = getFilename(filenameWithPath);
6064
const folderPath = getFolderPath(filenameWithPath);
6165

lib/rules/folder-naming-convention.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
getFolderPath,
1010
getSubPaths,
1111
getAllFolders,
12+
toPosixPath,
1213
} = require('../utils/filename');
1314
const {
1415
checkSettings,
@@ -62,7 +63,7 @@ module.exports = {
6263
return;
6364
}
6465

65-
const filenameWithPath = context.getPhysicalFilename();
66+
const filenameWithPath = toPosixPath(context.getPhysicalFilename());
6667
const folderPath = getFolderPath(filenameWithPath);
6768
const subPaths = getSubPaths(folderPath);
6869

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 } = require('../utils/filename');
8+
const { getFilename, getBasename, toPosixPath } = 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 = context.getPhysicalFilename();
29+
const filenameWithPath = toPosixPath(context.getPhysicalFilename());
3030
const filename = getFilename(filenameWithPath);
3131
const basename = getBasename(filename);
3232

lib/utils/filename.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ const path = require('path');
88

99
/**
1010
* @type {string} filename without path
11-
* @param {string} p filename concat with path
11+
* @param {string} p filename concat with path in posix style
1212
*/
13-
const getFilename = (p) => path.basename(p);
13+
const getFilename = (p) => path.posix.basename(p);
1414

1515
/**
1616
* @type {string} path of folder
17-
* @param {string} p filename concat with path
17+
* @param {string} p filename concat with path in posix style
1818
*/
19-
const getFolderPath = (p) => path.join(path.dirname(p), path.sep);
19+
const getFolderPath = (p) => path.join(path.posix.dirname(p), path.posix.sep);
2020

2121
/**
2222
* @type {string} base name
@@ -31,14 +31,14 @@ const getBasename = (filename, ignoreMiddleExtensions = false) =>
3131

3232
/**
3333
* @type {string[]} all folders
34-
* @param {string} p path of folder
34+
* @param {string} p path of folder in posix style
3535
*/
3636
const getAllFolders = (p) =>
37-
p.split(path.sep).filter((folder) => folder !== '');
37+
p.split(path.posix.sep).filter((folder) => folder !== '');
3838

3939
/**
4040
* @type {string[]} all sub paths
41-
* @param {string} p path of folder
41+
* @param {string} p path of folder in posix style
4242
*/
4343
const getSubPaths = (p) => {
4444
const folders = getAllFolders(p);
@@ -49,8 +49,8 @@ const getSubPaths = (p) => {
4949
if (folder) {
5050
acc.push(
5151
index === 0
52-
? path.join(folder, path.sep)
53-
: path.join(acc[acc.length - 1], folder, path.sep)
52+
? path.join(folder, path.posix.sep)
53+
: path.join(acc[acc.length - 1], folder, path.posix.sep)
5454
);
5555
}
5656
return acc;
@@ -65,11 +65,17 @@ const getSubPaths = (p) => {
6565

6666
/**
6767
* @type {string} path from repository root
68-
* @param {string} fullPath filename with full path
69-
* @param {string} repositoryRoot path of repository root
68+
* @param {string} fullPath filename with full path in posix style
69+
* @param {string} repositoryRoot path of repository root in posix style
7070
*/
7171
const getPathFromRepositoryRoot = (fullPath, repositoryRoot) =>
72-
fullPath.replace(path.join(repositoryRoot, path.sep), '');
72+
fullPath.replace(path.join(repositoryRoot, path.posix.sep), '');
73+
74+
/**
75+
* @type {string} file path in posix style
76+
* @param {string} p file path based on the operating system
77+
*/
78+
const toPosixPath = (p) => p.split(path.sep).join(path.posix.sep);
7379

7480
module.exports = {
7581
getFolderPath,
@@ -78,4 +84,5 @@ module.exports = {
7884
getSubPaths,
7985
getAllFolders,
8086
getPathFromRepositoryRoot,
87+
toPosixPath,
8188
};

0 commit comments

Comments
 (0)