Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/languages/json-source-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class JSONSourceCode extends TextSourceCodeBase {
* @param {DocumentNode} options.ast The root AST node.
*/
constructor({ text, ast }) {
super({ text, ast });
super({ text, ast, lineEndingPattern: /\r\n|[\r\n]/u });
this.ast = ast;

const { comments, starts, ends } = processTokens(this.ast.tokens ?? []);
Expand Down
46 changes: 45 additions & 1 deletion tests/languages/json-source-code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ describe("JSONSourceCode", () => {
});

describe("lines", () => {
it("should return an array of lines", () => {
it("should split lines on LF line endings", () => {
const file = { body: "{\n//test\n}", path: "test.jsonc" };
const language = new JSONLanguage({ mode: "jsonc" });
const parseResult = language.parse(file);
Expand All @@ -357,6 +357,50 @@ describe("JSONSourceCode", () => {

assert.deepStrictEqual(sourceCode.lines, ["{", "//test", "}"]);
});

it("should split lines on CR line endings", () => {
const file = { body: "{\r//test\r}", path: "test.jsonc" };
const language = new JSONLanguage({ mode: "jsonc" });
const parseResult = language.parse(file);
const sourceCode = new JSONSourceCode({
text: file.body,
ast: parseResult.ast,
});

assert.deepStrictEqual(sourceCode.lines, ["{", "//test", "}"]);
});

it("should split lines on CRLF line endings", () => {
const file = { body: "{\r\n//test\r\n}", path: "test.jsonc" };
const language = new JSONLanguage({ mode: "jsonc" });
const parseResult = language.parse(file);
const sourceCode = new JSONSourceCode({
text: file.body,
ast: parseResult.ast,
});

assert.deepStrictEqual(sourceCode.lines, ["{", "//test", "}"]);
});

it("should split lines with mixed line endings (LF, CRLF, CR)", () => {
const file = {
body: "{\n//one\r\n//two\r}",
path: "test.jsonc",
};
const language = new JSONLanguage({ mode: "jsonc" });
const parseResult = language.parse(file);
const sourceCode = new JSONSourceCode({
text: file.body,
ast: parseResult.ast,
});

assert.deepStrictEqual(sourceCode.lines, [
"{",
"//one",
"//two",
"}",
]);
});
});

describe("getParent()", () => {
Expand Down