Skip to content

Commit

Permalink
refactor: reformat the code via eslint rules (#104)
Browse files Browse the repository at this point in the history
* refactor(useIndex): simplify retur statement

* chore: update eslint rules and prettier config
  • Loading branch information
HashCookie authored May 20, 2024
1 parent 6a06b7a commit 9b80a83
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 281 deletions.
10 changes: 4 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
{
"plugins": [
],
"extends": [
"next/core-web-vitals"
],
"plugins": [],
"extends": ["next/core-web-vitals"],
"rules": {
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-empty-pattern": "off",
"no-unused-vars": "off",
"arrow-parens": ["error", "always"],
"arrow-body-style": ["error", "always"]
"arrow-body-style": ["error", "always"],
"max-len": ["error", { "code": 150 }]
}
}
4 changes: 3 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"semi": true,
"singleQuote": true
"singleQuote": true,
"printWidth": 150,
"tabWidth": 2
}
29 changes: 5 additions & 24 deletions app/components/editor/casbin-mode/casbin-conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {
IndentContext,
LanguageSupport,
StreamLanguage,
StringStream,
} from '@codemirror/language';
import { IndentContext, LanguageSupport, StreamLanguage, StringStream } from '@codemirror/language';
import { tags as t } from '@lezer/highlight';

const token = (stream: StringStream, state) => {
Expand Down Expand Up @@ -77,10 +72,7 @@ const token = (stream: StringStream, state) => {

if (stream.sol()) {
if (state.sec !== '') {
if (
(state.sec === 'g' && stream.match(new RegExp('^g[2-9]?'))) ||
stream.match(state.sec)
) {
if ((state.sec === 'g' && stream.match(new RegExp('^g[2-9]?'))) || stream.match(state.sec)) {
if (stream.peek() === ' ' || stream.peek() === '=') {
return 'builtin';
} else {
Expand Down Expand Up @@ -136,11 +128,7 @@ const token = (stream: StringStream, state) => {
}

// Match: e = [some]([where] (p.eft == allow))
if (
stream.match('some') ||
stream.match('where') ||
stream.match('priority')
) {
if (stream.match('some') || stream.match('where') || stream.match('priority')) {
return 'keyword';
}

Expand All @@ -167,10 +155,7 @@ const token = (stream: StringStream, state) => {
}

// Match: m = [g](r.sub, p.sub) && r.obj == p.obj && r.act == p.act
if (
stream.match(new RegExp('^[_a-zA-Z][_a-zA-Z0-9]*')) &&
stream.peek() === '('
) {
if (stream.match(new RegExp('^[_a-zA-Z][_a-zA-Z0-9]*')) && stream.peek() === '(') {
return 'def';
}
}
Expand All @@ -187,11 +172,7 @@ export const CasbinConfLang = StreamLanguage.define({
token: token,
blankLine: (state: {}, indentUnit: number): void => {},
copyState: (state: {}) => {},
indent: (
state: {},
textAfter: string,
context: IndentContext,
): number | null => {
indent: (state: {}, textAfter: string, context: IndentContext): number | null => {
return 0;
},
languageData: {
Expand Down
13 changes: 2 additions & 11 deletions app/components/editor/casbin-mode/casbin-csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {
IndentContext,
LanguageSupport,
StreamLanguage,
StringStream,
} from '@codemirror/language';
import { IndentContext, LanguageSupport, StreamLanguage, StringStream } from '@codemirror/language';
import { tags as t } from '@lezer/highlight';

const token = (stream: StringStream, state) => {
Expand Down Expand Up @@ -56,11 +51,7 @@ export const CasbinPolicyLang = StreamLanguage.define({
token: token,
blankLine: (state: {}, indentUnit: number): void => {},
copyState: (state: {}) => {},
indent: (
state: {},
textAfter: string,
context: IndentContext,
): number | null => {
indent: (state: {}, textAfter: string, context: IndentContext): number | null => {
return 0;
},
languageData: {
Expand Down
55 changes: 9 additions & 46 deletions app/components/editor/hooks/useIndex.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import React, { isValidElement, ReactNode, useEffect, useState } from 'react';
import {
defaultCustomConfig,
defaultEnforceContext,
example,
ModelKind,
} from '@/app/components/editor/casbin-mode/example';
import { defaultCustomConfig, defaultEnforceContext, example, ModelKind } from '@/app/components/editor/casbin-mode/example';
import { ShareFormat } from '@/app/components/editor/hooks/useShareInfo';
import { defaultEnforceContextData } from '@/app/components/editor/hooks/useSetupEnforceContext';

Expand All @@ -17,9 +12,7 @@ export default function useIndex() {
const [requestResult, setRequestResult] = useState('');
const [customConfig, setCustomConfig] = useState('');
const [share, setShare] = useState('');
const [enforceContextData, setEnforceContextData] = useState(
new Map(defaultEnforceContextData),
);
const [enforceContextData, setEnforceContextData] = useState(new Map(defaultEnforceContextData));

function setPolicyPersistent(text: string): void {
setPolicy(text);
Expand Down Expand Up @@ -58,9 +51,7 @@ export default function useIndex() {
setRequestPersistent(sharedContent.request);
setRequestPersistent(sharedContent.request);
if (sharedContent.enforceContext) {
setEnforceContextDataPersistent(
new Map(Object.entries(sharedContent.enforceContext)),
);
setEnforceContextDataPersistent(new Map(Object.entries(sharedContent.enforceContext)));
}
setRequestResult('');
window.location.hash = ''; // prevent duplicate load
Expand All @@ -77,15 +68,7 @@ export default function useIndex() {
setModelText(example[modelKind].model);
setRequest(example[modelKind].request);
setCustomConfig(defaultCustomConfig);
setEnforceContextData(
new Map(
Object.entries(
JSON.parse(
example[modelKind].enforceContext || defaultEnforceContext,
),
),
),
);
setEnforceContextData(new Map(Object.entries(JSON.parse(example[modelKind].enforceContext || defaultEnforceContext))));
}, [modelKind]);

function handleShare(v: ReactNode | string) {
Expand All @@ -98,29 +81,9 @@ export default function useIndex() {
}
}
return {
modelKind,
setModelKind,
modelText,
setModelText,
policy,
setPolicy,
request,
setRequest,
echo,
setEcho,
requestResult,
setRequestResult,
customConfig,
setCustomConfig,
share,
setShare,
enforceContextData,
setEnforceContextData,
setPolicyPersistent,
setModelTextPersistent,
setCustomConfigPersistent,
setRequestPersistent,
setEnforceContextDataPersistent,
handleShare,
};
modelKind, setModelKind, modelText, setModelText, policy, setPolicy, request,
setRequest, echo, setEcho, requestResult, setRequestResult, customConfig, setCustomConfig, share, setShare,
enforceContextData, setEnforceContextData, setPolicyPersistent, setModelTextPersistent,
setCustomConfigPersistent, setRequestPersistent, setEnforceContextDataPersistent, handleShare,
} ;
}
44 changes: 8 additions & 36 deletions app/components/editor/hooks/useRunTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@
// limitations under the License.

import React from 'react';
import {
DefaultRoleManager,
newEnforcer,
newModel,
StringAdapter,
Util,
} from 'casbin';
import { DefaultRoleManager, newEnforcer, newModel, StringAdapter, Util } from 'casbin';
import { newEnforceContext } from '@/app/components/editor/hooks/useSetupEnforceContext';

interface RunTestProps {
Expand Down Expand Up @@ -92,10 +86,7 @@ async function enforcer(props: RunTestProps) {
const startTime = performance.now();
const result = [];
try {
const e = await newEnforcer(
newModel(props.model),
props.policy ? new StringAdapter(props.policy) : undefined,
);
const e = await newEnforcer(newModel(props.model), props.policy ? new StringAdapter(props.policy) : undefined);

const customConfigCode = props.customConfig;
if (customConfigCode) {
Expand Down Expand Up @@ -133,15 +124,9 @@ async function enforcer(props: RunTestProps) {
}
if (typeof matchingForGFunction === 'string') {
if (matchingForGFunction in config.functions) {
await rm.addMatchingFunc(
config.functions[matchingForGFunction],
);
await rm.addMatchingFunc(config.functions[matchingForGFunction]);
} else {
props.onResponse(
<div>
Must sure the {matchingForGFunction}() in config.functions
</div>,
);
props.onResponse(<div>Must sure the {matchingForGFunction}() in config.functions</div>);
return;
}
}
Expand All @@ -154,27 +139,16 @@ async function enforcer(props: RunTestProps) {
}
if (typeof matchingDomainForGFunction === 'string') {
if (matchingDomainForGFunction in config.functions) {
await rm.addDomainMatchingFunc(
config.functions[matchingDomainForGFunction],
);
await rm.addDomainMatchingFunc(config.functions[matchingDomainForGFunction]);
} else {
props.onResponse(
<div>
Must sure the {matchingDomainForGFunction}() in
config.functions
</div>,
);
props.onResponse(<div>Must sure the {matchingDomainForGFunction}() in config.functions</div>);
return;
}
}
}
}
} catch (e) {
props.onResponse(
<div>
Please check syntax in Custom Function Editor: {(e as any).message}
</div>,
);
props.onResponse(<div>Please check syntax in Custom Function Editor: {(e as any).message}</div>);
return;
}
}
Expand Down Expand Up @@ -204,9 +178,7 @@ async function enforcer(props: RunTestProps) {

const stopTime = performance.now();

props.onResponse(
<div>{'Done in ' + (stopTime - startTime).toFixed(2) + 'ms'}</div>,
);
props.onResponse(<div>{'Done in ' + (stopTime - startTime).toFixed(2) + 'ms'}</div>);
props.onResponse(result);
} catch (e) {
props.onResponse(<div>{(e as any).message}</div>);
Expand Down
16 changes: 3 additions & 13 deletions app/components/editor/hooks/useSetupEnforceContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,11 @@ export const defaultEnforceContextData = new Map<string, string>([
]);

export const newEnforceContext = (data: Map<string, string>) => {
return new EnforceContext(
data.get(r)!,
data.get(p)!,
data.get(e)!,
data.get(m)!,
);
return new EnforceContext(data.get(r)!, data.get(p)!, data.get(e)!, data.get(m)!);
};

export default function useSetupEnforceContext({
onChange,
data,
}: SetupEnforceContextProps) {
const [enforceContextData, setEnforceContextData] = useState(
new Map(defaultEnforceContextData),
);
export default function useSetupEnforceContext({ onChange, data }: SetupEnforceContextProps) {
const [enforceContextData, setEnforceContextData] = useState(new Map(defaultEnforceContextData));
const handleEnforceContextChange = (key: string, value: string) => {
onChange(data.set(key, value));
};
Expand Down
Loading

0 comments on commit 9b80a83

Please sign in to comment.