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

Make final new line optional #1014

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ export default {
lineEnding: 'auto',
// Control the line ending. See options at https://github.com/ryanve/eol

insertFinalNewline: true,
// Insert line ending at the end of output files

locales: ['en', 'fr'],
// An array of the locales in your applications

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export interface UserConfig {
default?: (SupportedLexer | CustomLexer | LexerConfig)[];
};
lineEnding?: "auto" | "crlf" | "\r\n" | "cr" | "\r" | "lf" | "\n";
insertFinalNewline?: boolean;
locales?: string[];
namespaceSeparator?: string | false;
output?: string;
Expand Down
5 changes: 4 additions & 1 deletion src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class i18nTransform extends Transform {
keySeparator: '.',
lexers: {},
lineEnding: 'auto',
insertFinalNewline: true,
locales: ['en', 'fr'],
namespaceSeparator: ':',
pluralSeparator: '_',
Expand Down Expand Up @@ -403,7 +404,9 @@ export default class i18nTransform extends Transform {
...this.options.yamlOptions,
})
} else {
text = JSON.stringify(contents, null, this.options.indentation) + '\n'
text =
JSON.stringify(contents, null, this.options.indentation) +
(this.options.insertFinalNewline ? '\n' : '')
// Convert non-printable Unicode characters to unicode escape sequence
// https://unicode.org/reports/tr18/#General_Category_Property
text = text.replace(/[\p{Z}\p{Cc}\p{Cf}]/gu, (chr) => {
Expand Down
24 changes: 24 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,30 @@ describe('parser', () => {
i18nextParser.end(fakeFile)
})

it('supports insertFinalNewline', (done) => {
let result
const i18nextParser = new i18nTransform({
lineEnding: '\r\n',
insertFinalNewline: false,
})
const fakeFile = new Vinyl({
contents: Buffer.from("t('first')"),
path: 'file.js',
})

i18nextParser.on('data', (file) => {
if (file.relative.endsWith(enLibraryPath)) {
result = file.contents.toString()
}
})
i18nextParser.once('end', () => {
assert.equal(result, '{\r\n "first": ""\r\n}')
done()
})

i18nextParser.end(fakeFile)
})

it('parses Trans from js file with lexer override to JsxLexer', (done) => {
let result
const i18nextParser = new i18nTransform({
Expand Down