Skip to content

Commit

Permalink
feat: use eslint-compat-utils (#270)
Browse files Browse the repository at this point in the history
* feat: use eslint-compat-utils

* Create fluffy-phones-battle.md
  • Loading branch information
ota-meshi committed Oct 8, 2023
1 parent d3f9519 commit 96a031f
Show file tree
Hide file tree
Showing 36 changed files with 150 additions and 68 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-phones-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-yml": minor
---

feat: use eslint-compat-utils
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ module.exports = {
"no-shadow": "off",
"no-void": ["error", { allowAsStatement: true }],
"jsonc/array-element-newline": "off",
"no-restricted-properties": [
"error",
{ object: "context", property: "getSourceCode" },
{ object: "context", property: "getFilename" },
{ object: "context", property: "getCwd" },
{ object: "context", property: "getScope" },
{ object: "context", property: "parserServices" },
],
},
overrides: [
{
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"homepage": "https://ota-meshi.github.io/eslint-plugin-yml/",
"dependencies": {
"debug": "^4.3.2",
"eslint-compat-utils": "^0.1.0",
"lodash": "^4.17.21",
"natural-compare": "^1.4.0",
"yaml-eslint-parser": "^1.2.1"
Expand Down
5 changes: 3 additions & 2 deletions src/rules/block-mapping-colon-indicator-newline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AST } from "yaml-eslint-parser";
import { createRule } from "../utils";
import { isColon } from "../utils/ast-utils";
import { getSourceCode } from "../utils/compat";

export default createRule("block-mapping-colon-indicator-newline", {
meta: {
Expand Down Expand Up @@ -28,8 +29,8 @@ export default createRule("block-mapping-colon-indicator-newline", {
type: "layout",
},
create(context) {
const sourceCode = context.getSourceCode();
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}
const option: "never" | "always" = context.options[0] || "never";
Expand Down
5 changes: 3 additions & 2 deletions src/rules/block-mapping-question-indicator-newline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createRule } from "../utils";
import { isQuestion } from "../utils/ast-utils";
import { getSourceCode } from "../utils/compat";

export default createRule("block-mapping-question-indicator-newline", {
meta: {
Expand All @@ -24,8 +25,8 @@ export default createRule("block-mapping-question-indicator-newline", {
type: "layout",
},
create(context) {
const sourceCode = context.getSourceCode();
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}
const option: "never" | "always" = context.options[0] || "never";
Expand Down
6 changes: 4 additions & 2 deletions src/rules/block-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
unwrapMeta,
processIndentFix,
} from "../utils/yaml";
import { getSourceCode } from "../utils/compat";

// ----------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -99,7 +100,8 @@ export default createRule("block-mapping", {
type: "layout",
},
create(context) {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}
const options = parseOptions(context.options[0]);
Expand Down Expand Up @@ -292,7 +294,7 @@ function canFixToFlow(mappingInfo: Stack, node: AST.YAMLBlockMapping) {
*/
function buildFixFlowToBlock(node: AST.YAMLFlowMapping, context: RuleContext) {
return function* (fixer: RuleFixer): IterableIterator<Fix> {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const open = sourceCode.getFirstToken(node);
const close = sourceCode.getLastToken(node);
if (open?.value !== "{" || close?.value !== "}") {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/block-sequence-hyphen-indicator-newline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AST } from "yaml-eslint-parser";
import { createRule } from "../utils";
import { isHyphen } from "../utils/ast-utils";
import { getSourceCode } from "../utils/compat";

export default createRule("block-sequence-hyphen-indicator-newline", {
meta: {
Expand Down Expand Up @@ -31,8 +32,8 @@ export default createRule("block-sequence-hyphen-indicator-newline", {
type: "layout",
},
create(context) {
const sourceCode = context.getSourceCode();
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}
const style: "never" | "always" = context.options[0] || "never";
Expand Down
10 changes: 6 additions & 4 deletions src/rules/block-sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
SourceCode,
} from "../types";
import { isComma } from "../utils/ast-utils";
import { getSourceCode } from "../utils/compat";

// ----------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -109,7 +110,8 @@ export default createRule("block-sequence", {
type: "layout",
},
create(context) {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}
const options = parseOptions(context.options[0]);
Expand Down Expand Up @@ -201,7 +203,7 @@ export default createRule("block-sequence", {
}

const canFix =
canFixToBlock(sequenceInfo, node, context.getSourceCode()) &&
canFixToBlock(sequenceInfo, node, sourceCode) &&
!hasTabIndent(context);
context.report({
loc: node.loc,
Expand Down Expand Up @@ -314,7 +316,7 @@ function canFixToFlow(
*/
function buildFixFlowToBlock(node: AST.YAMLFlowSequence, context: RuleContext) {
return function* (fixer: RuleFixer): IterableIterator<Fix> {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const open = sourceCode.getFirstToken(node);
const close = sourceCode.getLastToken(node);
if (open?.value !== "[" || close?.value !== "]") {
Expand Down Expand Up @@ -418,7 +420,7 @@ function buildFixBlockToFlow(
node: AST.YAMLBlockSequence,
context: RuleContext,
) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
return function* (fixer: RuleFixer): IterableIterator<Fix> {
const entries = node.entries.filter(
(
Expand Down
6 changes: 4 additions & 2 deletions src/rules/file-extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "path";
import { createRule } from "../utils";
import { getFilename, getSourceCode } from "../utils/compat";

export default createRule("file-extension", {
meta: {
Expand Down Expand Up @@ -29,15 +30,16 @@ export default createRule("file-extension", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}
const expected: string = context.options[0]?.extension || "yaml";
const caseSensitive: string = context.options[0]?.caseSensitive ?? true;

return {
Program(node) {
const filename = context.getFilename();
const filename = getFilename(context);
const actual = path.extname(filename);
if (
(caseSensitive ? actual : actual.toLocaleLowerCase()) ===
Expand Down
6 changes: 3 additions & 3 deletions src/rules/flow-mapping-curly-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
hasTabIndent,
isKeyNode,
} from "../utils/yaml";
import { getSourceCode } from "../utils/compat";

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -126,11 +127,10 @@ export default createRule("flow-mapping-curly-newline", {
type: "layout",
},
create(context) {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}

const sourceCode = context.getSourceCode();
const options = normalizeOptionValue(context.options[0]);

/**
Expand Down
4 changes: 3 additions & 1 deletion src/rules/flow-mapping-curly-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getProxyNode,
getCoreRule,
} from "../utils";
import { getSourceCode } from "../utils/compat";
const coreRule = getCoreRule("object-curly-spacing");

export default createRule("flow-mapping-curly-spacing", {
Expand All @@ -22,7 +23,8 @@ export default createRule("flow-mapping-curly-spacing", {
type: coreRule.meta!.type!,
},
create(context) {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}

Expand Down
5 changes: 3 additions & 2 deletions src/rules/flow-sequence-bracket-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../utils/yaml";
import { isTokenOnSameLine, isCommentToken } from "../utils/ast-utils";
import type { YAMLToken } from "../types";
import { getSourceCode } from "../utils/compat";

type UserOption =
| "always"
Expand Down Expand Up @@ -59,10 +60,10 @@ export default createRule("flow-sequence-bracket-newline", {
type: "layout",
},
create(context) {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}
const sourceCode = context.getSourceCode();

//----------------------------------------------------------------------
// Helpers
Expand Down
4 changes: 3 additions & 1 deletion src/rules/flow-sequence-bracket-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getProxyNode,
getCoreRule,
} from "../utils";
import { getSourceCode } from "../utils/compat";
const coreRule = getCoreRule("array-bracket-spacing");

export default createRule("flow-sequence-bracket-spacing", {
Expand All @@ -22,7 +23,8 @@ export default createRule("flow-sequence-bracket-spacing", {
type: coreRule.meta!.type!,
},
create(context) {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}

Expand Down
6 changes: 3 additions & 3 deletions src/rules/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createRule } from "../utils";
import { hasTabIndent, getNumOfIndent } from "../utils/yaml";
import type { YAMLToken, Fix, RuleFixer, RuleContext } from "../types";
import { isHyphen, isQuestion, isColon } from "../utils/ast-utils";
import { getSourceCode } from "../utils/compat";

// ----------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -114,7 +115,8 @@ export default createRule("indent", {
type: "layout",
},
create(context) {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}

Expand All @@ -126,8 +128,6 @@ export default createRule("indent", {
const { numOfIndent, indentBlockSequences, indicatorValueIndent } =
parseOptions(context);

const sourceCode = context.getSourceCode();

const indents = new Map<YAMLToken, IndentInfo>();
const indicators = new Set<YAMLToken>();
const blockLiteralMarks = new Set<YAMLToken>();
Expand Down
7 changes: 4 additions & 3 deletions src/rules/key-name-casing.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { AST } from "yaml-eslint-parser";
import { getStaticYAMLValue } from "yaml-eslint-parser";
import type { RuleListener } from "../types";
import { createRule } from "../utils";
import type { CasingKind } from "../utils/casing";
import { getChecker } from "../utils/casing";
import { allowedCaseOptions } from "../utils/casing";
import { getSourceCode } from "../utils/compat";

type Option = {
[key in CasingKind]?: boolean;
Expand Down Expand Up @@ -63,8 +63,9 @@ export default createRule("key-name-casing", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isYAML) {
return {} as RuleListener;
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}
const option: Option = { ...context.options[0] };
if (option.camelCase !== false) {
Expand Down
6 changes: 3 additions & 3 deletions src/rules/key-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { AST } from "yaml-eslint-parser";
import type { RuleListener, RuleContext } from "../types";
import { createRule } from "../utils";
import { isColon, isQuestion } from "../utils/ast-utils";
import { getSourceCode } from "../utils/compat";

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -269,7 +270,8 @@ export default createRule("key-spacing", {
* Create rule visitor
*/
function create(context: RuleContext): RuleListener {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}
/**
Expand All @@ -287,8 +289,6 @@ function create(context: RuleContext): RuleListener {
align: alignmentOptions,
} = initOptions(options);

const sourceCode = context.getSourceCode();

/**
* Determines if the given property is key-value property.
* @param {ASTNode} property Property node to check.
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-empty-document.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AST } from "yaml-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

export default createRule("no-empty-document", {
meta: {
Expand All @@ -16,7 +17,8 @@ export default createRule("no-empty-document", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}

Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-empty-key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AST } from "yaml-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

export default createRule("no-empty-key", {
meta: {
Expand All @@ -16,7 +17,8 @@ export default createRule("no-empty-key", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}

Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-empty-mapping-value.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AST } from "yaml-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "../utils/compat";

export default createRule("no-empty-mapping-value", {
meta: {
Expand All @@ -16,7 +17,8 @@ export default createRule("no-empty-mapping-value", {
type: "suggestion",
},
create(context) {
if (!context.parserServices.isYAML) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isYAML) {
return {};
}

Expand Down

0 comments on commit 96a031f

Please sign in to comment.