flow-api-translator 0.36.1 → 0.37.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.
@@ -30,7 +30,7 @@ export function analyzeFunctionReturn(func: AFunction): TypeAnnotation {
30
30
  export function analyzeTypeDependencies(
31
31
  rootNode: ESNode,
32
32
  context: TranslationContext,
33
- ): $ReadOnlyArray<Dep> {
33
+ ): ReadonlyArray<Dep> {
34
34
  const deps = [];
35
35
  SimpleTraverser.traverse(rootNode, {
36
36
  enter(node: ESNode) {
@@ -38,6 +38,11 @@ export function analyzeTypeDependencies(
38
38
  const variable = context.referenceMap.get(node);
39
39
  if (variable != null) {
40
40
  deps.push(variable.name);
41
+ } else if (context.variableMap.has(node.name)) {
42
+ // The scope manager may not track type references to value
43
+ // variables (e.g. `const Foo = require('foo')` used as `Foo`
44
+ // in a GenericTypeAnnotation). Fall back to variableMap.
45
+ deps.push(node.name);
41
46
  }
42
47
  }
43
48
  },
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.extractPropertyKeyLiterals = extractPropertyKeyLiterals;
7
+
8
+ var TSESTree = _interopRequireWildcard(require("./ts-estree-ast-types"));
9
+
10
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
+
12
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
13
+
14
+ const DUMMY_LOC = {
15
+ start: {
16
+ line: 1,
17
+ column: 0
18
+ },
19
+ end: {
20
+ line: 1,
21
+ column: 0
22
+ }
23
+ };
24
+
25
+ function extractPropertyKeyLiterals(members) {
26
+ if (members.length === 0) {
27
+ return null;
28
+ }
29
+
30
+ const literals = [];
31
+
32
+ for (const member of members) {
33
+ if (member.type !== 'TSPropertySignature' && member.type !== 'TSMethodSignature') {
34
+ return null;
35
+ }
36
+
37
+ if (member.computed === true) {
38
+ return null;
39
+ }
40
+
41
+ const {
42
+ key
43
+ } = member;
44
+
45
+ if (key.type === 'Identifier') {
46
+ literals.push({
47
+ type: 'TSLiteralType',
48
+ loc: DUMMY_LOC,
49
+ literal: {
50
+ type: 'Literal',
51
+ loc: DUMMY_LOC,
52
+ value: key.name,
53
+ raw: `'${key.name}'`
54
+ }
55
+ });
56
+ } else if (key.type === 'Literal') {
57
+ literals.push({
58
+ type: 'TSLiteralType',
59
+ loc: DUMMY_LOC,
60
+ literal: key
61
+ });
62
+ } else {
63
+ return null;
64
+ }
65
+ }
66
+
67
+ return literals;
68
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict-local
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import type {SourceLocation} from 'hermes-estree';
14
+
15
+ import * as TSESTree from './ts-estree-ast-types';
16
+
17
+ const DUMMY_LOC: SourceLocation = {
18
+ start: {line: 1, column: 0},
19
+ end: {line: 1, column: 0},
20
+ };
21
+
22
+ /**
23
+ * Extract statically known property key names from a list of TS type element
24
+ * nodes and return them as `TSLiteralType` (string literal) AST nodes.
25
+ *
26
+ * Returns `null` if any member has a computed or non-extractable key,
27
+ * or if the list is empty.
28
+ */
29
+ export function extractPropertyKeyLiterals(
30
+ members: ReadonlyArray<TSESTree.TypeElement>,
31
+ ): Array<TSESTree.TSLiteralType> | null {
32
+ // An empty member list has no keys to extract. Returning `null` here lets
33
+ // the caller fall back to `keyof {}`, preserving the original output for
34
+ // spread-only objects (e.g. `{...T1}`) where a literal union would be
35
+ // invalid TS.
36
+ if (members.length === 0) {
37
+ return null;
38
+ }
39
+ const literals: Array<TSESTree.TSLiteralType> = [];
40
+ for (const member of members) {
41
+ if (
42
+ member.type !== 'TSPropertySignature' &&
43
+ member.type !== 'TSMethodSignature'
44
+ ) {
45
+ return null;
46
+ }
47
+ if (member.computed === true) {
48
+ return null;
49
+ }
50
+ const {key} = member;
51
+ if (key.type === 'Identifier') {
52
+ literals.push({
53
+ type: 'TSLiteralType',
54
+ loc: DUMMY_LOC,
55
+ literal: {
56
+ type: 'Literal',
57
+ loc: DUMMY_LOC,
58
+ value: key.name,
59
+ raw: `'${key.name}'`,
60
+ } as TSESTree.StringLiteral,
61
+ });
62
+ } else if (key.type === 'Literal') {
63
+ // A string or numeric literal key is already a valid literal type.
64
+ literals.push({
65
+ type: 'TSLiteralType',
66
+ loc: DUMMY_LOC,
67
+ literal: key,
68
+ });
69
+ } else {
70
+ return null;
71
+ }
72
+ }
73
+ return literals;
74
+ }
@@ -1,12 +1,3 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- *
8
- * @format
9
- */
10
1
  'use strict';
11
2
 
12
3
  Object.defineProperty(exports, "__esModule", {
@@ -1,30 +1 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- *
8
- * @format
9
- */
10
-
11
- /**
12
- * The following types have been adapted by hand from
13
- * https://unpkg.com/browse/@typescript-eslint/types@5.41.0/dist/generated/ast-spec.d.ts
14
- *
15
- * Changes:
16
- * - remove and inline `ValueOf` type
17
- * - `undefined` -> `void`
18
- * - remove all `declare` keywords
19
- * - comment out `bigint` type
20
- * -> flow doesn't support it yet
21
- * - remove `range` and `loc` from `NodeOrTokenData`
22
- * -> during conversion our locations will be all off, so we'll rely on prettier to print later
23
- * - make all properties readonly and all arrays $ReadOnlyArray
24
- * -> unlike TS - flow enforces subtype constraints strictly!
25
- * - add `type` to interfaces that previously relied upon inheriting the `type`
26
- * -> this is because flow sentinel refinement does not check inherited members
27
- * - create "Ambiguous" versions for some nodes that have unions (like PropertyDefinition, MemberDefinition)
28
- * -> makes it easier to construct them from other nodes that have unions
29
- */
30
1
  'use strict';