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

feat(@lexical/eslint-plugin): new package with eslint rules for lexical #5908

Merged
merged 4 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
const restrictedGlobals = require('confusing-browser-globals');

const OFF = 0;
const WARN = 1;
const ERROR = 2;

module.exports = {
Expand Down Expand Up @@ -64,6 +65,7 @@ module.exports = {
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@lexical/all',
],
files: ['**/*.ts', '**/*.tsx'],
parser: '@typescript-eslint/parser',
Expand All @@ -72,15 +74,28 @@ module.exports = {
},
plugins: ['react', '@typescript-eslint', 'header'],
rules: {
'@lexical/rules-of-lexical': [
WARN,
/** @type import('./packages/lexical-eslint-plugin/src').RulesOfLexicalOptions */ ({
isDollarFunction: ['^INTERNAL_\\$'],
isIgnoredFunction: [
// @lexical/yjs
'createBinding',
],
isSafeDollarFunction: '$createRootNode',
Copy link
Contributor

Choose a reason for hiding this comment

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

just a question, is this supposed to be array as well like other ?

isSafeDollarFunction: ['$createRootNode'],

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Either works, I wrote it this way mostly to demonstrate that you do not need to wrap it with an array. Basically a convenience for OSS users who may just have one pattern or string to configure.

Here are some of the relevant types, I pasted them here together:

/**
 * @typedef {(name: string, node: Identifier) => boolean} NameIdentifierMatcher
 * @typedef {NameIdentifierMatcher | string | RegExp | undefined} ToMatcher
 * @typedef {Partial<BaseMatchers<ToMatcher | ToMatcher[]>>} RulesOfLexicalOptions
 */
/**
 * @template T
 * @typedef {Object} BaseMatchers<T>
 * @property {T} isDollarFunction Catch all identifiers that begin with '$' or 'INTERNAL_$' followed by a lowercase Latin character or underscore
 * @property {T} isIgnoredFunction These functions may call any $functions even though they do not have the isDollarFunction naming convention
 * @property {T} isLexicalProvider Certain calls through the editor or editorState allow for implicit access to call $functions: read, registerCommand, registerNodeTransform, update.
 * @property {T} isSafeDollarFunction It's usually safe to call $isNode functions, so any '$is' or 'INTERNAL_$is' function may be called in any context.
 */

}),
],
'@typescript-eslint/ban-ts-comment': OFF,
'@typescript-eslint/no-this-alias': OFF,
'@typescript-eslint/no-unused-vars': [ERROR, {args: 'none'}],
'header/header': [2, 'scripts/www/headerTemplate.js'],
},
},
{
// These aren't compiled, but they're written in module JS
files: ['packages/lexical-playground/esm/*.mjs'],
files: [
// These aren't compiled, but they're written in module JS
'packages/lexical-playground/esm/*.mjs',
],
parserOptions: {
sourceType: 'module',
},
Expand Down Expand Up @@ -119,6 +134,7 @@ module.exports = {
'react',
'no-only-tests',
'lexical',
'@lexical',
],

// Stop ESLint from looking for a configuration file in parent folders
Expand Down
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.name_mapper='^@lexical/clipboard$' -> '<PROJECT_ROOT>/packages/lexical-cl
module.name_mapper='^@lexical/code$' -> '<PROJECT_ROOT>/packages/lexical-code/flow/LexicalCode.js.flow'
module.name_mapper='^@lexical/devtools-core$' -> '<PROJECT_ROOT>/packages/lexical-devtools-core/flow/LexicalDevtoolsCore.js.flow'
module.name_mapper='^@lexical/dragon$' -> '<PROJECT_ROOT>/packages/lexical-dragon/flow/LexicalDragon.js.flow'
module.name_mapper='^@lexical/eslint-plugin$' -> '<PROJECT_ROOT>/packages/lexical-eslint-plugin/flow/LexicalEslintPlugin.js.flow'
module.name_mapper='^@lexical/file$' -> '<PROJECT_ROOT>/packages/lexical-file/flow/LexicalFile.js.flow'
module.name_mapper='^@lexical/hashtag$' -> '<PROJECT_ROOT>/packages/lexical-hashtag/flow/LexicalHashtag.js.flow'
module.name_mapper='^@lexical/headless$' -> '<PROJECT_ROOT>/packages/lexical-headless/flow/LexicalHeadless.js.flow'
Expand Down
2 changes: 1 addition & 1 deletion eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"description": "ESLint plugin for lexical",
"main": "src/index.js",
"peerDependencies": {
"eslint": ">=4.19.1"
"eslint": "^7.31.0 || ^8.0.0"
}
}
35 changes: 28 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"@babel/preset-flow": "^7.24.1",
"@babel/preset-react": "^7.24.1",
"@babel/preset-typescript": "^7.24.1",
"@lexical/eslint-plugin": "file:./packages/lexical-eslint-plugin",
"@playwright/test": "^1.41.2",
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-babel": "^6.0.4",
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-devtools/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@lexical/code": ["../lexical-code/src/index.ts"],
"@lexical/devtools-core": ["../lexical-devtools-core/src/index.ts"],
"@lexical/dragon": ["../lexical-dragon/src/index.ts"],
"@lexical/eslint-plugin": ["../lexical-eslint-plugin/src/index.ts"],
"@lexical/file": ["../lexical-file/src/index.ts"],
"@lexical/hashtag": ["../lexical-hashtag/src/index.ts"],
"@lexical/headless": ["../lexical-headless/src/index.ts"],
Expand Down
12 changes: 12 additions & 0 deletions packages/lexical-eslint-plugin/LexicalEslintPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 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 and still comply with the monorepo conventions
module.exports = require('./src/LexicalEslintPlugin.js');
Loading
Loading