An ActionScript formatter for AS3 projects
ActionScript 3 lives on in Apache Flex and Adobe AIR applications, Haxe ports and plenty of legacy code that still needs maintenance. This ActionScript formatter uses js-beautify, which suits AS3 well because the language is a close relative of ECMAScript, plus a fix-up step that keeps AS3 modifiers such as public, private, static and override on the same line as the declaration they belong to.
Example
package com.example{import flash.events.MouseEvent;public class Button extends Sprite{private var _label:String;public function Button(label:String){_label=label;addEventListener(MouseEvent.CLICK,onClick);}private function onClick(e:MouseEvent):void{trace("clicked "+_label);}}}
package com.example {
import flash.events.MouseEvent;
public class Button extends Sprite {
private var _label: String;
public function Button(label: String) {
_label = label;
addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(e: MouseEvent): void {
trace("clicked " + _label);
}
}
}
The package, class and method bodies are indented, each statement is on its own line, and operators are spaced. Because the underlying beautifier treats type annotations like object-literal colons, it prints them as label: String. Adobe's own examples usually write label:String without a space; both compile the same. If you want the compact form, a find-and-replace of : before type names restores it.
What the formatter handles
packageblocks,importstatements andclass/interfacedeclarations withextendsandimplements.- Access and other modifiers (
public,protected,internal,static,override,final,dynamic) kept together withfunction,varandconst. - Getters and setters (
public function get label(): String). - Metadata tags like
[Bindable]written on their own line, and event listener code.
Known limitation: Vector types
The typed-array syntax Vector.<int> is unique to ActionScript. A plain JavaScript beautifier would read its angle brackets as comparison operators and print Vector. < int >, which does not compile. This formatter recognises Vector types, including nested ones such as Vector.<Vector.<String>>, and keeps them exactly as written while it re-indents the code around them.
AS3 conventions the formatter leaves to you
Formatting only changes whitespace and line breaks, so a few ActionScript 3 rules still need a manual check after you tidy a file:
- One public class per file. The compiler expects the public class or interface in a
.asfile to have the same name as the file, soButton.asmust declarepublic class Button. - Package path matches the folder.
package com.example.uibelongs incom/example/ui/under a source path, otherwise imports fail. - Naming. Classes use UpperCamelCase, methods and variables lowerCamelCase, and constants such as
static const MAX_ITEMSare upper case. A leading underscore for private backing fields of getters and setters is a common Flex habit.
Keeping these consistent makes legacy Flex or AIR code far easier to review once the layout itself is clean.
Tips
AS3 code usually uses 4-space indentation and braces on the same line, which is the default here. The formatter does not check that code compiles, so an unbalanced brace shows up as indentation drifting to the right. MXML files are XML and can be formatted with the XML Formatter. For JavaScript that has been ported from AS3, the JavaScript Formatter offers more brace-style options. Your code is processed only in your browser.