Skip to content

Commit

Permalink
style: update code style
Browse files Browse the repository at this point in the history
  • Loading branch information
rEl1cx committed Jan 10, 2025
1 parent 58b2a06 commit 6456465
Show file tree
Hide file tree
Showing 45 changed files with 131 additions and 123 deletions.
1 change: 1 addition & 0 deletions dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"indentWidth": 2,
"quoteStyle": "preferDouble",
"quoteProps": "asNeeded",
"bracePosition": "maintain",
"importDeclaration.sortNamedImports": "maintain",
"exportDeclaration.sortNamedExports": "maintain",
"module.sortImportDeclarations": "maintain",
Expand Down
11 changes: 9 additions & 2 deletions eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const p11tGroups = {

const enableTypeCheckedRules = {
...tseslint.configs.strictTypeCheckedOnly
.map(x => x.rules)
.map((x) => x.rules)
.reduce((a, b) => ({ ...a, ...b }), {}),
...eslintPluginSafeTypeScript.configs.recommended.rules,
"@susisu/safe-typescript/no-unsafe-object-property-check": "off",
Expand All @@ -90,7 +90,7 @@ const enableTypeCheckedRules = {
}],
} as const;

const disableTypeCheckedRules = Object.fromEntries(Object.keys(enableTypeCheckedRules).map(x => [x, "off"]));
const disableTypeCheckedRules = Object.fromEntries(Object.keys(enableTypeCheckedRules).map((x) => [x, "off"]));

export default tseslint.config(
eslintConfigFlatGitignore(),
Expand Down Expand Up @@ -211,7 +211,14 @@ export default tseslint.config(
"simple-import-sort/exports": "warn",
"simple-import-sort/imports": "warn",
// Part: stylistic rules
"@stylistic/arrow-parens": ["warn", "always"],
"@stylistic/curly-newline": ["warn", "always"],
"@stylistic/no-multi-spaces": ["warn"],
"@stylistic/operator-linebreak": [
"warn",
"before",
],
"@stylistic/quote-props": ["error", "as-needed"],
// Part: perfectionist rules
"perfectionist/sort-exports": "off",
"perfectionist/sort-imports": "off",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/component/component-collector-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function useComponentCollectorLegacy() {
_: key,
id,
kind: "class",
name: O.flatMapNullable(id, n => n.name),
name: O.flatMapNullable(id, (n) => n.name),
node,
// TODO: Get displayName of class component
displayName: O.none(),
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/component/component-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function useComponentCollector(
const shouldDrop = AST.getNestedReturnStatements(entry.node.body)
.slice()
.reverse()
.some(r => {
.some((r) => {
return context.sourceCode.getScope(r).block === entry.node
&& r.argument != null
&& !JSX.isJSXValue(r.argument, jsxCtx, hint);
Expand Down Expand Up @@ -144,15 +144,15 @@ export function useComponentCollector(
) {
const { left, right } = node;
const mbComponentName = match(left.object)
.with({ type: T.Identifier }, n => O.some(n.name))
.with({ type: T.Identifier }, (n) => O.some(n.name))
.otherwise(O.none);
if (O.isNone(mbComponentName)) {
return;
}
const componentName = mbComponentName.value;
const component = Array
.from(components.values())
.findLast(({ name }) => O.exists(name, n => n === componentName));
.findLast(({ name }) => O.exists(name, (n) => n === componentName));
if (component == null) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/component/component-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const RE_COMPONENT_NAME = /^_?[A-Z]/u;

export function getComponentNameFromIdentifier(node: TSESTree.Identifier | TSESTree.Identifier[]) {
return Array.isArray(node)
? node.map(n => n.name).join(".")
? node.map((n) => n.name).join(".")
: node.name;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/element/get-element-represent-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getElementRepresentName(node: TSESTree.JSXOpeningElement, contex
return F.pipe(
O.fromNullable(polymorphicPropName),
O.flatMap(JSX.findPropInAttributes(node.attributes, context.sourceCode.getScope(node))),
O.flatMap(attr => JSX.getPropValue(attr, context.sourceCode.getScope(attr))),
O.flatMap((attr) => JSX.getPropValue(attr, context.sourceCode.getScope(attr))),
O.filter(isString),
O.getOrElse(() => rawElementName),
);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/element/hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export function isChildrenOfCreateElement(
O.fromNullable(node.parent),
O.filter(AST.is(T.CallExpression)),
O.filter(isCreateElementCall(context)),
O.exists(n =>
O.exists((n) =>
n.arguments
.slice(2)
.some(arg => arg === node)
.some((arg) => arg === node)
),
);
}
6 changes: 3 additions & 3 deletions packages/core/src/render-prop/hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export function isDeclaredInRenderPropLoose(node: TSESTree.Node) {

return F.pipe(
AST.findParentNodeGuard(node, AST.is(T.JSXExpressionContainer)),
O.flatMapNullable(c => c.parent),
O.flatMapNullable((c) => c.parent),
O.filter(AST.is(T.JSXAttribute)),
O.flatMapNullable(a => a.name),
O.exists(n =>
O.flatMapNullable((a) => a.name),
O.exists((n) =>
n.type === T.JSXIdentifier
&& n.name.startsWith("render")
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default createRule<[], MessageID>({
F.pipe(
O.some("dangerouslySetInnerHTML"),
O.flatMap(JSX.findPropInAttributes(node.openingElement.attributes, context.sourceCode.getScope(node))),
O.map(prop => ({ messageId: "noDangerouslySetInnerhtml", node: prop } as const)),
O.map((prop) => ({ messageId: "noDangerouslySetInnerhtml", node: prop } as const)),
O.map(context.report),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default createRule<[], MessageID>({
const isJavaScript = F.pipe(
JSX.getPropValue(node, context.sourceCode.getScope(node)),
O.filter(isString),
O.exists(v => RE_JAVASCRIPT_PROTOCOL.test(v)),
O.exists((v) => RE_JAVASCRIPT_PROTOCOL.test(v)),
);
if (isJavaScript) {
context.report({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ export default createRule<[], MessageID>({
const prop = mbProp.value;
const isSafeSandboxValue = !F.pipe(
JSX.getPropValue(prop, context.sourceCode.getScope(prop)),
O.flatMapNullable(v =>
O.flatMapNullable((v) =>
match(v)
.with(P.string, F.identity)
.with({ sandbox: P.string }, ({ sandbox }) => sandbox)
.otherwise(F.constNull)
),
O.filter(isString),
O.map((value) => value.split(" ")),
O.exists(values =>
unsafeCombinations.some(combinations => combinations.every(unsafeValue => values.includes(unsafeValue)))
O.exists((values) =>
unsafeCombinations.some((combinations) => combinations.every((unsafeValue) => values.includes(unsafeValue)))
),
);
if (isSafeSandboxValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ export default createRule<[], MessageID>({
name: RULE_NAME,
create(context) {
const settings = getSettingsFromContext(context);
const additionalComponents = settings.additionalComponents.filter(c => c.as === "a");
const additionalComponents = settings.additionalComponents.filter((c) => c.as === "a");
function getReportDescriptor(node: TSESTree.JSXElement): O.Option<ReportDescriptor<MessageID>> {
const name = JSX.getElementName(node.openingElement);
const elementName = getElementRepresentName(node.openingElement, context);
if (elementName !== "a" && !additionalComponents.some(c => c.re.test(name))) {
if (elementName !== "a" && !additionalComponents.some((c) => c.re.test(name))) {
return O.none();
}
const { attributes } = node.openingElement;
const initialScope = context.sourceCode.getScope(node);
const additionalAttributes = additionalComponents
.findLast(c => c.re.test(name))
.findLast((c) => c.re.test(name))
?.attributes
?? [];
const [
Expand All @@ -67,16 +67,16 @@ export default createRule<[], MessageID>({
? O.fromNullable(targetPropDefaultValue)
: F.pipe(
targetProp,
O.flatMap(attr => JSX.getPropValue(attr, context.sourceCode.getScope(attr))),
O.flatMapNullable(v =>
O.flatMap((attr) => JSX.getPropValue(attr, context.sourceCode.getScope(attr))),
O.flatMapNullable((v) =>
match(v)
.with(P.string, F.identity)
.with({ [targetPropName]: P.string }, (v) => v[targetPropName])
.otherwise(F.constNull)
),
O.filter(isString),
);
if (!O.exists(targetPropValue, t => t === "_blank")) {
if (!O.exists(targetPropValue, (t) => t === "_blank")) {
return O.none();
}
const [
Expand All @@ -88,8 +88,8 @@ export default createRule<[], MessageID>({
? O.fromNullable(hrefPropDefaultValue)
: F.pipe(
hrefProp,
O.flatMap(attr => JSX.getPropValue(attr, context.sourceCode.getScope(attr))),
O.flatMapNullable(v =>
O.flatMap((attr) => JSX.getPropValue(attr, context.sourceCode.getScope(attr))),
O.flatMapNullable((v) =>
match(v)
.with(P.string, F.identity)
.with({ [hrefPropName]: P.string }, (v) => v[hrefPropName])
Expand All @@ -109,8 +109,8 @@ export default createRule<[], MessageID>({
? O.fromNullable(relPropDefaultValue)
: F.pipe(
relProp,
O.flatMap(attr => JSX.getPropValue(attr, context.sourceCode.getScope(attr))),
O.flatMapNullable(v =>
O.flatMap((attr) => JSX.getPropValue(attr, context.sourceCode.getScope(attr))),
O.flatMapNullable((v) =>
match(v)
.with(P.string, F.identity)
.with({ [relPropName]: P.string }, (v) => v[relPropName])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,39 @@ export default createRule<[], MessageID>({
const hasEmptyDeps = F.pipe(
match(arg1)
.with({ type: T.ArrayExpression }, O.some)
.with({ type: T.Identifier }, n => {
.with({ type: T.Identifier }, (n) => {
return F.pipe(
VAR.findVariable(n.name, initialScope),
O.flatMap(VAR.getVariableNode(0)),
O.filter(AST.is(T.ArrayExpression)),
);
})
.otherwise(O.none),
O.exists(x => x.elements.length === 0),
O.exists((x) => x.elements.length === 0),
);
if (!hasEmptyDeps) {
return;
}
const isReferencedToComponentScope = F.pipe(
match(arg0)
.with({ type: T.ArrowFunctionExpression }, n => {
.with({ type: T.ArrowFunctionExpression }, (n) => {
if (n.body.type === T.ArrowFunctionExpression) {
return O.some(n.body);
}
return O.some(n);
})
.with({ type: T.FunctionExpression }, O.some)
.with({ type: T.Identifier }, n => {
.with({ type: T.Identifier }, (n) => {
return F.pipe(
VAR.findVariable(n.name, initialScope),
O.flatMap(VAR.getVariableNode(0)),
O.filter(AST.isFunction),
);
})
.otherwise(O.none),
O.map(n => context.sourceCode.getScope(n)),
O.map(s => VAR.getChidScopes(s).flatMap(x => x.references)),
O.exists(refs => refs.some(x => x.resolved?.scope.block === component)),
O.map((n) => context.sourceCode.getScope(n)),
O.map((s) => VAR.getChidScopes(s).flatMap((x) => x.references)),
O.exists((refs) => refs.some((x) => x.resolved?.scope.block === component)),
);
if (!isReferencedToComponentScope) {
context.report({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,48 +56,48 @@ export default createRule<[], MessageID>({
}
const hasCallInArg0 = F.pipe(
O.some(arg0),
O.filter(n => AST.isFunction(n)),
O.exists(n => [...AST.getNestedCallExpressions(n.body), ...AST.getNestedNewExpressions(n.body)].length > 0),
O.filter((n) => AST.isFunction(n)),
O.exists((n) => [...AST.getNestedCallExpressions(n.body), ...AST.getNestedNewExpressions(n.body)].length > 0),
);
if (hasCallInArg0) {
return;
}
const hasEmptyDeps = F.pipe(
match(arg1)
.with({ type: T.ArrayExpression }, O.some)
.with({ type: T.Identifier }, n => {
.with({ type: T.Identifier }, (n) => {
return F.pipe(
VAR.findVariable(n.name, initialScope),
O.flatMap(VAR.getVariableNode(0)),
O.filter(AST.is(T.ArrayExpression)),
);
})
.otherwise(O.none),
O.exists(x => x.elements.length === 0),
O.exists((x) => x.elements.length === 0),
);
if (!hasEmptyDeps) {
return;
}
const isReferencedToComponentScope = F.pipe(
match(arg0)
.with({ type: T.ArrowFunctionExpression }, n => {
.with({ type: T.ArrowFunctionExpression }, (n) => {
if (n.body.type === T.ArrowFunctionExpression) {
return O.some(n.body);
}
return O.some(n);
})
.with({ type: T.FunctionExpression }, O.some)
.with({ type: T.Identifier }, n => {
.with({ type: T.Identifier }, (n) => {
return F.pipe(
VAR.findVariable(n.name, initialScope),
O.flatMap(VAR.getVariableNode(0)),
O.filter(AST.isFunction),
);
})
.otherwise(O.none),
O.map(n => context.sourceCode.getScope(n)),
O.map(s => VAR.getChidScopes(s).flatMap(x => x.references)),
O.exists(refs => refs.some(x => x.resolved?.scope.block === component)),
O.map((n) => context.sourceCode.getScope(n)),
O.map((s) => VAR.getChidScopes(s).flatMap((x) => x.references)),
O.exists((refs) => refs.some((x) => x.resolved?.scope.block === component)),
);
if (!isReferencedToComponentScope) {
context.report({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function isFromUseStateCall(context: RuleContext, settings: ESLintReactSe
if (!("id" in parent && parent.id?.type === T.ArrayPattern)) {
return true;
}
return parent.id.elements.findIndex(e => e?.type === T.Identifier && e.name === topLevelId.name) === 1;
return parent.id.elements.findIndex((e) => e?.type === T.Identifier && e.name === topLevelId.name) === 1;
};
return isFromHookCall("useState", context, settings, predicate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function normalizeOptions(options: Options) {
}
return {
...opts,
excepts: opts.excepts?.map(pattern => new RegExp(pattern, "u")) ?? [],
excepts: opts.excepts?.map((pattern) => new RegExp(pattern, "u")) ?? [],
} as const;
}

Expand Down Expand Up @@ -174,8 +174,8 @@ export default createRule<Options, MessageID>({
for (const { node: component } of classComponents.values()) {
F.pipe(
AST.getClassIdentifier(component),
O.filter(id => !validate(id.name, options)),
O.map(id => {
O.filter((id) => !validate(id.name, options)),
O.map((id) => {
context.report({
messageId: "componentName",
node: id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default createRule<[], MessageID>({
F.pipe(
match<typeof id, O.Option<ReportDescriptor<MessageID>>>(id)
.with({ type: T.Identifier }, F.constant(descriptor))
.with({ type: T.ArrayPattern }, n => {
.with({ type: T.ArrayPattern }, (n) => {
const [state, setState] = n.elements;
if (state?.type === T.ObjectPattern && setState?.type === T.Identifier) {
return isSetterNameLoose(setState.name)
Expand Down
Loading

0 comments on commit 6456465

Please sign in to comment.