Skip to content

Commit

Permalink
Migrate exported configs to flat config model
Browse files Browse the repository at this point in the history
  • Loading branch information
mkacmar committed Sep 29, 2024
1 parent d55346f commit 2f7577d
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 323 deletions.
56 changes: 29 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# eslint-plugin-sdl

![Node CI](https://github.com/microsoft/eslint-plugin-sdl/workflows/Node%20CI/badge.svg?branch=main&event=push)
![E2E integration](https://github.com/microsoft/eslint-plugin-sdl/workflows/E2E%20integration/badge.svg?branch=main&event=push)

Expand All @@ -16,50 +17,51 @@ or
yarn add microsoft/eslint-plugin-sdl
```

## Usage
When you run npm install within your project's root folder, the plugin will be added automatically to your package.json and package-lock.json files. You can also add the plugin to your package.json file manually by specifying the name and version number in the dependencies section like so:
## Configs

```sh
"dependencies": {
"@microsoft/eslint-plugin-sdl": "^0.2.2"
}
```
Including an ESLint configuration file in your project allows you to customize how ESLint applies rules to your project. You can include the plugin in your [configuration file](https://eslint.org/docs/latest/use/configure/configuration-files) by adding:

Run npm install within your root folder to install everything listed in the dependencies section of package.json. If the plugin is listed in your package.json dependencies, eslint will enforce all plugin rules using default settings.
```js
const pluginMicrosoftSdl = require("@microsoft/eslint-plugin-sdl");

## Configs
Including an eslint configuration file in your project allows you to customize how eslint applies rules to your project. If you are using an .eslintrc file, you can include the plugin by adding:

```sh
plugins: ["@microsoft/eslint-plugin-sdl"]
module.exports = [
...pluginMicrosoftSdl.configs.recommended
];
```

Eslint will then only enforce rules you specify in the rules section of your .eslintrc file at the severity level you designate. The severity level options are 0 (no error), 1 (warning), and 2 (error). For example:
ESLint will then only enforce rules you specify in the rules section of your configuration file at the [severity level](https://eslint.org/docs/latest/use/configure/rules) you designate. For example:

```sh
rules: {
"no-eval": 2,
"@microsoft/sdl/no-inner-html": 2
}
```js
const pluginMicrosoftSdl = require("@microsoft/eslint-plugin-sdl");

module.exports = [
...pluginMicrosoftSdl.configs.recommended,
{
rules: {
"no-eval": "error",
"@microsoft/sdl/no-inner-html": "error"
}
}
];
```

You can also used the below Shareable config files as guidelines depending on the type of project.
You can also used the below Shareable Config files using flat config model as guidelines depending on the type of project.

Plugin is shipped with following [Shareable Configs](http://eslint.org/docs/developer-guide/shareable-configs):

- [angular](config/angular.js) - Set of rules for [Angular](https://angular.io) applications
- [angularjs](config/angularjs.js) - Set of rules for [AngularJS](https://docs.angularjs.org) applications
- [angular](config/angular.js) - Set of rules for modern [Angular](https://angular.io) applications
- [angularjs](config/angularjs.js) - Set of rules for legacy [AngularJS](https://docs.angularjs.org) applications
- [common](config/common.js) - Set of rules for common JavaScript applications
- [electron](config/electron.js) - Set of rules for Electron applications
- [node](config/node.js) - Set of rules for Node applications
- [node](config/node.js) - Set of rules for Node.js applications
- [react](config/react.js) - Set of rules for [ReactJS](https://reactjs.org) applications
- [**recommended**](config/recommended.js) - SDL Recommended rules for all applications
- [**required**](config/required.js) - SDL Required rules for all applications
- [**recommended**](lib/index.js) - SDL Recommended rules for all applications
- [**required**](lib/index.js) - SDL Required rules for all applications
- [typescript](config/typescript.js) - Set of rules for TypeScript applications

## Rules

Where possible, we leverage existing rules from [ESLint](https://eslint.org/docs/rules/) and community plugins such as [react](https://github.com/yannickcr/eslint-plugin-react), [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#supported-rules) or [security](https://github.com/nodesecurity/eslint-plugin-security#rules).
Where possible, we leverage existing rules from [ESLint](https://eslint.org/docs/rules/) and community plugins such as [react](https://github.com/jsx-eslint/eslint-plugin-react), [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/eslint-plugin#supported-rules) or [security](https://github.com/nodesecurity/eslint-plugin-security#rules).

We also implemented several [custom rules](./lib/rules) where we did not find sufficient alternative in the community.

Expand Down Expand Up @@ -102,4 +104,4 @@ provided by the bot. You will only need to do this once across all repos using o

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
21 changes: 11 additions & 10 deletions config/angular.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* Shareable config for Angular apps.
*/

"use strict";

module.exports = {
plugins: [
"@microsoft/sdl"
],
rules: {
// Generates shareable config for modern Angular (https://angular.dev/) apps.
module.exports = (pluginSdl) => {
return [
{
plugins: {
"@microsoft/sdl": pluginSdl
},
rules: {
"@microsoft/sdl/no-angular-bypass-sanitizer": "error"
}
}
}
];
};
29 changes: 15 additions & 14 deletions config/angularjs.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* Shareable config for AngularJS apps.
*/

"use strict";

module.exports = {
plugins: [
"@microsoft/sdl"
],
rules: {
"@microsoft/sdl/no-angularjs-enable-svg": "error",
"@microsoft/sdl/no-angularjs-sanitization-whitelist": "error",
"@microsoft/sdl/no-angularjs-bypass-sce": "error"
}
}
// Generates shareable config for legacy AngularJS (https://angularjs.org/) apps.
module.exports = (pluginSdl) => {
return [
{
plugins: {
"@microsoft/sdl": pluginSdl
},
rules: {
"@microsoft/sdl/no-angularjs-enable-svg": "error",
"@microsoft/sdl/no-angularjs-sanitization-whitelist": "error",
"@microsoft/sdl/no-angularjs-bypass-sce": "error"
}
}
];
};
50 changes: 25 additions & 25 deletions config/common.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* Shareable config for common JavaScript apps.
*/

"use strict";

module.exports = {
plugins: [
"@microsoft/sdl"
],
rules: {
"no-caller": "error",
"no-delete-var": "error",
"no-eval": "error",
"no-implied-eval": "error",
"no-new-func": "error",
"@microsoft/sdl/no-cookies": "error",
"@microsoft/sdl/no-document-domain": "error",
"@microsoft/sdl/no-document-write": "error",
"@microsoft/sdl/no-html-method": "error",
"@microsoft/sdl/no-inner-html": "error",
"@microsoft/sdl/no-insecure-url": "error",
"@microsoft/sdl/no-msapp-exec-unsafe": "error",
"@microsoft/sdl/no-postmessage-star-origin": "error",
"@microsoft/sdl/no-winjs-html-unsafe": "error"
}
}
module.exports = (pluginSdl) => {
return [
{
plugins: {
"@microsoft/sdl": pluginSdl
},
rules: {
"no-caller": "error",
"no-delete-var": "error",
"no-eval": "error",
"no-implied-eval": "error",
"no-new-func": "error",
"@microsoft/sdl/no-cookies": "error",
"@microsoft/sdl/no-document-domain": "error",
"@microsoft/sdl/no-document-write": "error",
"@microsoft/sdl/no-html-method": "error",
"@microsoft/sdl/no-inner-html": "error",
"@microsoft/sdl/no-insecure-url": "error",
"@microsoft/sdl/no-msapp-exec-unsafe": "error",
"@microsoft/sdl/no-postmessage-star-origin": "error",
"@microsoft/sdl/no-winjs-html-unsafe": "error"
}
}
];
};
24 changes: 12 additions & 12 deletions config/electron.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* Shareable config for Electron apps.
*/

"use strict";

module.exports = {
plugins: [
"@microsoft/sdl"
],
rules: {
"@microsoft/sdl/no-electron-node-integration": "error"
}
}
module.exports = (pluginSdl) => {
return [
{
plugins: {
"@microsoft/sdl": pluginSdl
},
rules: {
"@microsoft/sdl/no-electron-node-integration": "error"
}
}
];
};
36 changes: 22 additions & 14 deletions config/node.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* Shareable config for Node apps.
*/

"use strict";

module.exports = {
plugins: [
"@microsoft/sdl",
"node"
],
rules: {
"@microsoft/sdl/no-unsafe-alloc": "error",
"node/no-deprecated-api": "error"
}
}
const pluginN = require("eslint-plugin-n");

module.exports = (pluginSdl) => {
return [
{
plugins: {
n: pluginN
},
rules: {
"n/no-deprecated-api": "error"
}
},
{
plugins: {
"@microsoft/sdl": pluginSdl
},
rules: {
"@microsoft/sdl/no-unsafe-alloc": "error"
}
},
];
};
62 changes: 37 additions & 25 deletions config/react.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* Shareable config for React apps.
*/

"use strict";

module.exports = {
parserOptions: {
ecmaFeatures: {
jsx: true,
const pluginReact = require("eslint-plugin-react");

module.exports = (pluginSdl) => {
return [
{
languageOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true
}
}
}
},
{
plugins: {
"react": pluginReact
},
rules: {
"react/no-danger": "error",
"react/jsx-no-target-blank": ["error",
{
allowReferrer: false,
enforceDynamicLinks: "always",
warnOnSpreadAttributes: true,
}
]
}
},
},
plugins: [
"react",
"@microsoft/sdl"
],
rules: {
"react/no-danger": "error",
"@microsoft/sdl/react-iframe-missing-sandbox": "error",
"react/jsx-no-target-blank": ["error",
{
allowReferrer: false,
enforceDynamicLinks: 'always',
warnOnSpreadAttributes: true,
}
]
}
}
{
plugins: {
"@microsoft/sdl": pluginSdl,
},
rules: {
"@microsoft/sdl/react-iframe-missing-sandbox": "error"
}
}
];
};
26 changes: 0 additions & 26 deletions config/recommended.js

This file was deleted.

Loading

0 comments on commit 2f7577d

Please sign in to comment.