Skip to content

Commit 89b615d

Browse files
authored
add lint rule to prevent type discrimination properties in API types (microsoft#180829)
re microsoft#63943 and other
1 parent 6fc6189 commit 89b615d

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as eslint from 'eslint';
7+
import { TSESTree } from '@typescript-eslint/experimental-utils';
8+
9+
export = new class ApiTypeDiscrimination implements eslint.Rule.RuleModule {
10+
11+
readonly meta: eslint.Rule.RuleMetaData = {
12+
docs: { url: 'https://github.com/microsoft/vscode/wiki/Extension-API-guidelines' },
13+
messages: {
14+
noTypeDiscrimination: 'Do not use type descrimination properties'
15+
}
16+
};
17+
18+
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
19+
return {
20+
['TSPropertySignature[optional=undefined] TSTypeAnnotation TSLiteralType Literal']: (node: any) => {
21+
22+
const raw = String((<TSESTree.Literal>node).raw)
23+
24+
if (/^('|").*\1$/.test(raw)) {
25+
26+
context.report({
27+
node: node,
28+
messageId: 'noTypeDiscrimination'
29+
});
30+
}
31+
}
32+
}
33+
}
34+
};

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
"rules": {
131131
"local/vscode-dts-create-func": "warn",
132132
"local/vscode-dts-literal-or-types": "warn",
133+
"local/vscode-dts-string-type-literals": "warn",
133134
"local/vscode-dts-interface-naming": "warn",
134135
"local/vscode-dts-cancellation": "warn",
135136
"local/vscode-dts-use-thenable": "warn",

src/vscode-dts/vscode.proposed.debugFocus.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ declare module 'vscode' {
88
// See https://github.com/microsoft/vscode/issues/63943
99

1010
export interface ThreadFocus {
11+
// eslint-disable-next-line local/vscode-dts-string-type-literals
1112
kind: 'thread';
1213

1314
/**
@@ -22,6 +23,7 @@ declare module 'vscode' {
2223
}
2324

2425
export interface StackFrameFocus {
26+
// eslint-disable-next-line local/vscode-dts-string-type-literals
2527
kind: 'stackFrame';
2628

2729
/**

src/vscode-dts/vscode.proposed.interactive.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ declare module 'vscode' {
159159
}
160160

161161
export interface InteractiveSessionVoteAction {
162+
// eslint-disable-next-line local/vscode-dts-string-type-literals
162163
kind: 'vote';
163164
responseId: string;
164165
direction: InteractiveSessionVoteDirection;
@@ -171,6 +172,7 @@ declare module 'vscode' {
171172
}
172173

173174
export interface InteractiveSessionCopyAction {
175+
// eslint-disable-next-line local/vscode-dts-string-type-literals
174176
kind: 'copy';
175177
responseId: string;
176178
codeBlockIndex: number;
@@ -181,6 +183,7 @@ declare module 'vscode' {
181183
}
182184

183185
export interface InteractiveSessionInsertAction {
186+
// eslint-disable-next-line local/vscode-dts-string-type-literals
184187
kind: 'insert';
185188
responseId: string;
186189
codeBlockIndex: number;
@@ -189,13 +192,15 @@ declare module 'vscode' {
189192
}
190193

191194
export interface InteractiveSessionTerminalAction {
195+
// eslint-disable-next-line local/vscode-dts-string-type-literals
192196
kind: 'runInTerminal';
193197
responseId: string;
194198
codeBlockIndex: number;
195199
languageId?: string;
196200
}
197201

198202
export interface InteractiveSessionCommandAction {
203+
// eslint-disable-next-line local/vscode-dts-string-type-literals
199204
kind: 'command';
200205
command: InteractiveResponseCommand;
201206
}

src/vscode-dts/vscode.proposed.resolvers.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ declare module 'vscode' {
173173
export interface ResourceLabelFormatting {
174174
label: string; // myLabel:/${path}
175175
// For historic reasons we use an or string here. Once we finalize this API we should start using enums instead and adopt it in extensions.
176-
// eslint-disable-next-line local/vscode-dts-literal-or-types
176+
// eslint-disable-next-line local/vscode-dts-literal-or-types, local/vscode-dts-string-type-literals
177177
separator: '/' | '\\' | '';
178178
tildify?: boolean;
179179
normalizeDriveLetter?: boolean;

0 commit comments

Comments
 (0)