Refactor: Use ignoredProperties for all languages (#9586)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-11-05 14:12:14 +08:00
1 parent 3d7016e2dc
commit 309ffdd562
7 files changed
+128 -109

No files matched your search

+12 -12
View File
@@ -3,19 +3,17 @@
const { isFrontMatterNode } = require("../common/util");
const getLast = require("../utils/get-last");
function clean(ast, newObj, parent) {
[
"raw", // front-matter
"raws",
"sourceIndex",
"source",
"before",
"after",
"trailingComma",
].forEach((name) => {
delete newObj[name];
});
const ignoredProperties = new Set([
"raw", // front-matter
"raws",
"sourceIndex",
"source",
"before",
"after",
"trailingComma",
]);
function clean(ast, newObj, parent) {
if (isFrontMatterNode(ast) && ast.lang === "yaml") {
delete newObj.value;
}
@@ -170,6 +168,8 @@ function clean(ast, newObj, parent) {
}
}
clean.ignoredProperties = ignoredProperties;
function cleanCSSStrings(value) {
return value.replace(/'/g, '"').replace(/\\([^\dA-Fa-f])/g, "$1");
}
+2 -4
View File
@@ -677,10 +677,8 @@ function printInterfaces(path, options, print) {
return parts;
}
function clean(node, newNode /*, parent*/) {
delete newNode.loc;
delete newNode.comments;
}
function clean(/*node, newNode , parent*/) {}
clean.ignoredProperties = new Set(["loc", "comments"]);
module.exports = {
print: genericPrint,
+5 -5
View File
@@ -1,9 +1,6 @@
"use strict";
module.exports = function (ast, newNode) {
delete newNode.loc;
delete newNode.selfClosing;
function clean(ast, newNode /*, parent*/) {
// (Glimmer/HTML) ignore TextNode whitespace
if (ast.type === "TextNode") {
const trimmed = ast.chars.trim();
@@ -12,4 +9,7 @@ module.exports = function (ast, newNode) {
}
newNode.chars = trimmed;
}
};
}
clean.ignoredProperties = new Set(["loc", "selfClosing"]);
module.exports = clean;
+13 -7
View File
@@ -2,13 +2,15 @@
const { isFrontMatterNode } = require("../common/util");
module.exports = function (ast, newNode) {
delete newNode.sourceSpan;
delete newNode.startSourceSpan;
delete newNode.endSourceSpan;
delete newNode.nameSpan;
delete newNode.valueSpan;
const ignoredProperties = new Set([
"sourceSpan",
"startSourceSpan",
"endSourceSpan",
"nameSpan",
"valueSpan",
]);
function clean(ast, newNode) {
if (ast.type === "text" || ast.type === "comment") {
return null;
}
@@ -25,4 +27,8 @@ module.exports = function (ast, newNode) {
if (ast.type === "docType") {
delete newNode.value;
}
};
}
clean.ignoredProperties = ignoredProperties;
module.exports = clean;
+15 -10
View File
@@ -58,23 +58,28 @@ function genericPrint(path, options, print) {
}
}
function clean(node, newNode /*, parent*/) {
delete newNode.start;
delete newNode.end;
delete newNode.extra;
delete newNode.loc;
delete newNode.comments;
delete newNode.errors;
delete newNode.range;
const ignoredProperties = new Set([
"start",
"end",
"extra",
"loc",
"comments",
"errors",
"range",
]);
if (node.type === "Identifier") {
function clean(node, newNode /*, parent*/) {
const { type } = node;
if (type === "Identifier") {
return { type: "StringLiteral", value: node.name };
}
if (node.type === "UnaryExpression" && node.operator === "+") {
if (type === "UnaryExpression" && node.operator === "+") {
return newNode.argument;
}
}
clean.ignoredProperties = ignoredProperties;
module.exports = {
preprocess,
print: genericPrint,
+77
View File
@@ -0,0 +1,77 @@
"use strict";
const { isFrontMatterNode } = require("../common/util");
const { startWithPragma } = require("./pragma");
const ignoredProperties = new Set([
"position",
"raw", // front-matter
]);
function clean(ast, newObj, parent) {
// for codeblock
if (
ast.type === "front-matter" ||
ast.type === "code" ||
ast.type === "yaml" ||
ast.type === "import" ||
ast.type === "export" ||
ast.type === "jsx"
) {
delete newObj.value;
}
if (ast.type === "list") {
delete newObj.isAligned;
}
if (ast.type === "list" || ast.type === "listItem") {
delete newObj.spread;
delete newObj.loose;
}
// texts can be splitted or merged
if (ast.type === "text") {
return null;
}
if (ast.type === "inlineCode") {
newObj.value = ast.value.replace(/[\t\n ]+/g, " ");
}
if (ast.type === "wikiLink") {
newObj.value = ast.value.trim().replace(/[\t\n]+/g, " ");
}
if (ast.type === "definition" || ast.type === "linkReference") {
newObj.label = ast.label
.trim()
.replace(/[\t\n ]+/g, " ")
.toLowerCase();
}
if (
(ast.type === "definition" ||
ast.type === "link" ||
ast.type === "image") &&
ast.title
) {
newObj.title = ast.title.replace(/\\(["')])/g, "$1");
}
// for insert pragma
if (
parent &&
parent.type === "root" &&
parent.children.length > 0 &&
(parent.children[0] === ast ||
(isFrontMatterNode(parent.children[0]) && parent.children[1] === ast)) &&
ast.type === "html" &&
startWithPragma(ast.value)
) {
return null;
}
}
clean.ignoredProperties = ignoredProperties;
module.exports = clean;
+4 -71
View File
@@ -25,10 +25,11 @@ const {
utils: { normalizeDoc },
printer: { printDocToString },
} = require("../document");
const { replaceEndOfLineWith, isFrontMatterNode } = require("../common/util");
const { replaceEndOfLineWith } = require("../common/util");
const embed = require("./embed");
const pragma = require("./pragma");
const { insertPragma } = require("./pragma");
const preprocess = require("./preprocess");
const clean = require("./clean");
const {
getFencedCodeBlockValue,
hasGitDiffFriendlyOrderedList,
@@ -935,74 +936,6 @@ function clamp(value, min, max) {
return value < min ? min : value > max ? max : value;
}
function clean(ast, newObj, parent) {
delete newObj.position;
delete newObj.raw; // front-matter
// for codeblock
if (
ast.type === "front-matter" ||
ast.type === "code" ||
ast.type === "yaml" ||
ast.type === "import" ||
ast.type === "export" ||
ast.type === "jsx"
) {
delete newObj.value;
}
if (ast.type === "list") {
delete newObj.isAligned;
}
if (ast.type === "list" || ast.type === "listItem") {
delete newObj.spread;
delete newObj.loose;
}
// texts can be splitted or merged
if (ast.type === "text") {
return null;
}
if (ast.type === "inlineCode") {
newObj.value = ast.value.replace(/[\t\n ]+/g, " ");
}
if (ast.type === "wikiLink") {
newObj.value = ast.value.trim().replace(/[\t\n]+/g, " ");
}
if (ast.type === "definition" || ast.type === "linkReference") {
newObj.label = ast.label
.trim()
.replace(/[\t\n ]+/g, " ")
.toLowerCase();
}
if (
(ast.type === "definition" ||
ast.type === "link" ||
ast.type === "image") &&
ast.title
) {
newObj.title = ast.title.replace(/\\(["')])/g, "$1");
}
// for insert pragma
if (
parent &&
parent.type === "root" &&
parent.children.length > 0 &&
(parent.children[0] === ast ||
(isFrontMatterNode(parent.children[0]) && parent.children[1] === ast)) &&
ast.type === "html" &&
pragma.startWithPragma(ast.value)
) {
return null;
}
}
function hasPrettierIgnore(path) {
const index = +path.getName();
@@ -1020,5 +953,5 @@ module.exports = {
embed,
massageAstNode: clean,
hasPrettierIgnore,
insertPragma: pragma.insertPragma,
insertPragma,
};