Skip to content

Commit ffc3ed2

Browse files
author
Kartik Raj
committed
Make all properties readonly
1 parent 6b4734d commit ffc3ed2

File tree

2 files changed

+47
-40
lines changed

2 files changed

+47
-40
lines changed

src/client/proposedApi.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ export function reportActiveInterpreterChanged(e: ActiveEnvironmentChangeEvent):
3939
const onEnvironmentsChanged = new EventEmitter<EnvironmentsChangeEvent>();
4040
const environmentsReference = new Map<string, EnvironmentReference>();
4141

42+
/**
43+
* Make all properties in T mutable.
44+
*/
45+
type Mutable<T> = {
46+
-readonly [P in keyof T]: Mutable<T[P]>;
47+
};
48+
4249
export class EnvironmentReference implements Environment {
4350
readonly id: string;
4451

@@ -47,23 +54,23 @@ export class EnvironmentReference implements Environment {
4754
}
4855

4956
get executable() {
50-
return this.internal.executable;
57+
return Object.freeze(this.internal.executable);
5158
}
5259

5360
get environment() {
54-
return this.internal.environment;
61+
return Object.freeze(this.internal.environment);
5562
}
5663

5764
get version() {
58-
return this.internal.version;
65+
return Object.freeze(this.internal.version);
5966
}
6067

6168
get tools() {
62-
return this.internal.tools;
69+
return Object.freeze(this.internal.tools);
6370
}
6471

6572
get path() {
66-
return this.internal.path;
73+
return Object.freeze(this.internal.path);
6774
}
6875

6976
updateEnv(newInternal: Environment) {
@@ -239,7 +246,7 @@ function convertKind(kind: PythonEnvKind): EnvironmentTools | undefined {
239246
}
240247

241248
export function convertEnvInfo(env: PythonEnvInfo): Environment {
242-
const convertedEnv = convertCompleteEnvInfo(env) as Environment;
249+
const convertedEnv = convertCompleteEnvInfo(env) as Mutable<Environment>;
243250
if (convertedEnv.executable.sysPrefix === '') {
244251
convertedEnv.executable.sysPrefix = undefined;
245252
}
@@ -258,7 +265,7 @@ export function convertEnvInfo(env: PythonEnvInfo): Environment {
258265
if (convertedEnv.version.minor === -1) {
259266
convertedEnv.version.minor = undefined;
260267
}
261-
return convertedEnv;
268+
return convertedEnv as Environment;
262269
}
263270

264271
function convertEnvInfoAndGetReference(env: PythonEnvInfo): Environment {

src/client/proposedApiTypes.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -73,52 +73,52 @@ export type Environment = EnvironmentId & {
7373
/**
7474
* Carries details about python executable.
7575
*/
76-
executable: {
76+
readonly executable: {
7777
/**
7878
* Uri of the python interpreter/executable. Carries `undefined` in case an executable does not belong to
7979
* the environment.
8080
*/
81-
uri: Uri | undefined;
81+
readonly uri: Uri | undefined;
8282
/**
8383
* Bitness if known at this moment.
8484
*/
85-
bitness: Architecture | undefined;
85+
readonly bitness: Architecture | undefined;
8686
/**
8787
* Value of `sys.prefix` in sys module if known at this moment.
8888
*/
89-
sysPrefix: string | undefined;
89+
readonly sysPrefix: string | undefined;
9090
};
9191
/**
9292
* Carries details if it is an environment, otherwise `undefined` in case of global interpreters and others.
9393
*/
94-
environment:
94+
readonly environment:
9595
| {
9696
/**
9797
* Type of the environment.
9898
*/
99-
type: EnvironmentType;
99+
readonly type: EnvironmentType;
100100
/**
101101
* Name to the environment if any.
102102
*/
103-
name: string | undefined;
103+
readonly name: string | undefined;
104104
/**
105105
* Uri of the environment folder.
106106
*/
107-
folderUri: Uri;
107+
readonly folderUri: Uri;
108108
/**
109109
* Any specific workspace folder this environment is created for.
110110
*/
111-
workspaceFolder: Uri | undefined;
111+
readonly workspaceFolder: Uri | undefined;
112112
}
113113
| undefined;
114114
/**
115115
* Carries Python version information known at this moment.
116116
*/
117-
version: VersionInfo & {
117+
readonly version: VersionInfo & {
118118
/**
119119
* Value of `sys.version` in sys module if known at this moment.
120120
*/
121-
sysVersion: string | undefined;
121+
readonly sysVersion: string | undefined;
122122
};
123123
/**
124124
* Tools/plugins which created the environment or where it came from. First value in array corresponds
@@ -127,7 +127,7 @@ export type Environment = EnvironmentId & {
127127
* Array is empty if no tool is responsible for creating/managing the environment. Usually the case for
128128
* global interpreters.
129129
*/
130-
tools: EnvironmentTools[];
130+
readonly tools: readonly EnvironmentTools[];
131131
};
132132

133133
/**
@@ -138,47 +138,47 @@ export type ResolvedEnvironment = Environment & {
138138
/**
139139
* Carries complete details about python executable.
140140
*/
141-
executable: {
141+
readonly executable: {
142142
/**
143143
* Uri of the python interpreter/executable. Carries `undefined` in case an executable does not belong to
144144
* the environment.
145145
*/
146-
uri: Uri | undefined;
146+
readonly uri: Uri | undefined;
147147
/**
148148
* Bitness of the environment.
149149
*/
150-
bitness: Architecture;
150+
readonly bitness: Architecture;
151151
/**
152152
* Value of `sys.prefix` in sys module.
153153
*/
154-
sysPrefix: string;
154+
readonly sysPrefix: string;
155155
};
156156
/**
157157
* Carries complete Python version information.
158158
*/
159-
version: ResolvedVersionInfo & {
159+
readonly version: ResolvedVersionInfo & {
160160
/**
161161
* Value of `sys.version` in sys module if known at this moment.
162162
*/
163-
sysVersion: string;
163+
readonly sysVersion: string;
164164
};
165165
};
166166

167167
export type EnvironmentsChangeEvent = {
168-
env: Environment;
168+
readonly env: Environment;
169169
/**
170170
* * "add": New environment is added.
171171
* * "remove": Existing environment in the list is removed.
172172
* * "update": New information found about existing environment.
173173
*/
174-
type: 'add' | 'remove' | 'update';
174+
readonly type: 'add' | 'remove' | 'update';
175175
};
176176

177177
export type ActiveEnvironmentIdChangeEvent = EnvironmentId & {
178178
/**
179179
* Workspace folder the environment changed for.
180180
*/
181-
resource: WorkspaceFolder | undefined;
181+
readonly resource: WorkspaceFolder | undefined;
182182
};
183183

184184
/**
@@ -190,13 +190,13 @@ export type EnvironmentId = {
190190
/**
191191
* The ID of the environment.
192192
*/
193-
id: string;
193+
readonly id: string;
194194
/**
195195
* Path to environment folder or path to python executable that uniquely identifies an environment. Environments
196196
* lacking a python executable are identified by environment folder paths, whereas other envs can be identified
197197
* using python executable path.
198198
*/
199-
path: string;
199+
readonly path: string;
200200
};
201201

202202
/**
@@ -240,20 +240,20 @@ export type PythonReleaseLevel = 'alpha' | 'beta' | 'candidate' | 'final';
240240
* Release information for a Python version.
241241
*/
242242
export type PythonVersionRelease = {
243-
level: PythonReleaseLevel;
244-
serial: number;
243+
readonly level: PythonReleaseLevel;
244+
readonly serial: number;
245245
};
246246

247247
export type VersionInfo = {
248-
major: number | undefined;
249-
minor: number | undefined;
250-
micro: number | undefined;
251-
release: PythonVersionRelease | undefined;
248+
readonly major: number | undefined;
249+
readonly minor: number | undefined;
250+
readonly micro: number | undefined;
251+
readonly release: PythonVersionRelease | undefined;
252252
};
253253

254254
export type ResolvedVersionInfo = {
255-
major: number;
256-
minor: number;
257-
micro: number;
258-
release: PythonVersionRelease;
255+
readonly major: number;
256+
readonly minor: number;
257+
readonly micro: number;
258+
readonly release: PythonVersionRelease;
259259
};

0 commit comments

Comments
 (0)