Skip to content

Commit

Permalink
refactor: change ts to js (#155)
Browse files Browse the repository at this point in the history
* refactor: change ts to js

* refactor: refactor to use NODE_TYPES

* refactor: remove unused code

* ci: add refactor types
  • Loading branch information
yeonjuan authored Nov 15, 2023
1 parent 7f8ab10 commit 671664a
Show file tree
Hide file tree
Showing 24 changed files with 203 additions and 193 deletions.
1 change: 1 addition & 0 deletions .github/workflows/sementic-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
test
chore
ci
refactor
requireScope: false
subjectPattern: ^[a-z].+[^\.]$
subjectPatternError: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ next-env.d.ts
packages/web-linter/out
packages/**/dist
*.tsbuildinfo
packages/parser/types
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ packages/website2/.next
out
.next
dist
packages/parser/types
2 changes: 0 additions & 2 deletions packages/eslint-plugin/lib/constants/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const NODE_TYPES = require("./node-types");
const RULE_CATEGORY = require("./rule-category");
const OBSOLETE_TAGS = require("./obsolete-tags");
const VOID_ELEMENTS = require("./void-elements");

module.exports = {
NODE_TYPES,
RULE_CATEGORY,
OBSOLETE_TAGS,
VOID_ELEMENTS,
Expand Down
13 changes: 0 additions & 13 deletions packages/eslint-plugin/lib/constants/node-types.js

This file was deleted.

4 changes: 3 additions & 1 deletion packages/eslint-plugin/lib/rules/element-newline.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { RULE_CATEGORY } = require("../constants");
const { NODE_TYPES } = require("@html-eslint/parser");

const MESSAGE_IDS = {
EXPECT_NEW_LINE_AFTER: "expectAfter",
Expand Down Expand Up @@ -99,7 +100,8 @@ module.exports = {
return;
}

const children = node.type === "Program" ? node.body : node.children;
const children =
node.type === NODE_TYPES.Program ? node.body : node.children;
checkSiblings(children);
if (skipTags.includes(node.name)) {
isInSkipTags = true;
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/lib/rules/require-attrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @typedef {import("../types").Rule} Rule
*/

const { NODE_TYPES } = require("@html-eslint/parser");
const { RULE_CATEGORY } = require("../constants");

const MESSAGE_IDS = {
Expand Down Expand Up @@ -91,7 +92,7 @@ module.exports = {

return {
[["StyleTag", "ScriptTag"].join(",")](node) {
const tagName = node.type === "StyleTag" ? "style" : "script";
const tagName = node.type === NODE_TYPES.StyleTag ? "style" : "script";
if (!tagOptionsMap.has(tagName)) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/lib/rules/require-meta-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @typedef {import("es-html-parser").TagNode} TagNode
*/

const { NODE_TYPES } = require("@html-eslint/parser");
const { RULE_CATEGORY } = require("../constants");
const { NodeUtils } = require("./utils");

Expand All @@ -16,7 +17,7 @@ const MESSAGE_IDS = {
* @returns {boolean}
*/
function isMetaTagNode(node) {
return node.type === "Tag" && node.name === "meta";
return node.type === NODE_TYPES.Tag && node.name === "meta";
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/lib/rules/require-meta-viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @typedef {import("es-html-parser").TagNode} TagNode
*/

const { NODE_TYPES } = require("@html-eslint/parser");
const { RULE_CATEGORY } = require("../constants");
const { NodeUtils } = require("./utils");

Expand All @@ -17,7 +18,7 @@ const MESSAGE_IDS = {
* @returns {node is TagNode} Return true if the given node is a meta tag with viewport attribute, otherwise false.
*/
function isMetaViewport(node) {
if (node.type === "Tag" && node.name === "meta") {
if (node.type === NODE_TYPES.Tag && node.name === "meta") {
const nameAttribute = NodeUtils.findAttr(node, "name");
return (
nameAttribute &&
Expand Down
5 changes: 3 additions & 2 deletions packages/eslint-plugin/lib/rules/require-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @typedef {import("es-html-parser").TagNode} TagNode
* @typedef {import("es-html-parser").TextNode} TextNode
*/
const { NODE_TYPES } = require("@html-eslint/parser");
const { RULE_CATEGORY } = require("../constants");

const MESSAGE_IDS = {
Expand All @@ -16,7 +17,7 @@ const MESSAGE_IDS = {
* @returns {node is TagNode} Returns true if the given node is a title TagNode, otherwise false
*/
function isTitleTagNode(node) {
return node.type === "Tag" && node.name === "title";
return node.type === NODE_TYPES.Tag && node.name === "title";
}

/**
Expand All @@ -25,7 +26,7 @@ function isTitleTagNode(node) {
* @returns {node is TextNode} Returns true if the given node is a TextNode with non-empty value, otherwise false
*/
function isNonEmptyTextNode(node) {
return node.type === "Text" && node.value.trim().length > 0;
return node.type === NODE_TYPES.Text && node.value.trim().length > 0;
}

/**
Expand Down
35 changes: 2 additions & 33 deletions packages/eslint-plugin/lib/rules/utils/node-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

module.exports = {
/*
/**
* @param {TagNode} node
* @param {string} name
* @returns {AttributeNode | undefined}
Expand All @@ -20,20 +20,7 @@ module.exports = {
(attr) => attr.key && attr.key.value.toLowerCase() === name.toLowerCase()
);
},
/**
* Checks a node has attribute with the given name or not.
* @param {TagNode} node node
* @param {string} name attribute name
* @return {boolean} `true` if the node has a attribute, otherwise `false`.
*/
hasAttr(node, name) {
return (
!!node &&
(node.attributes || []).some(
(attr) => attr.key && attr.key.value === name
)
);
},

/**
* Checks whether a node's all tokens are on the same line or not.
* @param {AnyNode} node A node to check
Expand All @@ -43,24 +30,6 @@ module.exports = {
return node.loc.start.line === node.loc.end.line;
},

/**
* Checks whether a node is a TextNode or not.
* @param {Object} node A node to check
* @returns {node is TextNode} `true` if a node is `TextNode`, otherwise `false`.
*/
isTextNode(node) {
return !!(node && node.type === "text" && typeof node.value === "string");
},

/**
* Checks whether a node is a CommentNode or not.
* @param {Object} node A node to check
* @returns {node is CommentNode} `true` if a node is `CommentNode`, otherwise `false`.
*/
isCommentNode(node) {
return !!(node && node.type === "comment");
},

/**
*
* @param {TextNode | CommentContentNode} node
Expand Down
24 changes: 24 additions & 0 deletions packages/parser/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
extends: ["eslint:recommended"],
plugins: ["jest", "node"],
parserOptions: {
ecmaVersion: 9,
},
env: {
node: true,
es6: true,
},
rules: {
"node/no-unsupported-features/es-builtins": "error",
"node/no-unsupported-features/es-syntax": "error",
"node/no-unsupported-features/node-builtins": "error",
},
overrides: [
{
files: ["*.test.js"],
env: {
"jest/globals": true,
},
},
],
};
5 changes: 5 additions & 0 deletions packages/parser/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { parseForESLint } = require("./parser");
const { NODE_TYPES } = require("./node-types");

module.exports.parseForESLint = parseForESLint;
module.exports.NODE_TYPES = NODE_TYPES;
1 change: 0 additions & 1 deletion packages/parser/lib/index.ts

This file was deleted.

13 changes: 13 additions & 0 deletions packages/parser/lib/node-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { NodeTypes } = require("es-html-parser");

// eslint-disable-next-line no-unused-vars
const { Document, ...restNodeTypes } = NodeTypes;

const NODE_TYPES = {
Program: "Program",
...restNodeTypes,
};

module.exports = {
NODE_TYPES,
};
46 changes: 46 additions & 0 deletions packages/parser/lib/parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @typedef {import("./types").ProgramNode} ProgramNode
*/
const { parse } = require("es-html-parser");
const { visitorKeys } = require("./visitor-keys");
const { traverse } = require("./traverse");
const { NODE_TYPES } = require("./node-types");

/**
* @param {string} code
* @returns {ProgramNode}
*/
module.exports.parseForESLint = function parseForESLint(code) {
const { ast, tokens } = parse(code);

const programNode = {
type: "Program",
body: ast.children,
loc: ast.loc,
range: ast.range,
tokens: tokens.filter(
(token) =>
token.type !== NODE_TYPES.CommentContent &&
token.type !== NODE_TYPES.CommentOpen &&
token.type !== NODE_TYPES.CommentClose
),
comments: [],
};

traverse(programNode, (node) => {
if (node.type === NODE_TYPES.CommentContent) {
programNode.comments.push({
type: node.type,
range: node.range,
loc: node.loc,
value: node.value,
});
}
});

return {
ast: programNode,
visitorKeys,
scopeManager: null,
};
};
39 changes: 0 additions & 39 deletions packages/parser/lib/parser.ts

This file was deleted.

39 changes: 39 additions & 0 deletions packages/parser/lib/traverse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @typedef {import("./types").AnyHTMLNode} AnyHTMLNode
*/
const { visitorKeys } = require("./visitor-keys");

/**
*
* @param {AnyHTMLNode} node
* @param {(arg: AnyHTMLNode) => void} visitor
* @returns {void}
*/
function traverse(node, visitor) {
if (!node) {
return;
}
visitor(node);
const type = node.type;
const keys = visitorKeys[type];

if (!keys || keys.length <= 0) {
return;
}

keys.forEach((key) => {
// @ts-ignore
const value = node[key];
if (value) {
if (Array.isArray(value)) {
value.forEach((n) => traverse(n, visitor));
} else {
traverse(value, visitor);
}
}
});
}

module.exports = {
traverse,
};
30 changes: 0 additions & 30 deletions packages/parser/lib/traverse.ts

This file was deleted.

Loading

0 comments on commit 671664a

Please sign in to comment.