first commit

This commit is contained in:
2026-03-10 16:18:05 +00:00
commit 11f9c069b5
31635 changed files with 3187747 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
# 0.0.2
- Update README documentation
# 0.0.1
- Initial version

21
node_modules/babel-plugin-transform-flow-enums/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) Facebook, Inc. and its affiliates.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,14 @@
babel-plugin-transform-flow-enums
=================================
This Babel transform turns [Flow Enums](https://flow.org/en/docs/enums/) `EnumDeclaration` nodes into calls to the [Flow Enums runtime](https://www.npmjs.com/package/flow-enums-runtime).
This plugin requires the [Flow syntax plugin](https://babeljs.io/docs/en/babel-plugin-syntax-flow), with `{enums: true}` enabled.
Read more about how to [enable Flow Enums in your project](https://flow.org/en/docs/enums/enabling-enums/).
## Options
- `getRuntime`:
- Optional function. Called with [Babel types](https://babeljs.io/docs/en/babel-types) as the first argument.
- If supplied, will be called to produce a Babel AST node which will be a reference to the Flow Enums runtime.
- If omitted, `require('flow-enums-runtime')` will be used.

View File

@@ -0,0 +1,86 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const pluginTester = require('babel-plugin-tester').default;
const plugin = require('../index');
pluginTester({
plugin,
pluginName: 'transform-flow-enums',
tests: {
boolean: {
code: `enum E {A = true, B = false}`,
output: `const E = require("flow-enums-runtime")({
A: true,
B: false
});`,
},
number: {
code: `enum E {A = 1, B = 2}`,
output: `const E = require("flow-enums-runtime")({
A: 1,
B: 2
});`,
},
'string-initialized': {
code: `enum E {A = 'a', B = 'b'}`,
output: `const E = require("flow-enums-runtime")({
A: 'a',
B: 'b'
});`,
},
'string-defaulted': {
code: `enum E {A, B}`,
output: `const E = require("flow-enums-runtime").Mirrored(["A", "B"]);`,
},
symbol: {
code: `enum E of symbol {A, B}`,
output: `const E = require("flow-enums-runtime")({
A: Symbol("A"),
B: Symbol("B")
});`,
},
export: {
code: `export enum E {A = 1, B = 2}`,
output: `export const E = require("flow-enums-runtime")({
A: 1,
B: 2
});`,
},
export_default: {
code: `export default enum E {A = 1, B = 2}`,
output: `const E = require("flow-enums-runtime")({
A: 1,
B: 2
});
export default E;`,
},
},
});
// Test `getRuntime` plugin option
pluginTester({
plugin,
pluginName: 'transform-flow-enums',
pluginOptions: {
getRuntime: t => t.identifier('Enum'),
},
tests: {
getRuntime: {
code: `enum E {A = 1, B = 2}`,
output: `const E = Enum({
A: 1,
B: 2
});`,
},
},
});

View File

@@ -0,0 +1,85 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const syntaxFlow = require('@babel/plugin-syntax-flow').default;
function memberInit(t, bodyType, member) {
if (bodyType === 'EnumSymbolBody') {
return t.callExpression(t.identifier('Symbol'), [
t.stringLiteral(member.id.name),
]);
}
return member.init;
}
module.exports = function transformEnums(babel) {
const t = babel.types;
return {
name: 'transform-flow-enums',
inherits: (api, options, dirname) =>
syntaxFlow(api, {...options, enums: true}, dirname),
visitor: {
EnumDeclaration(path, state) {
const opts = state.opts;
const enumModule =
opts.getRuntime != null
? opts.getRuntime(t)
: t.callExpression(t.identifier('require'), [
t.stringLiteral('flow-enums-runtime'),
]);
const body = path.node.body;
const members = body.members;
const mirrored =
body.type === 'EnumStringBody' &&
(!members.length || members[0].type === 'EnumDefaultedMember');
const enumExpression = mirrored
? t.callExpression(
t.memberExpression(enumModule, t.identifier('Mirrored')),
[
t.arrayExpression(
members.map(member => t.stringLiteral(member.id.name)),
),
],
)
: t.callExpression(enumModule, [
t.objectExpression(
members.map(member => {
return t.objectProperty(
member.id,
memberInit(t, body.type, member),
);
}),
),
]);
const enumDeclaration = t.variableDeclaration('const', [
t.variableDeclarator(path.node.id, enumExpression),
]);
// Default exports do not support variable declaration statements as
// children, instead we need to replace the statement and append a
// default export of the identifier. e.g.
// export default enum A {} -> const A = ...; export default A;
if (t.isExportDefaultDeclaration(path.parentPath)) {
path.parentPath.replaceWithMultiple([
enumDeclaration,
t.exportDefaultDeclaration(t.identifier(path.node.id.name)),
]);
return;
}
path.replaceWith(enumDeclaration);
},
},
};
};

View File

@@ -0,0 +1,21 @@
{
"name": "babel-plugin-transform-flow-enums",
"version": "0.0.2",
"description": "Babel transform for Flow Enums.",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/facebook/flow.git"
},
"dependencies": {
"@babel/plugin-syntax-flow": "^7.12.1"
},
"devDependencies": {
"babel-plugin-tester": "^10.0.0",
"jest": "^26.6.3"
}
}