Skip to content

Commit

Permalink
[prettierx] merge updates from Prettier main branch
Browse files Browse the repository at this point in the history
Merge branch 'main' of https://github.com/prettier/prettier into 0.19.0-01-update-branch
  • Loading branch information
brodybits committed Jul 5, 2021
2 parents eaa31de + 159fc3d commit d4e08c9
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 210 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dev-package-test.yml
Expand Up @@ -33,7 +33,7 @@ jobs:
uses: actions/checkout@v2.3.4

- name: Setup Node.js
uses: actions/setup-node@v2.1.5
uses: actions/setup-node@v2.2.0
with:
node-version: ${{ matrix.node }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dev-test.yml
Expand Up @@ -53,7 +53,7 @@ jobs:
fetch-depth: 2

- name: Setup Node.js
uses: actions/setup-node@v2.1.5
uses: actions/setup-node@v2.2.0
with:
node-version: ${{ matrix.node }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/eslint-rules.yml
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: actions/checkout@v2.3.4

- name: Setup Node.js
uses: actions/setup-node@v2.1.5
uses: actions/setup-node@v2.2.0

- name: Install Dependencies
run: yarn install --frozen-lockfile
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions/checkout@v2.3.4

- name: Setup Node.js
uses: actions/setup-node@v2.1.5
uses: actions/setup-node@v2.2.0

- name: Install Dependencies
run: yarn install --frozen-lockfile
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/prod-test.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions/checkout@v2.3.4

- name: Setup Node.js
uses: actions/setup-node@v2.1.5
uses: actions/setup-node@v2.2.0

- name: Install Dependencies
run: yarn install --frozen-lockfile
Expand Down Expand Up @@ -64,7 +64,7 @@ jobs:
uses: actions/checkout@v2.3.4

- name: Setup Node.js
uses: actions/setup-node@v2.1.5
uses: actions/setup-node@v2.2.0

- name: Install Dependencies
run: yarn install --frozen-lockfile
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
uses: actions/checkout@v2.3.4

- name: Setup Node.js
uses: actions/setup-node@v2.1.5
uses: actions/setup-node@v2.2.0
with:
node-version: ${{ matrix.node }}

Expand Down
24 changes: 12 additions & 12 deletions package.json
Expand Up @@ -42,9 +42,9 @@
"@babel/code-frame": "7.14.5",
"@babel/parser": "7.14.7",
"@brodybits/remark-parse": "8.0.5",
"@glimmer/syntax": "0.79.4",
"@glimmer/syntax": "0.80.0",
"@iarna/toml": "2.2.5",
"@typescript-eslint/typescript-estree": "4.28.0",
"@typescript-eslint/typescript-estree": "4.28.1",
"angular-estree-parser": "2.4.0",
"angular-html-parser": "1.8.0",
"camelcase": "6.2.0",
Expand Down Expand Up @@ -107,30 +107,30 @@
"@babel/core": "7.14.6",
"@babel/preset-env": "7.14.7",
"@babel/types": "7.14.5",
"@glimmer/reference": "0.79.4",
"@glimmer/reference": "0.80.0",
"@rollup/plugin-alias": "3.1.2",
"@rollup/plugin-babel": "5.3.0",
"@rollup/plugin-commonjs": "18.1.0",
"@rollup/plugin-json": "4.1.0",
"@rollup/plugin-node-resolve": "13.0.0",
"@rollup/plugin-replace": "2.4.2",
"@types/estree": "0.0.48",
"babel-jest": "27.0.5",
"@types/estree": "0.0.49",
"babel-jest": "27.0.6",
"babel-loader": "8.2.2",
"benchmark": "2.1.4",
"builtin-modules": "3.2.0",
"core-js": "3.15.1",
"core-js": "3.15.2",
"cross-env": "7.0.3",
"cspell": "4.2.8",
"eslint": "7.29.0",
"eslint": "7.30.0",
"eslint-config-prettier": "8.3.0",
"eslint-formatter-friendly": "7.0.0",
"eslint-plugin-import": "2.23.4",
"eslint-plugin-jest": "24.3.6",
"eslint-plugin-prettier-internal-rules": "file:./scripts/tools/eslint-plugin-prettier-internal-rules",
"eslint-plugin-react": "7.24.0",
"eslint-plugin-regexp": "0.12.1",
"eslint-plugin-unicorn": "33.0.1",
"eslint-plugin-regexp": "0.13.0",
"eslint-plugin-unicorn": "34.0.1",
"esm-utils": "1.1.0",
"execa": "5.1.1",
"flow-parser": "0.154.0",
Expand All @@ -143,15 +143,15 @@
"prettier": "2.3.2",
"pretty-bytes": "5.6.0",
"rimraf": "3.0.2",
"rollup": "2.52.4",
"rollup": "2.52.7",
"rollup-plugin-polyfill-node": "0.6.2",
"rollup-plugin-terser": "7.0.2",
"shelljs": "0.8.4",
"snapshot-diff": "0.9.0",
"tempy": "1.0.1",
"terser-webpack-plugin": "5.1.4",
"typescript": "4.3.4",
"webpack": "5.40.0"
"typescript": "4.3.5",
"webpack": "5.42.0"
},
"peerDependenciesMeta": {
"flow-parser": {
Expand Down
19 changes: 19 additions & 0 deletions src/language-html/ast.js
Expand Up @@ -72,13 +72,32 @@ class Node {
return fn(newNode || this);
}

walk(fn) {
for (const NODES_KEY in NODES_KEYS) {
const nodes = this[NODES_KEY];
if (nodes) {
for (let i = 0; i < nodes.length; i++) {
nodes[i].walk(fn);
}
}
}
fn(this);
}

/**
* @param {Object} [overrides]
*/
clone(overrides) {
return new Node(overrides ? { ...this, ...overrides } : this);
}

/**
* @param {Array} [children]
*/
setChildren(children) {
this._setNodes("children", children);
}

get firstChild() {
// @ts-ignore
return isNonEmptyArray(this.children) ? this.children[0] : null;
Expand Down
89 changes: 41 additions & 48 deletions src/language-html/print-preprocess.js
Expand Up @@ -31,14 +31,15 @@ const PREPROCESS_PIPELINE = [
];

function preprocess(ast, options) {
const res = ast.map((node) => node);
for (const fn of PREPROCESS_PIPELINE) {
ast = fn(ast, options);
fn(res, options);
}
return ast;
return res;
}

function removeIgnorableFirstLf(ast /*, options */) {
return ast.map((node) => {
ast.walk((node) => {
if (
node.type === "element" &&
node.tagDefinition.ignoreFirstLf &&
Expand All @@ -47,14 +48,12 @@ function removeIgnorableFirstLf(ast /*, options */) {
node.children[0].value[0] === "\n"
) {
const [text, ...rest] = node.children;
return node.clone({
children:
text.value.length === 1
? rest
: [text.clone({ value: text.value.slice(1) }), ...rest],
});
node.setChildren(
text.value.length === 1
? rest
: [text.clone({ value: text.value.slice(1) }), ...rest]
);
}
return node;
});
}

Expand All @@ -72,7 +71,7 @@ function mergeIeConditonalStartEndCommentIntoElementOpeningTag(
node.firstChild &&
node.firstChild.type === "ieConditionalEndComment" &&
node.firstChild.sourceSpan.start.offset === node.startSourceSpan.end.offset;
return ast.map((node) => {
ast.walk((node) => {
if (node.children) {
const isTargetResults = node.children.map(isTarget);
if (isTargetResults.some(Boolean)) {
Expand Down Expand Up @@ -114,15 +113,14 @@ function mergeIeConditonalStartEndCommentIntoElementOpeningTag(
newChildren.push(child);
}

return node.clone({ children: newChildren });
node.setChildren(newChildren);
}
}
return node;
});
}

function mergeNodeIntoText(ast, shouldMerge, getValue) {
return ast.map((node) => {
ast.walk((node) => {
if (node.children) {
const shouldMergeResults = node.children.map(shouldMerge);
if (shouldMergeResults.some(Boolean)) {
Expand Down Expand Up @@ -159,11 +157,9 @@ function mergeNodeIntoText(ast, shouldMerge, getValue) {
})
);
}
return node.clone({ children: newChildren });
node.setChildren(newChildren);
}
}

return node;
});
}

Expand Down Expand Up @@ -192,7 +188,7 @@ function mergeSimpleElementIntoText(ast /*, options */) {
node.prev.type === "text" &&
node.next &&
node.next.type === "text";
return ast.map((node) => {
ast.walk((node) => {
if (node.children) {
const isSimpleElementResults = node.children.map(isSimpleElement);
if (isSimpleElementResults.some(Boolean)) {
Expand Down Expand Up @@ -223,22 +219,21 @@ function mergeSimpleElementIntoText(ast /*, options */) {
newChildren.push(child);
}
}
return node.clone({ children: newChildren });
node.setChildren(newChildren);
}
}
return node;
});
}

function extractInterpolation(ast, options) {
if (options.parser === "html") {
return ast;
return;
}

const interpolationRegex = /{{(.+?)}}/gs;
return ast.map((node) => {
ast.walk((node) => {
if (!canHaveInterpolation(node)) {
return node;
return;
}

const newChildren = [];
Expand Down Expand Up @@ -292,7 +287,7 @@ function extractInterpolation(ast, options) {
}
}

return node.clone({ children: newChildren });
node.setChildren(newChildren);
});
}

Expand All @@ -305,9 +300,9 @@ function extractInterpolation(ast, options) {
*/
const WHITESPACE_NODE = { type: "whitespace" };
function extractWhitespaces(ast /*, options*/) {
return ast.map((node) => {
ast.walk((node) => {
if (!node.children) {
return node;
return;
}

if (
Expand All @@ -316,19 +311,16 @@ function extractWhitespaces(ast /*, options*/) {
node.children[0].type === "text" &&
htmlTrim(node.children[0].value).length === 0)
) {
return node.clone({
children: [],
hasDanglingSpaces: node.children.length > 0,
});
node.hasDanglingSpaces = node.children.length > 0;
node.children = [];
return;
}

const isWhitespaceSensitive = isWhitespaceSensitiveNode(node);
const isIndentationSensitive = isIndentationSensitiveNode(node);

return node.clone({
isWhitespaceSensitive,
isIndentationSensitive,
children: node.children
node.setChildren(
node.children
// extract whitespace nodes
.flatMap((child) => {
if (child.type !== "text" || isWhitespaceSensitive) {
Expand Down Expand Up @@ -374,13 +366,15 @@ function extractWhitespaces(ast /*, options*/) {
};
})
// filter whitespace nodes
.filter(Boolean),
});
.filter(Boolean)
);
node.isWhitespaceSensitive = isWhitespaceSensitive;
node.isIndentationSensitive = isIndentationSensitive;
});
}

function addIsSelfClosing(ast /*, options */) {
return ast.map((node) =>
ast.walk((node) =>
Object.assign(node, {
isSelfClosing:
!node.children ||
Expand All @@ -393,7 +387,7 @@ function addIsSelfClosing(ast /*, options */) {
}

function addHasHtmComponentClosingTag(ast, options) {
return ast.map((node) =>
ast.walk((node) =>
node.type !== "element"
? node
: Object.assign(node, {
Expand All @@ -410,7 +404,7 @@ function addHasHtmComponentClosingTag(ast, options) {
}

function addCssDisplay(ast, options) {
return ast.map((node) =>
ast.walk((node) =>
Object.assign(node, { cssDisplay: getNodeCssStyleDisplay(node, options) })
);
}
Expand All @@ -421,19 +415,18 @@ function addCssDisplay(ast, options) {
* - add `isDanglingSpaceSensitive` field for parent nodes
*/
function addIsSpaceSensitive(ast, options) {
return ast.map((node) => {
ast.walk((node) => {
if (!node.children) {
return node;
return;
}

if (node.children.length === 0) {
return node.clone({
isDanglingSpaceSensitive: isDanglingSpaceSensitiveNode(node),
});
node.isDanglingSpaceSensitive = isDanglingSpaceSensitiveNode(node);
return;
}

return node.clone({
children: node.children
node.setChildren(
node.children
.map((child) => ({
...child,
isLeadingSpaceSensitive: isLeadingSpaceSensitiveNode(child, options),
Expand All @@ -454,8 +447,8 @@ function addIsSpaceSensitive(ast, options) {
? child.isTrailingSpaceSensitive
: children[index + 1].isLeadingSpaceSensitive &&
child.isTrailingSpaceSensitive,
})),
});
}))
);
});
}

Expand Down

0 comments on commit d4e08c9

Please sign in to comment.