add lint rule to prevent type discrimination properties in API types (#180829)

re https://github.com/microsoft/vscode/issues/63943 and other
This commit is contained in:
Johannes Rieken 2023-04-25 20:22:28 +02:00 committed by GitHub
parent 6fc61894a1
commit 89b615d105
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 1 deletions

View File

@ -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'
});
}
}
}
}
};

View File

@ -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",

View File

@ -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';
/**
@ -22,6 +23,7 @@ declare module 'vscode' {
}
export interface StackFrameFocus {
// eslint-disable-next-line local/vscode-dts-string-type-literals
kind: 'stackFrame';
/**

View File

@ -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;
@ -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;
@ -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;
@ -189,6 +192,7 @@ declare module 'vscode' {
}
export interface InteractiveSessionTerminalAction {
// eslint-disable-next-line local/vscode-dts-string-type-literals
kind: 'runInTerminal';
responseId: string;
codeBlockIndex: number;
@ -196,6 +200,7 @@ declare module 'vscode' {
}
export interface InteractiveSessionCommandAction {
// eslint-disable-next-line local/vscode-dts-string-type-literals
kind: 'command';
command: InteractiveResponseCommand;
}

View File

@ -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;