Skip to content

Commit

Permalink
Merge pull request #262 from vim-denops/asconst
Browse files Browse the repository at this point in the history
👍 change array argument types to readonly
  • Loading branch information
lambdalisue authored Aug 26, 2024
2 parents dae0f5e + 38bb82b commit 0fe0e5e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
14 changes: 10 additions & 4 deletions argument/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const shortPattern = /^-([a-zA-Z0-9])(.*)/;
* }
* ```
*/
export function parseFlags(args: string[]): [Flags, string[]] {
export function parseFlags(args: readonly string[]): [Flags, string[]] {
const patterns = [longPattern, shortPattern];
const flags: Flags = {};
const residue: string[] = [];
Expand Down Expand Up @@ -86,7 +86,10 @@ export function parseFlags(args: string[]): [Flags, string[]] {
* }
* ```
*/
export function validateFlags(flags: Flags, knownAttributes: string[]): void {
export function validateFlags(
flags: Flags,
knownAttributes: readonly string[],
): void {
Object.keys(flags).forEach((v) => {
if (!knownAttributes.includes(v)) {
if (v.length === 1) {
Expand Down Expand Up @@ -116,7 +119,7 @@ export function validateFlags(flags: Flags, knownAttributes: string[]): void {
*/
export function formatFlag(
key: string,
value: string | string[] | undefined,
value: string | readonly string[] | undefined,
): string[] {
if (value == undefined) {
return [];
Expand Down Expand Up @@ -154,7 +157,10 @@ export function formatFlag(
* }
* ```
*/
export function formatFlags(flags: Flags, includes?: string[]): string[] {
export function formatFlags(
flags: Flags,
includes?: readonly string[],
): string[] {
let entries = Object.entries(flags);
if (includes != undefined) {
entries = entries.filter(([k, _]) => includes.includes(k));
Expand Down
2 changes: 1 addition & 1 deletion argument/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ import { type Flags, parseFlags } from "./flags.ts";
* }
* ```
*/
export function parse(args: string[]): [Opts, Flags, string[]] {
export function parse(args: readonly string[]): [Opts, Flags, string[]] {
const [opts, intermediate] = parseOpts(args);
const [flags, residue] = parseFlags(intermediate);
return [opts, flags, residue];
Expand Down
11 changes: 7 additions & 4 deletions argument/opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const builtinOpts = [
"nobinary",
"bad",
"edit",
];
] as const;

const optPattern = /^\+\+([a-zA-Z0-9-]+)(?:=(.*))?/;

Expand Down Expand Up @@ -48,7 +48,7 @@ const optPattern = /^\+\+([a-zA-Z0-9-]+)(?:=(.*))?/;
* }
* ```
*/
export function parseOpts(args: string[]): [Opts, string[]] {
export function parseOpts(args: readonly string[]): [Opts, string[]] {
const opts: Opts = {};
const residue: string[] = [];
for (let i = 0; i < args.length; i++) {
Expand Down Expand Up @@ -100,7 +100,10 @@ export function parseOpts(args: string[]): [Opts, string[]] {
* }
* ```
*/
export function validateOpts(opts: Opts, knownAttributes: string[]): void {
export function validateOpts(
opts: Opts,
knownAttributes: readonly string[],
): void {
Object.keys(opts).forEach((v) => {
if (!knownAttributes.includes(v)) {
throw new Error(`Unknown option '++${v}' is specified.`);
Expand Down Expand Up @@ -153,7 +156,7 @@ export function formatOpt(key: string, value: string | undefined): string[] {
* }
* ```
*/
export function formatOpts(opts: Opts, includes?: string[]): string[] {
export function formatOpts(opts: Opts, includes?: readonly string[]): string[] {
let entries = Object.entries(opts);
if (includes != undefined) {
entries = entries.filter(([k, _]) => includes.includes(k));
Expand Down

0 comments on commit 0fe0e5e

Please sign in to comment.