From d3fdb4a9d3e89c538a02e9736a1b1b1a463703e5 Mon Sep 17 00:00:00 2001 From: Chris Oakman Date: Sat, 19 Oct 2024 13:06:08 -0500 Subject: [PATCH] GitHub Issue #103 - convert CRLF to LF line endings (#133) --- lib/standard-clojure-style.js | 7 +++++++ test/format.test.js | 19 +++++++++++++++++++ test_format/line_endings/crlf_input.clj | 24 ++++++++++++++++++++++++ test_format/line_endings/lf_output.clj | 24 ++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 test_format/line_endings/crlf_input.clj create mode 100644 test_format/line_endings/lf_output.clj diff --git a/lib/standard-clojure-style.js b/lib/standard-clojure-style.js index 36aad1e..9cb8702 100644 --- a/lib/standard-clojure-style.js +++ b/lib/standard-clojure-style.js @@ -245,6 +245,10 @@ return s.replace(find, replace) } + function crlfToLf (txt) { + return txt.replace(/\r\n/g, '\n') + } + function strSplit (str, ch) { return str.split(ch) } @@ -3780,6 +3784,9 @@ // Parses inputTxt (Clojure code) and returns a String of it formatted according // to Standard Clojure Style. function format (inputTxt) { + // replace any CRLF with LF before we do anything + inputTxt = crlfToLf(inputTxt) + // FIXME: wrap this in try/catch and return error code if found const tree = parse(inputTxt) const nodesArr = flattenTree(tree) diff --git a/test/format.test.js b/test/format.test.js index 5bda2a0..4d08e98 100644 --- a/test/format.test.js +++ b/test/format.test.js @@ -116,9 +116,28 @@ allTestCases.forEach(testCase => { } }) +const inputFileWithCRLF = fs.readFileSync(path.join(rootDir, 'test_format/line_endings/crlf_input.clj'), 'utf8') +const outputFileWithLF = fs.readFileSync(path.join(rootDir, 'test_format/line_endings/lf_output.clj'), 'utf8') + +test('crlf to lf', () => { + expect(isString(inputFileWithCRLF)).toBe(true) + expect(isString(outputFileWithLF)).toBe(true) + expect(inputFileWithCRLF.includes('\r\n')).toBe(true) + expect(outputFileWithLF.includes('\r\n')).toBe(false) + + const result = scsLib.format(inputFileWithCRLF) + + expect(result.status).toBe('success') + expect(result.out + '\n').toBe(outputFileWithLF) +}) + // ----------------------------------------------------------------------------- // Util function isEnoFile (f) { return path.extname(f) === '.eno' } + +function isString (s) { + return typeof s === 'string' +} diff --git a/test_format/line_endings/crlf_input.clj b/test_format/line_endings/crlf_input.clj new file mode 100644 index 0000000..0877dcb --- /dev/null +++ b/test_format/line_endings/crlf_input.clj @@ -0,0 +1,24 @@ +(ns wolframite.base.cep + (:require + [wolframite.lib.options :as options] + [wolframite.base.convert :as convert] + [wolframite.base.evaluate :as evaluate] + [wolframite.base.parse :as parse])) + +(defn- identity-first [x & _] x) + +(defn cep + "Convert-Evaluate-Parse pipeline. + Convert: from clj data to jlink Expr + Evaluate: the Expr on (some) Wolfram Engine + Parse: returned result into clj data. + Each stage can be skipped with appropriate `opts` `:flag` e.g. `:no-parse`" + [expr {:keys [flags] + :as opts}] + (let [convert (if (options/flag?' flags :convert) convert/convert identity-first) + evaluate (if (options/flag?' flags :evaluate) evaluate/evaluate identity-first) + parse (if (options/flag?' flags :parse) parse/parse identity-first)] + (-> expr + (convert opts) + (evaluate opts) + (parse opts)))) diff --git a/test_format/line_endings/lf_output.clj b/test_format/line_endings/lf_output.clj new file mode 100644 index 0000000..97950fa --- /dev/null +++ b/test_format/line_endings/lf_output.clj @@ -0,0 +1,24 @@ +(ns wolframite.base.cep + (:require + [wolframite.base.convert :as convert] + [wolframite.base.evaluate :as evaluate] + [wolframite.base.parse :as parse] + [wolframite.lib.options :as options])) + +(defn- identity-first [x & _] x) + +(defn cep + "Convert-Evaluate-Parse pipeline. + Convert: from clj data to jlink Expr + Evaluate: the Expr on (some) Wolfram Engine + Parse: returned result into clj data. + Each stage can be skipped with appropriate `opts` `:flag` e.g. `:no-parse`" + [expr {:keys [flags] + :as opts}] + (let [convert (if (options/flag?' flags :convert) convert/convert identity-first) + evaluate (if (options/flag?' flags :evaluate) evaluate/evaluate identity-first) + parse (if (options/flag?' flags :parse) parse/parse identity-first)] + (-> expr + (convert opts) + (evaluate opts) + (parse opts))))