flow-api-translator 0.36.0 → 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.
@@ -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
+ }
@@ -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", {
@@ -15,7 +6,8 @@ Object.defineProperty(exports, "__esModule", {
15
6
  exports.createTranslationContext = createTranslationContext;
16
7
 
17
8
  function createTranslationContext(code, scopeManager, {
18
- recoverFromErrors
9
+ recoverFromErrors,
10
+ mungeUnderscores = true
19
11
  }) {
20
12
  const referenceMap = new Map();
21
13
  const variableMap = new Map();
@@ -37,6 +29,7 @@ function createTranslationContext(code, scopeManager, {
37
29
  referenceMap,
38
30
  variableMap,
39
31
  recoverFromErrors,
32
+ mungeUnderscores,
40
33
  code
41
34
  };
42
35
  }
@@ -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';
@@ -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", {
@@ -21,7 +12,6 @@ function removeAtFlowFromDocblock(docblock) {
21
12
  }
22
13
 
23
14
  return {
24
- // $FlowExpectedError[cannot-spread-interface]
25
15
  comment: { ...docblock.comment,
26
16
  value: docblock.comment.value.replace(FLOW_DIRECTIVE, '')
27
17
  },
@@ -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", {
@@ -37,18 +28,7 @@ function flowFixMeOrError(container, message, context) {
37
28
  class TranslationErrorBase extends Error {
38
29
  constructor(node, message, context) {
39
30
  const framedMessage = buildCodeFrame(node, message, context.code);
40
- super( // jest error snapshots will use a hard-coded string representation if
41
- // `instanceof Error` which makes the code frame look awful and hard to verify:
42
- //
43
- // [TranslationError: > 12 | code
44
- // | ^^^^ error]
45
- //
46
- // this just adds a newline in jest tests so that it all lines up nicely
47
- //
48
- // [TranslationError:
49
- // > 12 | code
50
- // | ^^^^ error]
51
- process.env.JEST_WORKER_ID == null ? framedMessage : `\n${framedMessage}`);
31
+ super(process.env.JEST_WORKER_ID == null ? framedMessage : `\n${framedMessage}`);
52
32
  this.name = 'TranslationError';
53
33
  }
54
34
 
@@ -83,7 +63,6 @@ function unexpectedTranslationError(node, message, context) {
83
63
  }
84
64
 
85
65
  function buildCodeFrame(node, message, code, highlightCode = process.env.NODE_ENV !== 'test') {
86
- // babel uses 1-indexed columns
87
66
  const locForBabel = {
88
67
  start: {
89
68
  line: node.loc.start.line,
@@ -33,7 +33,7 @@ class TranslationErrorBase extends Error {
33
33
  constructor(
34
34
  node: ObjectWithLoc,
35
35
  message: string,
36
- context: $ReadOnly<{code: string, ...}>,
36
+ context: Readonly<{code: string, ...}>,
37
37
  ) {
38
38
  const framedMessage = buildCodeFrame(node, message, context.code);
39
39
  super(
@@ -59,7 +59,7 @@ export class ExpectedTranslationError extends TranslationErrorBase {
59
59
  export function translationError(
60
60
  node: ObjectWithLoc,
61
61
  message: string,
62
- context: $ReadOnly<{code: string, ...}>,
62
+ context: Readonly<{code: string, ...}>,
63
63
  ): ExpectedTranslationError {
64
64
  return new ExpectedTranslationError(node, message, context);
65
65
  }
@@ -70,7 +70,7 @@ export class UnexpectedTranslationError extends TranslationErrorBase {
70
70
  export function unexpectedTranslationError(
71
71
  node: ObjectWithLoc,
72
72
  message: string,
73
- context: $ReadOnly<{code: string, ...}>,
73
+ context: Readonly<{code: string, ...}>,
74
74
  ): UnexpectedTranslationError {
75
75
  return new UnexpectedTranslationError(node, message, context);
76
76
  }
@@ -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", {
@@ -24,9 +15,7 @@ function analyzeFunctionReturn(func) {
24
15
 
25
16
  if (returnType != null) {
26
17
  return returnType;
27
- } // We trust Flow has validated this function to only return void
28
- // $FlowFixMe[incompatible-type]
29
-
18
+ }
30
19
 
31
20
  return _hermesTransform.t.TypeAnnotation({
32
21
  typeAnnotation: _hermesTransform.t.VoidTypeAnnotation()
@@ -43,6 +32,8 @@ function analyzeTypeDependencies(rootNode, context) {
43
32
 
44
33
  if (variable != null) {
45
34
  deps.push(variable.name);
35
+ } else if (context.variableMap.has(node.name)) {
36
+ deps.push(node.name);
46
37
  }
47
38
  }
48
39
  },
@@ -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", {
@@ -15,7 +6,8 @@ Object.defineProperty(exports, "__esModule", {
15
6
  exports.createTranslationContext = createTranslationContext;
16
7
 
17
8
  function createTranslationContext(code, scopeManager, {
18
- recoverFromErrors
9
+ recoverFromErrors,
10
+ mungeUnderscores = true
19
11
  }) {
20
12
  const referenceMap = new Map();
21
13
  const variableMap = new Map();
@@ -37,6 +29,7 @@ function createTranslationContext(code, scopeManager, {
37
29
  referenceMap,
38
30
  variableMap,
39
31
  recoverFromErrors,
32
+ mungeUnderscores,
40
33
  code
41
34
  };
42
35
  }
@@ -14,19 +14,23 @@ import type {Identifier, JSXIdentifier} from 'hermes-estree';
14
14
  import type {ScopeManager, Variable} from 'hermes-eslint';
15
15
 
16
16
  export type Dep = string;
17
- export type TranslationOptions = {recoverFromErrors: boolean};
17
+ export type TranslationOptions = {
18
+ recoverFromErrors: boolean,
19
+ mungeUnderscores?: boolean,
20
+ };
18
21
  export type TranslationContext = {
19
22
  scopeManager: ScopeManager,
20
23
  referenceMap: Map<Identifier | JSXIdentifier, Variable>,
21
24
  variableMap: Map<Dep, Variable>,
22
25
  recoverFromErrors: boolean,
26
+ mungeUnderscores: boolean,
23
27
  code: string,
24
28
  };
25
29
 
26
30
  export function createTranslationContext(
27
31
  code: string,
28
32
  scopeManager: ScopeManager,
29
- {recoverFromErrors}: TranslationOptions,
33
+ {recoverFromErrors, mungeUnderscores = true}: TranslationOptions,
30
34
  ): TranslationContext {
31
35
  const referenceMap = new Map<Identifier | JSXIdentifier, Variable>();
32
36
  const variableMap = new Map<Dep, Variable>();
@@ -40,5 +44,12 @@ export function createTranslationContext(
40
44
  variableMap.set(variable.name, variable);
41
45
  }
42
46
  }
43
- return {scopeManager, referenceMap, variableMap, recoverFromErrors, code};
47
+ return {
48
+ scopeManager,
49
+ referenceMap,
50
+ variableMap,
51
+ recoverFromErrors,
52
+ mungeUnderscores,
53
+ code,
54
+ };
44
55
  }
@@ -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';