Skip to content

Commit

Permalink
Make final new line optional
Browse files Browse the repository at this point in the history
  • Loading branch information
simonnilsson committed Jun 11, 2024
1 parent 7f78ad7 commit ad3d143
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
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: 5 additions & 0 deletions 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 @@ -429,6 +430,10 @@ export default class i18nTransform extends Transform {
text = eol.lf(text)
}

if (!this.options.insertFinalNewline) {
text = text.trim()
}

const file = new VirtualFile({
path,
contents: Buffer.from(text),
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

0 comments on commit ad3d143

Please sign in to comment.