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

testUtils: refactor out dedentString from dedent #3010

Merged
merged 1 commit into from Apr 2, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/__testUtils__/__tests__/dedent-test.js
@@ -1,11 +1,11 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { dedent } from '../dedent';
import { dedent, dedentString } from '../dedent';

describe('dedent', () => {
describe('dedentString', () => {
it('removes indentation in typical usage', () => {
const output = dedent`
const output = dedentString(`
type Query {
me: User
}
Expand All @@ -14,7 +14,7 @@ describe('dedent', () => {
id: ID
name: String
}
`;
`);
expect(output).to.equal(
[
'type Query {',
Expand All @@ -30,23 +30,23 @@ describe('dedent', () => {
});

it('removes only the first level of indentation', () => {
const output = dedent`
const output = dedentString(`
first
second
third
fourth
`;
`);
expect(output).to.equal(
['first', ' second', ' third', ' fourth'].join('\n'),
);
});

it('does not escape special characters', () => {
const output = dedent`
const output = dedentString(`
type Root {
field(arg: String = "wi\th de\fault"): String
}
`;
`);
expect(output).to.equal(
[
'type Root {',
Expand All @@ -57,38 +57,49 @@ describe('dedent', () => {
});

it('also removes indentation using tabs', () => {
const output = dedent`
const output = dedentString(`
\t\t type Query {
\t\t me: User
\t\t }
`;
`);
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});

it('removes leading and trailing newlines', () => {
const output = dedent`
const output = dedentString(`


type Query {
me: User
}


`;
`);
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});

it('removes all trailing spaces and tabs', () => {
const output = dedent`
const output = dedentString(`
type Query {
me: User
}
\t\t \t `;
\t\t \t `);
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});

it('works on text without leading newline', () => {
const output = dedent` type Query {
const output = dedentString(` type Query {
me: User
}
`);
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
});
});

describe('dedent', () => {
it('removes indentation in typical usage', () => {
const output = dedent`
type Query {
me: User
}
`;
Expand Down
31 changes: 18 additions & 13 deletions src/__testUtils__/dedent.js
@@ -1,3 +1,20 @@
export function dedentString(string: string): string {
const trimmedStr = string
.replace(/^\n*/m, '') // remove leading newline
.replace(/[ \t\n]*$/, ''); // remove trailing spaces and tabs

// fixes indentation by removing leading spaces and tabs from each line
let indent = '';
for (const char of trimmedStr) {
if (char !== ' ' && char !== '\t') {
break;
}
indent += char;
}

return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent
}

/**
* An ES6 string tag that fixes indentation and also trims string.
*
Expand Down Expand Up @@ -25,17 +42,5 @@ export function dedent(
}
}

const trimmedStr = str
.replace(/^\n*/m, '') // remove leading newline
.replace(/[ \t\n]*$/, ''); // remove trailing spaces and tabs

// fixes indentation by removing leading spaces and tabs from each line
let indent = '';
for (const char of trimmedStr) {
if (char !== ' ' && char !== '\t') {
break;
}
indent += char;
}
return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent
return dedentString(str);
}