-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: @lexical/eslint-plugin - add a rules-of-lexical linter to help …
…with $function rules
- Loading branch information
Showing
19 changed files
with
926 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
'use strict'; | ||
/** | ||
* This file is here for bootstrapping reasons so we can use it without | ||
* building anything | ||
*/ | ||
module.exports = require('./src/LexicalEslintPlugin.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# `@lexical/eslint-plugin` | ||
|
||
This ESLint plugin enforces the [Lexical $function convention](https://lexical.dev/docs/intro#reading-and-updating-editor-state). | ||
|
||
## Installation | ||
|
||
Assuming you already have ESLint installed, run: | ||
|
||
```sh | ||
npm install @lexical/eslint-plugin --save-dev | ||
``` | ||
|
||
Then extend the recommended eslint config: | ||
|
||
```js | ||
{ | ||
"extends": [ | ||
// ... | ||
"plugin:@lexical/recommended" | ||
] | ||
} | ||
``` | ||
|
||
### Custom Configuration | ||
|
||
If you want more fine-grained configuration, you can instead add a snippet like this to your ESLint configuration file: | ||
|
||
```js | ||
{ | ||
"plugins": [ | ||
// ... | ||
"@lexical" | ||
], | ||
"rules": { | ||
// ... | ||
"@lexical/rules-of-lexical": "error" | ||
} | ||
} | ||
``` | ||
|
||
|
||
## Valid and Invalid Examples | ||
|
||
### Valid Examples | ||
|
||
\$functions may be called by other \$functions | ||
|
||
```js | ||
function $namedCorrectly() { | ||
return $getRoot(); | ||
} | ||
``` | ||
|
||
\$functions may be called in `editor.update` or `editorState.read` | ||
|
||
```js | ||
function validUsesEditorOrState(editor) { | ||
editor.update(() => $getRoot()); | ||
editor.getLatestState().read(() => $getRoot()); | ||
} | ||
``` | ||
|
||
\$functions may be called from class methods | ||
|
||
```js | ||
class CustomNode extends ElementNode { | ||
appendText(string) { | ||
this.appendChild($createTextNode(string)); | ||
} | ||
} | ||
``` | ||
|
||
### Invalid Examples | ||
|
||
#### Rename autofix | ||
|
||
```js | ||
function invalidFunction() { | ||
return $getRoot(); | ||
} | ||
function $callsInvalidFunction() { | ||
return invalidFunction(); | ||
} | ||
``` | ||
|
||
*Autofix:* The function is renamed with a $ prefix. Any references to this | ||
name in this module are also always renamed. | ||
|
||
```js | ||
function $invalidFunction() { | ||
return $getRoot(); | ||
} | ||
function $callsInvalidFunction() { | ||
return $invalidFunction(); | ||
} | ||
``` | ||
|
||
#### Rename & deprecate autofix | ||
|
||
```js | ||
export function exportedInvalidFunction() { | ||
return $getRoot(); | ||
} | ||
``` | ||
|
||
*Autofix:* The exported function is renamed with a $ prefix. The previous name | ||
is also exported and marked deprecated, because automatic renaming of | ||
references to that name is limited to the module's scope. | ||
|
||
```js | ||
export function $exportedInvalidFunction() { | ||
return $getRoot(); | ||
} | ||
/** @deprecated renamed to $exportedInvalidFunction by @lexical/eslint-plugin rules-of-lexical */ | ||
export const exportedInvalidFunction = $exportedInvalidFunction; | ||
``` | ||
|
||
#### Rename scope conflict | ||
|
||
```js | ||
import {$getRoot} from 'lexical'; | ||
function InvalidComponent({editor}) { | ||
const [editor] = useLexicalComposerContext(); | ||
const getRoot = useCallback(() => $getRoot(), []); | ||
return (<button onClick={() => editor.update(() => getRoot())} />); | ||
} | ||
``` | ||
|
||
*Autofix:* The function is renamed with a $ prefix and _ suffix since the suggested name was already in scope. | ||
|
||
```js | ||
import {$getRoot} from 'lexical'; | ||
function InvalidComponent({editor}) { | ||
const [editor] = useLexicalComposerContext(); | ||
const $getRoot_ = useCallback(() => $getRoot(), []); | ||
return (<button onClick={() => editor.update(() => $getRoot_())} />); | ||
} | ||
``` |
11 changes: 11 additions & 0 deletions
11
packages/lexical-eslint-plugin/flow/LexicalEslintPlugin.js.flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
/** | ||
* LexicalEslintPlugin | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"name": "@lexical/eslint-plugin", | ||
"description": "Lexical specific linting rules for ESLint", | ||
"keywords": [ | ||
"eslint", | ||
"eslint-plugin", | ||
"eslintplugin", | ||
"lexical", | ||
"editor" | ||
], | ||
"version": "0.14.5", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/facebook/lexical.git", | ||
"directory": "packages/lexical-eslint-plugin" | ||
}, | ||
"main": "LexicalEslintPlugin.js", | ||
"types": "index.d.ts", | ||
"bugs": { | ||
"url": "https://github.com/facebook/lexical/issues" | ||
}, | ||
"homepage": "https://github.com/facebook/lexical#readme", | ||
"sideEffects": false, | ||
"peerDependencies": { | ||
"eslint": ">=7.31.0 || ^8.0.0" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./index.d.ts", | ||
"development": "./LexicalEslintPlugin.dev.mjs", | ||
"production": "./LexicalEslintPlugin.prod.mjs", | ||
"node": "./LexicalEslintPlugin.node.mjs", | ||
"default": "./LexicalEslintPlugin.mjs" | ||
}, | ||
"require": { | ||
"types": "./index.d.ts", | ||
"development": "./LexicalEslintPlugin.dev.js", | ||
"production": "./LexicalEslintPlugin.prod.js", | ||
"default": "./LexicalEslintPlugin.js" | ||
} | ||
} | ||
}, | ||
"devDependencies": { | ||
"@types/eslint": "^8.56.9" | ||
}, | ||
"module": "LexicalEslintPlugin.mjs" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
// @ts-check | ||
|
||
const {name, version} = require('../package.json'); | ||
const rulesOfLexical = require('./rules/rules-of-lexical.js'); | ||
|
||
const all = { | ||
plugins: ['@lexical'], | ||
rules: { | ||
'@lexical/rules-of-lexical': 'warn', | ||
}, | ||
}; | ||
|
||
const plugin = { | ||
configs: { | ||
all, | ||
recommended: all, | ||
}, | ||
meta: {name, version}, | ||
rules: { | ||
'rules-of-lexical': rulesOfLexical, | ||
}, | ||
}; | ||
|
||
module.exports = plugin; |
Oops, something went wrong.