forked from vendure-ecommerce/ngx-translate-extract
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-m or --marker is an old option that was in biesbjerg/ngx-translate-extract it was removed and replaced by the ngx-extract-translate-marker mechanism but this new mechanism does not allow to easily customize what the marker function does (some users have use cases where this is not a no-op)
- Loading branch information
Showing
4 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { tsquery } from '@phenomnomnominal/tsquery'; | ||
|
||
import { ParserInterface } from './parser.interface.js'; | ||
import { TranslationCollection } from '../utils/translation.collection.js'; | ||
import { getStringsFromExpression, findSimpleCallExpressions } from '../utils/ast-helpers.js'; | ||
import pkg from 'typescript'; | ||
const { isIdentifier } = pkg; | ||
|
||
export class FunctionParser implements ParserInterface { | ||
constructor(private fnName: string) {} | ||
|
||
public extract(source: string, filePath: string): TranslationCollection | null { | ||
const sourceFile = tsquery.ast(source, filePath); | ||
|
||
let collection: TranslationCollection = new TranslationCollection(); | ||
|
||
const callExpressions = findSimpleCallExpressions(sourceFile, this.fnName); | ||
callExpressions.forEach((callExpression) => { | ||
if (!isIdentifier(callExpression.expression) | ||
|| callExpression.expression.escapedText != this.fnName) { | ||
return | ||
} | ||
|
||
const [firstArg] = callExpression.arguments; | ||
if (!firstArg) { | ||
return; | ||
} | ||
const strings = getStringsFromExpression(firstArg); | ||
collection = collection.addKeys(strings); | ||
}); | ||
return collection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { FunctionParser } from '../../src/parsers/function.parser.js'; | ||
|
||
describe('FunctionParser', () => { | ||
const componentFilename: string = 'test.component.ts'; | ||
|
||
let parser: FunctionParser; | ||
|
||
beforeEach(() => { | ||
parser = new FunctionParser('MK'); | ||
}); | ||
|
||
it('should extract strings using marker function', () => { | ||
const contents = ` | ||
MK('Hello world'); | ||
MK(['I', 'am', 'extracted']); | ||
otherFunction('But I am not'); | ||
MK(message || 'binary expression'); | ||
MK(message ? message : 'conditional operator'); | ||
MK('FOO.bar'); | ||
`; | ||
const keys = parser.extract(contents, componentFilename).keys(); | ||
expect(keys).to.deep.equal(['Hello world', 'I', 'am', 'extracted', 'binary expression', 'conditional operator', 'FOO.bar']); | ||
}); | ||
|
||
it('should extract split strings', () => { | ||
const contents = ` | ||
MK('Hello ' + 'world'); | ||
MK('This is a ' + 'very ' + 'very ' + 'very ' + 'very ' + 'long line.'); | ||
MK('Mix ' + \`of \` + 'different ' + \`types\`); | ||
`; | ||
const keys = parser.extract(contents, componentFilename).keys(); | ||
expect(keys).to.deep.equal(['Hello world', 'This is a very very very very long line.', 'Mix of different types']); | ||
}); | ||
|
||
it('should extract split strings while keeping html tags', () => { | ||
const contents = ` | ||
MK('Hello ' + 'world'); | ||
MK('This <em>is</em> a ' + 'very ' + 'very ' + 'very ' + 'very ' + 'long line.'); | ||
MK('Mix ' + \`of \` + 'different ' + \`types\`); | ||
`; | ||
const keys = parser.extract(contents, componentFilename).keys(); | ||
expect(keys).to.deep.equal(['Hello world', 'This <em>is</em> a very very very very long line.', 'Mix of different types']); | ||
}); | ||
|
||
it('should extract the strings', () => { | ||
const contents = ` | ||
export class AppModule { | ||
constructor() { | ||
MK('DYNAMIC_TRAD.val1'); | ||
MK('DYNAMIC_TRAD.val2'); | ||
} | ||
} | ||
`; | ||
const keys = parser.extract(contents, componentFilename).keys(); | ||
expect(keys).to.deep.equal(['DYNAMIC_TRAD.val1', 'DYNAMIC_TRAD.val2']); | ||
}); | ||
|
||
}); |