forked from BuilderIO/mitosis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Misc: ESLint plugin testing (BuilderIO#456)
* fix: root eslint config * examples: add eslint plugin to example * docs(eslint): document recommended config * chore: remove commented out code * fix buggy name * chore(eslint): move rule into file * fix(eslint): strongly type recommended rules to match rules * fix(tests): move test and fix import path * publish eslint plugin * publish eslint plugin
- Loading branch information
Showing
14 changed files
with
444 additions
and
144 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
undecided: | ||
- "@builder.io/mitosis-repo" | ||
- "@builder.io/basic-example" |
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,2 @@ | ||
node_modules | ||
output/ |
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,20 @@ | ||
module.exports = { | ||
env: { | ||
browser: true, | ||
es2021: true, | ||
}, | ||
extends: [ | ||
'eslint:recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
'plugin:@builder.io/mitosis/recommended', | ||
], | ||
parser: '@typescript-eslint/parser', | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
}, | ||
plugins: ['@typescript-eslint', '@builder.io/mitosis'], | ||
}; |
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
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 |
---|---|---|
@@ -1,24 +1,33 @@ | ||
import { rules } from '../rules'; | ||
|
||
const PLUGIN_NAME = '@builder.io/mitosis' as const; | ||
|
||
type RulesKeys = `${typeof PLUGIN_NAME}/${keyof typeof rules}`; | ||
|
||
const recommendedRules: Record<RulesKeys, string> = { | ||
'@builder.io/mitosis/css-no-vars': 'error', | ||
'@builder.io/mitosis/jsx-callback-arg-name': 'error', | ||
'@builder.io/mitosis/jsx-callback-arrow-function': 'error', | ||
'@builder.io/mitosis/no-assign-props-to-state': 'error', | ||
'@builder.io/mitosis/no-async-methods-on-state': 'error', | ||
'@builder.io/mitosis/no-conditional-logic-in-component-render': 'error', | ||
'@builder.io/mitosis/no-state-destructuring': 'error', | ||
'@builder.io/mitosis/no-var-declaration-in-jsx': 'error', | ||
'@builder.io/mitosis/no-var-declaration-or-assignment-in-component': 'error', | ||
'@builder.io/mitosis/no-var-name-same-as-state-property': 'error', | ||
'@builder.io/mitosis/only-default-function-and-imports': 'error', | ||
'@builder.io/mitosis/ref-no-current': 'error', | ||
'@builder.io/mitosis/use-state-var-declarator': 'error', | ||
'@builder.io/mitosis/static-control-flow': 'error', | ||
'@builder.io/mitosis/no-var-name-same-as-prop-name': 'error', | ||
}; | ||
|
||
export default { | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
plugins: ['@builder.io/mitosis'], | ||
rules: { | ||
'@builder.io/mitosis/css-no-vars': 'error', | ||
'@builder.io/mitosis/jsx-callback-arg-name': 'error', | ||
'@builder.io/mitosis/jsx-callback-arrow-function': 'error', | ||
'@builder.io/mitosis/no-assign-props-to-state': 'error', | ||
'@builder.io/mitosis/no-async-methods-on-state': 'error', | ||
'@builder.io/mitosis/no-conditional-logic-in-component-render': 'error', | ||
'@builder.io/mitosis/no-state-destructuring': 'error', | ||
'@builder.io/mitosis/no-var-declaration-in-jsx': 'error', | ||
'@builder.io/mitosis/no-var-declaration-or-assignment-in-component': | ||
'error', | ||
'@builder.io/mitosis/no-var-name-same-as-state-property': 'error', | ||
'@builder.io/mitosis/only-default-function-and-imports': 'error', | ||
'@builder.io/mitosis/ref-no-current': 'error', | ||
'@builder.io/mitosis/use-state-var-declarator': 'error', | ||
}, | ||
plugins: [PLUGIN_NAME], | ||
rules: recommendedRules, | ||
}; |
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
2 changes: 1 addition & 1 deletion
2
...in/src/__tests__/no-conditional-render.ts → ...rc/rules/__tests__/static-control-flow.ts
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,35 @@ | ||
import cssNoVars from './css-no-vars'; | ||
import refNoCurrent from './ref-no-current'; | ||
import noStateDestructuring from './no-state-destructuring'; | ||
import jsxCallbackArgNameRule from './jsx-callback-arg-name'; | ||
import noAssignPropsToState from './no-assign-props-to-state'; | ||
import useStateVarDeclarator from './use-state-var-declarator'; | ||
import noAsyncMethodsOnState from './no-async-methods-on-state'; | ||
import notVarDeclarationInJSX from './no-var-declaration-in-jsx'; | ||
import jsxCallbackArrowFunction from './jsx-callback-arrow-function'; | ||
import noVarNameSameAsPropName from './no-var-name-same-as-prop-name'; | ||
import noVarNameSameAsStateProperty from './no-var-name-same-as-state-property'; | ||
import onlyDefaultFunctionAndImports from './only-default-function-and-imports'; | ||
import noConditionalLogicInComponentRender from './no-conditional-logic-in-component-render'; | ||
import noVarDeclarationOrAssignmentInComponent from './no-var-declaration-or-assignment-in-component'; | ||
import { staticControlFlow } from './static-control-flow'; | ||
|
||
export const rules = { | ||
'css-no-vars': cssNoVars, | ||
'ref-no-current': refNoCurrent, | ||
'static-control-flow': staticControlFlow, | ||
'no-state-destructuring': noStateDestructuring, | ||
'jsx-callback-arg-name': jsxCallbackArgNameRule, | ||
'no-assign-props-to-state': noAssignPropsToState, | ||
'use-state-var-declarator': useStateVarDeclarator, | ||
'no-async-methods-on-state': noAsyncMethodsOnState, | ||
'no-var-declaration-in-jsx': notVarDeclarationInJSX, | ||
'no-var-name-same-as-prop-name': noVarNameSameAsPropName, | ||
'jsx-callback-arrow-function': jsxCallbackArrowFunction, | ||
'no-var-name-same-as-state-property': noVarNameSameAsStateProperty, | ||
'only-default-function-and-imports': onlyDefaultFunctionAndImports, | ||
'no-conditional-logic-in-component-render': | ||
noConditionalLogicInComponentRender, | ||
'no-var-declaration-or-assignment-in-component': | ||
noVarDeclarationOrAssignmentInComponent, | ||
}; |
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,69 @@ | ||
import { Rule } from 'eslint'; | ||
import * as types from '@babel/types'; | ||
import isMitosisPath from '../helpers/isMitosisPath'; | ||
|
||
export const staticControlFlow: Rule.RuleModule = { | ||
create(context) { | ||
if (!isMitosisPath(context.getFilename())) return {}; | ||
|
||
return { | ||
VariableDeclarator(node) { | ||
if (types.isVariableDeclarator(node)) { | ||
if ( | ||
types.isObjectPattern(node.id) && | ||
types.isIdentifier(node.init) && | ||
node.init.name === 'state' | ||
) { | ||
context.report({ | ||
node: node as any, | ||
message: | ||
'Destructuring the state object is currently not supported', | ||
}); | ||
} | ||
} | ||
}, | ||
|
||
CallExpression(node) { | ||
if (types.isCallExpression(node)) { | ||
if ( | ||
types.isIdentifier(node.callee) && | ||
node.callee.name === 'useEffect' | ||
) { | ||
const useEffectMessage = | ||
'Only useEffect with an empty array second argument is allowed. E.g. useEffect(...) must be useEffect(..., [])'; | ||
const secondArg = node.arguments[1]; | ||
if ( | ||
!( | ||
secondArg && | ||
types.isArrayExpression(secondArg) && | ||
secondArg.elements.length === 0 | ||
) | ||
) { | ||
context.report({ | ||
node: node, | ||
message: useEffectMessage, | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
|
||
JSXExpressionContainer(node) { | ||
if (types.isJSXExpressionContainer(node)) { | ||
if (types.isConditionalExpression(node.expression)) { | ||
if ( | ||
types.isJSXElement(node.expression.consequent) || | ||
types.isJSXElement(node.expression.alternate) | ||
) { | ||
context.report({ | ||
node: node as any, | ||
message: | ||
'Ternaries around JSX Elements are not currently supported. Instead use binary expressions - e.g. {foo ? <bar /> : <baz />} should be {foo && <bar />}{!foo && <baz />}', | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Oops, something went wrong.