Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yamadayuki committed Jul 22, 2020
1 parent c95c6b3 commit 45c6701
Show file tree
Hide file tree
Showing 27 changed files with 249 additions and 266 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ typings/
# lerna-changelog cache directory
.changelog/

packages/eslint-plugin-wantedly/lib
packages/frolint/lib
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"author": "Yuki Yamada <[email protected]>",
"license": "MIT",
"devDependencies": {
"@types/eslint": "^7.2.0",
"@types/estree": "^0.0.45",
"@types/graphql": "^14.5.0",
"@types/jest": "^26.0.5",
"jest": "^26.1.0",
"lerna": "^3.22.1",
Expand Down Expand Up @@ -31,8 +34,8 @@
"lint": "frolint --branch master",
"lint:ci": "frolint --expect-no-errors",
"changelog": "yarn --silent lerna-changelog | cat - CHANGELOG.md | yarn --silent prettier --stdin-filepath CHANGELOG.md > CHANGELOG_BACKUP.md; mv CHANGELOG_BACKUP.md CHANGELOG.md",
"build": "lerna run build --scope frolint",
"build:watch": "lerna exec npm run build:watch --scope frolint",
"build": "lerna run build",
"build:watch": "lerna exec npm run build:watch",
"prerelease": "yarn install --force && yarn run build",
"release": "lerna publish --force-publish --preid beta --pre-dist-tag beta"
},
Expand Down
21 changes: 0 additions & 21 deletions packages/eslint-plugin-wantedly/index.js

This file was deleted.

22 changes: 21 additions & 1 deletion packages/eslint-plugin-wantedly/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
},
"repository": "https://github.com/wantedly/frolint/tree/master/packages/eslint-plugin-wantedly",
"scripts": {
"test": "jest"
"test": "jest",
"build": "tsc -b .",
"build:watch": "tsc -b . --watch"
},
"dependencies": {
"camel-case": "^4.1.1",
Expand All @@ -30,5 +32,23 @@
"peerDependencies": {
"eslint": "^6.0.0",
"graphql": "^14.6.0"
},
"jest": {
"roots": [
"<rootDir>/src"
],
"moduleFileExtensions": [
"ts",
"js"
],
"transform": {
"^.+\\.ts$": "ts-jest"
},
"globals": {
"ts-jest": {
"tsConfig": "tsconfig.json"
}
},
"testEnvironment": "node"
}
}
23 changes: 0 additions & 23 deletions packages/eslint-plugin-wantedly/rules/utils.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,45 +43,6 @@ Object {
"type": "suggestion",
},
},
"nexus-field-description": Object {
"create": [Function],
"meta": Object {
"docs": Object {
"url": "https://github.com/wantedly/frolint/tree/master/packages/eslint-plugin-wantedly/docs/rules/nexus-field-description.md",
},
"fixable": "code",
"type": "suggestion",
},
},
"nexus-pascal-case-type-name": Object {
"create": [Function],
"meta": Object {
"docs": Object {
"url": "https://github.com/wantedly/frolint/tree/master/packages/eslint-plugin-wantedly/docs/rules/nexus-pascal-case-type-name.md",
},
"fixable": "code",
"type": "suggestion",
},
},
"nexus-type-description": Object {
"create": [Function],
"meta": Object {
"docs": Object {
"url": "https://github.com/wantedly/frolint/tree/master/packages/eslint-plugin-wantedly/docs/rules/nexus-type-description.md",
},
"type": "suggestion",
},
},
"nexus-upper-case-enum-members": Object {
"create": [Function],
"meta": Object {
"docs": Object {
"url": "https://github.com/wantedly/frolint/tree/master/packages/eslint-plugin-wantedly/docs/rules/nexus-upper-case-enum-members.md",
},
"fixable": "code",
"type": "suggestion",
},
},
},
}
`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const plugin = require("..");
import * as plugin from "..";

it("should match snapshot", () => {
expect(plugin).toMatchSnapshot();
Expand Down
19 changes: 19 additions & 0 deletions packages/eslint-plugin-wantedly/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as GRAPHQL_OPERATION_NAME from "./rules/graphql-operation-name";
import * as GRAPHQL_PASCAL_CASE_TYPE_NAME from "./rules/graphql-pascal-case-type-name";
import * as NEXUS_CAMEL_CASE_FIELD_NAME from "./rules/nexus-camel-case-field-name";
import * as NEXUS_ENUM_VALUES_DESCRIPTION from "./rules/nexus-enum-values-description";
// import * as NEXUS_FIELD_DESCRIPTION from "./rules/nexus-field-description";
// import * as NEXUS_PASCAL_CASE_TYPE_NAME from "./rules/nexus-pascal-case-type-name";
// import * as NEXUS_TYPE_DESCRIPTION from "./rules/nexus-type-description";
// import * as NEXUS_UPPER_CASE_ENUM_MEMBERS from "./rules/nexus-upper-case-enum-members";

export const rules = {
[GRAPHQL_OPERATION_NAME.RULE_NAME]: GRAPHQL_OPERATION_NAME.RULE,
[GRAPHQL_PASCAL_CASE_TYPE_NAME.RULE_NAME]: GRAPHQL_PASCAL_CASE_TYPE_NAME.RULE,
[NEXUS_CAMEL_CASE_FIELD_NAME.RULE_NAME]: NEXUS_CAMEL_CASE_FIELD_NAME.RULE,
[NEXUS_ENUM_VALUES_DESCRIPTION.RULE_NAME]: NEXUS_ENUM_VALUES_DESCRIPTION.RULE,
// [NEXUS_FIELD_DESCRIPTION.RULE_NAME]: NEXUS_FIELD_DESCRIPTION.RULE,
// [NEXUS_PASCAL_CASE_TYPE_NAME.RULE_NAME]: NEXUS_PASCAL_CASE_TYPE_NAME.RULE,
// [NEXUS_TYPE_DESCRIPTION.RULE_NAME]: NEXUS_TYPE_DESCRIPTION.RULE,
// [NEXUS_UPPER_CASE_ENUM_MEMBERS.RULE_NAME]: NEXUS_UPPER_CASE_ENUM_MEMBERS.RULE,
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const RuleTester = require("eslint").RuleTester;
const ESLintConfigWantedly = require("eslint-config-wantedly/without-react");
const rule = require("../graphql-operation-name");
import { RuleTester } from "eslint";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import ESLintConfigWantedly from "eslint-config-wantedly/without-react";
import { RULE, RULE_NAME } from "../graphql-operation-name";

RuleTester.setDefaultConfig({
const ruleTester = new RuleTester({
parser: require.resolve(ESLintConfigWantedly.parser),
parserOptions: ESLintConfigWantedly.parserOptions,
});

const ruleTester = new RuleTester();
ruleTester.run(rule.RULE_NAME, rule.RULE, {
ruleTester.run(RULE_NAME, RULE, {
valid: [
{
code: `
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const RuleTester = require("eslint").RuleTester;
const ESLintConfigWantedly = require("eslint-config-wantedly/without-react");
const rule = require("../graphql-pascal-case-type-name");
import { RuleTester } from "eslint";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import ESLintConfigWantedly from "eslint-config-wantedly/without-react";
import { RULE, RULE_NAME } from "../graphql-pascal-case-type-name";

RuleTester.setDefaultConfig({
const ruleTester = new RuleTester({
parser: require.resolve(ESLintConfigWantedly.parser),
parserOptions: ESLintConfigWantedly.parserOptions,
});

const ruleTester = new RuleTester();
ruleTester.run(rule.RULE_NAME, rule.RULE, {
ruleTester.run(RULE_NAME, RULE, {
valid: [
{
code: `gql\`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const RuleTester = require("eslint").RuleTester;
const ESLintConfigWantedly = require("eslint-config-wantedly/without-react");
const rule = require("../nexus-camel-case-field-name");
import { RuleTester } from "eslint";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import ESLintConfigWantedly from "eslint-config-wantedly/without-react";
import { RULE, RULE_NAME } from "../nexus-camel-case-field-name";

RuleTester.setDefaultConfig({
const ruleTester = new RuleTester({
parser: require.resolve(ESLintConfigWantedly.parser),
parserOptions: ESLintConfigWantedly.parserOptions,
});

const ruleTester = new RuleTester();
ruleTester.run(rule.RULE_NAME, rule.RULE, {
ruleTester.run(RULE_NAME, RULE, {
valid: [],
invalid: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const RuleTester = require("eslint").RuleTester;
const ESLintConfigWantedly = require("eslint-config-wantedly/without-react");
const rule = require("../nexus-enum-values-description");
import { RuleTester } from "eslint";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import ESLintConfigWantedly from "eslint-config-wantedly/without-react";
import { RULE, RULE_NAME } from "../nexus-enum-values-description";

RuleTester.setDefaultConfig({
const ruleTester = new RuleTester({
parser: require.resolve(ESLintConfigWantedly.parser),
parserOptions: ESLintConfigWantedly.parserOptions,
});

const ruleTester = new RuleTester();
ruleTester.run(rule.RULE_NAME, rule.RULE, {
ruleTester.run(RULE_NAME, RULE, {
valid: [],
invalid: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { pascalCase } = require("pascal-case");
const { Linter } = require("eslint");
const { getOptionWithDefault, docsUrl } = require("./utils");
import { Linter, Rule } from "eslint";
import { pascalCase } from "pascal-case";
import { docsUrl, getOptionWithDefault } from "./utils";

const linter = new Linter();
const RULE_NAME = "graphql-operation-name";
export const RULE_NAME = "graphql-operation-name";

let GRAPHQL_INSTALLED = false;

Expand Down Expand Up @@ -34,32 +34,28 @@ linter.defineRule(RULE_NAME, {

const option = getOptionWithDefault(context, DEFAULT_OPTION);
const autofixEnabled = option.autofix;
const graphql = require("graphql");
const graphql = require("graphql") as typeof import("graphql");

return {
TaggedTemplateExpression(node) {
if (node.type !== "TaggedTemplateExpression") return;

// We assume that the tag name is gql which is originated from 'graphql-tag' or 'graphql.macro'
if (node.tag.type !== "Identifier" || node.tag.name !== "gql") {
return;
}
if (node.tag.type !== "Identifier" || node.tag.name !== "gql") return;

if (!(node.quasi.quasis.length > 0)) {
return;
}
if (!(node.quasi.quasis.length > 0)) return;

/** @type {string[]} */
const chunks = [];
const chunks: string[] = [];

const invalid = node.quasi.quasis.some((elem, i) => {
/** @type {string} */
let chunk = elem.value.cooked;
const value = node.quasi.expressions[i];

/**
* If the tagged template literal includes the interpolations,
* we should preserve the interpolation position with whitespaces for the GraphQL token location.
*/
if (value && value.name && value.name.length > 0) {
if (value && value.type === "Identifier" && value.name && value.name.length > 0) {
chunk = chunk.concat(" ".repeat(value.name.length + 3));
}

Expand Down Expand Up @@ -112,9 +108,9 @@ linter.defineRule(RULE_NAME, {
}

const nameLocation = operationDefinition.name.loc;
const [start] = node.quasi.range;
const errorStart = start + nameLocation.start + 1;
const errorEnd = start + nameLocation.start + operationName.length + 1;
const start = node.quasi.range?.[0];
const errorStart = (start ?? 0) + (nameLocation?.start ?? 0) + 1;
const errorEnd = (start ?? 0) + (nameLocation?.start ?? 0) + operationName.length + 1;
const sourceCode = context.getSourceCode();
const locStart = sourceCode.getLocFromIndex(errorStart);
const locEnd = sourceCode.getLocFromIndex(errorEnd);
Expand All @@ -130,6 +126,7 @@ linter.defineRule(RULE_NAME, {
if (autofixEnabled) {
return fixer.replaceTextRange([errorStart, errorEnd], pascalCased);
}
return null;
},
});
},
Expand All @@ -139,7 +136,4 @@ linter.defineRule(RULE_NAME, {
},
});

module.exports = {
RULE_NAME,
RULE: linter.getRules().get(RULE_NAME),
};
export const RULE = linter.getRules().get(RULE_NAME) as Rule.RuleModule;
Loading

0 comments on commit 45c6701

Please sign in to comment.