Skip to content

Commit

Permalink
Merge pull request #418 from mrmlnc/ISSUE-370_absolute_normalize
Browse files Browse the repository at this point in the history
ISSUE-370: Do not cast the path to POSIX style
  • Loading branch information
mrmlnc committed Aug 4, 2023
2 parents ddae0bf + 9917ede commit 1b251fa
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
7 changes: 2 additions & 5 deletions src/providers/transformers/entry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as path from 'node:path';

import Settings from '../../settings';
import * as tests from '../../tests';
import * as utils from '../../utils';
import EntryTransformer from './entry';

import type { EntryTransformerFunction } from '../../types';
Expand Down Expand Up @@ -64,8 +63,7 @@ describe('Providers → Transformers → Entry', () => {
const transformer = getTransformer({ absolute: true });
const entry = tests.entry.builder().path('root/file.txt').file().build();

const fullpath = path.join(process.cwd(), 'root', 'file.txt');
const expected = utils.path.unixify(fullpath);
const expected = path.join(process.cwd(), 'root', 'file.txt');

const actual = transformer(entry);

Expand All @@ -87,8 +85,7 @@ describe('Providers → Transformers → Entry', () => {
const transformer = getTransformer({ absolute: true, markDirectories: true });
const entry = tests.entry.builder().path('root/directory').directory().build();

const fullpath = path.join(process.cwd(), 'root', 'directory', '/');
const expected = utils.path.unixify(fullpath);
const expected = path.join(process.cwd(), 'root', 'directory', '/');

const actual = transformer(entry);

Expand Down
13 changes: 11 additions & 2 deletions src/providers/transformers/entry.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import * as path from 'node:path';

import * as utils from '../../utils';

import type Settings from '../../settings';
import type { Entry, EntryItem, EntryTransformerFunction } from '../../types';

export default class EntryTransformer {
readonly #settings: Settings;
readonly #pathSeparatorSymbol: string;

constructor(settings: Settings) {
this.#settings = settings;

this.#pathSeparatorSymbol = this.#getPathSeparatorSymbol();
}

public getTransformer(): EntryTransformerFunction {
Expand All @@ -19,11 +24,11 @@ export default class EntryTransformer {

if (this.#settings.absolute) {
filepath = utils.path.makeAbsolute(this.#settings.cwd, filepath);
filepath = utils.path.unixify(filepath);
filepath = utils.string.flatHeavilyConcatenatedString(filepath);
}

if (this.#settings.markDirectories && entry.dirent.isDirectory()) {
filepath += '/';
filepath += this.#pathSeparatorSymbol;
}

if (!this.#settings.objectMode) {
Expand All @@ -35,4 +40,8 @@ export default class EntryTransformer {
path: filepath,
};
}

#getPathSeparatorSymbol(): string {
return this.#settings.absolute ? path.sep : '/';
}
}
19 changes: 12 additions & 7 deletions src/tests/e2e/options/absolute.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import * as path from 'node:path';

import * as runner from '../runner';

const CWD = process.cwd().replaceAll('\\', '/');
const CWD = process.cwd();
const CWD_POSIX = CWD.replaceAll('\\', '/');

function resultTransform(item: string): string {
return item.replace(CWD, '<root>');
return item
.replace(CWD, '<root>')
// Backslashes are used on Windows.
// The `fixtures` directory is under our control, so we are confident that the conversions are correct.
.replaceAll(/[/\\]/g, '/');
}

runner.suite('Options Absolute', {
Expand Down Expand Up @@ -60,14 +65,14 @@ runner.suite('Options Absolute (ignore)', {
{
pattern: 'fixtures/*',
options: {
ignore: [path.posix.join(CWD, 'fixtures', '*')],
ignore: [path.posix.join(CWD_POSIX, 'fixtures', '*')],
absolute: true,
},
},
{
pattern: 'fixtures/**',
options: {
ignore: [path.posix.join(CWD, 'fixtures', '*')],
ignore: [path.posix.join(CWD_POSIX, 'fixtures', '*')],
absolute: true,
},
issue: 47,
Expand Down Expand Up @@ -125,23 +130,23 @@ runner.suite('Options Absolute (cwd & ignore)', {
{
pattern: '*',
options: {
ignore: [path.posix.join(CWD, 'fixtures', '*')],
ignore: [path.posix.join(CWD_POSIX, 'fixtures', '*')],
cwd: 'fixtures',
absolute: true,
},
},
{
pattern: '**',
options: {
ignore: [path.posix.join(CWD, 'fixtures', '*')],
ignore: [path.posix.join(CWD_POSIX, 'fixtures', '*')],
cwd: 'fixtures',
absolute: true,
},
},
{
pattern: '**',
options: {
ignore: [path.posix.join(CWD, 'fixtures', '**')],
ignore: [path.posix.join(CWD_POSIX, 'fixtures', '**')],
cwd: 'fixtures',
absolute: true,
},
Expand Down
10 changes: 0 additions & 10 deletions src/utils/path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ import * as path from 'node:path';
import * as util from './path';

describe('Utils → Path', () => {
describe('.unixify', () => {
it('should return path with converted slashes', () => {
const expected = 'directory/nested/file.md';

const actual = util.unixify('directory\\nested/file.md');

assert.strictEqual(actual, expected);
});
});

describe('.makeAbsolute', () => {
it('should return absolute filepath', () => {
const expected = path.join(process.cwd(), 'file.md');
Expand Down
7 changes: 0 additions & 7 deletions src/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ const DOS_DEVICE_PATH_RE = /^\\\\(?<path>[.?])/;
*/
const WINDOWS_BACKSLASHES_RE = /\\(?![!()+@{}])/g;

/**
* Designed to work only with simple paths: `dir\\file`.
*/
export function unixify(filepath: string): string {
return filepath.replaceAll('\\', '/');
}

export function makeAbsolute(cwd: string, filepath: string): string {
return path.resolve(cwd, filepath);
}
Expand Down
13 changes: 13 additions & 0 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ export function isString(input: unknown): input is string {
export function isEmpty(input: string): boolean {
return input === '';
}

/**
* Flattens the underlying C structures of a concatenated JavaScript string.
*
* More details: https://github.com/davidmarkclements/flatstr
*/
export function flatHeavilyConcatenatedString(input: string): string {
// @ts-expect-error Another solution can be `.trim`, but it changes the string.
// eslint-disable-next-line @typescript-eslint/no-unused-expressions, no-bitwise, unicorn/prefer-math-trunc
input | 0;

return input;
}

0 comments on commit 1b251fa

Please sign in to comment.