Skip to content

Commit 153959f

Browse files
authored
feat: Tolerate string literals w/ ppath.join / ppath.resolve (#5250)
* Allows generic as parameters for ppath.join / ppath.resolve * Removes "as" from callsites * Versions * Fixes lint * Replaces "(join\(.*`) as Filename" * Fixes lint
1 parent 575d2f2 commit 153959f

File tree

68 files changed

+564
-508
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+564
-508
lines changed

.yarn/versions/90a7a735.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
releases:
2+
"@yarnpkg/fslib": minor
3+
4+
declined:
5+
- "@yarnpkg/plugin-compat"
6+
- "@yarnpkg/plugin-constraints"
7+
- "@yarnpkg/plugin-dlx"
8+
- "@yarnpkg/plugin-essentials"
9+
- "@yarnpkg/plugin-exec"
10+
- "@yarnpkg/plugin-file"
11+
- "@yarnpkg/plugin-git"
12+
- "@yarnpkg/plugin-github"
13+
- "@yarnpkg/plugin-init"
14+
- "@yarnpkg/plugin-link"
15+
- "@yarnpkg/plugin-nm"
16+
- "@yarnpkg/plugin-npm"
17+
- "@yarnpkg/plugin-npm-cli"
18+
- "@yarnpkg/plugin-pack"
19+
- "@yarnpkg/plugin-patch"
20+
- "@yarnpkg/plugin-pnp"
21+
- "@yarnpkg/plugin-pnpm"
22+
- "@yarnpkg/plugin-stage"
23+
- "@yarnpkg/plugin-typescript"
24+
- "@yarnpkg/plugin-version"
25+
- "@yarnpkg/plugin-workspace-tools"
26+
- vscode-zipfs
27+
- "@yarnpkg/builder"
28+
- "@yarnpkg/cli"
29+
- "@yarnpkg/core"
30+
- "@yarnpkg/doctor"
31+
- "@yarnpkg/libzip"
32+
- "@yarnpkg/nm"
33+
- "@yarnpkg/pnp"
34+
- "@yarnpkg/pnpify"
35+
- "@yarnpkg/sdks"
36+
- "@yarnpkg/shell"

packages/acceptance-tests/pkg-tests-core/sources/utils/tests.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import {PortablePath, npath, toFilename, xfs, ppath, Filename} from '@yarnpkg/fslib';
2-
import assert from 'assert';
3-
import crypto from 'crypto';
4-
import finalhandler from 'finalhandler';
5-
import https from 'https';
6-
import {IncomingMessage, ServerResponse} from 'http';
7-
import http from 'http';
8-
import invariant from 'invariant';
9-
import {AddressInfo} from 'net';
10-
import os from 'os';
11-
import pem from 'pem';
12-
import semver from 'semver';
13-
import serveStatic from 'serve-static';
14-
import {promisify} from 'util';
15-
import {v5 as uuidv5} from 'uuid';
16-
import {Gzip} from 'zlib';
17-
18-
import {ExecResult} from './exec';
19-
import * as fsUtils from './fs';
1+
import {PortablePath, npath, toFilename, xfs, ppath} from '@yarnpkg/fslib';
2+
import assert from 'assert';
3+
import crypto from 'crypto';
4+
import finalhandler from 'finalhandler';
5+
import https from 'https';
6+
import {IncomingMessage, ServerResponse} from 'http';
7+
import http from 'http';
8+
import invariant from 'invariant';
9+
import {AddressInfo} from 'net';
10+
import os from 'os';
11+
import pem from 'pem';
12+
import semver from 'semver';
13+
import serveStatic from 'serve-static';
14+
import {promisify} from 'util';
15+
import {v5 as uuidv5} from 'uuid';
16+
import {Gzip} from 'zlib';
17+
18+
import {ExecResult} from './exec';
19+
import * as fsUtils from './fs';
2020

2121
const deepResolve = require(`super-resolve`);
2222
const staticServer = serveStatic(npath.fromPortablePath(require(`pkg-tests-fixtures`)));
@@ -675,7 +675,7 @@ export const generatePkgDriver = ({
675675
return Object.assign(async (): Promise<void> => {
676676
const homePath = await xfs.mktempPromise();
677677

678-
const path = ppath.join(homePath, `test` as Filename);
678+
const path = ppath.join(homePath, `test`);
679679
await xfs.mkdirPromise(path);
680680

681681
const registryUrl = await startPackageServer();

packages/acceptance-tests/pkg-tests-core/sources/utils/yarn.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ export async function writePackage(dir: PortablePath, manifest: {[key: string]:
2626
}
2727

2828
export function getPluginPath(dir: PortablePath, name: string) {
29-
return ppath.join(dir, `.yarn/plugins/${name}.cjs` as PortablePath);
29+
return ppath.join(dir, `.yarn/plugins/${name}.cjs`);
3030
}

packages/acceptance-tests/pkg-tests-specs/sources/commands/create.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import {xfs, ppath, Filename} from '@yarnpkg/fslib';
1+
import {xfs, ppath} from '@yarnpkg/fslib';
22

33
describe(`Commands`, () => {
44
describe(`create`, () => {
55
test(
66
`it should generate \`hello.txt\` correctly using a starter-kit-package`,
77
makeTemporaryEnv({}, async ({path, run, source}) => {
88
await run(`create`, `-q`, `test-app`);
9-
expect(xfs.readFileSync(ppath.join(path, `hello.txt` as Filename), `utf8`)).toEqual(`Hello World`);
9+
expect(xfs.readFileSync(ppath.join(path, `hello.txt`), `utf8`)).toEqual(`Hello World`);
1010
}),
1111
);
1212

1313
test(
1414
`it should generate \`hello.txt\` correctly using a scoped starter-kit-package`,
1515
makeTemporaryEnv({}, async ({path, run, source}) => {
1616
await run(`create`, `-q`, `@scoped/test-app`);
17-
expect(xfs.readFileSync(ppath.join(path, `hello.txt` as Filename), `utf8`)).toEqual(`Hello World`);
17+
expect(xfs.readFileSync(ppath.join(path, `hello.txt`), `utf8`)).toEqual(`Hello World`);
1818
}),
1919
);
2020

packages/acceptance-tests/pkg-tests-specs/sources/commands/exec.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Filename, npath, PortablePath, ppath, xfs} from '@yarnpkg/fslib';
1+
import {Filename, npath, ppath, xfs} from '@yarnpkg/fslib';
22

33
describe(`Commands`, () => {
44
describe(`exec`, () => {
@@ -27,9 +27,9 @@ describe(`Commands`, () => {
2727
test(
2828
`it should inject binaries the workspace has access to`,
2929
makeTemporaryEnv({}, async ({path, run, source}) => {
30-
await xfs.mkdirPromise(ppath.join(path, `bin` as Filename));
31-
await xfs.writeFilePromise(ppath.join(path, `bin/index.js` as PortablePath), `console.log(42)`);
32-
await xfs.writeJsonPromise(ppath.join(path, `bin` as Filename, Filename.manifest), {
30+
await xfs.mkdirPromise(ppath.join(path, `bin`));
31+
await xfs.writeFilePromise(ppath.join(path, `bin/index.js`), `console.log(42)`);
32+
await xfs.writeJsonPromise(ppath.join(path, `bin`, Filename.manifest), {
3333
name: `bin`,
3434
bin: `./index.js`,
3535
});

packages/acceptance-tests/pkg-tests-specs/sources/commands/info.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {PortablePath, ppath, xfs} from '@yarnpkg/fslib';
1+
import {ppath, xfs} from '@yarnpkg/fslib';
22

33
describe(`Commands`, () => {
44
describe(`info`, () => {
@@ -68,8 +68,8 @@ describe(`Commands`, () => {
6868
`workspace`,
6969
],
7070
}, async ({path, run, source}) => {
71-
await xfs.mkdirpPromise(ppath.join(path, `workspace` as PortablePath));
72-
await xfs.writeJsonPromise(ppath.join(path, `workspace/package.json` as PortablePath), {
71+
await xfs.mkdirpPromise(ppath.join(path, `workspace`));
72+
await xfs.writeJsonPromise(ppath.join(path, `workspace/package.json`), {
7373
dependencies: {
7474
[`no-deps`]: `1.0.0`,
7575
},
@@ -88,8 +88,8 @@ describe(`Commands`, () => {
8888
`workspace`,
8989
],
9090
}, async ({path, run, source}) => {
91-
await xfs.mkdirpPromise(ppath.join(path, `workspace` as PortablePath));
92-
await xfs.writeJsonPromise(ppath.join(path, `workspace/package.json` as PortablePath), {
91+
await xfs.mkdirpPromise(ppath.join(path, `workspace`));
92+
await xfs.writeJsonPromise(ppath.join(path, `workspace/package.json`), {
9393
dependencies: {
9494
[`no-deps`]: `1.0.0`,
9595
},

packages/acceptance-tests/pkg-tests-specs/sources/commands/init.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {Filename, PortablePath, ppath, xfs} from '@yarnpkg/fslib';
1+
import {Filename, ppath, xfs} from '@yarnpkg/fslib';
22

33
describe(`Commands`, () => {
44
describe(`init`, () => {
55
test(
66
`it should create a new package.json in the local directory if it doesn't exist`,
77
makeTemporaryEnv({}, async ({path, run, source}) => {
88
await xfs.mktempPromise(async tmpDir => {
9-
const pkgDir = ppath.join(tmpDir, `my-package` as PortablePath);
9+
const pkgDir = ppath.join(tmpDir, `my-package`);
1010
await xfs.mkdirpPromise(pkgDir);
1111

1212
await run(`init`, {
@@ -24,7 +24,7 @@ describe(`Commands`, () => {
2424
`it should create a new package.json in the specified directory if it doesn't exist`,
2525
makeTemporaryEnv({}, async ({path, run, source}) => {
2626
await xfs.mktempPromise(async tmpDir => {
27-
const pkgDir = ppath.join(tmpDir, `my-package` as PortablePath);
27+
const pkgDir = ppath.join(tmpDir, `my-package`);
2828
await xfs.mkdirpPromise(pkgDir);
2929

3030
await run(`./my-package`, `init`, {
@@ -42,7 +42,7 @@ describe(`Commands`, () => {
4242
`it should create a new package.json in the specified directory even if said directory doesn't exist`,
4343
makeTemporaryEnv({}, async ({path, run, source}) => {
4444
await xfs.mktempPromise(async tmpDir => {
45-
const pkgDir = ppath.join(tmpDir, `my-package` as PortablePath);
45+
const pkgDir = ppath.join(tmpDir, `my-package`);
4646

4747
await run(`./my-package`, `init`, {
4848
cwd: tmpDir,
@@ -59,14 +59,14 @@ describe(`Commands`, () => {
5959
`it should copy the currently running bundle when using --install`,
6060
makeTemporaryEnv({}, async ({path, run, source}) => {
6161
await xfs.mktempPromise(async tmpDir => {
62-
const pkgDir = ppath.join(tmpDir, `my-package` as PortablePath);
62+
const pkgDir = ppath.join(tmpDir, `my-package`);
6363
await xfs.mkdirpPromise(pkgDir);
6464

6565
await run(`init`, `--install=self`, {
6666
cwd: pkgDir,
6767
});
6868

69-
await expect(xfs.existsPromise(ppath.join(pkgDir, `.yarn/releases` as PortablePath))).resolves.toEqual(true);
69+
await expect(xfs.existsPromise(ppath.join(pkgDir, `.yarn/releases`))).resolves.toEqual(true);
7070
});
7171
}),
7272
);

packages/acceptance-tests/pkg-tests-specs/sources/commands/patch.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Filename, npath, ppath, xfs} from '@yarnpkg/fslib';
1+
import {npath, ppath, xfs} from '@yarnpkg/fslib';
22

33
describe(`Commands`, () => {
44
describe(`patch`, () => {
@@ -25,7 +25,7 @@ describe(`Commands`, () => {
2525
const {path: updateFolderN} = JSON.parse(stdout);
2626

2727
const updateFolder = npath.toPortablePath(updateFolderN);
28-
const updateFile = ppath.join(updateFolder, `foo.js` as Filename);
28+
const updateFile = ppath.join(updateFolder, `foo.js`);
2929

3030
const fileUser = `module.exports = 'foo';\n`;
3131
await xfs.writeFilePromise(updateFile, fileUser);
@@ -42,7 +42,7 @@ describe(`Commands`, () => {
4242
const {path: updateFolderN} = JSON.parse(stdout);
4343

4444
const updateFolder = npath.toPortablePath(updateFolderN);
45-
const updateFile = ppath.join(updateFolder, `bar.js` as Filename);
45+
const updateFile = ppath.join(updateFolder, `bar.js`);
4646

4747
const fileUser = `module.exports = 'bar';\n`;
4848
await xfs.writeFilePromise(updateFile, fileUser);
@@ -79,7 +79,7 @@ describe(`Commands`, () => {
7979
const {path: updateFolderN} = JSON.parse(stdout);
8080

8181
const updateFolder = npath.toPortablePath(updateFolderN);
82-
const updateFile = ppath.join(updateFolder, `foo.js` as Filename);
82+
const updateFile = ppath.join(updateFolder, `foo.js`);
8383

8484
const fileUser = `module.exports = 'foo';\n`;
8585
await xfs.writeFilePromise(updateFile, fileUser);
@@ -96,7 +96,7 @@ describe(`Commands`, () => {
9696
const {path: updateFolderN} = JSON.parse(stdout);
9797

9898
const updateFolder = npath.toPortablePath(updateFolderN);
99-
const updateFile = ppath.join(updateFolder, `bar.js` as Filename);
99+
const updateFile = ppath.join(updateFolder, `bar.js`);
100100

101101
const fileUser = `module.exports = 'bar';\n`;
102102
await xfs.writeFilePromise(updateFile, fileUser);

packages/acceptance-tests/pkg-tests-specs/sources/commands/patchCommit.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe(`Commands`, () => {
1515
const {path: updateFolderN} = JSON.parse(stdout);
1616

1717
const updateFolder = npath.toPortablePath(updateFolderN);
18-
const updateFile = ppath.join(updateFolder, `index.js` as Filename);
18+
const updateFile = ppath.join(updateFolder, `index.js`);
1919

2020
const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
2121
const fileUser = fileSource.replace(`require(dep)`, `42`);
@@ -38,7 +38,7 @@ describe(`Commands`, () => {
3838
const {path: updateFolderN} = JSON.parse(stdout);
3939

4040
const updateFolder = npath.toPortablePath(updateFolderN);
41-
const updateFile = ppath.join(updateFolder, `index.js` as Filename);
41+
const updateFile = ppath.join(updateFolder, `index.js`);
4242

4343
const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
4444
const fileUser = fileSource.replace(`require(dep)`, `42`);
@@ -68,7 +68,7 @@ describe(`Commands`, () => {
6868
const {path: updateFolderN} = JSON.parse(stdout);
6969

7070
const updateFolder = npath.toPortablePath(updateFolderN);
71-
const updateFile = ppath.join(updateFolder, `index.js` as Filename);
71+
const updateFile = ppath.join(updateFolder, `index.js`);
7272

7373
const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
7474
const fileUser = fileSource.replace(`require(dep)`, `42`);
@@ -99,7 +99,7 @@ describe(`Commands`, () => {
9999
const {path: updateFolderN} = JSON.parse(stdout);
100100

101101
const updateFolder = npath.toPortablePath(updateFolderN);
102-
const updateFile = ppath.join(updateFolder, `index.js` as Filename);
102+
const updateFile = ppath.join(updateFolder, `index.js`);
103103

104104
const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
105105
const fileUser = fileSource.replace(`require(dep)`, `42`);
@@ -133,7 +133,7 @@ describe(`Commands`, () => {
133133
const {path: updateFolderN} = JSON.parse(stdout);
134134

135135
const updateFolder = npath.toPortablePath(updateFolderN);
136-
const updateFile = ppath.join(updateFolder, `new.js` as Filename);
136+
const updateFile = ppath.join(updateFolder, `new.js`);
137137

138138
const fileUser = `module.exports = 42;\n`;
139139
await xfs.writeFilePromise(updateFile, fileUser);
@@ -152,7 +152,7 @@ describe(`Commands`, () => {
152152
const {path: updateFolderN} = JSON.parse(stdout);
153153

154154
const updateFolder = npath.toPortablePath(updateFolderN);
155-
const updateFile = ppath.join(updateFolder, `new.js` as Filename);
155+
const updateFile = ppath.join(updateFolder, `new.js`);
156156

157157
const fileUser = `module.exports = 21;\n`;
158158
await xfs.writeFilePromise(updateFile, fileUser);
@@ -181,7 +181,7 @@ describe(`Commands`, () => {
181181
const {path: updateFolderN} = JSON.parse(stdout);
182182

183183
const updateFolder = npath.toPortablePath(updateFolderN);
184-
const updateFile = ppath.join(updateFolder, `index.js` as Filename);
184+
const updateFile = ppath.join(updateFolder, `index.js`);
185185

186186
const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
187187
const fileUser = fileSource.replace(`require(dep)`, `42`);

packages/acceptance-tests/pkg-tests-specs/sources/commands/set/version.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe(`Commands`, () => {
6666
makeTemporaryEnv({}, {
6767
env: {COREPACK_ROOT: undefined},
6868
}, async ({path, run, source}) => {
69-
const yarnIndirection = ppath.join(path, `custom-yarn.cjs` as Filename);
69+
const yarnIndirection = ppath.join(path, `custom-yarn.cjs`);
7070
await xfs.writeFilePromise(yarnIndirection, ``);
7171

7272
await expect(run(`set`, `version`, yarnIndirection, `--no-yarn-path`)).rejects.toThrow();
@@ -78,7 +78,7 @@ describe(`Commands`, () => {
7878
makeTemporaryEnv({}, {
7979
env: {COREPACK_ROOT: undefined},
8080
}, async ({path, run, source}) => {
81-
const yarnIndirection = ppath.join(path, `custom-yarn.cjs` as Filename);
81+
const yarnIndirection = ppath.join(path, `custom-yarn.cjs`);
8282
await xfs.writeFilePromise(yarnIndirection, ``);
8383

8484
await run(`set`, `version`, yarnIndirection);
@@ -94,7 +94,7 @@ describe(`Commands`, () => {
9494
await run(`set`, `version`, `self`);
9595
await check(path, {corepackVersion: /[0-9]+\./, usePath: true});
9696

97-
const projectDir = ppath.join(path, `project` as Filename);
97+
const projectDir = ppath.join(path, `project`);
9898
await xfs.mkdirPromise(projectDir);
9999
await xfs.writeJsonPromise(ppath.join(projectDir, Filename.manifest), {});
100100
await xfs.writeFilePromise(ppath.join(projectDir, Filename.lockfile), ``);
@@ -129,7 +129,7 @@ describe(`Commands`, () => {
129129
await run(`set`, `version`, `self`);
130130
await check(path, {corepackVersion: /[0-9]+\./, usePath: true});
131131

132-
const projectDir = ppath.join(path, `project` as Filename);
132+
const projectDir = ppath.join(path, `project`);
133133
await xfs.mkdirPromise(projectDir);
134134
await xfs.writeJsonPromise(ppath.join(projectDir, Filename.manifest), {});
135135
await xfs.writeFilePromise(ppath.join(projectDir, Filename.lockfile), ``);
@@ -142,7 +142,7 @@ describe(`Commands`, () => {
142142
});
143143

144144
async function check(path: PortablePath, checks: {corepackVersion: string | RegExp, usePath: boolean}) {
145-
const releasesPath = ppath.join(path, `.yarn/releases` as PortablePath);
145+
const releasesPath = ppath.join(path, `.yarn/releases`);
146146
const yarnrcPath = ppath.join(path, Filename.rc);
147147
const manifestPath = ppath.join(path, Filename.manifest);
148148

0 commit comments

Comments
 (0)