Skip to content

Commit

Permalink
refactor: type empty arrays (#129)
Browse files Browse the repository at this point in the history
Type array variables which are initialized to empty arrays, so they don't default to `any[]`
  • Loading branch information
ComradeVanti authored Jan 18, 2024
1 parent 05eb4e1 commit 759c9ed
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/cmd-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import {
PackageReference,
splitPackageReference,
} from "./types/package-reference";
import { addScope, scopedRegistry } from "./types/scoped-registry";
import {
addScope,
ScopedRegistry,
scopedRegistry,
} from "./types/scoped-registry";
import {
addDependency,
addScopedRegistry,
Expand Down Expand Up @@ -73,7 +77,7 @@ export const add = async function (
const manifest = loadProjectManifest(env.cwd);
if (manifest === null) return { code: 1, dirty };
// packages that added to scope registry
const pkgsInScope: DomainName[] = [];
const pkgsInScope = Array.of<DomainName>();
if (version === undefined || !isPackageUrl(version)) {
// verify name
let packument = await fetchPackument(env.registry, name, client);
Expand Down Expand Up @@ -209,7 +213,7 @@ export const add = async function (
if (!isUpstreamPackage) {
// add to scopedRegistries
if (!manifest.scopedRegistries) {
manifest.scopedRegistries = [];
manifest.scopedRegistries = Array.of<ScopedRegistry>();
dirty = true;
}
let entry = tryGetScopedRegistryByUrl(manifest, env.registry.url);
Expand All @@ -234,7 +238,7 @@ export const add = async function (
};

// add
const results = [];
const results = Array.of<AddResult>();
for (const pkg of pkgs) results.push(await addSingle(pkg));
const result: AddResult = {
code: results.filter((x) => x.code != 0).length > 0 ? 1 : 0,
Expand Down
4 changes: 2 additions & 2 deletions src/cmd-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const remove = async function (
const manifest = loadProjectManifest(env.cwd);
if (manifest === null) return { code: 1, dirty };
// not found array
const pkgsNotFound = [];
const pkgsNotFound = Array.of<PackageReference>();
version = manifest.dependencies[name];
if (version) {
log.notice("manifest", `removed ${packageReference(name, version)}`);
Expand All @@ -79,7 +79,7 @@ export const remove = async function (
};

// remove
const results = [];
const results = Array.of<RemoveResult>();
for (const pkg of pkgs) results.push(await removeSingle(pkg));
const result: RemoveResult = {
code: results.filter((x) => x.code != 0).length > 0 ? 1 : 0,
Expand Down
2 changes: 1 addition & 1 deletion src/cmd-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const searchOld = async function (
const results = <OldSearchResult | undefined>(
await npmFetch.json("/-/all", getNpmFetchOptions(registry))
);
let packuments: SearchedPackument[] = [];
let packuments = Array.of<SearchedPackument>();
if (results) {
if (Array.isArray(results)) {
// results is an array of objects
Expand Down
4 changes: 2 additions & 2 deletions src/registry-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ export const fetchPackageDependencies = async function (
// a list of processed dependency {name, version}
const processedList = Array.of<NameVersionPair>();
// a list of dependency entry exists on the registry
const depsValid = [];
const depsValid = Array.of<Dependency>();
// a list of dependency entry doesn't exist on the registry
const depsInvalid = [];
const depsInvalid = Array.of<Dependency>();
// cached dict
let packageCache: PackumentCache = {};
while (pendingList.length > 0) {
Expand Down

0 comments on commit 759c9ed

Please sign in to comment.