Skip to content

Commit

Permalink
refactor: types (#249)
Browse files Browse the repository at this point in the history
* refactor: types

* Update ast.d.ts

* fix types
  • Loading branch information
yeonjuan authored Dec 13, 2024
1 parent 3749177 commit fb97ff0
Show file tree
Hide file tree
Showing 40 changed files with 503 additions and 484 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ packages/**/.yarn

# generated types
packages/**/types

!packages/**/lib/types

integration-test-projects/**/*.yarn
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ yarn.lock

# generated types
packages/**/types
!packages/**/lib/types

packages/eslint-plugin/tests/e2e/**/*.html
integration-test-projects
8 changes: 3 additions & 5 deletions packages/eslint-plugin/lib/rules/attrs-newline.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
* @typedef { import("../types").RuleFixer } RuleFixer
* @typedef { import("eslint").Rule.RuleFixer } RuleFixer
* @typedef { import("../types").RuleModule } RuleModule
* @typedef { import("../types").TagNode } TagNode
* @typedef {import("../types").RuleListener}
* @typedef {Object} MessageId
*
* @typedef {Object } MessageId
* @property {"closeStyleWrong"} CLOSE_STYLE_WRONG
* @property {"newlineMissing"} NEWLINE_MISSING
* @property {"newlineUnexpected"} NEWLINE_UNEXPECTED
Expand All @@ -15,7 +14,6 @@ const { createVisitors } = require("./utils/visitors");
/**
* @type {MessageId}
*/

const MESSAGE_ID = {
CLOSE_STYLE_WRONG: "closeStyleWrong",
NEWLINE_MISSING: "newlineMissing",
Expand Down
20 changes: 11 additions & 9 deletions packages/eslint-plugin/lib/rules/element-newline.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/**
* @typedef { import("../types").RuleModule } RuleModule
* @typedef { import("../types").TagNode } TagNode
* @typedef { import("../types").BaseNode } BaseNode
* @typedef { import("../types").CommentNode } CommentNode
* @typedef { import("../types").DoctypeNode } DoctypeNode
* @typedef { import("../types").ScriptTagNode } ScriptTagNode
* @typedef { import("../types").StyleTagNode } StyleTagNode
* @typedef { import("../types").TextNode } TextNode
* @typedef { CommentNode | DoctypeNode | ScriptTagNode | StyleTagNode | TagNode | TextNode } NewlineNode
* @typedef { import("../types").Tag } Tag
* @typedef { import("../types").Comment } Comment
* @typedef { import("../types").Doctype } Doctype
* @typedef { import("../types").ScriptTag } ScriptTag
* @typedef { import("../types").StyleTag } StyleTag
* @typedef { import("../types").Text } Text
* @typedef { Tag | Doctype | ScriptTag | StyleTag | Text } NewlineNode
* @typedef {{
* childFirst: NewlineNode | null;
* childLast: NewlineNode | null;
Expand Down Expand Up @@ -188,7 +187,10 @@ module.exports = {
}
}

if (nodeMeta.childLast.loc.end.line === node.close.loc.start.line) {
if (
node.close &&
nodeMeta.childLast.loc.end.line === node.close.loc.start.line
) {
if (isNotNewlineEnd(nodeMeta.childLast)) {
context.report({
node: node,
Expand Down
10 changes: 5 additions & 5 deletions packages/eslint-plugin/lib/rules/id-naming-convention.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @typedef { import("../types").RuleModule } RuleModule
* @typedef { import("../types").TagNode } TagNode
* @typedef { import("../types").ScriptTagNode } ScriptTagNode
* @typedef { import("../types").StyleTagNode } StyleTagNode
* @typedef { import("../types").Tag } Tag
* @typedef { import("../types").ScriptTag } ScriptTag
* @typedef { import("../types").StyleTag } StyleTag
*/

const { RULE_CATEGORY } = require("../constants");
Expand Down Expand Up @@ -87,7 +87,7 @@ module.exports = {
: CONVENTION_CHECKERS[convention];

/**
* @param {TagNode | ScriptTagNode | StyleTagNode} node
* @param {Tag | ScriptTag | StyleTag} node
*/
function check(node) {
if (isAttributesEmpty(node)) {
Expand All @@ -107,7 +107,7 @@ module.exports = {
}

/**
* @param {TagNode | ScriptTagNode | StyleTagNode} node
* @param {Tag | ScriptTag | StyleTag} node
*/
function checkInTemplate(node) {
if (isAttributesEmpty(node)) {
Expand Down
14 changes: 8 additions & 6 deletions packages/eslint-plugin/lib/rules/indent/indent.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/**
* @typedef { import("../../types").RuleModule } RuleModule
* @typedef { import("../../types").AnyNode } AnyNode
* @typedef { import("../../types").LineNode } LineNode
* @typedef { import("../../types").BaseNode } BaseNode
* @typedef { import("../../types").TagNode } TagNode
* @typedef { import("../../types").Line } Line
* @typedef { import("../../types").RuleListener } RuleListener
* @typedef { import("eslint").AST.Token } Token
* @typedef { import("eslint").SourceCode } SourceCode
* @typedef { import("estree").TemplateLiteral } TemplateLiteral
* @typedef { import("eslint").AST.Range } Range
* @typedef { import("eslint").AST.SourceLocation } SourceLocation
* @typedef { import("../../types").TemplateLiteral } TemplateLiteral
*
*
* @typedef {Object} IndentType
* @property {"tab"} TAB
* @property {"space"} SPACE
Expand Down Expand Up @@ -113,7 +115,7 @@ module.exports = {

/**
* @param {AnyNode} node
* @returns {node is LineNode}
* @returns {node is Line}
*/
function isLineNode(node) {
return node.type === "Line";
Expand Down Expand Up @@ -178,7 +180,7 @@ module.exports = {
/**
* @param {AnyNode} node
* @param {string} actualIndent
* @return {BaseNode}
* @return {{ range: Range, loc: SourceLocation }}
*/
function getIndentNodeToReport(node, actualIndent) {
let rangeStart = node.range[0];
Expand Down
15 changes: 7 additions & 8 deletions packages/eslint-plugin/lib/rules/lowercase.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
* @typedef { import("../types").RuleModule } RuleModule
* @typedef { import("../types").TagNode } TagNode
* @typedef { import("../types").StyleTagNode } StyleTagNode
* @typedef { import("../types").ScriptTagNode } ScriptTagNode
* @typedef { import("../types").RuleListener } RuleListener
* @typedef { import("../types").Tag } Tag
* @typedef { import("../types").StyleTag } StyleTag
* @typedef { import("../types").ScriptTag } ScriptTag
*/

const { NODE_TYPES } = require("@html-eslint/parser");
Expand Down Expand Up @@ -38,12 +37,12 @@ module.exports = {
create(context) {
const allowedAttrKeySet = new Set(SVG_CAMEL_CASE_ATTRIBUTES);
/**
* @type {TagNode[]}
* @type {Tag[]}
*/
const svgStack = [];

/**
* @param {TagNode} node
* @param {Tag} node
*/
function enterSvg(node) {
svgStack.push(node);
Expand All @@ -62,7 +61,7 @@ module.exports = {
}

/**
* @param {TagNode | StyleTagNode | ScriptTagNode} node
* @param {Tag | StyleTag | ScriptTag} node
*/
function nameOf(node) {
if (node.type === NODE_TYPES.ScriptTag) return "script";
Expand All @@ -71,7 +70,7 @@ module.exports = {
}

/**
* @param {TagNode | StyleTagNode | ScriptTagNode} node
* @param {Tag | StyleTag | ScriptTag} node
*/
function check(node) {
const raw = node.openStart.value.slice(1);
Expand Down
8 changes: 4 additions & 4 deletions packages/eslint-plugin/lib/rules/no-abstract-roles.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @typedef { import("../types").RuleModule } RuleModule
* @typedef { import("../types").TagNode } TagNode
* @typedef { import("../types").StyleTagNode } StyleTagNode
* @typedef { import("../types").ScriptTagNode } ScriptTagNode
* @typedef { import("../types").Tag } Tag
* @typedef { import("../types").StyleTag } StyleTag
* @typedef { import("../types").ScriptTag } ScriptTag
*/

const { RULE_CATEGORY } = require("../constants");
Expand Down Expand Up @@ -50,7 +50,7 @@ module.exports = {

create(context) {
/**
* @param {TagNode | ScriptTagNode | StyleTagNode} node
* @param {Tag | ScriptTag | StyleTag} node
*/
function check(node) {
const roleAttr = findAttr(node, "role");
Expand Down
8 changes: 4 additions & 4 deletions packages/eslint-plugin/lib/rules/no-accesskey-attrs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @typedef { import("../types").RuleModule } RuleModule
* @typedef { import("../types").TagNode } TagNode
* @typedef { import("../types").StyleTagNode } StyleTagNode
* @typedef { import("../types").ScriptTagNode } ScriptTagNode
* @typedef { import("../types").Tag } Tag
* @typedef { import("../types").StyleTag } StyleTag
* @typedef { import("../types").ScriptTag } ScriptTag
*/

const { RULE_CATEGORY } = require("../constants");
Expand Down Expand Up @@ -35,7 +35,7 @@ module.exports = {

create(context) {
/**
* @param {TagNode | ScriptTagNode | StyleTagNode} node
* @param {Tag | ScriptTag | StyleTag} node
*/
function check(node) {
const accessKeyAttr = findAttr(node, "accesskey");
Expand Down
8 changes: 4 additions & 4 deletions packages/eslint-plugin/lib/rules/no-duplicate-attrs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @typedef { import("../types").RuleModule } RuleModule
* @typedef { import("../types").TagNode } TagNode
* @typedef { import("../types").StyleTagNode } StyleTagNode
* @typedef { import("../types").ScriptTagNode } ScriptTagNode
* @typedef { import("../types").Tag } Tag
* @typedef { import("../types").StyleTag } StyleTag
* @typedef { import("../types").ScriptTag } ScriptTag
*/

const { RULE_CATEGORY } = require("../constants");
Expand Down Expand Up @@ -35,7 +35,7 @@ module.exports = {

create(context) {
/**
* @param {TagNode | StyleTagNode | ScriptTagNode} node
* @param {Tag | StyleTag | ScriptTag} node
*/
function check(node) {
if (Array.isArray(node.attributes)) {
Expand Down
14 changes: 7 additions & 7 deletions packages/eslint-plugin/lib/rules/no-duplicate-id.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @typedef { import("../types").RuleModule } RuleModule
* @typedef { import("../types").TagNode } TagNode
* @typedef { import("../types").StyleTagNode } StyleTagNode
* @typedef { import("../types").ScriptTagNode } ScriptTagNode
* @typedef { import("es-html-parser").AttributeValueNode } AttributeValueNode
* @typedef { import("../types").Tag } Tag
* @typedef { import("../types").StyleTag } StyleTag
* @typedef { import("../types").ScriptTag } ScriptTag
* @typedef { import("../types").AttributeValue } AttributeValue
*/

const { parse } = require("@html-eslint/template-parser");
Expand Down Expand Up @@ -42,11 +42,11 @@ module.exports = {
create(context) {
const htmlIdAttrsMap = new Map();
/**
* @param {Map<string, AttributeValueNode[]>} map
* @param {Map<string, AttributeValue[]>} map
*/
function createTagVisitor(map) {
/**
* @param {TagNode} node
* @param {Tag} node
*/
return function (node) {
if (!node.attributes || node.attributes.length <= 0) {
Expand All @@ -67,7 +67,7 @@ module.exports = {

/**
*
* @param {Map<string, AttributeValueNode[]>} map
* @param {Map<string, AttributeValue[]>} map
*/
function report(map) {
map.forEach((attrs) => {
Expand Down
28 changes: 14 additions & 14 deletions packages/eslint-plugin/lib/rules/no-extra-spacing-attrs.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* @typedef { import("../types").RuleModule } RuleModule
* @typedef { import("../types").AttributeNode } AttributeNode
* @typedef { import("../types").OpenTagEndNode } OpenTagEndNode
* @typedef { import("../types").OpenScriptTagEndNode } OpenScriptTagEndNode
* @typedef { import("../types").OpenStyleTagEndNode } OpenStyleTagEndNode
* @typedef { import("../types").OpenScriptTagStartNode } OpenScriptTagStartNode
* @typedef { import("../types").OpenTagStartNode } OpenTagStartNode
* @typedef { import("../types").OpenStyleTagStartNode } OpenStyleTagStartNode
* @typedef { import("../types").TagNode } TagNode
* @typedef { import("../types").StyleTagNode } StyleTagNode
* @typedef { import("../types").ScriptTagNode } ScriptTagNode
* @typedef { import("../types").Attribute } Attribute
* @typedef { import("../types").OpenTagEnd } OpenTagEnd
* @typedef { import("../types").OpenScriptTagEnd } OpenScriptTagEnd
* @typedef { import("../types").OpenStyleTagEnd } OpenStyleTagEnd
* @typedef { import("../types").OpenScriptTagStart } OpenScriptTagStart
* @typedef { import("../types").OpenTagStart } OpenTagStart
* @typedef { import("../types").OpenStyleTagStart } OpenStyleTagStart
* @typedef { import("../types").Tag } Tag
* @typedef { import("../types").StyleTag } StyleTag
* @typedef { import("../types").ScriptTag } ScriptTag
* @typedef { import("../types").AnyNode } AnyNode
*/

Expand Down Expand Up @@ -95,7 +95,7 @@ module.exports = {
const sourceCode = context.getSourceCode().text;

/**
* @param {AttributeNode[]} attrs
* @param {Attribute[]} attrs
*/
function checkExtraSpacesBetweenAttrs(attrs) {
attrs.forEach((current, index, attrs) => {
Expand Down Expand Up @@ -145,8 +145,8 @@ module.exports = {
}

/**
* @param {OpenScriptTagStartNode | OpenTagStartNode | OpenStyleTagStartNode} node
* @param {AttributeNode} firstAttr
* @param {OpenScriptTagStart | OpenTagStart | OpenStyleTagStart} node
* @param {Attribute} firstAttr
* @returns
*/
function checkExtraSpaceBefore(node, firstAttr) {
Expand Down Expand Up @@ -184,7 +184,7 @@ module.exports = {
}
}
/**
* @param {TagNode | StyleTagNode | ScriptTagNode} node
* @param {Tag | StyleTag | ScriptTag} node
* @returns
*/
function check(node) {
Expand Down
19 changes: 9 additions & 10 deletions packages/eslint-plugin/lib/rules/no-extra-spacing-text.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
* @typedef { import("../types").RuleModule } RuleModule
* @typedef { import("es-html-parser").CommentContentNode } CommentContentNode
* @typedef { import("es-html-parser").TagNode } TagNode
* @typedef { import("es-html-parser").CommentNode } CommentNode
* @typedef { import("../types").ContentNode } ContentNode
* @typedef { import("es-html-parser").TextNode } TextNode
* @typedef { import("../types").LineNode } LineNode
* @typedef { import("../types").Range } Range
* @typedef { import("../types").CommentContent } CommentContent
* @typedef { import("../types").Tag } Tag
* @typedef { import("../types").Comment } Comment
* @typedef { import("../types").Text } Text
* @typedef { import("../types").Line } Line
* @typedef { import("eslint").AST.Range } Range
*/

const { RULE_CATEGORY } = require("../constants");
Expand Down Expand Up @@ -60,12 +59,12 @@ module.exports = {
const skipTags = options.skip || [];
const sourceCode = getSourceCode(context);
/**
* @type {TagNode[]}
* @type {Tag[]}
*/
const tagStack = [];

/**
* @param {CommentNode | TextNode} node
* @param {Comment | Text} node
* @returns {boolean}
*/
function hasSkipTagOnParent(node) {
Expand All @@ -83,7 +82,7 @@ module.exports = {
}

/**
* @param {CommentContentNode | TextNode} node
* @param {CommentContent | Text} node
*/
function stripConsecutiveSpaces(node) {
const text = node.value;
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-plugin/lib/rules/no-inline-styles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* @typedef { import("../types").RuleModule } RuleModule
* @typedef { import("../types").RuleListener } RuleListener
*/

const { RULE_CATEGORY } = require("../constants");
Expand Down
Loading

0 comments on commit fb97ff0

Please sign in to comment.