@discord/babel-plugin-transform-discord-intl 0.7.1 → 0.8.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/traverse.js +91 -42
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/babel-plugin-transform-discord-intl",
3
- "version": "0.7.1",
3
+ "version": "0.8.0-rc.0",
4
4
  "license": "MIT",
5
5
  "type": "commonjs",
6
6
  "description": "Babel plugin for transforming message accesses and definitions",
@@ -11,7 +11,7 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "@typescript-eslint/scope-manager": "^8.8.1",
14
- "@discord/intl-loader-core": "0.7.1"
14
+ "@discord/intl-loader-core": "0.8.0-rc.0"
15
15
  },
16
16
  "devDependencies": {
17
17
  "@babel/types": "^7.24.9",
package/traverse.js CHANGED
@@ -1,6 +1,41 @@
1
1
  const nativePath = require('node:path');
2
2
  const { isMessageDefinitionsFile } = require('@discord/intl-loader-core');
3
3
 
4
+ /** @typedef {import('babel__traverse').Scope} Scope */
5
+ /** @typedef {import('babel__traverse').NodePath} NodePath */
6
+
7
+ /**
8
+ * Return all references to any of the given `names` that are accessible in `scope`.
9
+ * @param {string[]} names
10
+ * @param {Scope} scope
11
+ * @returns {Array<NodePath>}
12
+ */
13
+ function getBindingReferences(names, scope) {
14
+ return names.flatMap((name) => scope.bindings[name]?.referencePaths ?? []);
15
+ }
16
+
17
+ /**
18
+ * Returns the list of identifiers that can act as Messages objects if they are imported from
19
+ * `source`, based on the configuration provided for this visitor.
20
+ * @param {string} source
21
+ * @param {unknown} state
22
+ * @returns {[string[], boolean]} A tuple of the list of allowed specifiers and a boolean indicating whether the default import is allowed.
23
+ */
24
+ function getAllowedMessageObjectIdentifers(source, state) {
25
+ // @ts-expect-error state is untyped but contains `opts`
26
+ const importSource = state.opts.preserveImportSource
27
+ ? source
28
+ : nativePath.resolve(
29
+ // @ts-expect-error state is untyped but contains `file`
30
+ nativePath.dirname(state.file.opts.filename),
31
+ source,
32
+ );
33
+ const isDefinition = isMessageDefinitionsFile(importSource);
34
+ // @ts-expect-error state is untyped but contains `opts` from the config.
35
+ const extraImportSpecifiers = state.opts?.extraImports?.[importSource] ?? [];
36
+ return [extraImportSpecifiers, isDefinition];
37
+ }
38
+
4
39
  /**
5
40
  * Babel visitor for traversing all intl message accesses in a file. This visitor can be applied to
6
41
  * every file in a project, but only member expressions of imports from `.messages.js` files and any
@@ -16,60 +51,74 @@ const { isMessageDefinitionsFile } = require('@discord/intl-loader-core');
16
51
  * @returns {import("babel__traverse").Visitor} A visitor object for a Babel transform.
17
52
  */
18
53
  function traverseMessageAccesses(callback) {
54
+ /**
55
+ * @param {NodePath[]} references
56
+ */
57
+ function traverseReferences(references) {
58
+ for (const reference of references) {
59
+ const parent = reference.parent;
60
+ // We only care about member expressions that use the `.` syntax or
61
+ // use string literals, since other syntaxes could be invalid.
62
+ if (
63
+ parent.type !== 'MemberExpression' ||
64
+ (parent.computed && parent.property.type !== 'StringLiteral')
65
+ ) {
66
+ continue;
67
+ }
68
+
69
+ // We just want the actual value of the member being accessed.
70
+ /** @type {string | undefined} */
71
+ const messageName = (() => {
72
+ switch (parent.property.type) {
73
+ case 'StringLiteral':
74
+ return parent.property.value;
75
+ case 'Identifier':
76
+ return parent.property.name;
77
+ default:
78
+ return undefined;
79
+ }
80
+ })();
81
+
82
+ callback(parent, messageName);
83
+ }
84
+ }
85
+
19
86
  return {
20
87
  ImportDeclaration(path, state) {
21
- const importSource = nativePath.resolve(
22
- // @ts-expect-error state is untyped but contains `file`
23
- nativePath.dirname(state.file.opts.filename),
88
+ const [extraImports, allowDefault] = getAllowedMessageObjectIdentifers(
24
89
  path.node.source.value,
90
+ state,
25
91
  );
26
- const isDefinition = isMessageDefinitionsFile(importSource);
27
- // @ts-expect-error state is untyped but contains `opts` from the config.
28
- const extraImportSpecifiers = state.opts?.extraImports?.[importSource] ?? [];
29
- // This transformer only handles usages of intl messages, so only
30
- // imports of definitions files and configured extra specifiers need to
31
- // be handled.
32
- if (!isDefinition && extraImportSpecifiers.length === 0) {
33
- return;
34
- }
35
-
36
92
  const specifiers = path.node.specifiers
37
93
  .filter(
38
94
  (specifier) =>
39
- (isDefinition && specifier.type === 'ImportDefaultSpecifier') ||
40
- extraImportSpecifiers.includes(specifier.local.name),
95
+ (allowDefault && specifier.type === 'ImportDefaultSpecifier') ||
96
+ extraImports.includes(specifier.local.name),
41
97
  )
42
98
  .map((specifier) => specifier.local.name);
43
99
 
44
- const bindingReferences = specifiers.flatMap(
45
- (binding) => path.scope.bindings[binding]?.referencePaths ?? [],
46
- );
47
- for (const reference of bindingReferences) {
48
- const parent = reference.parent;
49
- // We only care about member expressions that use the `.` syntax or
50
- // use string literals, since other syntaxes could be invalid.
51
- if (
52
- parent.type !== 'MemberExpression' ||
53
- (parent.computed && parent.property.type !== 'StringLiteral')
54
- ) {
55
- continue;
56
- }
57
-
58
- // We just want the actual value of the member being accessed.
59
- /** @type {string | undefined} */
60
- const messageName = (() => {
61
- switch (parent.property.type) {
62
- case 'StringLiteral':
63
- return parent.property.value;
64
- case 'Identifier':
65
- return parent.property.name;
66
- default:
67
- return undefined;
68
- }
69
- })();
100
+ const bindingReferences = getBindingReferences(specifiers, path.scope);
101
+ traverseReferences(bindingReferences);
102
+ },
70
103
 
71
- callback(parent, messageName);
104
+ CallExpression(path, state) {
105
+ const call = path.node;
106
+ if (
107
+ call.callee.type !== 'Identifier' ||
108
+ call.callee.name !== 'require' ||
109
+ call.arguments.length !== 1
110
+ ) {
111
+ return;
72
112
  }
113
+ const modulePath = call.arguments[0];
114
+ if (modulePath.type !== 'StringLiteral') return;
115
+ if (typeof modulePath.value !== 'string') return;
116
+
117
+ // TODO: Inspect bindings created by these requires and traverse them as well.
118
+ // const [extraImports, allowDefault] = getAllowedMessageObjectIdentifers(
119
+ // modulePath.value,
120
+ // state,
121
+ // );
73
122
  },
74
123
  };
75
124
  }