From b673375f302a0253d35d3acebede1e32cd2de804 Mon Sep 17 00:00:00 2001 From: evil bocchi Date: Sun, 6 Apr 2025 20:11:44 +0800 Subject: [PATCH] Add more instance types --- README.md | 1 - package.json | 2 +- scripts/generate-declarations.js | 33 +++- src/instance-classes.ts | 301 +++++++++++++++++++++++++++++++ src/transformer.ts | 20 +- src/types.d.ts | 0 test.ts | 6 - 7 files changed, 338 insertions(+), 25 deletions(-) create mode 100644 src/instance-classes.ts create mode 100644 src/types.d.ts delete mode 100644 test.ts diff --git a/README.md b/README.md index aef2b60..e48e9ce 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ This transformer automatically converts constructor-style instance creation (`ne - 🔍 Full TypeScript type safety - ⚡ Zero runtime overhead (transforms at compile-time) - 🔄 Supports all creatable Roblox instance types -- 📦 Easy to install and use ## Installation diff --git a/package.json b/package.json index 392c3a4..d8cb5db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rbxts-transformer-instances", - "version": "1.0.0", + "version": "1.0.1", "description": "Enable the use of instance constructors, reducing boilerplate.", "main": "out/index.js", "author": "evilbocchi", diff --git a/scripts/generate-declarations.js b/scripts/generate-declarations.js index 69f8b99..cbb2a52 100644 --- a/scripts/generate-declarations.js +++ b/scripts/generate-declarations.js @@ -128,6 +128,26 @@ declare const CreatableInstancesWithConstructors: CreatableInstancesWithConstruc return content; } +/** + * Generate the instance-classes.ts file content + * @param {string[]} classNames List of creatable instance class names + * @returns {string} The content for the instance-classes.ts file + */ +function generateInstanceClassesFile(classNames) { + return `/** + * This file is auto-generated by the generate-instance-classes script. + * Do not modify manually. + */ + +/** + * Array of all creatable Roblox instance class names + * Used by the transformer to identify constructor calls + */ +export const CREATABLE_INSTANCES = [ + ${classNames.map(name => `"${name}"`).join(",\n ")} +];`; +} + /** * Main function */ @@ -135,12 +155,17 @@ function main() { const classNames = getCreatableInstanceNames(); console.log(`Found ${classNames.length} creatable instance types`); + // Generate and write declarations file const declarationsContent = generateDeclarations(classNames); - const outputPath = path.join(__dirname, '..', 'index.d.ts'); + const declarationsPath = path.join(__dirname, '..', 'index.d.ts'); + fs.writeFileSync(declarationsPath, declarationsContent, 'utf-8'); + console.log(`Successfully generated declarations in ${declarationsPath}`); - // Write to file - fs.writeFileSync(outputPath, declarationsContent, 'utf-8'); - console.log(`Successfully generated declarations in ${outputPath}`); + // Generate and write instance-classes.ts + const instanceClassesContent = generateInstanceClassesFile(classNames); + const instanceClassesPath = path.join(__dirname, '..', 'src', 'instance-classes.ts'); + fs.writeFileSync(instanceClassesPath, instanceClassesContent, 'utf-8'); + console.log(`Successfully generated instance classes in ${instanceClassesPath}`); console.log('Example declarations:'); const examples = classNames.slice(0, 5); diff --git a/src/instance-classes.ts b/src/instance-classes.ts new file mode 100644 index 0000000..508c0b1 --- /dev/null +++ b/src/instance-classes.ts @@ -0,0 +1,301 @@ +/** + * This file is auto-generated by the generate-instance-classes script. + * Do not modify manually. + */ + +/** + * Array of all creatable Roblox instance class names + * Used by the transformer to identify constructor calls + */ +export const CREATABLE_INSTANCES = [ + "Accessory", + "AccessoryDescription", + "Accoutrement", + "Actor", + "AdGui", + "AdPortal", + "AirController", + "AlignOrientation", + "AlignPosition", + "AngularVelocity", + "Animation", + "AnimationConstraint", + "AnimationController", + "AnimationRigData", + "Animator", + "Annotation", + "ArcHandles", + "Atmosphere", + "AtmosphereSensor", + "Attachment", + "AudioAnalyzer", + "AudioChannelMixer", + "AudioChannelSplitter", + "AudioChorus", + "AudioCompressor", + "AudioDeviceInput", + "AudioDeviceOutput", + "AudioDistortion", + "AudioEcho", + "AudioEmitter", + "AudioEqualizer", + "AudioFader", + "AudioFilter", + "AudioFlanger", + "AudioLimiter", + "AudioListener", + "AudioPitchShifter", + "AudioPlayer", + "AudioReverb", + "AudioSearchParams", + "AudioTextToSpeech", + "AuroraScript", + "Backpack", + "BallSocketConstraint", + "Beam", + "BillboardGui", + "BindableEvent", + "BindableFunction", + "BlockMesh", + "BloomEffect", + "BlurEffect", + "BodyAngularVelocity", + "BodyColors", + "BodyForce", + "BodyGyro", + "BodyPartDescription", + "BodyPosition", + "BodyThrust", + "BodyVelocity", + "Bone", + "BoolValue", + "BoxHandleAdornment", + "Breakpoint", + "BrickColorValue", + "BubbleChatMessageProperties", + "BuoyancySensor", + "CFrameValue", + "Camera", + "CanvasGroup", + "CharacterMesh", + "ChorusSoundEffect", + "ClickDetector", + "ClimbController", + "Clouds", + "Color3Value", + "ColorCorrectionEffect", + "ColorGradingEffect", + "CompressorSoundEffect", + "ConeHandleAdornment", + "Configuration", + "ControllerManager", + "ControllerPartSensor", + "CornerWedgePart", + "CurveAnimation", + "CustomLog", + "CylinderHandleAdornment", + "CylinderMesh", + "CylindricalConstraint", + "DataStoreGetOptions", + "DataStoreIncrementOptions", + "DataStoreOptions", + "DataStoreSetOptions", + "Decal", + "DepthOfFieldEffect", + "Dialog", + "DialogChoice", + "DistortionSoundEffect", + "DoubleConstrainedValue", + "DragDetector", + "Dragger", + "EchoSoundEffect", + "EqualizerSoundEffect", + "EulerRotationCurve", + "ExperienceInviteOptions", + "ExplorerFilter", + "Explosion", + "FaceControls", + "FileMesh", + "Fire", + "FlangeSoundEffect", + "FloatCurve", + "FloorWire", + "FluidForceSensor", + "Folder", + "ForceField", + "Frame", + "GetTextBoundsParams", + "Glue", + "GroundController", + "Handles", + "HapticEffect", + "Hat", + "HiddenSurfaceRemovalAsset", + "Highlight", + "HingeConstraint", + "Hole", + "Humanoid", + "HumanoidController", + "HumanoidDescription", + "HumanoidRigDescription", + "IKControl", + "ImageButton", + "ImageHandleAdornment", + "ImageLabel", + "InputAction", + "InputBinding", + "InputContext", + "IntConstrainedValue", + "IntValue", + "InternalSyncItem", + "IntersectOperation", + "Keyframe", + "KeyframeMarker", + "KeyframeSequence", + "LineForce", + "LineHandleAdornment", + "LinearVelocity", + "LocalScript", + "LocalizationTable", + "ManualGlue", + "ManualWeld", + "MarkerCurve", + "MaterialVariant", + "MeshPart", + "Model", + "ModuleScript", + "Motor", + "Motor6D", + "MotorFeature", + "NegateOperation", + "NoCollisionConstraint", + "Noise", + "NumberPose", + "NumberValue", + "ObjectValue", + "OperationGraph", + "Pants", + "Part", + "PartOperation", + "ParticleEmitter", + "Path2D", + "PathfindingLink", + "PathfindingModifier", + "PitchShiftSoundEffect", + "Plane", + "PlaneConstraint", + "PluginCapabilities", + "PointLight", + "Pose", + "PrismaticConstraint", + "ProximityPrompt", + "RTAnimationTracker", + "RayValue", + "RelativeGui", + "RemoteEvent", + "RemoteFunction", + "ReverbSoundEffect", + "RigidConstraint", + "RocketPropulsion", + "RodConstraint", + "RopeConstraint", + "Rotate", + "RotateP", + "RotateV", + "RotationCurve", + "ScreenGui", + "Script", + "ScrollingFrame", + "Seat", + "SelectionBox", + "SelectionPartLasso", + "SelectionPointLasso", + "SelectionSphere", + "Shirt", + "ShirtGraphic", + "SkateboardController", + "SkateboardPlatform", + "Sky", + "Smoke", + "Snap", + "Sound", + "SoundGroup", + "Sparkles", + "SpawnLocation", + "SpecialMesh", + "SphereHandleAdornment", + "SpotLight", + "SpringConstraint", + "StarterGear", + "StringValue", + "StudioAttachment", + "StudioCallout", + "StyleDerive", + "StyleLink", + "StyleRule", + "StyleSheet", + "SunRaysEffect", + "SurfaceAppearance", + "SurfaceGui", + "SurfaceLight", + "SurfaceSelection", + "SwimController", + "Team", + "TeleportOptions", + "TerrainDetail", + "TerrainRegion", + "TextBox", + "TextButton", + "TextChannel", + "TextChatCommand", + "TextChatMessageProperties", + "TextLabel", + "Texture", + "Tool", + "Torque", + "TorsionSpringConstraint", + "TrackerStreamAnimation", + "Trail", + "TremoloSoundEffect", + "TrussPart", + "UIAspectRatioConstraint", + "UICorner", + "UIDragDetector", + "UIFlexItem", + "UIGradient", + "UIGridLayout", + "UIListLayout", + "UIPadding", + "UIPageLayout", + "UIScale", + "UISizeConstraint", + "UIStroke", + "UITableLayout", + "UITextSizeConstraint", + "UnionOperation", + "UniversalConstraint", + "UnreliableRemoteEvent", + "Vector3Curve", + "Vector3Value", + "VectorForce", + "VehicleController", + "VehicleSeat", + "VelocityMotor", + "VideoDeviceInput", + "VideoDisplay", + "VideoFrame", + "VideoPlayer", + "ViewportFrame", + "VisualizationMode", + "VisualizationModeCategory", + "WedgePart", + "Weld", + "WeldConstraint", + "Wire", + "WireframeHandleAdornment", + "WorkspaceAnnotation", + "WorldModel", + "WrapDeformer", + "WrapLayer", + "WrapTarget" +]; \ No newline at end of file diff --git a/src/transformer.ts b/src/transformer.ts index c6a86c1..8979404 100644 --- a/src/transformer.ts +++ b/src/transformer.ts @@ -1,4 +1,5 @@ import ts from "typescript"; +import { CREATABLE_INSTANCES } from "./instance-classes"; /** * This is the transformer's configuration, the values are passed from the tsconfig. @@ -35,22 +36,15 @@ function visitExpression(context: TransformContext, node: ts.Expression): ts.Exp // Check for new expressions (new Part()) if (ts.isNewExpression(node)) { const { expression, arguments: args } = node; - + // Check if it's a simple identifier (e.g., Part, not Something.Part) if (ts.isIdentifier(expression)) { const className = expression.text; - - // Check if it's an Instance-derived class (assuming basic Roblox classes for now) - const instanceClasses = [ - "Part", "Model", "Folder", "Script", "LocalScript", "ModuleScript", - "Frame", "TextLabel", "TextButton", "ImageLabel", "ImageButton", - "Sound", "Animation", "BoolValue", "StringValue", "NumberValue", - // Add more Roblox classes as needed - ]; - - if (instanceClasses.includes(className)) { + + // Use the imported list of creatable instances + if (CREATABLE_INSTANCES.includes(className)) { const { factory } = context; - + // Create new Instance("ClassName") expression return factory.createNewExpression( factory.createIdentifier("Instance"), @@ -77,7 +71,7 @@ function visitNode(context: TransformContext, node: ts.Node): ts.Node | ts.Node[ return context.transform(node); } -export default function(program: ts.Program, config: TransformerConfig = { _: undefined }) { +export default function (program: ts.Program, config: TransformerConfig = { _: undefined }) { return (context: ts.TransformationContext) => { const transformContext = new TransformContext(program, context, config); return (file: ts.SourceFile) => { diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..e69de29 diff --git a/test.ts b/test.ts deleted file mode 100644 index 59ea61e..0000000 --- a/test.ts +++ /dev/null @@ -1,6 +0,0 @@ -/// - -type hi = CreatableInstancesWithConstructors; -new Frame() - -new Part().Name = "hi"; \ No newline at end of file