From d17e4a55c9ee67236322a761b7b72eea80211695 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 26 Nov 2020 14:08:49 +0100 Subject: [PATCH 01/15] Replace type assertion with annotation --- packages/eslint-plugin/rules/dependency-group.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/rules/dependency-group.js b/packages/eslint-plugin/rules/dependency-group.js index eaaf716ccade5d..52380418ac520c 100644 --- a/packages/eslint-plugin/rules/dependency-group.js +++ b/packages/eslint-plugin/rules/dependency-group.js @@ -1,7 +1,8 @@ /** @typedef {import('estree').Comment} Comment */ /** @typedef {import('estree').Node} Node */ -module.exports = /** @type {import('eslint').Rule.RuleModule} */ ( { +/** @type {import('eslint').Rule.RuleModule} */ +module.exports = { meta: { type: 'layout', docs: { @@ -254,4 +255,4 @@ module.exports = /** @type {import('eslint').Rule.RuleModule} */ ( { }, }; }, -} ); +}; From e417411be386f6468b3b95ffd12321303fc1a207 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 26 Nov 2020 14:28:57 +0100 Subject: [PATCH 02/15] Update module tsconfig --- packages/eslint-plugin/tsconfig.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/eslint-plugin/tsconfig.json b/packages/eslint-plugin/tsconfig.json index 0f0a4598184f80..10cc9250043324 100644 --- a/packages/eslint-plugin/tsconfig.json +++ b/packages/eslint-plugin/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { + "module": "CommonJS", "rootDir": "rules", "declarationDir": "build-types" }, From 50bafb0adcf71b8e9f0517d80b81c868e0a3c69a Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 26 Nov 2020 14:29:11 +0100 Subject: [PATCH 03/15] Add file to ts --- packages/eslint-plugin/tsconfig.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/eslint-plugin/tsconfig.json b/packages/eslint-plugin/tsconfig.json index 10cc9250043324..d292d97510c414 100644 --- a/packages/eslint-plugin/tsconfig.json +++ b/packages/eslint-plugin/tsconfig.json @@ -8,7 +8,5 @@ // NOTE: This package is being progressively typed. You are encouraged to // expand this array with files which can be type-checked. At some point in // the future, this can be simplified to an `includes` of `src/**/*`. - "files": [ - "rules/dependency-group.js" - ] + "files": [ "rules/dependency-group.js", "rules/no-unsafe-wp-apis.js" ] } From aae7b9afe8b407109f27892b303fead5c4abb7cc Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 26 Nov 2020 14:29:21 +0100 Subject: [PATCH 04/15] Add new rule --- .../docs/rules/no-unsafe-wp-apis.md | 44 +++++++ .../rules/__tests__/no-unsafe-wp-apis.js | 112 ++++++++++++++++++ .../eslint-plugin/rules/no-unsafe-wp-apis.js | 95 +++++++++++++++ 3 files changed, 251 insertions(+) create mode 100644 packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md create mode 100644 packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js create mode 100644 packages/eslint-plugin/rules/no-unsafe-wp-apis.js diff --git a/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md b/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md new file mode 100644 index 00000000000000..49e02a6497dadd --- /dev/null +++ b/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md @@ -0,0 +1,44 @@ +# Prevent unsafe API usage (no-unsafe-wp-apis) + +Prevent unsafe APIs from `@wordpress/*` packages from being imported. + +This includes experimental and unstable APIs which are expected to change and likely to cause issues in application code. +See the [documentation](https://github.com/WordPress/gutenberg/blob/master/docs/contributors/coding-guidelines.md#experimental-and-unstable-apis). + +> **There is no support commitment for experimental and unstable APIs.** They can and will be removed or changed without advance warning, including as part of a minor or patch release. As an external consumer, you should avoid these APIs. +> … +> +> - An **experimental API** is one which is planned for eventual public availability, but is subject to further experimentation, testing, and discussion. +> - An **unstable API** is one which serves as a means to an end. It is not desired to ever be converted into a public API. + +## Rule details + +Examples of **incorrect** code for this rule: + +```js +import { __experimentalFeature } from '@wordpress/foo'; +import { __unstableFeature } from '@wordpress/bar'; +``` + +Examples of **correct** code for this rule: + +```js +import { registerBlockType } from '@wordpress/blocks'; +``` + +## Options + +The rule can be configured via an object. +This should be an object where the keys are import package names and the values are arrays of allowed experimental imports. +Note that `unstable` features are not intended for public use and the rule cannot be configured to allow them. + +#### Example configuration + +```json +{ + "wpcalypso/wp-no-unsafe-features": [ + "error", + { "@wordpress/block-editor": [ "__experimentalBlock" ] } + ] +} +``` diff --git a/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js b/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js new file mode 100644 index 00000000000000..bb30e75dee1fd0 --- /dev/null +++ b/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js @@ -0,0 +1,112 @@ +/** + * External dependencies + */ +import { RuleTester } from 'eslint'; + +/** + * Internal dependencies + */ +import rule from '../no-unsafe-wp-apis'; + +const ruleTester = new RuleTester( { + parserOptions: { + sourceType: 'module', + ecmaVersion: 6, + }, +} ); + +const options = [ { '@wordpress/safe': [ '__experimentalSafe' ] } ]; + +ruleTester.run( 'wp-no-unsafe-features', rule, { + valid: [ + { code: "import _ from 'lodash';", options }, + { code: "import { map } from 'lodash';", options }, + { code: "import { __experimentalFoo } from 'lodash';", options }, + { code: "import { __unstableFoo } from 'lodash';", options }, + { code: "import _, { __unstableFoo } from 'lodash';", options }, + { code: "import * as _ from 'lodash';", options }, + + { code: "import _ from './x';", options }, + { code: "import { map } from './x';", options }, + { code: "import { __experimentalFoo } from './x';", options }, + { code: "import { __unstableFoo } from './x';", options }, + { code: "import _, { __unstableFoo } from './x';", options }, + { code: "import * as _ from './x';", options }, + + { code: "import s from '@wordpress/safe';", options }, + { code: "import { feature } from '@wordpress/safe';", options }, + { + code: "import { __experimentalSafe } from '@wordpress/safe';", + options, + }, + { + code: + "import { feature, __experimentalSafe } from '@wordpress/safe';", + options, + }, + { + code: "import s, { __experimentalSafe } from '@wordpress/safe';", + options, + }, + { code: "import * as s from '@wordpress/safe';", options }, + ], + + invalid: [ + { + code: "import { __experimentalUnsafe } from '@wordpress/safe';", + options, + errors: [ + { + message: + 'Usage of `__experimentalUnsafe` from `@wordpress/safe` is not allowed', + type: 'ImportSpecifier', + }, + ], + }, + { + code: "import { __experimentalSafe } from '@wordpress/unsafe';", + options, + errors: [ + { + message: + 'Usage of `__experimentalSafe` from `@wordpress/unsafe` is not allowed', + type: 'ImportSpecifier', + }, + ], + }, + { + code: + "import { feature, __experimentalSafe } from '@wordpress/unsafe';", + options, + errors: [ + { + message: + 'Usage of `__experimentalSafe` from `@wordpress/unsafe` is not allowed', + type: 'ImportSpecifier', + }, + ], + }, + { + code: "import s, { __experimentalUnsafe } from '@wordpress/safe';", + options, + errors: [ + { + message: + 'Usage of `__experimentalUnsafe` from `@wordpress/safe` is not allowed', + type: 'ImportSpecifier', + }, + ], + }, + { + code: "import { __unstableIsNeverSafe } from '@wordpress/safe';", + options, + errors: [ + { + message: + 'Usage of `__unstableIsNeverSafe` from `@wordpress/safe` is not allowed', + type: 'ImportSpecifier', + }, + ], + }, + ], +} ); diff --git a/packages/eslint-plugin/rules/no-unsafe-wp-apis.js b/packages/eslint-plugin/rules/no-unsafe-wp-apis.js new file mode 100644 index 00000000000000..a0889ab92904ec --- /dev/null +++ b/packages/eslint-plugin/rules/no-unsafe-wp-apis.js @@ -0,0 +1,95 @@ +/** @type {import('eslint').Rule.RuleModule} */ +module.exports = { + type: 'problem', + meta: { + messages: { + noUnsafeFeatures: + 'Usage of `{{importedName}}` from `{{sourceModule}}` is not allowed', + }, + schema: [ + { + type: 'object', + additionalProperties: false, + patternProperties: { + '^@wordpress\\/[a-zA-Z0-9_-]+$': { + type: 'array', + uniqueItems: true, + minItems: 1, + items: { + type: 'string', + pattern: '^__experimental', + }, + }, + }, + }, + ], + }, + create( context ) { + /** @type {AllowedImportsMap} */ + const allowedImports = + ( context.options && + typeof context.options[ 0 ] === 'object' && + context.options[ 0 ] ) || + {}; + const reporter = makeListener( { allowedImports, context } ); + + return { ImportDeclaration: reporter }; + }, +}; + +/** + * @param {Object} _ + * @param {AllowedImportsMap} _.allowedImports + * @param {import('eslint').Rule.RuleContext} _.context + * + * @return {(node: Node) => void} Listener function + */ +function makeListener( { allowedImports, context } ) { + return function reporter( node ) { + if ( node.type !== 'ImportDeclaration' ) { + return; + } + if ( typeof node.source.value !== 'string' ) { + return; + } + + const sourceModule = node.source.value.trim(); + + // Only interested in @wordpress/* packages + if ( ! sourceModule.startsWith( '@wordpress/' ) ) { + return; + } + + const allowedImportNames = allowedImports[ sourceModule ] || []; + + node.specifiers.forEach( ( specifierNode ) => { + if ( specifierNode.type !== 'ImportSpecifier' ) { + return; + } + + const importedName = specifierNode.imported.name; + + const data = { + sourceModule, + importedName, + }; + + if ( + // Unstable is never allowed + importedName.startsWith( '__unstable' ) || + // Experimental may be allowed + ( importedName.startsWith( '__experimental' ) && + ! allowedImportNames.includes( importedName ) ) + ) { + context.report( { + messageId: 'noUnsafeFeatures', + node: specifierNode, + data, + } ); + } + } ); + }; +} + +/** @typedef {import('estree').Node} Node */ +/** @typedef {Record} AllowedImportsMap */ From 97524e27a133d8b8a9440a1d1126892c2d387ce2 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 26 Nov 2020 14:31:25 +0100 Subject: [PATCH 05/15] Add rule to recommended set --- packages/eslint-plugin/configs/custom.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/eslint-plugin/configs/custom.js b/packages/eslint-plugin/configs/custom.js index bd3d2db3b7429e..8964c5e5b82fc7 100644 --- a/packages/eslint-plugin/configs/custom.js +++ b/packages/eslint-plugin/configs/custom.js @@ -7,6 +7,7 @@ module.exports = { '@wordpress/no-global-active-element': 'error', '@wordpress/no-global-get-selection': 'error', '@wordpress/no-global-event-listener': 'warn', + '@wordpress/no-unsafe-wp-apis': 'error', }, overrides: [ { From 8e617ef0d37cf100b0ce83f8ce5d13e40d9a37cf Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 26 Nov 2020 14:56:26 +0100 Subject: [PATCH 06/15] Allow unstable configuration --- .../rules/__tests__/no-unsafe-wp-apis.js | 33 ++++++++++------- .../eslint-plugin/rules/no-unsafe-wp-apis.js | 35 ++++++++++--------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js b/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js index bb30e75dee1fd0..0feb1602a5f7bd 100644 --- a/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js +++ b/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js @@ -15,7 +15,9 @@ const ruleTester = new RuleTester( { }, } ); -const options = [ { '@wordpress/safe': [ '__experimentalSafe' ] } ]; +const options = [ + { '@wordpress/package': [ '__experimentalSafe', '__unstableSafe' ] }, +]; ruleTester.run( 'wp-no-unsafe-features', rule, { valid: [ @@ -33,32 +35,36 @@ ruleTester.run( 'wp-no-unsafe-features', rule, { { code: "import _, { __unstableFoo } from './x';", options }, { code: "import * as _ from './x';", options }, - { code: "import s from '@wordpress/safe';", options }, - { code: "import { feature } from '@wordpress/safe';", options }, + { code: "import s from '@wordpress/package';", options }, + { code: "import { feature } from '@wordpress/package';", options }, { - code: "import { __experimentalSafe } from '@wordpress/safe';", + code: "import { __experimentalSafe } from '@wordpress/package';", + options, + }, + { + code: "import { __unstableSafe } from '@wordpress/package';", options, }, { code: - "import { feature, __experimentalSafe } from '@wordpress/safe';", + "import { feature, __experimentalSafe } from '@wordpress/package';", options, }, { - code: "import s, { __experimentalSafe } from '@wordpress/safe';", + code: "import s, { __experimentalSafe } from '@wordpress/package';", options, }, - { code: "import * as s from '@wordpress/safe';", options }, + { code: "import * as s from '@wordpress/package';", options }, ], invalid: [ { - code: "import { __experimentalUnsafe } from '@wordpress/safe';", + code: "import { __experimentalUnsafe } from '@wordpress/package';", options, errors: [ { message: - 'Usage of `__experimentalUnsafe` from `@wordpress/safe` is not allowed', + 'Usage of `__experimentalUnsafe` from `@wordpress/package` is not allowed', type: 'ImportSpecifier', }, ], @@ -87,23 +93,24 @@ ruleTester.run( 'wp-no-unsafe-features', rule, { ], }, { - code: "import s, { __experimentalUnsafe } from '@wordpress/safe';", + code: + "import s, { __experimentalUnsafe } from '@wordpress/package';", options, errors: [ { message: - 'Usage of `__experimentalUnsafe` from `@wordpress/safe` is not allowed', + 'Usage of `__experimentalUnsafe` from `@wordpress/package` is not allowed', type: 'ImportSpecifier', }, ], }, { - code: "import { __unstableIsNeverSafe } from '@wordpress/safe';", + code: "import { __unstableFeature } from '@wordpress/package';", options, errors: [ { message: - 'Usage of `__unstableIsNeverSafe` from `@wordpress/safe` is not allowed', + 'Usage of `__unstableFeature` from `@wordpress/package` is not allowed', type: 'ImportSpecifier', }, ], diff --git a/packages/eslint-plugin/rules/no-unsafe-wp-apis.js b/packages/eslint-plugin/rules/no-unsafe-wp-apis.js index a0889ab92904ec..1d2b81fe3db7c9 100644 --- a/packages/eslint-plugin/rules/no-unsafe-wp-apis.js +++ b/packages/eslint-plugin/rules/no-unsafe-wp-apis.js @@ -17,7 +17,7 @@ module.exports = { minItems: 1, items: { type: 'string', - pattern: '^__experimental', + pattern: '^(?:__experimental|__unstable)', }, }, }, @@ -55,7 +55,7 @@ function makeListener( { allowedImports, context } ) { const sourceModule = node.source.value.trim(); - // Only interested in @wordpress/* packages + // Ignore non-WordPress packages if ( ! sourceModule.startsWith( '@wordpress/' ) ) { return; } @@ -69,24 +69,25 @@ function makeListener( { allowedImports, context } ) { const importedName = specifierNode.imported.name; - const data = { - sourceModule, - importedName, - }; - if ( - // Unstable is never allowed - importedName.startsWith( '__unstable' ) || - // Experimental may be allowed - ( importedName.startsWith( '__experimental' ) && - ! allowedImportNames.includes( importedName ) ) + ! importedName.startsWith( '__unstable' ) && + ! importedName.startsWith( '__experimental' ) ) { - context.report( { - messageId: 'noUnsafeFeatures', - node: specifierNode, - data, - } ); + return; } + + if ( allowedImportNames.includes( importedName ) ) { + return; + } + + context.report( { + messageId: 'noUnsafeFeatures', + node: specifierNode, + data: { + sourceModule, + importedName, + }, + } ); } ); }; } From ddcbcd102435152fdb605ff98d432a8c76c91790 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 26 Nov 2020 15:00:01 +0100 Subject: [PATCH 07/15] Add changelog entry --- packages/eslint-plugin/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/eslint-plugin/CHANGELOG.md b/packages/eslint-plugin/CHANGELOG.md index a977dccfde44cb..8b7cdf1b474c8c 100644 --- a/packages/eslint-plugin/CHANGELOG.md +++ b/packages/eslint-plugin/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Enhancements + +- Add `no-unsafe-wp-apis` rule to discourage usage of unsafe APIs ([#27301](https://github.com/WordPress/gutenberg/pull/27301)). + ### Documentation - Include a note about the minimum version required for `node` (10.0.0) and `npm` (6.9.0). From 56a5f76d642bb5cf15eaa77a351b9c40228b15ff Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 26 Nov 2020 15:04:23 +0100 Subject: [PATCH 08/15] Disable for Gutenberg --- .eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintrc.js b/.eslintrc.js index 6d841c60444bed..3ebed48f03c79a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -54,6 +54,7 @@ module.exports = { allowedTextDomain: 'default', }, ], + '@wordpress/no-unsafe-wp-apis': 'off', 'no-restricted-syntax': [ 'error', // NOTE: We can't include the forward slash in our regex or From 55bf10502ee8c20e14d3bb07e895c140dac14576 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 26 Nov 2020 16:24:37 +0100 Subject: [PATCH 09/15] fixup! Allow unstable configuration --- packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md b/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md index 49e02a6497dadd..18a2a36417ad51 100644 --- a/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md +++ b/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md @@ -29,8 +29,7 @@ import { registerBlockType } from '@wordpress/blocks'; ## Options The rule can be configured via an object. -This should be an object where the keys are import package names and the values are arrays of allowed experimental imports. -Note that `unstable` features are not intended for public use and the rule cannot be configured to allow them. +This should be an object where the keys are import package names and the values are arrays of allowed unsafe imports. #### Example configuration From fca1fde25458fead964d4bb70063e6e67086b31c Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 26 Nov 2020 16:24:49 +0100 Subject: [PATCH 10/15] Fix rule name in config doc --- packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md b/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md index 18a2a36417ad51..59213648efcab5 100644 --- a/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md +++ b/packages/eslint-plugin/docs/rules/no-unsafe-wp-apis.md @@ -35,7 +35,7 @@ This should be an object where the keys are import package names and the values ```json { - "wpcalypso/wp-no-unsafe-features": [ + "@wordpress/no-unsafe-wp-apis": [ "error", { "@wordpress/block-editor": [ "__experimentalBlock" ] } ] From 4e312be512f08507ce6e87865bd44016a5101aed Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 26 Nov 2020 16:28:57 +0100 Subject: [PATCH 11/15] Fix rule name in test --- packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js b/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js index 0feb1602a5f7bd..dcf16bb5e84f3c 100644 --- a/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js +++ b/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js @@ -19,7 +19,7 @@ const options = [ { '@wordpress/package': [ '__experimentalSafe', '__unstableSafe' ] }, ]; -ruleTester.run( 'wp-no-unsafe-features', rule, { +ruleTester.run( 'no-unsafe-wp-apis', rule, { valid: [ { code: "import _ from 'lodash';", options }, { code: "import { map } from 'lodash';", options }, From 2485c50e8ac0795c4559e0ec50cf0de3a19f1c1a Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 27 Nov 2020 13:42:52 +0100 Subject: [PATCH 12/15] Simplify single message --- packages/eslint-plugin/rules/no-unsafe-wp-apis.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/packages/eslint-plugin/rules/no-unsafe-wp-apis.js b/packages/eslint-plugin/rules/no-unsafe-wp-apis.js index 1d2b81fe3db7c9..aa74b6d47bbea9 100644 --- a/packages/eslint-plugin/rules/no-unsafe-wp-apis.js +++ b/packages/eslint-plugin/rules/no-unsafe-wp-apis.js @@ -2,10 +2,6 @@ module.exports = { type: 'problem', meta: { - messages: { - noUnsafeFeatures: - 'Usage of `{{importedName}}` from `{{sourceModule}}` is not allowed', - }, schema: [ { type: 'object', @@ -81,12 +77,8 @@ function makeListener( { allowedImports, context } ) { } context.report( { - messageId: 'noUnsafeFeatures', + message: `Usage of \`${ importedName }\` from \`${ sourceModule }\` is not allowed`, node: specifierNode, - data: { - sourceModule, - importedName, - }, } ); } ); }; From e300a6ba38e1bb9524ec0a2c8360d2010bb43990 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 27 Nov 2020 14:02:03 +0100 Subject: [PATCH 13/15] Remove from recomended configuration --- .eslintrc.js | 1 - packages/eslint-plugin/configs/custom.js | 1 - 2 files changed, 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 3ebed48f03c79a..6d841c60444bed 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -54,7 +54,6 @@ module.exports = { allowedTextDomain: 'default', }, ], - '@wordpress/no-unsafe-wp-apis': 'off', 'no-restricted-syntax': [ 'error', // NOTE: We can't include the forward slash in our regex or diff --git a/packages/eslint-plugin/configs/custom.js b/packages/eslint-plugin/configs/custom.js index 8964c5e5b82fc7..bd3d2db3b7429e 100644 --- a/packages/eslint-plugin/configs/custom.js +++ b/packages/eslint-plugin/configs/custom.js @@ -7,7 +7,6 @@ module.exports = { '@wordpress/no-global-active-element': 'error', '@wordpress/no-global-get-selection': 'error', '@wordpress/no-global-event-listener': 'warn', - '@wordpress/no-unsafe-wp-apis': 'error', }, overrides: [ { From 545329b7e16f56c9dd7a747a86185b04388f361b Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 27 Nov 2020 14:51:38 +0100 Subject: [PATCH 14/15] Add link to unsafe API documentation --- .../rules/__tests__/no-unsafe-wp-apis.js | 20 +++++++++---------- .../eslint-plugin/rules/no-unsafe-wp-apis.js | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js b/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js index dcf16bb5e84f3c..ed7b0ce1684a4b 100644 --- a/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js +++ b/packages/eslint-plugin/rules/__tests__/no-unsafe-wp-apis.js @@ -63,8 +63,8 @@ ruleTester.run( 'no-unsafe-wp-apis', rule, { options, errors: [ { - message: - 'Usage of `__experimentalUnsafe` from `@wordpress/package` is not allowed', + message: `Usage of \`__experimentalUnsafe\` from \`@wordpress/package\` is not allowed. +See https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.`, type: 'ImportSpecifier', }, ], @@ -74,8 +74,8 @@ ruleTester.run( 'no-unsafe-wp-apis', rule, { options, errors: [ { - message: - 'Usage of `__experimentalSafe` from `@wordpress/unsafe` is not allowed', + message: `Usage of \`__experimentalSafe\` from \`@wordpress/unsafe\` is not allowed. +See https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.`, type: 'ImportSpecifier', }, ], @@ -86,8 +86,8 @@ ruleTester.run( 'no-unsafe-wp-apis', rule, { options, errors: [ { - message: - 'Usage of `__experimentalSafe` from `@wordpress/unsafe` is not allowed', + message: `Usage of \`__experimentalSafe\` from \`@wordpress/unsafe\` is not allowed. +See https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.`, type: 'ImportSpecifier', }, ], @@ -98,8 +98,8 @@ ruleTester.run( 'no-unsafe-wp-apis', rule, { options, errors: [ { - message: - 'Usage of `__experimentalUnsafe` from `@wordpress/package` is not allowed', + message: `Usage of \`__experimentalUnsafe\` from \`@wordpress/package\` is not allowed. +See https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.`, type: 'ImportSpecifier', }, ], @@ -109,8 +109,8 @@ ruleTester.run( 'no-unsafe-wp-apis', rule, { options, errors: [ { - message: - 'Usage of `__unstableFeature` from `@wordpress/package` is not allowed', + message: `Usage of \`__unstableFeature\` from \`@wordpress/package\` is not allowed. +See https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.`, type: 'ImportSpecifier', }, ], diff --git a/packages/eslint-plugin/rules/no-unsafe-wp-apis.js b/packages/eslint-plugin/rules/no-unsafe-wp-apis.js index aa74b6d47bbea9..65bcc2170d45ba 100644 --- a/packages/eslint-plugin/rules/no-unsafe-wp-apis.js +++ b/packages/eslint-plugin/rules/no-unsafe-wp-apis.js @@ -77,7 +77,7 @@ function makeListener( { allowedImports, context } ) { } context.report( { - message: `Usage of \`${ importedName }\` from \`${ sourceModule }\` is not allowed`, + message: `Usage of \`${ importedName }\` from \`${ sourceModule }\` is not allowed.\nSee https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/#experimental-and-unstable-apis for details.`, node: specifierNode, } ); } ); From e3d2795448d904165ab86671152df2db598d051f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 27 Nov 2020 14:55:55 +0100 Subject: [PATCH 15/15] Fix changelog type --- packages/eslint-plugin/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/CHANGELOG.md b/packages/eslint-plugin/CHANGELOG.md index 8b7cdf1b474c8c..794cde5a388552 100644 --- a/packages/eslint-plugin/CHANGELOG.md +++ b/packages/eslint-plugin/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -### Enhancements +### New Feature - Add `no-unsafe-wp-apis` rule to discourage usage of unsafe APIs ([#27301](https://github.com/WordPress/gutenberg/pull/27301)).