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

fix(formats): fixing formatting options in fileHeader #614

Merged
merged 1 commit into from
May 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions __tests__/common/formatHelpers/fileHeader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

const fileHeader = require('../../../lib/common/formatHelpers/fileHeader');

const defaultLine1 = `Do not edit directly`;
const defaultLine2 = `Generated on Sat, 01 Jan 2000 00:00:00 GMT`;

describe('common', () => {
describe('formatHelpers', () => {

describe('fileHeader', () => {
it(`should default to /**/ comment style`, () => {
const comment = fileHeader({});
expect(comment).toEqual(
`/**
* ${defaultLine1}
* ${defaultLine2}
*/

`);
});

it(`should handle commentStyle short`, () => {
const comment = fileHeader({commentStyle: 'short'});
expect(comment).toEqual(
`
// ${defaultLine1}
// ${defaultLine2}

`);
});

it(`should handle commentStyle xml`, () => {
const comment = fileHeader({commentStyle: 'xml'});
expect(comment).toEqual(
`<!--
${defaultLine1}
${defaultLine2}
-->`);
});

it(`should handle showFileHeader option`, () => {
const comment = fileHeader({
file: {
options: {
showFileHeader: false
}
}
});
expect(comment).toEqual('');
});

it(`should handle custom fileHeader function`, () => {
const comment = fileHeader({
file: {
options: {
fileHeader: () => {
return [
`Never gonna give you up`,
`Never gonna let you down`
]
}
}
}
});
expect(comment).toEqual(
`/**
* Never gonna give you up
* Never gonna let you down
*/

`);
});

it(`should handle custom fileHeader function with default`, () => {
const comment = fileHeader({
file: {
options: {
fileHeader: (defaultMessage) => {
return [
...defaultMessage,
`Never gonna give you up`,
`Never gonna let you down`
]
}
}
}
});
expect(comment).toEqual(
`/**
* ${defaultLine1}
* ${defaultLine2}
* Never gonna give you up
* Never gonna let you down
*/

`);
});

it('should handle custom formatting', () => {
const comment = fileHeader({formatting: {
prefix: ` `,
header: `{#\n`,
footer: `\n#}`
}});
expect(comment).toEqual(
`{#
${defaultLine1}
${defaultLine2}
#}`);
});
});
});
});
15 changes: 6 additions & 9 deletions lib/common/formatHelpers/fileHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
// no-op default
const defaultFileHeader = (arr) => arr

const lineSeparator = `\n`;
const defaultFormatting = {
lineSeparator: '\n',
prefix: '',
header: '',
footer: ''
lineSeparator,
prefix: ` * `,
header: `/**${lineSeparator}`,
footer: `${lineSeparator} */${lineSeparator}${lineSeparator}`
}

/**
Expand All @@ -44,7 +45,7 @@ const defaultFormatting = {
* });
* ```
*/
function fileHeader({file, commentStyle, formatting={}}) {
function fileHeader({file={}, commentStyle, formatting={}}) {
// showFileHeader is true by default
let showFileHeader = true;
if (file.options && typeof file.options.showFileHeader !== 'undefined') {
Expand Down Expand Up @@ -76,10 +77,6 @@ function fileHeader({file, commentStyle, formatting={}}) {
prefix = ` `;
header = `<!--${lineSeparator}`;
footer = `${lineSeparator}-->`;
} else {
prefix = ` * `;
header = `/**${lineSeparator}`;
footer = `${lineSeparator} */${lineSeparator}${lineSeparator}`;
}

return `${header}${fn(defaultHeader)
Expand Down