Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect EOL character from .editorconfig #590

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions lib/config-list.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { EOL } from 'node:os';
import {
BEGIN_CONFIG_LIST_MARKER,
END_CONFIG_LIST_MARKER,
} from './comment-markers.js';
import { markdownTable } from 'markdown-table';
import type { ConfigsToRules, ConfigEmojis, Plugin, Config } from './types.js';
import { ConfigFormat, configNameToDisplay } from './config-format.js';
import { sanitizeMarkdownTable } from './string.js';
import { getEndOfLine, sanitizeMarkdownTable } from './string.js';

const EOL = getEndOfLine();

/**
* Check potential locations for the config description.
Expand Down
4 changes: 3 additions & 1 deletion lib/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { EOL } from 'node:os';
import { existsSync } from 'node:fs';
import { dirname, join, relative, resolve } from 'node:path';
import { getAllNamedOptions, hasOptions } from './rule-options.js';
Expand Down Expand Up @@ -34,6 +33,9 @@ import { OPTION_TYPE, RuleModule } from './types.js';
import { replaceRulePlaceholder } from './rule-link.js';
import { updateRuleOptionsList } from './rule-options-list.js';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { getEndOfLine } from './string.js';

const EOL = getEndOfLine();

function stringOrArrayWithFallback<T extends string | readonly string[]>(
stringOrArray: undefined | T,
Expand Down
4 changes: 3 additions & 1 deletion lib/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { EOL } from 'node:os';
import { getEndOfLine } from './string.js';

const EOL = getEndOfLine();

// General helpers for dealing with markdown files / content.

Expand Down
4 changes: 3 additions & 1 deletion lib/rule-doc-notices.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { EOL } from 'node:os';
import { END_RULE_HEADER_MARKER } from './comment-markers.js';
import {
EMOJI_DEPRECATED,
Expand Down Expand Up @@ -27,9 +26,12 @@ import {
toSentenceCase,
removeTrailingPeriod,
addTrailingPeriod,
getEndOfLine,
} from './string.js';
import { ConfigFormat, configNameToDisplay } from './config-format.js';

const EOL = getEndOfLine();

function severityToTerminology(severity: SEVERITY_TYPE) {
switch (severity) {
case SEVERITY_TYPE.error: {
Expand Down
4 changes: 3 additions & 1 deletion lib/rule-list-legend.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { EOL } from 'node:os';
import {
EMOJI_DEPRECATED,
EMOJI_FIXABLE,
Expand All @@ -18,6 +17,9 @@ import {
} from './types.js';
import { RULE_TYPE_MESSAGES_LEGEND, RULE_TYPES } from './rule-type.js';
import { ConfigFormat, configNameToDisplay } from './config-format.js';
import { getEndOfLine } from './string.js';

const EOL = getEndOfLine();

export const SEVERITY_TYPE_TO_WORD: {
[key in SEVERITY_TYPE]: string;
Expand Down
9 changes: 7 additions & 2 deletions lib/rule-list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { EOL } from 'node:os';
import {
BEGIN_RULE_LIST_MARKER,
END_RULE_LIST_MARKER,
Expand Down Expand Up @@ -34,13 +33,19 @@ import type {
import { EMOJIS_TYPE } from './rule-type.js';
import { hasOptions } from './rule-options.js';
import { getLinkToRule } from './rule-link.js';
import { capitalizeOnlyFirstLetter, sanitizeMarkdownTable } from './string.js';
import {
capitalizeOnlyFirstLetter,
getEndOfLine,
sanitizeMarkdownTable,
} from './string.js';
import { noCase } from 'no-case';
import { getProperty } from 'dot-prop';
import { boolean, isBooleanable } from 'boolean';
import Ajv from 'ajv';
import { ConfigFormat } from './config-format.js';

const EOL = getEndOfLine();

function isBooleanableTrue(value: unknown): boolean {
return isBooleanable(value) && boolean(value);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/rule-options-list.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { EOL } from 'node:os';
import {
BEGIN_RULE_OPTIONS_LIST_MARKER,
END_RULE_OPTIONS_LIST_MARKER,
} from './comment-markers.js';
import { markdownTable } from 'markdown-table';
import type { RuleModule } from './types.js';
import { RuleOption, getAllNamedOptions } from './rule-options.js';
import { sanitizeMarkdownTable } from './string.js';
import { getEndOfLine, sanitizeMarkdownTable } from './string.js';

const EOL = getEndOfLine();

export enum COLUMN_TYPE {
// Alphabetical order.
Expand Down
25 changes: 24 additions & 1 deletion lib/string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { EOL } from 'node:os';
import editorconfig from 'editorconfig';

const endOfLine = getEndOfLine();

export function toSentenceCase(str: string) {
return str.replace(/^\w/u, function (txt) {
Expand All @@ -24,11 +27,31 @@ export function capitalizeOnlyFirstLetter(str: string) {
function sanitizeMarkdownTableCell(text: string): string {
return text
.replaceAll('|', String.raw`\|`)
.replaceAll(new RegExp(EOL, 'gu'), '<br/>');
.replaceAll(new RegExp(endOfLine, 'gu'), '<br/>');
}

export function sanitizeMarkdownTable(
text: readonly (readonly string[])[],
): readonly (readonly string[])[] {
return text.map((row) => row.map((col) => sanitizeMarkdownTableCell(col)));
}

// Gets the end of line string while respecting the
// `.editorconfig` and falling back to `EOL` from `node:os`.
export function getEndOfLine() {
// The passed string is the target file name, relative to process.cwd().
// Given that the docs markdown files are generated inside the `docs/rules` folder
// This should be the correct path to resolve the `.editorconfig` for these
// specific markdown files.
const config = editorconfig.parseSync('./docs/rules/markdown.md');

let endOfLine = EOL;

if (config.end_of_line === 'lf') {
endOfLine = '\n';
} else if (config.end_of_line === 'crlf') {
endOfLine = '\r\n';
}

return endOfLine;
}
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"cosmiconfig": "^9.0.0",
"deepmerge": "^4.2.2",
"dot-prop": "^9.0.0",
"editorconfig": "^2.0.0",
"jest-diff": "^29.2.1",
"json-schema": "^0.4.0",
"json-schema-traverse": "^1.0.0",
Expand Down
8 changes: 8 additions & 0 deletions test/lib/string-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {
addTrailingPeriod,
removeTrailingPeriod,
toSentenceCase,
getEndOfLine,
} from '../../lib/string.js';
import { EOL } from 'node:os';

describe('strings', function () {
describe('#addTrailingPeriod', function () {
Expand Down Expand Up @@ -34,4 +36,10 @@ describe('strings', function () {
expect(toSentenceCase('Hello World')).toStrictEqual('Hello World');
});
});

describe('#getEndOfLine', function () {
it('handles when .editorconfig is not available and fallbacks to `EOL` from `node:os`', function () {
expect(getEndOfLine()).toStrictEqual(EOL);
});
});
});
Loading