Skip to content

Commit

Permalink
Migrate away from restorers (ivov#127)
Browse files Browse the repository at this point in the history
* Refactor to `getExtendsValue`

* Remove unused function

* Pin ESLint version to n8n's version
  • Loading branch information
ivov authored Sep 14, 2022
1 parent 80ddf07 commit 8d83099
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 21 deletions.
23 changes: 14 additions & 9 deletions lib/ast/getters/credentialClassBody.getters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TSESTree } from "@typescript-eslint/utils";
import { id } from "../identifiers";
import { restoreArray } from "../restorers";
import type { StringClassField } from "../../types";
import { restoreValue } from "../utils/restoreValue";
import type { GenericContext, StringClassField } from "../../types";

// ----------------------------------
// credClassBody
Expand Down Expand Up @@ -37,13 +37,18 @@ export function getPlaceholder(classBody: TSESTree.ClassBody) {
return getStringClassField(id.credClassBody.isPlaceholder, classBody);
}

export function getExtendsOAuth2(classBody: TSESTree.ClassBody) {
const found = classBody.body.find(id.credClassBody.isFieldExtends);
/**
* Get the value for the `extends` field in a cred class, e.g. `[ 'oAuth2Api' ]`
*/
export function getExtendsValue(
classBody: TSESTree.ClassBody,
context: GenericContext
) {
const extendsNode = classBody.body.find(id.credClassBody.isFieldExtends);

if (!found) return null;
if (!extendsNode) return null;

return {
ast: found,
value: restoreArray(found.value.elements),
};
const extendsSource = context.getSourceCode().getText(extendsNode.value);

return restoreValue<string[]>(extendsSource) ?? null;
}
1 change: 1 addition & 0 deletions lib/ast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from "./utils/filename";
export * from "./utils/format";
export * from "./utils/insertion";
export * from "./utils/range";
export * from "./utils/restoreValue";
export * from "./utils/rule";
export * from "./utils/sort";
2 changes: 2 additions & 0 deletions lib/ast/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as filename from "./filename";
import * as format from "./format";
import * as insertion from "./insertion";
import * as range from "./range";
import * as restoreValue from "./restoreValue";
import * as rule from "./rule";
import * as sort from "./sort";

Expand All @@ -10,6 +11,7 @@ export const utils = {
...format,
...insertion,
...range,
...restoreValue,
...rule,
...sort,
};
7 changes: 7 additions & 0 deletions lib/ast/utils/restoreValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function restoreValue<T>(source: string): T | null {
try {
return eval(`(${source})`);
} catch (error) {
return null;
}
}
11 changes: 7 additions & 4 deletions lib/rules/cred-class-field-display-name-missing-oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default utils.createRule({
},
schema: [],
messages: {
addOAuth2: "Insert 'OAuth2' [non-autofixable]", // unpredictable input
addOAuth2: "Add 'OAuth2' [non-autofixable]",
},
},
defaultOptions: [],
Expand All @@ -22,16 +22,19 @@ export default utils.createRule({
ClassDeclaration(node) {
if (!id.isCredentialClass(node)) return;

const extendsOAuth2 = getters.credClassBody.getExtendsOAuth2(node.body);
const extendsValue = getters.credClassBody.getExtendsValue(
node.body,
context
);

if (!extendsOAuth2) return;
if (!extendsValue) return;

const displayName = getters.credClassBody.getDisplayName(node.body);

if (!displayName) return;

if (
extendsOAuth2.value.includes("oAuth2Api") &&
extendsValue.includes("oAuth2Api") &&
!displayName.value.endsWith("OAuth2 API")
) {
context.report({
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/cred-class-field-name-missing-oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ export default utils.createRule({

const { body: classBody } = node;

const extendsOAuth2 = getters.credClassBody.getExtendsOAuth2(classBody);
const extendsValue = getters.credClassBody.getExtendsValue(classBody, context);

if (!extendsOAuth2) return;
if (!extendsValue) return;

const name = getters.credClassBody.getName(classBody);

if (!name) return;

if (
extendsOAuth2.value.includes("oAuth2Api") &&
extendsValue.includes("oAuth2Api") &&
!name.value.endsWith("OAuth2Api")
) {
context.report({
Expand Down
10 changes: 7 additions & 3 deletions lib/rules/cred-class-name-missing-oauth2-suffix.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { utils } from "../ast/utils";
import { id } from "../ast/identifiers";
import { getters } from "../ast/getters";
import { RuleContext } from "@typescript-eslint/utils/dist/ts-eslint";

export default utils.createRule({
name: utils.getRuleName(module),
Expand All @@ -22,16 +23,19 @@ export default utils.createRule({
ClassDeclaration(node) {
if (!id.isCredentialClass(node)) return;

const extendsOAuth2 = getters.credClassBody.getExtendsOAuth2(node.body);
const extendsValue = getters.credClassBody.getExtendsValue(
node.body,
context
);

if (!extendsOAuth2) return;
if (!extendsValue) return;

const className = getters.getClassName(node);

if (!className) return;

if (
extendsOAuth2.value.includes("oAuth2Api") &&
extendsValue.includes("oAuth2Api") &&
!className.value.endsWith("OAuth2Api")
) {
context.report({
Expand Down
5 changes: 4 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
import type { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
import type { RuleContext } from "@typescript-eslint/utils/dist/ts-eslint";

type BaseProperty = TSESTree.PropertyNonComputedName;

Expand Down Expand Up @@ -44,3 +45,5 @@ export type TemplateStringProperty = BaseProperty & {
export type StringClassField = TSESTree.PropertyDefinitionNonComputedName & {
value: { value: string };
};

export type GenericContext = Readonly<RuleContext<any, any>>; // TODO: Type properly
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@typescript-eslint/parser": "^5.33.0",
"esbuild": "^0.15.0",
"esbuild-jest": "^0.5.0",
"eslint": "^8.0.0",
"eslint": "8.23.0",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-docgen": "^0.7.1",
Expand Down

0 comments on commit 8d83099

Please sign in to comment.