Skip to content

Commit

Permalink
fix: get rid of CRLF (#5494)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Dec 8, 2018
1 parent b878a54 commit 952bc0c
Show file tree
Hide file tree
Showing 131 changed files with 2,061 additions and 2,484 deletions.
5 changes: 1 addition & 4 deletions .gitattributes
@@ -1,4 +1 @@
# Make sure baseline files have consistent line endings
*.txt text eol=lf
*.snap text eol=lf
*.debug-check text eol=lf
* text=auto eol=lf
3 changes: 2 additions & 1 deletion azure-pipelines.yml
@@ -1,6 +1,6 @@
trigger:
- master
- releases-*
- release-*

variables:
AST_COMPARE: true
Expand Down Expand Up @@ -31,6 +31,7 @@ jobs:
vmImage: vs2017-win2016
variables:
node_version: 10
TEST_CRLF: true
steps:
- template: .azure-pipelines/dev.yml

Expand Down
10 changes: 3 additions & 7 deletions jest.config.js
Expand Up @@ -17,13 +17,9 @@ module.exports = {
"jest-snapshot-serializer-ansi"
],
testRegex: "jsfmt\\.spec\\.js$|__tests__/.*\\.js$",
testPathIgnorePatterns: ["tests/new_react", "tests/more_react"]
.concat(isOldNode ? requiresPrettierInternals : [])
.concat(
require("os").EOL == "\n"
? ["tests_integration/__tests__/eol-crlf.js"]
: ["tests_integration/__tests__/eol-lf.js"]
),
testPathIgnorePatterns: ["tests/new_react", "tests/more_react"].concat(
isOldNode ? requiresPrettierInternals : []
),
collectCoverage: ENABLE_COVERAGE,
collectCoverageFrom: ["src/**/*.js", "index.js", "!<rootDir>/node_modules/"],
coveragePathIgnorePatterns: [
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -71,8 +71,8 @@
"unicode-regex": "2.0.0",
"unified": "6.1.6",
"vnopts": "1.0.2",
"yaml": "1.0.0-rc.8",
"yaml-unist-parser": "1.0.0-rc.4"
"yaml": "1.0.2",
"yaml-unist-parser": "1.0.0"
},
"devDependencies": {
"@babel/cli": "7.1.5",
Expand Down
12 changes: 12 additions & 0 deletions src/common/util.js
Expand Up @@ -675,7 +675,19 @@ function isWithinParentArrayProperty(path, propertyName) {
return parent[propertyName][key] === node;
}

function replaceEndOfLineWith(text, replacement) {
const parts = [];
for (const part of text.split("\n")) {
if (parts.length !== 0) {
parts.push(replacement);
}
parts.push(part);
}
return parts;
}

module.exports = {
replaceEndOfLineWith,
getStringWidth,
getMaxContinuousCount,
getPrecedence,
Expand Down
6 changes: 1 addition & 5 deletions src/language-css/printer-postcss.js
Expand Up @@ -110,11 +110,7 @@ function genericPrint(path, options, print) {
}
case "css-comment": {
if (node.raws.content) {
return (
node.raws.content
// there's a bug in the less parser that trailing `\r`s are included in inline comments
.replace(/^(\/\/[^]+)\r+$/, "$1")
);
return node.raws.content;
}
const text = options.originalText.slice(
options.locStart(node),
Expand Down
1 change: 0 additions & 1 deletion src/language-html/parser-html.js
Expand Up @@ -265,7 +265,6 @@ function createParser({
allowHtmComponentClosingTags = false
} = {}) {
return {
preprocess: text => text.replace(/\r\n?/g, "\n"),
parse: (text, parsers, options) =>
_parse(text, options, {
recognizeSelfClosing,
Expand Down
34 changes: 17 additions & 17 deletions src/language-html/printer-html.js
Expand Up @@ -34,12 +34,11 @@ const {
isTextLikeNode,
normalizeParts,
preferHardlineAsLeadingSpaces,
replaceDocNewlines,
replaceNewlines,
shouldNotPrintClosingTag,
shouldPreserveContent,
unescapeQuoteEntities
} = require("./utils");
const { replaceEndOfLineWith } = require("../common/util");
const preprocess = require("./preprocess");
const assert = require("assert");
const { insertPragma } = require("./pragma");
Expand Down Expand Up @@ -147,10 +146,7 @@ function embed(path, print, textToDoc, options) {
hardline,
node.value.trim().length === 0
? ""
: replaceDocNewlines(
textToDoc(node.value, { parser: "yaml" }),
literalline
),
: textToDoc(node.value, { parser: "yaml" }),
"---"
])
);
Expand Down Expand Up @@ -287,7 +283,7 @@ function genericPrint(path, options, print) {
? node.value.replace(trailingNewlineRegex, "")
: node.value;
return concat([
concat(replaceNewlines(value, literalline)),
concat(replaceEndOfLineWith(value, literalline)),
hasTrailingNewline ? hardline : ""
]);
}
Expand Down Expand Up @@ -316,7 +312,7 @@ function genericPrint(path, options, print) {
return concat([
printOpeningTagPrefix(node, options),
concat(
replaceNewlines(
replaceEndOfLineWith(
options.originalText.slice(
options.locStart(node),
options.locEnd(node)
Expand All @@ -341,7 +337,7 @@ function genericPrint(path, options, print) {
"=",
quote,
concat(
replaceNewlines(
replaceEndOfLineWith(
quote === '"'
? value.replace(/"/g, "&quot;")
: value.replace(/'/g, "&apos;"),
Expand All @@ -354,7 +350,7 @@ function genericPrint(path, options, print) {
}
case "yaml":
case "toml":
return node.raw;
return concat(replaceEndOfLineWith(node.raw, literalline));
default:
throw new Error(`Unexpected node type ${node.type}`);
}
Expand Down Expand Up @@ -473,7 +469,7 @@ function printChildren(path, options, print) {
return concat(
[].concat(
printOpeningTagPrefix(child, options),
replaceNewlines(
replaceEndOfLineWith(
options.originalText.slice(
options.locStart(child) +
(child.prev &&
Expand All @@ -497,7 +493,7 @@ function printChildren(path, options, print) {
[].concat(
printOpeningTagPrefix(child, options),
group(printOpeningTag(childPath, options, print)),
replaceNewlines(
replaceEndOfLineWith(
options.originalText.slice(
child.startSourceSpan.end.offset +
(child.firstChild &&
Expand Down Expand Up @@ -620,7 +616,7 @@ function printOpeningTag(path, options, print) {
const attr = attrPath.getValue();
return hasPrettierIgnoreAttribute(attr)
? concat(
replaceNewlines(
replaceEndOfLineWith(
options.originalText.slice(
options.locStart(attr),
options.locEnd(attr)
Expand Down Expand Up @@ -894,8 +890,8 @@ function printClosingTagEndMarker(node, options) {
function getTextValueParts(node, value = node.value) {
return node.parent.isWhitespaceSensitive
? node.parent.isIndentationSensitive
? replaceNewlines(value, literalline)
: replaceNewlines(
? replaceEndOfLineWith(value, literalline)
: replaceEndOfLineWith(
dedentString(value.replace(/^\s*?\n|\n\s*?$/g, "")),
hardline
)
Expand Down Expand Up @@ -1037,7 +1033,7 @@ function printEmbeddedAttributeValue(node, originalTextToDoc, options) {
const parts = [];
value.split(interpolationRegex).forEach((part, index) => {
if (index % 2 === 0) {
parts.push(concat(replaceNewlines(part, literalline)));
parts.push(concat(replaceEndOfLineWith(part, literalline)));
} else {
try {
parts.push(
Expand All @@ -1056,7 +1052,11 @@ function printEmbeddedAttributeValue(node, originalTextToDoc, options) {
)
);
} catch (e) {
parts.push("{{", concat(replaceNewlines(part, literalline)), "}}");
parts.push(
"{{",
concat(replaceEndOfLineWith(part, literalline)),
"}}"
);
}
}
});
Expand Down
21 changes: 0 additions & 21 deletions src/language-html/utils.js
@@ -1,10 +1,5 @@
"use strict";

const {
builders: { concat },
utils: { mapDoc }
} = require("../doc");

const {
CSS_DISPLAY_TAGS,
CSS_DISPLAY_DEFAULT,
Expand Down Expand Up @@ -263,20 +258,6 @@ function isDanglingSpaceSensitiveNode(node) {
);
}

function replaceNewlines(text, replacement) {
return text
.split(/(\n)/g)
.map((data, index) => (index % 2 === 1 ? replacement : data));
}

function replaceDocNewlines(doc, replacement) {
return mapDoc(doc, currentDoc =>
typeof currentDoc === "string" && currentDoc.includes("\n")
? concat(replaceNewlines(currentDoc, replacement))
: currentDoc
);
}

function forceNextEmptyLine(node) {
return (
isFrontMatterNode(node) ||
Expand Down Expand Up @@ -645,8 +626,6 @@ module.exports = {
normalizeParts,
preferHardlineAsLeadingSpaces,
preferHardlineAsTrailingSpaces,
replaceDocNewlines,
replaceNewlines,
shouldNotPrintClosingTag,
shouldPreserveContent,
unescapeQuoteEntities
Expand Down
5 changes: 4 additions & 1 deletion src/language-js/printer-estree.js
Expand Up @@ -2395,7 +2395,10 @@ function printPathNoParens(path, options, print, args) {
"${" +
printDocToString(
doc,
Object.assign({}, options, { printWidth: Infinity })
Object.assign({}, options, {
printWidth: Infinity,
endOfLine: "lf"
})
).formatted +
"}"
);
Expand Down
2 changes: 1 addition & 1 deletion src/language-markdown/embed.js
Expand Up @@ -42,7 +42,7 @@ function embed(path, print, textToDoc, options) {
concat([
"---",
hardline,
node.value.trim()
node.value && node.value.trim()
? replaceNewlinesWithLiterallines(
textToDoc(node.value, { parser: "yaml" })
)
Expand Down
2 changes: 1 addition & 1 deletion src/language-markdown/pragma.js
Expand Up @@ -9,7 +9,7 @@ function startWithPragma(text) {
const regex = new RegExp(
[
`<!--\\s*${pragma}\\s*-->`,
`<!--.*\n[\\s\\S]*(^|\n)[^\\S\n]*${pragma}[^\\S\n]*($|\n)[\\s\\S]*\n.*-->`
`<!--.*\r?\n[\\s\\S]*(^|\n)[^\\S\n]*${pragma}[^\\S\n]*($|\n)[\\s\\S]*\n.*-->`
].join("|"),
"m"
);
Expand Down
33 changes: 20 additions & 13 deletions src/language-markdown/printer-markdown.js
Expand Up @@ -27,6 +27,7 @@ const {
splitText,
punctuationPattern
} = require("./utils");
const { replaceEndOfLineWith } = require("../common/util");

const TRAILING_HARDLINE_NODES = ["importExport"];

Expand Down Expand Up @@ -209,7 +210,10 @@ function genericPrint(path, options, print) {
const alignment = " ".repeat(4);
return align(
alignment,
concat([alignment, replaceNewlinesWith(node.value, hardline)])
concat([
alignment,
concat(replaceEndOfLineWith(node.value, hardline))
])
);
}

Expand All @@ -225,9 +229,11 @@ function genericPrint(path, options, print) {
style,
node.lang || "",
hardline,
replaceNewlinesWith(
getFencedCodeBlockValue(node, options.originalText),
hardline
concat(
replaceEndOfLineWith(
getFencedCodeBlockValue(node, options.originalText),
hardline
)
),
hardline,
style
Expand All @@ -247,9 +253,11 @@ function genericPrint(path, options, print) {
? node.value.trimRight()
: node.value;
const isHtmlComment = /^<!--[\s\S]*-->$/.test(value);
return replaceNewlinesWith(
value,
isHtmlComment ? hardline : markAsRoot(literalline)
return concat(
replaceEndOfLineWith(
value,
isHtmlComment ? hardline : markAsRoot(literalline)
)
);
}
case "list": {
Expand Down Expand Up @@ -394,7 +402,7 @@ function genericPrint(path, options, print) {
? concat([" ", markAsRoot(literalline)])
: concat(["\\", hardline]);
case "liquidNode":
return replaceNewlinesWith(node.value, hardline);
return concat(replaceEndOfLineWith(node.value, hardline));
// MDX
case "importExport":
case "jsx":
Expand All @@ -404,7 +412,10 @@ function genericPrint(path, options, print) {
"$$",
hardline,
node.value
? concat([replaceNewlinesWith(node.value, hardline), hardline])
? concat([
concat(replaceEndOfLineWith(node.value, hardline)),
hardline
])
: "",
"$$"
]);
Expand Down Expand Up @@ -467,10 +478,6 @@ function getNthListSiblingIndex(node, parentNode) {
);
}

function replaceNewlinesWith(str, doc) {
return join(doc, str.replace(/\r\n?/g, "\n").split("\n"));
}

function getNthSiblingIndex(node, parentNode, condition) {
condition = condition || (() => true);

Expand Down
2 changes: 1 addition & 1 deletion src/language-markdown/utils.js
Expand Up @@ -148,7 +148,7 @@ function getFencedCodeBlockValue(node, originalText) {
const leadingSpaceCount = text.match(/^\s*/)[0].length;
const replaceRegex = new RegExp(`^\\s{0,${leadingSpaceCount}}`);

const lineContents = text.replace(/\r\n?/g, "\n").split("\n");
const lineContents = text.split("\n");

const markerStyle = text[leadingSpaceCount]; // ` or ~
const marker = text
Expand Down
6 changes: 1 addition & 5 deletions src/language-yaml/parser-yaml.js
Expand Up @@ -29,11 +29,7 @@ const parser = {
parse,
hasPragma,
locStart: node => node.position.start.offset,
locEnd: node => node.position.end.offset,

// workaround for https://github.com/eemeli/yaml/issues/20
preprocess: text =>
text.indexOf("\r") === -1 ? text : text.replace(/\r\n?/g, "\n")
locEnd: node => node.position.end.offset
};

module.exports = {
Expand Down

0 comments on commit 952bc0c

Please sign in to comment.