-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@formatjs/cli-lib): add support for gts, gjs and hbs files.
- Loading branch information
1 parent
6773452
commit 1693515
Showing
17 changed files
with
811 additions
and
59 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,4 @@ | |
}, | ||
"keepClassNames": false | ||
} | ||
} | ||
} |
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
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
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,18 @@ | ||
import {Preprocessor} from 'content-tag' | ||
import {parseFile as parseHbsFile} from './hbs_extractor' | ||
import {parseScript} from './parse_script' | ||
let p = new Preprocessor() | ||
|
||
export function parseFile(source: string, fileName: string, options: any) { | ||
const scriptParseFn = parseScript(options, fileName) | ||
const transformedSource = p.process(source, {filename: fileName}) | ||
|
||
scriptParseFn(transformedSource) | ||
|
||
// extract template from transformed source to then run through hbs processor | ||
const parseResult = p.parse(source, {filename: fileName}) | ||
|
||
for (let parsed of parseResult) { | ||
parseHbsFile(parsed.contents, fileName, options) | ||
} | ||
} |
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,59 @@ | ||
import {transform} from 'ember-template-recast' | ||
import type {AST} from '@glimmer/syntax' | ||
import {Opts} from '@formatjs/ts-transformer' | ||
|
||
function extractText( | ||
node: AST.MustacheStatement | AST.SubExpression, | ||
fileName: string, | ||
options: Opts | ||
) { | ||
if (!options.onMsgExtracted) return | ||
if (!options.overrideIdFn) return | ||
|
||
if (node.path.type !== 'PathExpression') return | ||
|
||
if (['format-message', 'formatMessage'].includes(node.path.original)) { | ||
let [first, second] = node.params | ||
|
||
if (first.type !== 'StringLiteral') return | ||
|
||
let message = first?.value | ||
|
||
let desc: string | undefined | ||
if (second?.type === 'StringLiteral') { | ||
desc = second.value?.trim().replace(/\s+/gm, ' ') | ||
} | ||
|
||
let defaultMessage = message?.trim().replace(/\s+/gm, ' ') | ||
|
||
let id = | ||
typeof options.overrideIdFn === 'string' | ||
? options.overrideIdFn | ||
: options.overrideIdFn(undefined, defaultMessage, desc, fileName) | ||
|
||
options.onMsgExtracted(fileName, [ | ||
{ | ||
id: id, | ||
defaultMessage: defaultMessage, | ||
description: desc, | ||
}, | ||
]) | ||
} | ||
} | ||
|
||
export function parseFile(source: string, fileName: string, options: any) { | ||
let visitor = function () { | ||
return { | ||
MustacheStatement(node: AST.MustacheStatement) { | ||
extractText(node, fileName, options) | ||
}, | ||
SubExpression(node: AST.SubExpression) { | ||
extractText(node, fileName, options) | ||
}, | ||
} | ||
} | ||
|
||
// SAFETY: ember-template-recast's types are out of date, | ||
// but it does not affect runtime | ||
transform(source, visitor as any) | ||
} |
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,87 @@ | ||
import formatMessage from 'ember-intl/helpers/format-message'; | ||
import or from 'ember-truth-helpers/helpers/or'; | ||
import Component from '@glimmer/component'; | ||
import {service} from '@ember/service'; | ||
|
||
/* | ||
* We don't want to support ids passed to formatMessage in templates, | ||
* because the accidental risk of collision is too high. | ||
* | ||
* They are supported in JS because it's an API that we don't control. | ||
*/ | ||
const Header = <template> | ||
<header>{{formatMessage "G'day!, from a secondary component in the same GJS file"}}</header> | ||
</template>; | ||
|
||
export default class Comp extends Component { | ||
@service intl; | ||
|
||
get message() { | ||
return this.intl.formatMessage({ | ||
defaultMessage: 'js getter with an id', | ||
id: 'getter-message', | ||
}); | ||
} | ||
|
||
get message2() { | ||
return this.intl.formatMessage({ | ||
defaultMessage: 'js getter with no id', | ||
}); | ||
} | ||
|
||
<template> | ||
<Header /> | ||
<p> | ||
{{formatMessage 'in template' 'in template desc'}} | ||
</p> | ||
|
||
<p> | ||
{{formatMessage | ||
'{connectorName, select, | ||
none {Install Service} | ||
other {Install {connectorName}} | ||
}' | ||
name=(or this.name 'none') | ||
}} | ||
</p> | ||
|
||
<!-- prettier-ignore --> | ||
<p> | ||
{{formatMessage | ||
'{connectorName, select, | ||
none {Install Service} | ||
other {Install {connectorName}} | ||
}' | ||
name=(or this.name 'none') | ||
}} | ||
</p> | ||
|
||
<p> | ||
{{formatMessage | ||
"Very long message with | ||
multiple'' breaklines | ||
and multiple spaces | ||
'<a href={href}>' Link '</a>'" | ||
'Nice description' | ||
href='/whatever/link' | ||
htmlSafe=true | ||
}} | ||
</p> | ||
|
||
<!-- this is the same as above but is nested so it contains more whitespaces --> | ||
<div> | ||
<p> | ||
{{formatMessage | ||
"Very long message with | ||
multiple'' breaklines | ||
and multiple spaces | ||
'<a href={href}>' Link '</a>'" | ||
'Nice | ||
description' | ||
href='/whatever/link' | ||
htmlSafe=true | ||
}} | ||
</p> | ||
</div> | ||
</template> | ||
} |
Oops, something went wrong.