Skip to content

Commit

Permalink
add lint rule to prevent type discrimination properties in API types (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken authored Apr 25, 2023
1 parent 6fc6189 commit 89b615d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .eslintplugin/vscode-dts-string-type-literals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as eslint from 'eslint';
import { TSESTree } from '@typescript-eslint/experimental-utils';

export = new class ApiTypeDiscrimination implements eslint.Rule.RuleModule {

readonly meta: eslint.Rule.RuleMetaData = {
docs: { url: 'https://github.com/microsoft/vscode/wiki/Extension-API-guidelines' },
messages: {
noTypeDiscrimination: 'Do not use type descrimination properties'
}
};

create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
return {
['TSPropertySignature[optional=undefined] TSTypeAnnotation TSLiteralType Literal']: (node: any) => {

const raw = String((<TSESTree.Literal>node).raw)

if (/^('|").*\1$/.test(raw)) {

context.report({
node: node,
messageId: 'noTypeDiscrimination'
});
}
}
}
}
};
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"rules": {
"local/vscode-dts-create-func": "warn",
"local/vscode-dts-literal-or-types": "warn",
"local/vscode-dts-string-type-literals": "warn",
"local/vscode-dts-interface-naming": "warn",
"local/vscode-dts-cancellation": "warn",
"local/vscode-dts-use-thenable": "warn",
Expand Down
2 changes: 2 additions & 0 deletions src/vscode-dts/vscode.proposed.debugFocus.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare module 'vscode' {
// See https://github.com/microsoft/vscode/issues/63943

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

/**
Expand All @@ -22,6 +23,7 @@ declare module 'vscode' {
}

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

/**
Expand Down
5 changes: 5 additions & 0 deletions src/vscode-dts/vscode.proposed.interactive.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ declare module 'vscode' {
}

export interface InteractiveSessionVoteAction {
// eslint-disable-next-line local/vscode-dts-string-type-literals
kind: 'vote';
responseId: string;
direction: InteractiveSessionVoteDirection;
Expand All @@ -171,6 +172,7 @@ declare module 'vscode' {
}

export interface InteractiveSessionCopyAction {
// eslint-disable-next-line local/vscode-dts-string-type-literals
kind: 'copy';
responseId: string;
codeBlockIndex: number;
Expand All @@ -181,6 +183,7 @@ declare module 'vscode' {
}

export interface InteractiveSessionInsertAction {
// eslint-disable-next-line local/vscode-dts-string-type-literals
kind: 'insert';
responseId: string;
codeBlockIndex: number;
Expand All @@ -189,13 +192,15 @@ declare module 'vscode' {
}

export interface InteractiveSessionTerminalAction {
// eslint-disable-next-line local/vscode-dts-string-type-literals
kind: 'runInTerminal';
responseId: string;
codeBlockIndex: number;
languageId?: string;
}

export interface InteractiveSessionCommandAction {
// eslint-disable-next-line local/vscode-dts-string-type-literals
kind: 'command';
command: InteractiveResponseCommand;
}
Expand Down
2 changes: 1 addition & 1 deletion src/vscode-dts/vscode.proposed.resolvers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ declare module 'vscode' {
export interface ResourceLabelFormatting {
label: string; // myLabel:/${path}
// 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.
// eslint-disable-next-line local/vscode-dts-literal-or-types
// eslint-disable-next-line local/vscode-dts-literal-or-types, local/vscode-dts-string-type-literals
separator: '/' | '\\' | '';
tildify?: boolean;
normalizeDriveLetter?: boolean;
Expand Down

0 comments on commit 89b615d

Please sign in to comment.