Skip to content

Commit 9d93159

Browse files
committed
fix(tools): new-rule.ts cross platform path handling
1 parent 8cb54c6 commit 9d93159

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

packages/eslint-plugin-svelte/tools/new-rule.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import path from 'node:path';
22
import fs from 'node:fs';
33
import cp from 'node:child_process';
4-
import url from 'node:url';
54
import { writeAndFormat } from './lib/write.js';
6-
7-
const __filename = url.fileURLToPath(import.meta.url);
8-
const __dirname = path.dirname(__filename);
5+
import { fileURLToPath } from 'node:url';
96

107
const logger = console;
118

@@ -21,39 +18,39 @@ void (async ([ruleId, ...args]) => {
2118
process.exitCode = 1;
2219
return;
2320
}
24-
const utilsPath = path.resolve(__dirname, `../src/utils/index.ts`);
25-
const testUtilsPath = path.resolve(__dirname, `../tests/utils/utils.ts`);
21+
const utilsURL = new URL(`../src/utils/index.ts`, import.meta.url);
22+
const testUtilsURL = new URL(`../tests/utils/utils.ts`, import.meta.url);
2623

27-
const ruleFile = path.resolve(__dirname, `../src/rules/${ruleId}.ts`);
28-
const testFile = path.resolve(__dirname, `../tests/src/rules/${ruleId}.ts`);
29-
const docFile = path.resolve(__dirname, `../../../docs/rules/${ruleId}.md`);
30-
const fixturesRoot = path.resolve(__dirname, `../tests/fixtures/rules/${ruleId}/`);
24+
const ruleFileURL = new URL(`../src/rules/${ruleId}.ts`, import.meta.url);
25+
const testFileURL = new URL(`../tests/src/rules/${ruleId}.ts`, import.meta.url);
26+
const docFileURL = new URL(`../../../docs/rules/${ruleId}.md`, import.meta.url);
27+
const fixturesRootURL = new URL(`../tests/fixtures/rules/${ruleId}/`, import.meta.url);
3128
try {
32-
fs.mkdirSync(path.dirname(ruleFile), { recursive: true });
29+
fs.mkdirSync(new URL('./', ruleFileURL), { recursive: true });
3330
} catch {
3431
// ignore
3532
}
3633
try {
37-
fs.mkdirSync(path.dirname(testFile), { recursive: true });
34+
fs.mkdirSync(new URL('./', testFileURL), { recursive: true });
3835
} catch {
3936
// ignore
4037
}
4138
try {
42-
fs.mkdirSync(path.dirname(docFile), { recursive: true });
39+
fs.mkdirSync(new URL('./', docFileURL), { recursive: true });
4340
} catch {
4441
// ignore
4542
}
4643
try {
47-
fs.mkdirSync(path.resolve(fixturesRoot, 'valid'), { recursive: true });
48-
fs.mkdirSync(path.resolve(fixturesRoot, 'invalid'), { recursive: true });
44+
fs.mkdirSync(new URL('./valid', fixturesRootURL), { recursive: true });
45+
fs.mkdirSync(new URL('./invalid', fixturesRootURL), { recursive: true });
4946
} catch {
5047
// ignore
5148
}
5249

5350
await writeAndFormat(
54-
ruleFile,
51+
ruleFileURL,
5552
`import { AST } from 'svelte-eslint-parser';
56-
import { createRule } from '${getModulePath(ruleFile, utilsPath)}';
53+
import { createRule } from '${getModulePath(ruleFileURL, utilsURL)}';
5754
5855
export default createRule('${ruleId}', {
5956
meta: {
@@ -74,10 +71,10 @@ export default createRule('${ruleId}', {
7471
`
7572
);
7673
await writeAndFormat(
77-
testFile,
74+
testFileURL,
7875
`import { RuleTester } from '../../utils/eslint-compat.js';
79-
import rule from '${getModulePath(testFile, ruleFile)}';
80-
import { loadTestCases } from '${getModulePath(testFile, testUtilsPath)}';
76+
import rule from '${getModulePath(testFileURL, ruleFileURL)}';
77+
import { loadTestCases } from '${getModulePath(testFileURL, testUtilsURL)}';
8178
8279
const tester = new RuleTester({
8380
languageOptions: {
@@ -90,7 +87,7 @@ tester.run('${ruleId}', rule as any, loadTestCases('${ruleId}'));
9087
`
9188
);
9289
await writeAndFormat(
93-
docFile,
90+
docFileURL,
9491
`# (svelte/${ruleId})
9592
9693
> description
@@ -139,17 +136,20 @@ This rule reports ???.
139136
try {
140137
// Use code -v to know if vscode is installed and do not print anything to the console
141138
cp.execSync('code -v', { stdio: 'ignore' });
142-
cp.execSync(`code "${ruleFile}"`);
143-
cp.execSync(`code "${testFile}"`);
144-
cp.execSync(`code "${docFile}"`);
139+
cp.execSync(`code "${fileURLToPath(ruleFileURL)}"`);
140+
cp.execSync(`code "${fileURLToPath(testFileURL)}"`);
141+
cp.execSync(`code "${fileURLToPath(docFileURL)}"`);
145142
} catch {
146143
logger.error('Unable to find code command. Will not open files with VSCode.');
147144
}
148145
})(process.argv.slice(2));
149146

150147
/** Get module path */
151-
function getModulePath(from: string, module: string): string {
152-
return path.relative(path.dirname(from), module).replace(/.ts$/u, '.js');
148+
function getModulePath(from: URL, module: URL): string {
149+
const fromDir = fileURLToPath(new URL('./', from));
150+
const modulePath = fileURLToPath(module);
151+
152+
return path.relative(fromDir, modulePath).replace(/\\/g, '/').replace(/.ts$/u, '.js');
153153
}
154154

155155
/** Argument parsing */

0 commit comments

Comments
 (0)