forked from mainmatter/ember-formatjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-plugin.js
More file actions
129 lines (105 loc) · 3.22 KB
/
Copy pathtemplate-plugin.js
File metadata and controls
129 lines (105 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
/* eslint-env node */
const { interpolateName } = require('@formatjs/ts-transformer');
const defaults = require('./lib/babel/defaults');
/**
* node.path.original is DEPRECATED in newer versions of @glimmer/syntax
* '.value' is the new property to use, but it does not exist in older versions
* of @glimmer/syntax
*
* Exact deprecation messages
* - [on a StringLiteral] The original property on literal nodes is deprecated, use value instead
* - [on a PathExpression] The parts property on path nodes is deprecated, use head and tail instead
*/
function getValue(node) {
if (!node) {
return;
}
const name = 'value' in node ? node.value : node.original;
return name;
}
function setValue(node, newValue) {
if (!node) {
return;
}
if ('value' in node) {
node.value = newValue;
return;
}
node.original = newValue;
}
const CWD = process.cwd();
const NAME = require(`${CWD}/package.json`).name;
const NOOP = {
name: 'ember-intl/ember-formatjs/noop-template-plugin',
visitor: {},
};
/**
* A template transform operates on all templates.
* We need to lock our transform down to
* - templates not in node_modules
* - unless template is in .embroider/rewritten-app (old embroider)
* - templates within our CWD
*
* Don't double-transform files.
* We expect libraries to have built themselves with our translation system.
*
* @param {string} fileName
*/
function isRelevant(fileName) {
if (fileName.startsWith(NAME)) {
return true;
}
const isInOurDirectory = fileName.startsWith(CWD);
if (!isInOurDirectory) {
return false;
}
const isInNodeModules = fileName.includes('/node_modules/');
const isOldEmbroider = fileName.includes('.embroider/rewritten-app');
return !isInNodeModules || isOldEmbroider;
}
function buildTransform(passedOptions) {
const options = Object.assign({}, defaults, passedOptions);
return function (env) {
const fileName = env.meta.moduleName ?? env.moduleName;
if (!isRelevant(fileName)) {
return NOOP;
}
function transformHelper(node) {
const name = getValue(node.path);
if (name === 'format-message' || name === 'formatMessage') {
/**
* defaultMessage and description are trimmed and multiple white spaces are replaced with a single space when generating the ID to ensure white space does not result in a new id
*/
const defaultMessage = getValue(node.params[0])?.trim().replace(/\s+/gm, ' ');
const description = getValue(node.params[1])?.trim().replace(/\s+/gm, ' ');
const id = interpolateName(
{
resourcePath: fileName,
},
options.idInterpolationPattern,
{
content: description ? `${defaultMessage}#${description}` : defaultMessage,
},
);
// We don't want to change gjs/gts usage
// instead, we'll import the t helper as formatMessage
if (name !== 'formatMessage') {
setValue(node.path, 't');
}
// set the hashed value
node.params[0].value = id;
// there _may_ be more params, but here we discard all of them except the first
node.params = [node.params[0]];
}
}
return {
name: 'ember-intl/ember-formatjs/template-plugin',
visitor: {
MustacheStatement: transformHelper,
SubExpression: transformHelper,
},
};
};
}
module.exports = buildTransform;