Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Const enum recommendation Eslint plugin #2246

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
bbbeee5
added custom eslint rule to recommend using const enums
noahdarveau-MSFT Apr 1, 2024
2875a84
changefile
noahdarveau-MSFT Apr 1, 2024
76f12bb
removed commented out code
noahdarveau-MSFT Apr 1, 2024
7307b88
Merge branch 'main' into noahdarveau/custom-eslint-plugin
noahdarveau-MSFT Apr 5, 2024
a770c63
Merge branch 'main' into noahdarveau/custom-eslint-plugin
noahdarveau-MSFT Apr 5, 2024
00ad529
Merge branch 'main' into noahdarveau/custom-eslint-plugin
noahdarveau-MSFT Apr 9, 2024
0faa4b3
added eslint ignores to all public non const enums
noahdarveau-MSFT Apr 10, 2024
ced650a
Merge branch 'main' into noahdarveau/custom-eslint-plugin
noahdarveau-MSFT Apr 11, 2024
54f7d05
made test enums const
noahdarveau-MSFT Apr 11, 2024
35c7a2b
Merge branch 'main' into noahdarveau/custom-eslint-plugin
noahdarveau-MSFT Apr 11, 2024
0640432
Merge branch 'main' into noahdarveau/custom-eslint-plugin
noahdarveau-MSFT Apr 15, 2024
4c86843
added link to wiki in warning message
noahdarveau-MSFT Apr 15, 2024
fd05560
Merge branch 'main' into noahdarveau/custom-eslint-plugin
AE-MS Apr 16, 2024
a4e6681
merged main
noahdarveau-MSFT Apr 16, 2024
bbdd7a5
added plugin surpression explanation comments
noahdarveau-MSFT Apr 16, 2024
bfef846
merged main
noahdarveau-MSFT Apr 16, 2024
1953247
fixed lock file
noahdarveau-MSFT Apr 16, 2024
a24007a
Merge branch 'main' into noahdarveau/custom-eslint-plugin
AE-MS Apr 19, 2024
3467499
Merge branch 'main' into noahdarveau/custom-eslint-plugin
noahdarveau-MSFT Apr 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Added an eslint plugin to recommend the use of const enums when possible",
"packageName": "@microsoft/teams-js",
"email": "noahdarveau@microsoft.com",
"dependentChangeType": "patch"
}
3 changes: 2 additions & 1 deletion packages/teams-js/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
parserOptions: {
project: './tsconfig.eslint.json',
},
plugins: ['strict-null-checks'],
plugins: ['strict-null-checks', 'recommend-const-enums'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/no-namespace': 'off',
Expand All @@ -13,5 +13,6 @@ module.exports = {
],
'no-inner-declarations': 'off',
'strict-null-checks/all': 'warn',
'recommend-const-enums/recommend-const-enums': 'warn',
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const recommendConstEnumsRule = require('./recommendConstEnums.js');

const plugin = { rules: { 'recommend-const-enums': recommendConstEnumsRule } };
module.exports = plugin;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "eslint-plugin-recommend-const-enums",
"version": "0.0.0",
"license": "MIT",
"author": "Noah",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noah

lol

"description": "Recommend using const enums",
"type": "commonjs",
"keywords": [
"eslint",
"eslint-plugin",
"eslintplugin"
],
"peerDependencies": {
"eslint": ">=5.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Recommend using const enums if possible to minimize bundle size',
category: 'Best Practices',
recommended: true,
},
fixable: 'code',
schema: [],
},

create: function (context) {
return {
TSEnumDeclaration: function (node) {
const enumName = node.id.name;
const enumType = node.const ? 'const' : 'regular';

if (enumType === 'regular') {
context.report({
node: node,
message: `Please consider if you can use a const enum for ${enumName} to minimize bundle size. If not, add "/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */" to the line above to disable this warning,
as well as a comment explaining why you can't use a const enum. To learn more about why we typically want to use const enums, see https://github.com/OfficeDev/microsoft-teams-library-js/wiki/Notes-on-Enum-Usage.`,
severity: 2,
});
}
},
};
},
};
3 changes: 2 additions & 1 deletion packages/teams-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"debug": "^4.3.3"
},
"devDependencies": {
"@types/debug": "^4.1.7"
"@types/debug": "^4.1.7",
"eslint-plugin-recommend-const-enums": "file:./eslint-rules/eslint-plugin-recommend-const-enums"
},
"license": "MIT",
"files": [
Expand Down
2 changes: 2 additions & 0 deletions packages/teams-js/src/private/remoteCamera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export namespace remoteCamera {
* @internal
* Limited to Microsoft-internal use
*/
//This enum is not const and the eslint rule is being surpressed because in the teams-test-app, the ControlCommand enum object is being accessed at runtime through the Object.values method.
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
noahdarveau-MSFT marked this conversation as resolved.
Show resolved Hide resolved
export enum ControlCommand {
Reset = 'Reset',
ZoomIn = 'ZoomIn',
Expand Down
12 changes: 12 additions & 0 deletions packages/teams-js/src/public/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ export namespace app {
/**
* Describes errors that caused app initialization to fail
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum FailedReason {
/**
* Authentication failed
Expand All @@ -246,6 +249,9 @@ export namespace app {
* Describes expected errors that occurred during an otherwise successful
* app initialization
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum ExpectedFailureReason {
/**
* There was a permission error
Expand All @@ -272,6 +278,9 @@ export namespace app {
/**
* Represents the failed request sent during a failed app initialization.
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export interface IFailedRequest {
/**
* The reason for the failure
Expand All @@ -286,6 +295,9 @@ export namespace app {
/**
* Represents the failure request sent during an erroneous app initialization.
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export interface IExpectedFailureRequest {
/**
* The reason for the failure
Expand Down
3 changes: 3 additions & 0 deletions packages/teams-js/src/public/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,9 @@ export namespace authentication {
* @internal
* Limited to Microsoft-internal use
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum DataResidency {
/**
* Public
Expand Down
3 changes: 3 additions & 0 deletions packages/teams-js/src/public/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const callTelemetryVersionNumber: ApiVersionNumber = ApiVersionNumber.V_2;
*/
export namespace call {
/** Modalities that can be associated with a call. */
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum CallModalities {
/** Indicates that the modality is unknown or undefined. */
Unknown = 'unknown',
Expand Down
21 changes: 21 additions & 0 deletions packages/teams-js/src/public/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/** HostClientType represents the different client platforms on which host can be run. */
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum HostClientType {
/** Represents the desktop client of host, which is installed on a user's computer and runs as a standalone application. */
desktop = 'desktop',
Expand Down Expand Up @@ -30,6 +33,9 @@ export enum HostClientType {
}

/** HostName indicates the possible hosts for your application. */
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum HostName {
/**
* Office.com and Office Windows App
Expand Down Expand Up @@ -78,6 +84,9 @@ export enum HostName {
* If the app is running in the content context, the developer may want to display information relevant to
* the content the user is currently viewing.
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum FrameContexts {
/**
* App's frame context from where settings page can be accessed.
Expand All @@ -104,6 +113,9 @@ export enum FrameContexts {
* Indicates the team type, currently used to distinguish between different team
* types in Office 365 for Education (team types 1, 2, 3, and 4).
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum TeamType {
/** Represents a standard or classic team in host that is designed for ongoing collaboration and communication among a group of people. */
Standard = 0,
Expand All @@ -120,6 +132,9 @@ export enum TeamType {
/**
* Indicates the various types of roles of a user in a team.
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum UserTeamRole {
/** Represents that the user is an owner or administrator of the team. */
Admin = 0,
Expand All @@ -132,6 +147,9 @@ export enum UserTeamRole {
/**
* Dialog module dimension enum
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum DialogDimension {
/** Represents a large-sized dialog box, which is typically used for displaying large amounts of content or complex workflows that require more space. */
Large = 'large',
Expand All @@ -153,6 +171,9 @@ import { HostVersionsInfo } from './interfaces';
/**
* The type of the channel with which the content is associated.
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum ChannelType {
/** The default channel type. Type of channel is used for general collaboration and communication within a team. */
Regular = 'Regular',
Expand Down
18 changes: 18 additions & 0 deletions packages/teams-js/src/public/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ export interface LocaleInfo {
/**
* Allowed user file open preferences
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum FileOpenPreference {
/** Indicates that the user should be prompted to open the file in inline. */
Inline = 'inline',
Expand All @@ -200,6 +203,9 @@ export enum FileOpenPreference {
*
* @beta
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum ActionObjectType {
/** Represents content within a Microsoft 365 application. */
M365Content = 'm365content',
Expand Down Expand Up @@ -250,6 +256,9 @@ export interface SecondaryId {
* See [commonly accessed resources](https://learn.microsoft.com/graph/api/resources/onedrive?view=graph-rest-1.0#commonly-accessed-resources).
* @beta
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum SecondaryM365ContentIdName {
/** OneDrive ID */
DriveId = 'driveId',
Expand Down Expand Up @@ -990,6 +999,9 @@ export function isSdkError(err: unknown): err is SdkError {
}

/** Error codes used to identify different types of errors that can occur while developing apps. */
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum ErrorCode {
/**
* API not supported in the current platform.
Expand Down Expand Up @@ -1054,6 +1066,9 @@ export enum ErrorCode {
}

/** @hidden */
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum DevicePermission {
GeoLocation = 'geolocation',
Media = 'media',
Expand All @@ -1077,6 +1092,9 @@ export interface AdaptiveCardVersion {
/**
* Currently supported Mime type
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum ClipboardSupportedMimeType {
TextPlain = 'text/plain',
TextHtml = 'text/html',
Expand Down
6 changes: 6 additions & 0 deletions packages/teams-js/src/public/liveShareHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export namespace liveShare {
* Used in Live Share for its role verification feature.
* For more information, visit https://learn.microsoft.com/microsoftteams/platform/apps-in-teams-meetings/teams-live-share-capabilities?tabs=javascript#role-verification-for-live-data-structures
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum UserMeetingRole {
/**
* Guest role.
Expand All @@ -46,6 +49,9 @@ export namespace liveShare {
* State of the current Live Share session's Fluid container.
* This is used internally by the `LiveShareClient` when joining a Live Share session.
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum ContainerState {
/**
* The call to `LiveShareHost.setContainerId()` successfully created the container mapping
Expand Down
3 changes: 3 additions & 0 deletions packages/teams-js/src/public/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ export namespace mail {
}

/** Defines compose mail types. */
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum ComposeMailType {
/** Compose a new mail message. */
New = 'new',
Expand Down
6 changes: 6 additions & 0 deletions packages/teams-js/src/public/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ export namespace marketplace {
* Represents the persona creating the cart.
* @beta
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum Intent {
/**
* @hidden
Expand All @@ -226,6 +229,9 @@ export namespace marketplace {
* Represents the status of the cart.
* @beta
*/
/* This enum is not const and the eslint rule is being surpressed because it existed before the PR to make all enums const and is public.
Since it is public, we can not know how clients are using the enum, so if we make it const, it could potentially break their code. */
/* eslint-disable-next-line recommend-const-enums/recommend-const-enums */
export enum CartStatus {
/**
* @hidden
Expand Down