Remove carriage returns
MODIFY
packages/@romejs/cli-diagnostics/printAdvice.ts
packages/@romejs/cli-diagnostics/printAdvice.ts
@@ -32,6 +32,7 @@ import DiagnosticsPrinter, {
import {AbsoluteFilePathSet} from '@romejs/path';
import {RAW_CODE_MAX_LENGTH} from './constants';
import {Diffs, diffConstants} from '@romejs/string-diff';
+import {removeCarriageReturn} from '@romejs/string-utils';
type AdvicePrintOptions = {
printer: DiagnosticsPrinter;
@@ -107,10 +108,6 @@ function printInspect(
return DID_PRINT;
}
-function removeCRLF(str: string): string {
- return str.replace(/\r/g, '');
-}
-
function generateDiffHint(diffs: Diffs): undefined | DiagnosticAdviceItem {
let expected = '';
let received = '';
@@ -143,7 +140,7 @@ function generateDiffHint(diffs: Diffs): undefined | DiagnosticAdviceItem {
};
}
- const receivedNoCRLF = removeCRLF(received);
+ const receivedNoCRLF = removeCarriageReturn(received);
if (expected === receivedNoCRLF) {
return {
type: 'log',
@@ -152,7 +149,7 @@ function generateDiffHint(diffs: Diffs): undefined | DiagnosticAdviceItem {
};
}
- const expectedNoCRLF = removeCRLF(expected);
+ const expectedNoCRLF = removeCarriageReturn(expected);
if (received === expectedNoCRLF) {
return {
type: 'log',

MODIFY
packages/@romejs/js-parser/index.test.ts
packages/@romejs/js-parser/index.test.ts
@@ -11,6 +11,7 @@ import {createFixtureTests} from '@romejs/test-helpers';
import prettyFormat from '@romejs/pretty-format';
import {ConstProgramSyntax} from '@romejs/js-ast';
import {writeFileSync} from '@romejs/fs';
+import {removeCarriageReturn} from '@romejs/string-utils';
const promise = createFixtureTests(
(fixture, t) => {
@@ -29,7 +30,7 @@ const promise = createFixtureTests(
const outputFile = files.get('output.txt');
let outputContent = undefined;
if (outputFile !== undefined) {
- outputContent = outputFile.content.toString().replace(/\r/g, '');
+ outputContent = removeCarriageReturn(outputFile.content.toString());
}
const sourceTypeProp = options.get('sourceType');
@@ -67,7 +68,7 @@ const promise = createFixtureTests(
},
});
- const inputContent = inputFile.content.toString().replace(/\r/g, '');
+ const inputContent = removeCarriageReturn(inputFile.content.toString());
t.addToAdvice({
type: 'log',
category: 'info',

MODIFY
packages/@romejs/js-parser/tokenizer/index.ts
packages/@romejs/js-parser/tokenizer/index.ts
@@ -41,6 +41,7 @@ import {
number0Neg1,
number0,
} from '@romejs/ob1';
+import {removeCarriageReturn} from '@romejs/string-utils';
const HEX_NUMBER = /^[\da-fA-F]+$/;
const DECIMAL_NUMBER = /^\d+$/;
@@ -286,10 +287,7 @@ function pushComment(parser: JSParser, opts: {
if (opts.block) {
comment = {
type: 'CommentBlock',
- // Remove carriage returns. We want to have predictable line endings.
- // If this is indeed a Windows systems then we'll correctly retain their line endings
- // in the lint autofixer.
- value: opts.text.replace(/\r/g, ''),
+ value: removeCarriageReturn(opts.text),
loc,
};
} else {

MODIFY
packages/@romejs/parser-core/index.ts
packages/@romejs/parser-core/index.ts
@@ -43,6 +43,7 @@ import {
import {escapeMarkup} from '@romejs/string-markup';
import {UnknownFilePath, createUnknownFilePath} from '@romejs/path';
import {Class, OptionalProps} from '@romejs/typescript-helpers';
+import {removeCarriageReturn} from '@romejs/string-utils';
export * from './types';
@@ -105,19 +106,28 @@ type ParserSnapshot<Tokens extends TokensShape, State> = {
state: State;
};
+function normalizeInput(opts: ParserOptions): string {
+ const {input} = opts;
+ if (input === undefined) {
+ return '';
+ } else {
+ return removeCarriageReturn(input);
+ }
+}
+
export class ParserCore<Tokens extends TokensShape, State> {
constructor(
opts: ParserOptions,
diagnosticCategory: DiagnosticCategory,
initialState: State,
) {
- const {path, mtime, input, offsetPosition} = opts;
+ const {path, mtime, offsetPosition} = opts;
// Input information
this.path = path === undefined ? undefined : createUnknownFilePath(path);
this.filename = this.path === undefined ? undefined : this.path.join();
this.mtime = mtime;
- this.input = input === undefined ? '' : input;
+ this.input = normalizeInput(opts);
this.length = coerce0(this.input.length);
this.eofToken = {

MODIFY
packages/@romejs/string-utils/index.ts
packages/@romejs/string-utils/index.ts
@@ -26,3 +26,5 @@ export * from './orderByNatural';
export * from './toKebabCase';
export * from './toCamelCase';
+
+export * from './removeCarriageReturn';

ADD
packages/@romejs/string-utils/removeCarriageReturn.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+export function removeCarriageReturn(str: string): string {
+ return str.replace(/\r/g, '');
+}

Remove carriage returns