├── .gitignore ├── .nojekyll ├── CONTRIBUTING.md ├── Cakefile ├── JSX_TODO ├── LICENSE ├── README.md ├── bin ├── cake └── coffee ├── bower.json ├── docs ├── CNAME ├── android-chrome-192x192.png ├── android-chrome-512x512.png ├── apple-touch-icon.png ├── browserconfig.xml ├── favicon-16x16.png ├── favicon-32x32.png ├── favicon.ico ├── index.html ├── jsxy │ └── index.html ├── manifest.json ├── mstile-150x150.png ├── safari-pinned-tab.svg ├── v1 │ ├── annotated-source │ │ ├── browser.html │ │ ├── cake.html │ │ ├── coffee-script.html │ │ ├── command.html │ │ ├── docco.css │ │ ├── grammar.html │ │ ├── helpers.html │ │ ├── index.html │ │ ├── lexer.html │ │ ├── nodes.html │ │ ├── optparse.html │ │ ├── public │ │ │ ├── fonts │ │ │ │ ├── aller-bold.eot │ │ │ │ ├── aller-bold.ttf │ │ │ │ ├── aller-bold.woff │ │ │ │ ├── aller-light.eot │ │ │ │ ├── aller-light.ttf │ │ │ │ ├── aller-light.woff │ │ │ │ ├── roboto-black.eot │ │ │ │ ├── roboto-black.ttf │ │ │ │ └── roboto-black.woff │ │ │ └── stylesheets │ │ │ │ └── normalize.css │ │ ├── register.html │ │ ├── repl.html │ │ ├── rewriter.html │ │ ├── scope.html │ │ └── sourcemap.html │ ├── browser-compiler │ │ └── coffee-script.js │ ├── index.html │ └── test.html └── v2 │ ├── annotated-source │ ├── browser.html │ ├── cake.html │ ├── coffeescript.html │ ├── command.html │ ├── docco.css │ ├── grammar.html │ ├── helpers.html │ ├── index.html │ ├── lexer.html │ ├── nodes.html │ ├── optparse.html │ ├── public │ │ ├── fonts │ │ │ ├── aller-bold.eot │ │ │ ├── aller-bold.ttf │ │ │ ├── aller-bold.woff │ │ │ ├── aller-light.eot │ │ │ ├── aller-light.ttf │ │ │ ├── aller-light.woff │ │ │ ├── roboto-black.eot │ │ │ ├── roboto-black.ttf │ │ │ └── roboto-black.woff │ │ └── stylesheets │ │ │ └── normalize.css │ ├── register.html │ ├── repl.html │ ├── rewriter.html │ ├── scope.html │ └── sourcemap.html │ ├── browser-compiler │ └── coffeescript.js │ ├── index.html │ └── test.html ├── documentation ├── examples │ ├── aliases.coffee │ ├── array_comprehensions.coffee │ ├── array_spread.coffee │ ├── async.coffee │ ├── breaking_change_bound_generator_function.coffee │ ├── breaking_change_destructuring_default_values.coffee │ ├── breaking_change_fat_arrow.coffee │ ├── breaking_change_function_parameter_default_values.coffee │ ├── breaking_change_super_in_non-class_methods_refactor_with_apply.coffee │ ├── breaking_change_super_in_non-class_methods_refactor_with_class.coffee │ ├── breaking_change_super_with_arguments.coffee │ ├── breaking_change_super_without_arguments.coffee │ ├── cake_tasks.coffee │ ├── chaining.coffee │ ├── classes.coffee │ ├── comment.coffee │ ├── comparisons.coffee │ ├── conditionals.coffee │ ├── constructor_destructuring.coffee │ ├── default_args.coffee │ ├── do.coffee │ ├── embedded.coffee │ ├── embedded_block.coffee │ ├── embedded_escaped.coffee │ ├── existence.coffee │ ├── existence_declared.coffee │ ├── existence_undeclared.coffee │ ├── expansion.coffee │ ├── expressions.coffee │ ├── expressions_assignment.coffee │ ├── expressions_comprehension.coffee │ ├── expressions_try.coffee │ ├── fat_arrow.coffee │ ├── functions.coffee │ ├── generator_iteration.coffee │ ├── generators.coffee │ ├── get_set.coffee │ ├── heredocs.coffee │ ├── heregexes.coffee │ ├── interpolation.coffee │ ├── jsx.coffee │ ├── jsxy_attributes.coffee │ ├── jsxy_capitalized_class_name.coffee │ ├── jsxy_dereference_noncapitalized_component_name.coffee │ ├── jsxy_dotted_element_name.coffee │ ├── jsxy_dynamic_class_names.coffee │ ├── jsxy_elements.coffee │ ├── jsxy_else.coffee │ ├── jsxy_expressions.coffee │ ├── jsxy_filters.coffee │ ├── jsxy_for.coffee │ ├── jsxy_if.coffee │ ├── jsxy_leading_dot_class_vs_chain.coffee │ ├── jsxy_nested_elements.coffee │ ├── jsxy_postfix_for.coffee │ ├── jsxy_postfix_if.coffee │ ├── jsxy_shorthand_elements.coffee │ ├── jsxy_switch.coffee │ ├── jsxy_text_content.coffee │ ├── jsxy_unless.coffee │ ├── jsxy_whitespace_operator.coffee │ ├── modules.coffee │ ├── modulo.coffee │ ├── multiple_return_values.coffee │ ├── object_comprehensions.coffee │ ├── object_extraction.coffee │ ├── object_spread.coffee │ ├── objects_and_arrays.coffee │ ├── objects_reserved.coffee │ ├── objects_shorthand.coffee │ ├── overview.coffee │ ├── parallel_assignment.coffee │ ├── patterns_and_splats.coffee │ ├── prototypes.coffee │ ├── range_comprehensions.coffee │ ├── scope.coffee │ ├── slices.coffee │ ├── soaks.coffee │ ├── splats.coffee │ ├── splices.coffee │ ├── static.coffee │ ├── strings.coffee │ ├── switch.coffee │ ├── switch_with_no_expression.coffee │ ├── tagged_template_literals.coffee │ ├── try.coffee │ ├── type_annotations.coffee │ └── while.coffee ├── images │ ├── icon.svg │ └── logo.svg ├── index.html ├── index_jsxy.html ├── jsxy │ └── code.coffee ├── sections │ ├── annotated_source.md │ ├── async_functions.md │ ├── books.md │ ├── breaking_change_fat_arrow.md │ ├── breaking_changes.md │ ├── breaking_changes_argument_parsing_and_shebang_lines.md │ ├── breaking_changes_bound_generator_functions.md │ ├── breaking_changes_classes.md │ ├── breaking_changes_default_values.md │ ├── breaking_changes_jsx_and_the_less_than_and_greater_than_operators.md │ ├── breaking_changes_literate_coffeescript.md │ ├── breaking_changes_super_extends.md │ ├── cake.md │ ├── chaining.md │ ├── changelog.md │ ├── chat.md │ ├── classes.md │ ├── coffeescript_2.md │ ├── command_line_interface.md │ ├── comments.md │ ├── comparisons.md │ ├── conditionals.md │ ├── contributing.md │ ├── destructuring.md │ ├── embedded.md │ ├── es2015plus_output.md │ ├── examples.md │ ├── existential_operator.md │ ├── expressions.md │ ├── fat_arrow.md │ ├── functions.md │ ├── generators.md │ ├── heregexes.md │ ├── installation.md │ ├── introduction.md │ ├── jsx.md │ ├── jsxy.md │ ├── language.md │ ├── lexical_scope.md │ ├── literate.md │ ├── loops.md │ ├── modules.md │ ├── nodejs_usage.md │ ├── objects_and_arrays.md │ ├── operators.md │ ├── overview.md │ ├── prototypal_inheritance.md │ ├── resources.md │ ├── screencasts.md │ ├── scripts.md │ ├── slices.md │ ├── source_maps.md │ ├── splats.md │ ├── strings.md │ ├── switch.md │ ├── tagged_template_literals.md │ ├── test.md │ ├── try.md │ ├── type_annotations.md │ ├── unsupported.md │ ├── unsupported_get_set.md │ ├── unsupported_let_const.md │ ├── unsupported_named_functions.md │ └── usage.md ├── test.html ├── v1 │ ├── body.html │ ├── body_jsxy.html │ ├── code.coffee │ ├── docs.coffee │ ├── docs.css │ ├── scripts.html │ ├── styles.html │ └── tomorrow.css └── v2 │ ├── body.html │ ├── code.coffee │ ├── code.html │ ├── docs.coffee │ ├── docs.css │ ├── navbar.html │ ├── scripts.html │ ├── sidebar.html │ ├── styles.html │ ├── try.html │ └── twilight.css ├── jsx_examples ├── greeting.coffee ├── mailbox.coffee ├── name_form.coffee ├── table_items.coffee └── toggle.coffee ├── lib └── coffeescript │ ├── browser.js │ ├── cake.js │ ├── coffeescript.js │ ├── command.js │ ├── grammar.js │ ├── helpers.js │ ├── index.js │ ├── lexer.js │ ├── nodes.js │ ├── optparse.js │ ├── parser.js │ ├── register.js │ ├── repl.js │ ├── rewriter.js │ ├── scope.js │ └── sourcemap.js ├── package-lock.json ├── package.json ├── register.js ├── repl.js ├── src ├── browser.coffee ├── cake.coffee ├── coffeescript.coffee ├── command.coffee ├── grammar.coffee ├── helpers.coffee ├── index.coffee ├── lexer.coffee ├── nodes.coffee ├── optparse.coffee ├── register.coffee ├── repl.coffee ├── rewriter.coffee ├── scope.litcoffee └── sourcemap.litcoffee └── test ├── argument_parsing.coffee ├── arrays.coffee ├── assignment.coffee ├── async.coffee ├── booleans.coffee ├── classes.coffee ├── cluster.coffee ├── comments.coffee ├── compilation.coffee ├── comprehensions.coffee ├── control_flow.coffee ├── error_messages.coffee ├── eval.coffee ├── exception_handling.coffee ├── formatting.coffee ├── function_invocation.coffee ├── functions.coffee ├── generators.coffee ├── helpers.coffee ├── importing.coffee ├── importing ├── .coffee ├── .coffee.md ├── .import.coffee ├── .import.coffee.md ├── .import2 ├── import.coffee ├── import.coffee.md ├── import.extension.coffee ├── import.extension.coffee.md ├── import.extension.js ├── import.js ├── import.litcoffee ├── import.unknownextension ├── import2 ├── index.coffee.md ├── shebang.coffee ├── shebang_extra_args.coffee ├── shebang_initial_space.coffee └── shebang_initial_space_extra_args.coffee ├── interpolation.coffee ├── invocation_argument_parsing.coffee ├── javascript_literals.coffee ├── jsx.coffee ├── literate.litcoffee ├── literate_tabbed.litcoffee ├── location.coffee ├── modules.coffee ├── numbers.coffee ├── objects.coffee ├── operators.coffee ├── option_parser.coffee ├── parser.coffee ├── ranges.coffee ├── regexps.coffee ├── repl.coffee ├── scope.coffee ├── slicing_and_splicing.coffee ├── soaks.coffee ├── sourcemap.coffee ├── strict.coffee ├── strings.coffee ├── support └── helpers.coffee └── tagged_template_literals.coffee /.gitignore: -------------------------------------------------------------------------------- 1 | raw 2 | presentation 3 | test.coffee 4 | test*.coffee 5 | test.litcoffee 6 | test*.litcoffee 7 | test/*.js 8 | parser.output 9 | /node_modules 10 | npm-debug.log* 11 | yarn.lock 12 | -------------------------------------------------------------------------------- /.nojekyll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/.nojekyll -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## How to contribute to CoffeeScript 2 | 3 | * Before you open a ticket or send a pull request, [search](https://github.com/jashkenas/coffeescript/issues) for previous discussions about the same feature or issue. Add to the earlier ticket if you find one. 4 | 5 | * Before sending a pull request for a feature, be sure to have [tests](https://github.com/jashkenas/coffeescript/tree/master/test). 6 | 7 | * Use the same coding style as the rest of the [codebase](https://github.com/jashkenas/coffeescript/tree/master/src). If you’re just getting started with CoffeeScript, there’s a nice [style guide](https://github.com/polarmobile/coffeescript-style-guide). 8 | 9 | * In your pull request, do not add documentation to `index.html` or re-build the minified `coffeescript.js` file. We’ll do those things before cutting a new release. You _should,_ however, commit the updated compiled JavaScript files in `lib`. -------------------------------------------------------------------------------- /JSX_TODO: -------------------------------------------------------------------------------- 1 | - allow interpolated string attribute values eg %h1( id="recipe-#{ x }" ) 2 | - allow simple unquoted attribute values eg %p( key=i ) (or is this confusing/not preferable to HTML allowing unquoted attribute values?) 3 | - handle inline element (with or without body?) followed by indent eg: 4 | if isSomething %abc 5 | x 6 | - refine attribute error reporting so that eg %h1( x=[abc] ) will specifically tell you the attribute value is what's wrong 7 | - refactor attribute value nested lexing to share code with matchWithInterpolations() 8 | - compile empty element to eg instead of ? is there a semantic difference to JSX and/or HTML? 9 | - className and other attribute-name translations (automatically camelCase, that's what JSX wants?) 10 | - allow line-ending inline comments after inline elements eg %h1 # this is an h1 tag 11 | - allow normal Coffeescript (line) comments inside (indented) element body 12 | - support object spread operator (pre and post ...'s?) in all 3 types of attributes 13 | - handle no-value attributes in () and <> attributes (implicit value is true?) 14 | - support haml-style nested object attributes? eg ng: { click: ... } - or is that ambiguous w/ possible object-literal value of top-level attribute? 15 | - handle comments inside object attributes? 16 | - don't wrap string literal object attribute values in braces eg %h1{ a: 'b' } >>

, not

17 | - support haml-style class and id attribute merging? should also imitate haml wrt other duplicate attributes eg %a( b='c' ){ b: 'd' } >> (looks like object always wins)? 18 | - should it allow else nested at same depth as = if? eg: 19 | %h1 20 | = if x 21 | %abc 22 | else 23 | %def 24 | - allow haml-style self-closing tag syntax? eg %hr/ 25 | - handle inline else eg if x then %a else %b 26 | - handle chain after function glyph eg: 27 | x -> 28 | .y 29 | - handle namespaced (:) element names 30 | - handle elements as attribute values? 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009-2017 Jeremy Ashkenas 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /bin/cake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var path = require('path'); 4 | var fs = require('fs'); 5 | 6 | var potentialPaths = [ 7 | path.join(process.cwd(), 'node_modules/coffeescript/lib/coffeescript'), 8 | path.join(process.cwd(), 'node_modules/coffeescript/lib/coffee-script'), 9 | path.join(process.cwd(), 'node_modules/coffee-script/lib/coffee-script'), 10 | path.join(__dirname, '../lib/coffeescript') 11 | ]; 12 | 13 | for (var i = 0, len = potentialPaths.length; i < len; i++) { 14 | if (fs.existsSync(potentialPaths[i])) { 15 | require(potentialPaths[i] + '/cake').run(); 16 | break; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /bin/coffee: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var path = require('path'); 4 | var fs = require('fs'); 5 | 6 | var potentialPaths = [ 7 | path.join(process.cwd(), 'node_modules/coffeescript/lib/coffeescript'), 8 | path.join(process.cwd(), 'node_modules/coffeescript/lib/coffee-script'), 9 | path.join(process.cwd(), 'node_modules/coffee-script/lib/coffee-script'), 10 | path.join(__dirname, '../lib/coffeescript') 11 | ]; 12 | 13 | for (var i = 0, len = potentialPaths.length; i < len; i++) { 14 | if (fs.existsSync(potentialPaths[i])) { 15 | require(potentialPaths[i] + '/command').run(); 16 | break; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "coffeescript", 3 | "main": [ 4 | "lib/coffeescript/browser.js" 5 | ], 6 | "description": "Unfancy JavaScript", 7 | "keywords": [ 8 | "javascript", 9 | "language", 10 | "coffeescript", 11 | "compiler" 12 | ], 13 | "author": { 14 | "name": "Jeremy Ashkenas" 15 | }, 16 | "ignore": [ 17 | "test" 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /docs/CNAME: -------------------------------------------------------------------------------- 1 | coffeescript.org 2 | -------------------------------------------------------------------------------- /docs/android-chrome-192x192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/android-chrome-192x192.png -------------------------------------------------------------------------------- /docs/android-chrome-512x512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/android-chrome-512x512.png -------------------------------------------------------------------------------- /docs/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/apple-touch-icon.png -------------------------------------------------------------------------------- /docs/browserconfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | #da532c 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /docs/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/favicon-16x16.png -------------------------------------------------------------------------------- /docs/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/favicon-32x32.png -------------------------------------------------------------------------------- /docs/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/favicon.ico -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | v1/index.html -------------------------------------------------------------------------------- /docs/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "", 3 | "icons": [ 4 | { 5 | "src": "\/android-chrome-192x192.png", 6 | "sizes": "192x192", 7 | "type": "image\/png" 8 | }, 9 | { 10 | "src": "\/android-chrome-512x512.png", 11 | "sizes": "512x512", 12 | "type": "image\/png" 13 | } 14 | ], 15 | "theme_color": "#ffffff", 16 | "display": "standalone" 17 | } 18 | -------------------------------------------------------------------------------- /docs/mstile-150x150.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/mstile-150x150.png -------------------------------------------------------------------------------- /docs/safari-pinned-tab.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 | Created by potrace 1.11, written by Peter Selinger 2001-2013 9 | 10 | 12 | 22 | 29 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/v1/annotated-source/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | index.coffee 6 | 7 | 8 | 9 | 10 | 11 |
12 |
13 | 14 |
101 | 102 | 127 |
128 | 129 | 130 | -------------------------------------------------------------------------------- /docs/v1/annotated-source/public/fonts/aller-bold.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v1/annotated-source/public/fonts/aller-bold.eot -------------------------------------------------------------------------------- /docs/v1/annotated-source/public/fonts/aller-bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v1/annotated-source/public/fonts/aller-bold.ttf -------------------------------------------------------------------------------- /docs/v1/annotated-source/public/fonts/aller-bold.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v1/annotated-source/public/fonts/aller-bold.woff -------------------------------------------------------------------------------- /docs/v1/annotated-source/public/fonts/aller-light.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v1/annotated-source/public/fonts/aller-light.eot -------------------------------------------------------------------------------- /docs/v1/annotated-source/public/fonts/aller-light.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v1/annotated-source/public/fonts/aller-light.ttf -------------------------------------------------------------------------------- /docs/v1/annotated-source/public/fonts/aller-light.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v1/annotated-source/public/fonts/aller-light.woff -------------------------------------------------------------------------------- /docs/v1/annotated-source/public/fonts/roboto-black.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v1/annotated-source/public/fonts/roboto-black.eot -------------------------------------------------------------------------------- /docs/v1/annotated-source/public/fonts/roboto-black.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v1/annotated-source/public/fonts/roboto-black.ttf -------------------------------------------------------------------------------- /docs/v1/annotated-source/public/fonts/roboto-black.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v1/annotated-source/public/fonts/roboto-black.woff -------------------------------------------------------------------------------- /docs/v2/annotated-source/public/fonts/aller-bold.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v2/annotated-source/public/fonts/aller-bold.eot -------------------------------------------------------------------------------- /docs/v2/annotated-source/public/fonts/aller-bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v2/annotated-source/public/fonts/aller-bold.ttf -------------------------------------------------------------------------------- /docs/v2/annotated-source/public/fonts/aller-bold.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v2/annotated-source/public/fonts/aller-bold.woff -------------------------------------------------------------------------------- /docs/v2/annotated-source/public/fonts/aller-light.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v2/annotated-source/public/fonts/aller-light.eot -------------------------------------------------------------------------------- /docs/v2/annotated-source/public/fonts/aller-light.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v2/annotated-source/public/fonts/aller-light.ttf -------------------------------------------------------------------------------- /docs/v2/annotated-source/public/fonts/aller-light.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v2/annotated-source/public/fonts/aller-light.woff -------------------------------------------------------------------------------- /docs/v2/annotated-source/public/fonts/roboto-black.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v2/annotated-source/public/fonts/roboto-black.eot -------------------------------------------------------------------------------- /docs/v2/annotated-source/public/fonts/roboto-black.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v2/annotated-source/public/fonts/roboto-black.ttf -------------------------------------------------------------------------------- /docs/v2/annotated-source/public/fonts/roboto-black.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/helixbass/coffeescript-jsxy/f2dd087810f5823b4fd626f6d5cde66ece44f10c/docs/v2/annotated-source/public/fonts/roboto-black.woff -------------------------------------------------------------------------------- /documentation/examples/aliases.coffee: -------------------------------------------------------------------------------- 1 | launch() if ignition is on 2 | 3 | volume = 10 if band isnt SpinalTap 4 | 5 | letTheWildRumpusBegin() unless answer is no 6 | 7 | if car.speed < limit then accelerate() 8 | 9 | winner = yes if pick in [47, 92, 13] 10 | 11 | print inspect "My name is #{@name}" 12 | -------------------------------------------------------------------------------- /documentation/examples/array_comprehensions.coffee: -------------------------------------------------------------------------------- 1 | # Eat lunch. 2 | eat = (food) -> "#{food} eaten." 3 | eat food for food in ['toast', 'cheese', 'wine'] 4 | 5 | # Fine five course dining. 6 | courses = ['greens', 'caviar', 'truffles', 'roast', 'cake'] 7 | menu = (i, dish) -> "Menu Item #{i}: #{dish}" 8 | menu i + 1, dish for dish, i in courses 9 | 10 | # Health conscious meal. 11 | foods = ['broccoli', 'spinach', 'chocolate'] 12 | eat food for food in foods when food isnt 'chocolate' 13 | -------------------------------------------------------------------------------- /documentation/examples/array_spread.coffee: -------------------------------------------------------------------------------- 1 | popular = ['pepperoni', 'sausage', 'cheese'] 2 | unwanted = ['anchovies', 'olives'] 3 | 4 | all = [popular..., unwanted..., 'mushrooms'] 5 | -------------------------------------------------------------------------------- /documentation/examples/async.coffee: -------------------------------------------------------------------------------- 1 | # Your browser must support async/await and speech synthesis 2 | # to run this example. 3 | 4 | sleep = (ms) -> 5 | new Promise (resolve) -> 6 | window.setTimeout resolve, ms 7 | 8 | say = (text) -> 9 | window.speechSynthesis.cancel() 10 | window.speechSynthesis.speak new SpeechSynthesisUtterance text 11 | 12 | countdown = (seconds) -> 13 | for i in [seconds..1] 14 | say i 15 | await sleep 1000 # wait one second 16 | say "Blastoff!" 17 | 18 | countdown 3 19 | -------------------------------------------------------------------------------- /documentation/examples/breaking_change_bound_generator_function.coffee: -------------------------------------------------------------------------------- 1 | self = this 2 | f = -> yield self 3 | -------------------------------------------------------------------------------- /documentation/examples/breaking_change_destructuring_default_values.coffee: -------------------------------------------------------------------------------- 1 | {a = 1} = {a: null} 2 | 3 | a # Equals 1 in CoffeeScript 1.x, null in CoffeeScript 2 4 | -------------------------------------------------------------------------------- /documentation/examples/breaking_change_fat_arrow.coffee: -------------------------------------------------------------------------------- 1 | outer = -> 2 | inner = => Array.from arguments 3 | inner() 4 | 5 | outer(1, 2) # Returns '' in CoffeeScript 1.x, '1, 2' in CoffeeScript 2 6 | -------------------------------------------------------------------------------- /documentation/examples/breaking_change_function_parameter_default_values.coffee: -------------------------------------------------------------------------------- 1 | f = (a = 1) -> a 2 | 3 | f(null) # Returns 1 in CoffeeScript 1.x, null in CoffeeScript 2 4 | -------------------------------------------------------------------------------- /documentation/examples/breaking_change_super_in_non-class_methods_refactor_with_apply.coffee: -------------------------------------------------------------------------------- 1 | # Helper functions 2 | hasProp = {}.hasOwnProperty 3 | extend = (child, parent) -> 4 | ctor = -> 5 | @constructor = child 6 | return 7 | for key of parent 8 | if hasProp.call(parent, key) 9 | child[key] = parent[key] 10 | ctor.prototype = parent.prototype 11 | child.prototype = new ctor 12 | child 13 | 14 | 15 | A = -> 16 | B = -> 17 | extend B, A 18 | B.prototype.foo = -> A::foo.apply this, arguments 19 | -------------------------------------------------------------------------------- /documentation/examples/breaking_change_super_in_non-class_methods_refactor_with_class.coffee: -------------------------------------------------------------------------------- 1 | class A 2 | class B extends A 3 | foo: -> super arguments... 4 | -------------------------------------------------------------------------------- /documentation/examples/breaking_change_super_with_arguments.coffee: -------------------------------------------------------------------------------- 1 | class B extends A 2 | foo: -> super arguments... 3 | -------------------------------------------------------------------------------- /documentation/examples/breaking_change_super_without_arguments.coffee: -------------------------------------------------------------------------------- 1 | class B extends A 2 | foo: -> super() 3 | -------------------------------------------------------------------------------- /documentation/examples/cake_tasks.coffee: -------------------------------------------------------------------------------- 1 | fs = require 'fs' 2 | 3 | option '-o', '--output [DIR]', 'directory for compiled code' 4 | 5 | task 'build:parser', 'rebuild the Jison parser', (options) -> 6 | require 'jison' 7 | code = require('./lib/grammar').parser.generate() 8 | dir = options.output or 'lib' 9 | fs.writeFile "#{dir}/parser.js", code 10 | -------------------------------------------------------------------------------- /documentation/examples/chaining.coffee: -------------------------------------------------------------------------------- 1 | $ 'body' 2 | .click (e) -> 3 | $ '.box' 4 | .fadeIn 'fast' 5 | .addClass 'show' 6 | .css 'background', 'white' 7 | -------------------------------------------------------------------------------- /documentation/examples/classes.coffee: -------------------------------------------------------------------------------- 1 | class Animal 2 | constructor: (@name) -> 3 | 4 | move: (meters) -> 5 | alert @name + " moved #{meters}m." 6 | 7 | class Snake extends Animal 8 | move: -> 9 | alert "Slithering..." 10 | super 5 11 | 12 | class Horse extends Animal 13 | move: -> 14 | alert "Galloping..." 15 | super 45 16 | 17 | sam = new Snake "Sammy the Python" 18 | tom = new Horse "Tommy the Palomino" 19 | 20 | sam.move() 21 | tom.move() 22 | -------------------------------------------------------------------------------- /documentation/examples/comment.coffee: -------------------------------------------------------------------------------- 1 | ### 2 | Fortune Cookie Reader v1.0 3 | Released under the MIT License 4 | ### 5 | 6 | sayFortune = (fortune) -> 7 | console.log fortune # in bed! 8 | -------------------------------------------------------------------------------- /documentation/examples/comparisons.coffee: -------------------------------------------------------------------------------- 1 | cholesterol = 127 2 | 3 | healthy = 200 > cholesterol > 60 4 | -------------------------------------------------------------------------------- /documentation/examples/conditionals.coffee: -------------------------------------------------------------------------------- 1 | mood = greatlyImproved if singing 2 | 3 | if happy and knowsIt 4 | clapsHands() 5 | chaChaCha() 6 | else 7 | showIt() 8 | 9 | date = if friday then sue else jill 10 | -------------------------------------------------------------------------------- /documentation/examples/constructor_destructuring.coffee: -------------------------------------------------------------------------------- 1 | class Person 2 | constructor: (options) -> 3 | {@name, @age, @height = 'average'} = options 4 | 5 | tim = new Person name: 'Tim', age: 4 6 | -------------------------------------------------------------------------------- /documentation/examples/default_args.coffee: -------------------------------------------------------------------------------- 1 | fill = (container, liquid = "coffee") -> 2 | "Filling the #{container} with #{liquid}..." 3 | -------------------------------------------------------------------------------- /documentation/examples/do.coffee: -------------------------------------------------------------------------------- 1 | for filename in list 2 | do (filename) -> 3 | if filename not in ['.DS_Store', 'Thumbs.db', 'ehthumbs.db'] 4 | fs.readFile filename, (err, contents) -> 5 | compile filename, contents.toString() 6 | -------------------------------------------------------------------------------- /documentation/examples/embedded.coffee: -------------------------------------------------------------------------------- 1 | hi = `function() { 2 | return [document.title, "Hello JavaScript"].join(": "); 3 | }` 4 | -------------------------------------------------------------------------------- /documentation/examples/embedded_block.coffee: -------------------------------------------------------------------------------- 1 | ``` 2 | function time() { 3 | return `The time is ${new Date().toLocaleTimeString()}`; 4 | } 5 | ``` 6 | -------------------------------------------------------------------------------- /documentation/examples/embedded_escaped.coffee: -------------------------------------------------------------------------------- 1 | markdown = `function () { 2 | return \`In Markdown, write code like \\\`this\\\`\`; 3 | }` 4 | -------------------------------------------------------------------------------- /documentation/examples/existence.coffee: -------------------------------------------------------------------------------- 1 | solipsism = true if mind? and not world? 2 | 3 | speed = 0 4 | speed ?= 15 5 | 6 | footprints = yeti ? "bear" 7 | -------------------------------------------------------------------------------- /documentation/examples/existence_declared.coffee: -------------------------------------------------------------------------------- 1 | major = 'Computer Science' 2 | 3 | unless major? 4 | signUpForClass 'Introduction to Wines' 5 | -------------------------------------------------------------------------------- /documentation/examples/existence_undeclared.coffee: -------------------------------------------------------------------------------- 1 | if window? 2 | environment = 'browser (probably)' 3 | -------------------------------------------------------------------------------- /documentation/examples/expansion.coffee: -------------------------------------------------------------------------------- 1 | text = "Every literary critic believes he will 2 | outwit history and have the last word" 3 | 4 | [first, ..., last] = text.split " " 5 | -------------------------------------------------------------------------------- /documentation/examples/expressions.coffee: -------------------------------------------------------------------------------- 1 | grade = (student) -> 2 | if student.excellentWork 3 | "A+" 4 | else if student.okayStuff 5 | if student.triedHard then "B" else "B-" 6 | else 7 | "C" 8 | 9 | eldest = if 24 > 21 then "Liz" else "Ike" 10 | -------------------------------------------------------------------------------- /documentation/examples/expressions_assignment.coffee: -------------------------------------------------------------------------------- 1 | six = (one = 1) + (two = 2) + (three = 3) 2 | -------------------------------------------------------------------------------- /documentation/examples/expressions_comprehension.coffee: -------------------------------------------------------------------------------- 1 | # The first ten global properties. 2 | 3 | globals = (name for name of window)[0...10] 4 | -------------------------------------------------------------------------------- /documentation/examples/expressions_try.coffee: -------------------------------------------------------------------------------- 1 | alert( 2 | try 3 | nonexistent / undefined 4 | catch error 5 | "And the error is ... #{error}" 6 | ) 7 | -------------------------------------------------------------------------------- /documentation/examples/fat_arrow.coffee: -------------------------------------------------------------------------------- 1 | Account = (customer, cart) -> 2 | @customer = customer 3 | @cart = cart 4 | 5 | $('.shopping_cart').on 'click', (event) => 6 | @customer.purchase @cart 7 | -------------------------------------------------------------------------------- /documentation/examples/functions.coffee: -------------------------------------------------------------------------------- 1 | square = (x) -> x * x 2 | cube = (x) -> square(x) * x 3 | -------------------------------------------------------------------------------- /documentation/examples/generator_iteration.coffee: -------------------------------------------------------------------------------- 1 | fibonacci = -> 2 | [previous, current] = [1, 1] 3 | loop 4 | [previous, current] = [current, previous + current] 5 | yield current 6 | return 7 | 8 | getFibonacciNumbers = (length) -> 9 | results = [1] 10 | for n from fibonacci() 11 | results.push n 12 | break if results.length is length 13 | results 14 | -------------------------------------------------------------------------------- /documentation/examples/generators.coffee: -------------------------------------------------------------------------------- 1 | perfectSquares = -> 2 | num = 0 3 | loop 4 | num += 1 5 | yield num * num 6 | return 7 | 8 | window.ps or= perfectSquares() 9 | -------------------------------------------------------------------------------- /documentation/examples/get_set.coffee: -------------------------------------------------------------------------------- 1 | screen = 2 | width: 1200 3 | ratio: 16/9 4 | 5 | Object.defineProperty screen, 'height', 6 | get: -> 7 | this.width / this.ratio 8 | set: (val) -> 9 | this.width = val / this.ratio 10 | -------------------------------------------------------------------------------- /documentation/examples/heredocs.coffee: -------------------------------------------------------------------------------- 1 | html = """ 2 | 3 | cup of coffeescript 4 | 5 | """ 6 | -------------------------------------------------------------------------------- /documentation/examples/heregexes.coffee: -------------------------------------------------------------------------------- 1 | NUMBER = /// 2 | ^ 0b[01]+ | # binary 3 | ^ 0o[0-7]+ | # octal 4 | ^ 0x[\da-f]+ | # hex 5 | ^ \d*\.?\d+ (?:e[+-]?\d+)? # decimal 6 | ///i 7 | -------------------------------------------------------------------------------- /documentation/examples/interpolation.coffee: -------------------------------------------------------------------------------- 1 | author = "Wittgenstein" 2 | quote = "A picture is a fact. -- #{ author }" 3 | 4 | sentence = "#{ 22 / 7 } is a decent approximation of π" 5 | -------------------------------------------------------------------------------- /documentation/examples/jsx.coffee: -------------------------------------------------------------------------------- 1 | renderStarRating = ({ rating, maxStars }) -> 2 | 10 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_attributes.coffee: -------------------------------------------------------------------------------- 1 | %input{ 2 | type: 'text' 3 | @onChange 4 | props... 5 | } 6 | 7 | %Greeting( name='Juan' ) 8 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_capitalized_class_name.coffee: -------------------------------------------------------------------------------- 1 | %div.('Class1') 2 | 3 | %div{ className: 'Class1' } 4 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_dereference_noncapitalized_component_name.coffee: -------------------------------------------------------------------------------- 1 | {field: Field} = Form 2 | 3 | %Field 4 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_dotted_element_name.coffee: -------------------------------------------------------------------------------- 1 | %Form.Field 2 | 3 | %Form.field 4 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_dynamic_class_names.coffee: -------------------------------------------------------------------------------- 1 | %section.( 2 | highlighted: index > 2 3 | 'active active-2': @props.is_active 4 | ) 5 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_elements.coffee: -------------------------------------------------------------------------------- 1 | %div 2 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_else.coffee: -------------------------------------------------------------------------------- 1 | %div 2 | = if loggedIn 3 | %Welcome 4 | else if forbidden 5 | %GoAway 6 | else 7 | %Login 8 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_expressions.coffee: -------------------------------------------------------------------------------- 1 | %h1= page_title 2 | 3 | %h2 4 | Welcome, {name}! 5 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_filters.coffee: -------------------------------------------------------------------------------- 1 | %a ${amount | currency} 2 | 3 | %strong= octalVal | parseInt ~ 8 4 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_for.coffee: -------------------------------------------------------------------------------- 1 | render: -> 2 | {steps} = @props 3 | 4 | %ul.steps-container 5 | = for {ingredients, description}, i in steps 6 | %Step{ 7 | ingredients, description 8 | key: i 9 | } 10 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_if.coffee: -------------------------------------------------------------------------------- 1 | %div 2 | = if headerText 3 | %Header{ headerText } 4 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_leading_dot_class_vs_chain.coffee: -------------------------------------------------------------------------------- 1 | a 2 | 3 | .b(x='c') 4 | 5 | a 6 | .b(x='c') 7 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_nested_elements.coffee: -------------------------------------------------------------------------------- 1 | .row 2 | .col-sm-3 3 | %h1( title='Accounts' ) Accounts 4 | %Accounts{ accounts: @props.all_accounts } 5 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_postfix_for.coffee: -------------------------------------------------------------------------------- 1 | render: -> 2 | {steps} = @props 3 | 4 | %ul.steps-container 5 | = %Step{ 6 | ingredients, description 7 | key: i 8 | } for {ingredients, description}, i in steps 9 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_postfix_if.coffee: -------------------------------------------------------------------------------- 1 | %div 2 | = %Header{ headerText } if headerText? 3 | = %Footer unless dontIncludeFooter 4 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_shorthand_elements.coffee: -------------------------------------------------------------------------------- 1 | #app 2 | 3 | .container 4 | 5 | %h1.heading#main_title 6 | 7 | .col-sm-6.col-md-3 8 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_switch.coffee: -------------------------------------------------------------------------------- 1 | Home = ({loggedIn, forbidden}) -> 2 | %div 3 | = switch 4 | when loggedIn then %Welcome 5 | when forbidden then %GoAway 6 | else %Login 7 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_text_content.coffee: -------------------------------------------------------------------------------- 1 | %h1 Greetings 2 | 3 | %p 4 | Welcome to Top Notch Hotels. 5 | We hope you enjoy your stay 6 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_unless.coffee: -------------------------------------------------------------------------------- 1 | %div 2 | = unless dontIncludeHeader 3 | %Header{ headerText } 4 | -------------------------------------------------------------------------------- /documentation/examples/jsxy_whitespace_operator.coffee: -------------------------------------------------------------------------------- 1 | %p 2 | Oh man, 3 | =^ name 4 | is going to 5 | %strong{ style: fontSize: '2em' }^ 6 | = action 7 | when he finds out! 8 | -------------------------------------------------------------------------------- /documentation/examples/modules.coffee: -------------------------------------------------------------------------------- 1 | import 'local-file.coffee' 2 | import 'coffeescript' 3 | 4 | import _ from 'underscore' 5 | import * as underscore from 'underscore' 6 | 7 | import { now } from 'underscore' 8 | import { now as currentTimestamp } from 'underscore' 9 | import { first, last } from 'underscore' 10 | import utilityBelt, { each } from 'underscore' 11 | 12 | export default Math 13 | export square = (x) -> x * x 14 | export class Mathematics 15 | least: (x, y) -> if x < y then x else y 16 | 17 | export { sqrt } 18 | export { sqrt as squareRoot } 19 | export { Mathematics as default, sqrt as squareRoot } 20 | 21 | export * from 'underscore' 22 | export { max, min } from 'underscore' 23 | -------------------------------------------------------------------------------- /documentation/examples/modulo.coffee: -------------------------------------------------------------------------------- 1 | -7 % 5 == -2 # The remainder of 7 / 5 2 | -7 %% 5 == 3 # n %% 5 is always between 0 and 4 3 | 4 | tabs.selectTabAtIndex((tabs.currentIndex - count) %% tabs.length) 5 | -------------------------------------------------------------------------------- /documentation/examples/multiple_return_values.coffee: -------------------------------------------------------------------------------- 1 | weatherReport = (location) -> 2 | # Make an Ajax request to fetch the weather... 3 | [location, 72, "Mostly Sunny"] 4 | 5 | [city, temp, forecast] = weatherReport "Berkeley, CA" 6 | -------------------------------------------------------------------------------- /documentation/examples/object_comprehensions.coffee: -------------------------------------------------------------------------------- 1 | yearsOld = max: 10, ida: 9, tim: 11 2 | 3 | ages = for child, age of yearsOld 4 | "#{child} is #{age}" 5 | -------------------------------------------------------------------------------- /documentation/examples/object_extraction.coffee: -------------------------------------------------------------------------------- 1 | futurists = 2 | sculptor: "Umberto Boccioni" 3 | painter: "Vladimir Burliuk" 4 | poet: 5 | name: "F.T. Marinetti" 6 | address: [ 7 | "Via Roma 42R" 8 | "Bellagio, Italy 22021" 9 | ] 10 | 11 | {sculptor} = futurists 12 | 13 | {poet: {name, address: [street, city]}} = futurists 14 | -------------------------------------------------------------------------------- /documentation/examples/object_spread.coffee: -------------------------------------------------------------------------------- 1 | user = 2 | name: 'Werner Heisenberg' 3 | occupation: 'theoretical physicist' 4 | 5 | currentUser = { user..., status: 'Uncertain' } 6 | -------------------------------------------------------------------------------- /documentation/examples/objects_and_arrays.coffee: -------------------------------------------------------------------------------- 1 | song = ["do", "re", "mi", "fa", "so"] 2 | 3 | singers = {Jagger: "Rock", Elvis: "Roll"} 4 | 5 | bitlist = [ 6 | 1, 0, 1 7 | 0, 0, 1 8 | 1, 1, 0 9 | ] 10 | 11 | kids = 12 | brother: 13 | name: "Max" 14 | age: 11 15 | sister: 16 | name: "Ida" 17 | age: 9 18 | -------------------------------------------------------------------------------- /documentation/examples/objects_reserved.coffee: -------------------------------------------------------------------------------- 1 | $('.account').prop class: 'active' 2 | 3 | log object.class 4 | -------------------------------------------------------------------------------- /documentation/examples/objects_shorthand.coffee: -------------------------------------------------------------------------------- 1 | name = "Michelangelo" 2 | mask = "orange" 3 | weapon = "nunchuks" 4 | turtle = {name, mask, weapon} 5 | output = "#{turtle.name} wears an #{turtle.mask} mask. Watch out for his #{turtle.weapon}!" 6 | -------------------------------------------------------------------------------- /documentation/examples/overview.coffee: -------------------------------------------------------------------------------- 1 | # Assignment: 2 | number = 42 3 | opposite = true 4 | 5 | # Conditions: 6 | number = -42 if opposite 7 | 8 | # Functions: 9 | square = (x) -> x * x 10 | 11 | # Arrays: 12 | list = [1, 2, 3, 4, 5] 13 | 14 | # Objects: 15 | math = 16 | root: Math.sqrt 17 | square: square 18 | cube: (x) -> x * square x 19 | 20 | # Splats: 21 | race = (winner, runners...) -> 22 | print winner, runners 23 | 24 | # Existence: 25 | alert "I knew it!" if elvis? 26 | 27 | # Array comprehensions: 28 | cubes = (math.cube num for num in list) 29 | -------------------------------------------------------------------------------- /documentation/examples/parallel_assignment.coffee: -------------------------------------------------------------------------------- 1 | theBait = 1000 2 | theSwitch = 0 3 | 4 | [theBait, theSwitch] = [theSwitch, theBait] 5 | -------------------------------------------------------------------------------- /documentation/examples/patterns_and_splats.coffee: -------------------------------------------------------------------------------- 1 | tag = "" 2 | 3 | [open, contents..., close] = tag.split("") 4 | -------------------------------------------------------------------------------- /documentation/examples/prototypes.coffee: -------------------------------------------------------------------------------- 1 | String::dasherize = -> 2 | this.replace /_/g, "-" 3 | -------------------------------------------------------------------------------- /documentation/examples/range_comprehensions.coffee: -------------------------------------------------------------------------------- 1 | countdown = (num for num in [10..1]) 2 | -------------------------------------------------------------------------------- /documentation/examples/scope.coffee: -------------------------------------------------------------------------------- 1 | outer = 1 2 | changeNumbers = -> 3 | inner = -1 4 | outer = 10 5 | inner = changeNumbers() 6 | -------------------------------------------------------------------------------- /documentation/examples/slices.coffee: -------------------------------------------------------------------------------- 1 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] 2 | 3 | start = numbers[0..2] 4 | 5 | middle = numbers[3...-2] 6 | 7 | end = numbers[-2..] 8 | 9 | copy = numbers[..] 10 | -------------------------------------------------------------------------------- /documentation/examples/soaks.coffee: -------------------------------------------------------------------------------- 1 | zip = lottery.drawWinner?().address?.zipcode 2 | -------------------------------------------------------------------------------- /documentation/examples/splats.coffee: -------------------------------------------------------------------------------- 1 | gold = silver = rest = "unknown" 2 | 3 | awardMedals = (first, second, others...) -> 4 | gold = first 5 | silver = second 6 | rest = others 7 | 8 | contenders = [ 9 | "Michael Phelps" 10 | "Liu Xiang" 11 | "Yao Ming" 12 | "Allyson Felix" 13 | "Shawn Johnson" 14 | "Roman Sebrle" 15 | "Guo Jingjing" 16 | "Tyson Gay" 17 | "Asafa Powell" 18 | "Usain Bolt" 19 | ] 20 | 21 | awardMedals contenders... 22 | 23 | alert """ 24 | Gold: #{gold} 25 | Silver: #{silver} 26 | The Field: #{rest.join ', '} 27 | """ 28 | -------------------------------------------------------------------------------- /documentation/examples/splices.coffee: -------------------------------------------------------------------------------- 1 | numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 2 | 3 | numbers[3..6] = [-3, -4, -5, -6] 4 | -------------------------------------------------------------------------------- /documentation/examples/static.coffee: -------------------------------------------------------------------------------- 1 | class Teenager 2 | @say: (speech) -> 3 | words = speech.split ' ' 4 | fillers = ['uh', 'um', 'like', 'actually', 'so', 'maybe'] 5 | output = [] 6 | for word, index in words 7 | output.push word 8 | output.push fillers[Math.floor(Math.random() * fillers.length)] unless index is words.length - 1 9 | output.join ', ' 10 | -------------------------------------------------------------------------------- /documentation/examples/strings.coffee: -------------------------------------------------------------------------------- 1 | mobyDick = "Call me Ishmael. Some years ago -- 2 | never mind how long precisely -- having little 3 | or no money in my purse, and nothing particular 4 | to interest me on shore, I thought I would sail 5 | about a little and see the watery part of the 6 | world..." 7 | -------------------------------------------------------------------------------- /documentation/examples/switch.coffee: -------------------------------------------------------------------------------- 1 | switch day 2 | when "Mon" then go work 3 | when "Tue" then go relax 4 | when "Thu" then go iceFishing 5 | when "Fri", "Sat" 6 | if day is bingoDay 7 | go bingo 8 | go dancing 9 | when "Sun" then go church 10 | else go work 11 | -------------------------------------------------------------------------------- /documentation/examples/switch_with_no_expression.coffee: -------------------------------------------------------------------------------- 1 | score = 76 2 | grade = switch 3 | when score < 60 then 'F' 4 | when score < 70 then 'D' 5 | when score < 80 then 'C' 6 | when score < 90 then 'B' 7 | else 'A' 8 | # grade == 'C' 9 | -------------------------------------------------------------------------------- /documentation/examples/tagged_template_literals.coffee: -------------------------------------------------------------------------------- 1 | upperCaseExpr = (textParts, expressions...) -> 2 | textParts.reduce (text, textPart, i) -> 3 | text + expressions[i - 1].toUpperCase() + textPart 4 | 5 | greet = (name, adjective) -> 6 | upperCaseExpr""" 7 | Hi #{name}. You look #{adjective}! 8 | """ 9 | -------------------------------------------------------------------------------- /documentation/examples/try.coffee: -------------------------------------------------------------------------------- 1 | try 2 | allHellBreaksLoose() 3 | catsAndDogsLivingTogether() 4 | catch error 5 | print error 6 | finally 7 | cleanUp() 8 | -------------------------------------------------------------------------------- /documentation/examples/type_annotations.coffee: -------------------------------------------------------------------------------- 1 | # @flow 2 | 3 | fn = (str ###: string ###, num ###: number ###) ###: string ### -> 4 | str + num 5 | -------------------------------------------------------------------------------- /documentation/examples/while.coffee: -------------------------------------------------------------------------------- 1 | # Econ 101 2 | if this.studyingEconomics 3 | buy() while supply > demand 4 | sell() until supply > demand 5 | 6 | # Nursery Rhyme 7 | num = 6 8 | lyrics = while num -= 1 9 | "#{num} little monkeys, jumping on the bed. 10 | One fell out and bumped his head." 11 | -------------------------------------------------------------------------------- /documentation/images/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | CoffeeScript Logo 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /documentation/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CoffeeScript 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | <%= include('styles.html') %> 17 | 18 | 19 | 20 | <%= include('body.html') %> 21 | 22 | <%= include('scripts.html') %> 23 | 24 | 25 | -------------------------------------------------------------------------------- /documentation/index_jsxy.html: -------------------------------------------------------------------------------- 1 | <%= include('styles.html') %> 2 | <%= include('body_jsxy.html') %> 3 | -------------------------------------------------------------------------------- /documentation/jsxy/code.coffee: -------------------------------------------------------------------------------- 1 | fs = require 'fs' 2 | CoffeeScript = require '../../lib/coffeescript' 3 | 4 | # https://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript 5 | escapeHtml = (unsafe) -> 6 | unsafe 7 | .replace /&/g, "&" 8 | .replace //g, ">" 10 | .replace /"/g, """ 11 | .replace /'/g, "'" 12 | 13 | module.exports = -> 14 | counter = 0 15 | # hljs = require 'highlight.js' 16 | # hljs.configure classPrefix: '' 17 | (file, executable = no, showLoad = yes) -> 18 | counter++ 19 | cs = fs.readFileSync "documentation/examples/#{file}.coffee", 'utf-8' 20 | js = CoffeeScript.compile cs, bare: yes 21 | js = js.replace /^\/\/ generated.*?\n/i, '' 22 | 23 | # cshtml = "
#{hljs.highlight('coffeescript', cs).value}
" 24 | # jshtml = "
#{hljs.highlight('javascript', js).value}
" 25 | cshtml = "
#{escapeHtml cs}
" 26 | jshtml = "
#{escapeHtml js}
" 27 | # append = if executable is yes then '' else "alert(#{executable});".replace /"/g, '"' 28 | # if executable and executable isnt yes 29 | # cs.replace /(\S)\s*\Z/m, "$1\n\nalert #{executable}" 30 | # run = if executable is yes then 'run' else "run: #{executable}" 31 | # name = "example#{counter}" 32 | # script = "" 33 | # load = if showLoad then "
load
" else '' 34 | # button = if executable then """
#{run}
""" else '' 35 | "
#{cshtml}#{jshtml}
" 36 | -------------------------------------------------------------------------------- /documentation/sections/annotated_source.md: -------------------------------------------------------------------------------- 1 | ## Annotated Source 2 | 3 | You can browse the CoffeeScript <%= fullVersion %> source in readable, annotated form [here](http://coffeescript.org/v<%= majorVersion %>/annotated-source/). You can also jump directly to a particular source file: 4 | 5 | - [Grammar Rules — src/grammar](http://coffeescript.org/v<%= majorVersion %>/annotated-source/grammar.html) 6 | - [Lexing Tokens — src/lexer](http://coffeescript.org/v<%= majorVersion %>/annotated-source/lexer.html) 7 | - [The Rewriter — src/rewriter](http://coffeescript.org/v<%= majorVersion %>/annotated-source/rewriter.html) 8 | - [The Syntax Tree — src/nodes](http://coffeescript.org/v<%= majorVersion %>/annotated-source/nodes.html) 9 | - [Lexical Scope — src/scope](http://coffeescript.org/v<%= majorVersion %>/annotated-source/scope.html) 10 | - [Helpers & Utility Functions — src/helpers](http://coffeescript.org/v<%= majorVersion %>/annotated-source/helpers.html) 11 | - [The CoffeeScript Module — src/coffeescript](http://coffeescript.org/v<%= majorVersion %>/annotated-source/coffeescript.html) 12 | - [Cake & Cakefiles — src/cake](http://coffeescript.org/v<%= majorVersion %>/annotated-source/cake.html) 13 | - [“coffee” Command-Line Utility — src/command](http://coffeescript.org/v<%= majorVersion %>/annotated-source/command.html) 14 | - [Option Parsing — src/optparse](http://coffeescript.org/v<%= majorVersion %>/annotated-source/optparse.html) 15 | - [Interactive REPL — src/repl](http://coffeescript.org/v<%= majorVersion %>/annotated-source/repl.html) 16 | - [Source Maps — src/sourcemap](http://coffeescript.org/v<%= majorVersion %>/annotated-source/sourcemap.html) 17 | -------------------------------------------------------------------------------- /documentation/sections/async_functions.md: -------------------------------------------------------------------------------- 1 | ## Async Functions 2 | 3 | ES2017’s [async functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) are supported through the `await` keyword. Like with generators, there's no need for an `async` keyword; an async function in CoffeeScript is simply a function that awaits. 4 | 5 | Similar to how `yield return` forces a generator, `await return` may be used to force a function to be async. 6 | 7 | ``` 8 | codeFor('async', true) 9 | ``` 10 | -------------------------------------------------------------------------------- /documentation/sections/books.md: -------------------------------------------------------------------------------- 1 | ## Books 2 | 3 | There are a number of excellent resources to help you get started with CoffeeScript, some of which are freely available online. 4 | 5 | * [The Little Book on CoffeeScript](http://arcturo.github.io/library/coffeescript/) is a brief 5-chapter introduction to CoffeeScript, written with great clarity and precision by [Alex MacCaw](http://alexmaccaw.co.uk/). 6 | * [Smooth CoffeeScript](http://autotelicum.github.io/Smooth-CoffeeScript/) is a reimagination of the excellent book [Eloquent JavaScript](http://eloquentjavascript.net/), as if it had been written in CoffeeScript instead. Covers language features as well as the functional and object oriented programming styles. By [E. Hoigaard](https://github.com/autotelicum). 7 | * [CoffeeScript: Accelerated JavaScript Development](http://pragprog.com/book/tbcoffee/coffeescript) is [Trevor Burnham](http://trevorburnham.com/)’s thorough introduction to the language. By the end of the book, you’ll have built a fast-paced multiplayer word game, writing both the client-side and Node.js portions in CoffeeScript. 8 | * [CoffeeScript Programming with jQuery, Rails, and Node.js](https://www.packtpub.com/web-development/coffeescript-programming-jquery-rails-and-nodejs) is a new book by Michael Erasmus that covers CoffeeScript with an eye towards real-world usage both in the browser (jQuery) and on the server-side (Rails, Node). 9 | * [CoffeeScript Ristretto](https://leanpub.com/coffeescript-ristretto/read) is a deep dive into CoffeeScript’s semantics from simple functions up through closures, higher-order functions, objects, classes, combinators, and decorators. By [Reg Braithwaite](http://braythwayt.com/). 10 | * [Testing with CoffeeScript](https://efendibooks.com/minibooks/testing-with-coffeescript) is a succinct and freely downloadable guide to building testable applications with CoffeeScript and Jasmine. 11 | * [CoffeeScript Application Development](https://www.packtpub.com/web-development/coffeescript-application-development) from Packt, introduces CoffeeScript while walking through the process of building a demonstration web application. A [CoffeeScript Application Development Coookbook](https://www.packtpub.com/web-development/coffeescript-application-development-cookbook) with over 90 “recipes” is also available. 12 | * [CoffeeScript in Action](https://www.manning.com/books/coffeescript-in-action) from Manning Publications, covers CoffeeScript syntax, composition techniques and application development. 13 | * [CoffeeScript: Die Alternative zu JavaScript](https://www.dpunkt.de/buecher/4021/coffeescript.html) from dpunkt.verlag, is the first CoffeeScript book in Deutsch. 14 | -------------------------------------------------------------------------------- /documentation/sections/breaking_change_fat_arrow.md: -------------------------------------------------------------------------------- 1 | ### Bound (fat arrow) functions 2 | 3 | In CoffeeScript 1.x, `=>` compiled to a regular `function` but with references to `this`/`@` rewritten to use the outer scope’s `this`, or with the inner function bound to the outer scope via `.bind` (hence the name “bound function”). In CoffeeScript 2, `=>` compiles to [ES2015’s `=>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), which behaves slightly differently. The largest difference is that in ES2015, `=>` functions lack an `arguments` object: 4 | 5 | ``` 6 | codeFor('breaking_change_fat_arrow', 'outer(1, 2)') 7 | ``` -------------------------------------------------------------------------------- /documentation/sections/breaking_changes.md: -------------------------------------------------------------------------------- 1 | ## Breaking Changes From CoffeeScript 1.x to 2 2 | 3 | CoffeeScript 2 aims to output as much idiomatic ES2015+ syntax as possible with as few breaking changes from CoffeeScript 1.x as possible. Some breaking changes, unfortunately, were unavoidable. 4 | -------------------------------------------------------------------------------- /documentation/sections/breaking_changes_argument_parsing_and_shebang_lines.md: -------------------------------------------------------------------------------- 1 | ### Argument parsing and shebang (`#!`) lines 2 | 3 | In CoffeeScript 1.x, `--` was required after the path and filename of the script to be run, but before any arguments passed to that script. This convention is now deprecated. So instead of: 4 | 5 | ```bash 6 | coffee [options] path/to/script.coffee -- [args] 7 | ``` 8 | 9 | Now you would just type: 10 | 11 | ```bash 12 | coffee [options] path/to/script.coffee [args] 13 | ``` 14 | 15 | The deprecated version will still work, but it will print a warning before running the script. 16 | 17 | On non-Windows platforms, a `.coffee` file can be made executable by adding a shebang (`#!`) line at the top of the file and marking the file as executable. For example: 18 | 19 | ```coffee 20 | #!/usr/bin/env coffee 21 | 22 | x = 2 + 2 23 | console.log x 24 | ``` 25 | 26 | If this were saved as `executable.coffee`, it could be made executable and run: 27 | 28 | ```bash 29 | ▶ chmod +x ./executable.coffee 30 | ▶ ./executable.coffee 31 | 4 32 | ``` 33 | 34 | In CoffeeScript 1.x, this used to fail when trying to pass arguments to the script. Some users on OS X worked around the problem by using `#!/usr/bin/env coffee --` as the first line of the file. That didn’t work on Linux, however, which cannot parse shebang lines with more than a single argument. While such scripts will still run on OS X, CoffeeScript will now display a warning before compiling or evaluating files that begin with a too-long shebang line. Now that CoffeeScript 2 supports passing arguments without needing `--`, we recommend simply changing the shebang lines in such scripts to just `#!/usr/bin/env coffee`. -------------------------------------------------------------------------------- /documentation/sections/breaking_changes_bound_generator_functions.md: -------------------------------------------------------------------------------- 1 | ### Bound generator functions 2 | 3 | Bound generator functions, a.k.a. generator arrow functions, [aren’t allowed in ECMAScript](http://stackoverflow.com/questions/27661306/can-i-use-es6s-arrow-function-syntax-with-generators-arrow-notation). You can write `function*` or `=>`, but not both. Therefore, CoffeeScript code like this: 4 | 5 | ```coffee 6 | f = => yield this 7 | # Throws a compiler error 8 | ``` 9 | 10 | Needs to be rewritten the old-fashioned way: 11 | 12 | ``` 13 | codeFor('breaking_change_bound_generator_function') 14 | ``` 15 | -------------------------------------------------------------------------------- /documentation/sections/breaking_changes_classes.md: -------------------------------------------------------------------------------- 1 | ### Classes are compiled to ES2015 classes 2 | 3 | ES2015 classes and their methods have some restrictions beyond those on regular functions. 4 | 5 | Class constructors can’t be invoked without `new`: 6 | 7 | ```coffee 8 | (class)() 9 | # Throws a TypeError at runtime 10 | ``` 11 | 12 | Derived (extended) class `constructor`s cannot use `this` before calling `super`: 13 | 14 | ```coffee 15 | class B extends A 16 | constructor: -> this # Throws a compiler error 17 | ``` 18 | 19 | ES2015 classes don’t allow bound (fat arrow) methods. The CoffeeScript compiler goes through some contortions to preserve support for them, but one thing that can’t be accommodated is calling a bound method before it is bound: 20 | 21 | ```coffee 22 | class Base 23 | constructor: -> 24 | @onClick() # This works 25 | clickHandler = @onClick 26 | clickHandler() # This throws a runtime error 27 | 28 | class Component extends Base 29 | onClick: => 30 | console.log 'Clicked!', @ 31 | ``` 32 | 33 | Class methods can’t be used with `new` (uncommon): 34 | 35 | ```coffee 36 | class Namespace 37 | @Klass = -> 38 | new Namespace.Klass # Throws a TypeError at runtime 39 | ``` 40 | 41 | Due to the hoisting required to compile to ES2015 classes, dynamic keys in class methods can’t use values from the executable class body unless the methods are assigned in prototype style. 42 | 43 | ```coffee 44 | class A 45 | name = 'method' 46 | "#{name}": -> # This method will be named 'undefined' 47 | @::[name] = -> # This will work; assigns to `A.prototype.method` 48 | ``` 49 | -------------------------------------------------------------------------------- /documentation/sections/breaking_changes_default_values.md: -------------------------------------------------------------------------------- 1 | ### Default values for function parameters and destructured elements 2 | 3 | Per the [ES2015 spec regarding function default parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters) and [destructuring default values](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Default_values), default values are only applied when a value is missing or `undefined`. In CoffeeScript 1.x, the default value would be applied in those cases but also if the value was `null`. 4 | 5 | ``` 6 | codeFor('breaking_change_function_parameter_default_values', 'f(null)') 7 | ``` 8 | 9 | ``` 10 | codeFor('breaking_change_destructuring_default_values', 'a') 11 | ``` 12 | -------------------------------------------------------------------------------- /documentation/sections/breaking_changes_jsx_and_the_less_than_and_greater_than_operators.md: -------------------------------------------------------------------------------- 1 | ### JSX and the `<` and `>` operators 2 | 3 | With the addition of [JSX](#jsx), the `<` and `>` characters serve as both the “less than” and “greater than” operators and as the delimiters for XML tags, like `
`. For best results, in general you should always wrap the operators in spaces to distinguish them from XML tags: `i < len`, not `i super 8 | # Throws a compiler error 9 | ``` 10 | 11 | Arguments can be forwarded explicitly using splats: 12 | 13 | ``` 14 | codeFor('breaking_change_super_with_arguments') 15 | ``` 16 | 17 | Or if you know that the parent function doesn’t require arguments, just call `super()`: 18 | 19 | ``` 20 | codeFor('breaking_change_super_without_arguments') 21 | ``` 22 | 23 | CoffeeScript 1.x allowed the `extends` keyword to set up prototypal inheritance between functions, and `super` could be used manually prototype-assigned functions: 24 | 25 | ```coffee 26 | A = -> 27 | B = -> 28 | B extends A 29 | B.prototype.foo = -> super arguments... 30 | # Last two lines each throw compiler errors in CoffeeScript 2 31 | ``` 32 | 33 | Due to the switch to ES2015 `extends` and `super`, using these keywords for prototypal functions are no longer supported. The above case could be refactored to: 34 | 35 | ``` 36 | codeFor('breaking_change_super_in_non-class_methods_refactor_with_apply') 37 | ``` 38 | 39 | or 40 | 41 | ``` 42 | codeFor('breaking_change_super_in_non-class_methods_refactor_with_class') 43 | ``` 44 | -------------------------------------------------------------------------------- /documentation/sections/cake.md: -------------------------------------------------------------------------------- 1 | ## Cake, and Cakefiles 2 | 3 | CoffeeScript includes a (very) simple build system similar to [Make](http://www.gnu.org/software/make/) and [Rake](http://rake.rubyforge.org/). Naturally, it’s called Cake, and is used for the tasks that build and test the CoffeeScript language itself. Tasks are defined in a file named `Cakefile`, and can be invoked by running `cake [task]` from within the directory. To print a list of all the tasks and options, just type `cake`. 4 | 5 | Task definitions are written in CoffeeScript, so you can put arbitrary code in your Cakefile. Define a task with a name, a long description, and the function to invoke when the task is run. If your task takes a command-line option, you can define the option with short and long flags, and it will be made available in the `options` object. Here’s a task that uses the Node.js API to rebuild CoffeeScript’s parser: 6 | 7 | ``` 8 | codeFor('cake_tasks') 9 | ``` 10 | 11 | If you need to invoke one task before another — for example, running `build` before `test`, you can use the `invoke` function: `invoke 'build'`. Cake tasks are a minimal way to expose your CoffeeScript functions to the command line, so [don’t expect any fanciness built-in](/v<%= majorVersion %>/annotated-source/cake.html). If you need dependencies, or async callbacks, it’s best to put them in your code itself — not the cake task. 12 | -------------------------------------------------------------------------------- /documentation/sections/chaining.md: -------------------------------------------------------------------------------- 1 | ## Chaining Function Calls 2 | 3 | Leading `.` closes all open calls, allowing for simpler chaining syntax. 4 | 5 | ``` 6 | codeFor('chaining') 7 | ``` 8 | -------------------------------------------------------------------------------- /documentation/sections/chat.md: -------------------------------------------------------------------------------- 1 | ## Web Chat (IRC) 2 | 3 | Quick help and advice can often be found in the CoffeeScript IRC room `#coffeescript` on `irc.freenode.net`, which you can [join via your web browser](http://webchat.freenode.net/?channels=coffeescript). 4 | -------------------------------------------------------------------------------- /documentation/sections/classes.md: -------------------------------------------------------------------------------- 1 | ## Classes 2 | 3 | CoffeeScript 1 provided the `class` and `extends` keywords as syntactic sugar for working with prototypal functions. With ES2015, JavaScript has adopted those keywords; so CoffeeScript 2 compiles its `class` and `extends` keywords to ES2015 classes. 4 | 5 | ``` 6 | codeFor('classes', true) 7 | ``` 8 | 9 | Static methods can be defined using `@` before the method name: 10 | 11 | ``` 12 | codeFor('static', 'Teenager.say("Are we there yet?")') 13 | ``` 14 | 15 | Finally, class definitions are blocks of executable code, which make for interesting metaprogramming possibilities. In the context of a class definition, `this` is the class object itself; therefore, you can assign static properties by using `@property: value`. 16 | -------------------------------------------------------------------------------- /documentation/sections/coffeescript_2.md: -------------------------------------------------------------------------------- 1 | ## CoffeeScript 2 2 | 3 | ### What’s New In CoffeeScript 2? 4 | 5 | The biggest change in CoffeeScript 2 is that now the CoffeeScript compiler produces modern, ES2015+ JavaScript. A CoffeeScript `=>` becomes an ES `=>`, a CoffeeScript `class` becomes an ES `class` and so on. With the exception of [modules](#modules) (`import` and `export` statements) and [JSX](#jsx), all the ES2015+ features that CoffeeScript supports can run natively in Node 7.6+, meaning that Node can run CoffeeScript’s output without any further processing required. You can [run the tests in your browser](http://coffeescript.org/v<%= majorVersion %>/test.html) to see if your browser can do the same; Chrome has supported all features since version 55. 6 | 7 | Support for ES2015+ syntax is important to ensure compatibility with frameworks that assume ES2015. Now that CoffeeScript compiles classes to the ES `class` keyword, it’s possible to `extend` an ES class; that wasn’t possible in CoffeeScript 1. Parity in how language features work is also important on its own; CoffeeScript “is just JavaScript,” and so things like [function parameter default values](#breaking-changes-default-values) should behave the same in CoffeeScript as in JavaScript. 8 | 9 | Many ES2015+ features have been backported to CoffeeScript 1.11 and 1.12, including [modules](#modules), [`for…of`](#generator-iteration), and [tagged template literals](#tagged-template-literals). Major new features unique to CoffeeScript 2 are support for ES2017’s [async functions](#async-functions) and for [JSX](#jsx). More details are in the [changelog](#changelog). 10 | 11 | There are very few [breaking changes from CoffeeScript 1.x to 2](#breaking-changes); we hope the upgrade process is smooth for most projects. 12 | 13 | ### Why CoffeeScript When There’s ES2015? 14 | 15 | CoffeeScript introduced many new features to the JavaScript world, such as [`=>`](#fat-arrow) and [destructuring](#destructuring) and [classes](#classes). We are happy that ECMA has seen their utility and adopted them into ECMAScript. 16 | 17 | CoffeeScript’s intent, however, was never to be a superset of JavaScript. One of the guiding principles of CoffeeScript has been _simplicity:_ not just removing JavaScript’s “bad parts,” but providing an elegant, concise syntax that eschews unnecessary punctuation whenever possible, to make code easier to read and reason about. This benefit of CoffeeScript remains, even in an ES2015 world. 18 | -------------------------------------------------------------------------------- /documentation/sections/command_line_interface.md: -------------------------------------------------------------------------------- 1 | ### Command Line 2 | 3 | Once installed, you should have access to the `coffee` command, which can execute scripts, compile `.coffee` files into `.js`, and provide an interactive REPL. The `coffee` command takes the following options: 4 | 5 | | Option | Description | 6 | | --- | --- | 7 | | `-c, --compile` | Compile a `.coffee` script into a `.js` JavaScript file of the same name. | 8 | | `-m, --map` | Generate source maps alongside the compiled JavaScript files. Adds `sourceMappingURL` directives to the JavaScript as well. | 9 | | `-M, --inline-map` | Just like `--map`, but include the source map directly in the compiled JavaScript files, rather than in a separate file. | 10 | | `-i, --interactive` | Launch an interactive CoffeeScript session to try short snippets. Identical to calling `coffee` with no arguments. | 11 | | `-o, --output [DIR]` | Write out all compiled JavaScript files into the specified directory. Use in conjunction with `--compile` or `--watch`. | 12 | | `-w, --watch` | Watch files for changes, rerunning the specified command when any file is updated. | 13 | | `-p, --print` | Instead of writing out the JavaScript as a file, print it directly to **stdout**. | 14 | | `-s, --stdio` | Pipe in CoffeeScript to STDIN and get back JavaScript over STDOUT. Good for use with processes written in other languages. An example:
`cat src/cake.coffee | coffee -sc` | 15 | | `-l, --literate` | Parses the code as Literate CoffeeScript. You only need to specify this when passing in code directly over **stdio**, or using some sort of extension-less file name. | 16 | | `-e, --eval` | Compile and print a little snippet of CoffeeScript directly from the command line. For example:
`coffee -e "console.log num for num in [10..1]"` | 17 | | `-r, --require [MODULE]`  | `require()` the given module before starting the REPL or evaluating the code given with the `--eval` flag. | 18 | | `-b, --bare` | Compile the JavaScript without the [top-level function safety wrapper](#lexical-scope). | 19 | | `-t, --tokens` | Instead of parsing the CoffeeScript, just lex it, and print out the token stream. Used for debugging the compiler. | 20 | | `-n, --nodes` | Instead of compiling the CoffeeScript, just lex and parse it, and print out the parse tree. Used for debugging the compiler. | 21 | | `--nodejs` | The `node` executable has some useful options you can set, such as `--debug`, `--debug-brk`, `--max-stack-size`, and `--expose-gc`. Use this flag to forward options directly to Node.js. To pass multiple flags, use `--nodejs` multiple times. | 22 | | `--no-header` | Suppress the “Generated by CoffeeScript” header. | 23 | 24 | #### Examples: 25 | 26 | * Compile a directory tree of `.coffee` files in `src` into a parallel tree of `.js` files in `lib`:
27 | `coffee --compile --output lib/ src/` 28 | * Watch a file for changes, and recompile it every time the file is saved:
29 | `coffee --watch --compile experimental.coffee` 30 | * Concatenate a list of files into a single script:
31 | `coffee --join project.js --compile src/*.coffee` 32 | * Print out the compiled JS from a one-liner:
33 | `coffee -bpe "alert i for i in [0..10]"` 34 | * All together now, watch and recompile an entire project as you work on it:
35 | `coffee -o lib/ -cw src/` 36 | * Start the CoffeeScript REPL (`Ctrl-D` to exit, `Ctrl-V`for multi-line):
37 | `coffee` 38 | -------------------------------------------------------------------------------- /documentation/sections/comments.md: -------------------------------------------------------------------------------- 1 | ## Comments 2 | 3 | In CoffeeScript, comments are denoted by the `#` character to the end of a line, or from `###` to the next appearance of `###`. Comments are ignored by the compiler, though the compiler makes its best effort at reinserting your comments into the output JavaScript after compilation. 4 | 5 | ``` 6 | codeFor('comment') 7 | ``` 8 | 9 | Inline `###` comments make [type annotations](#type-annotations) possible. -------------------------------------------------------------------------------- /documentation/sections/comparisons.md: -------------------------------------------------------------------------------- 1 | ## Chained Comparisons 2 | 3 | CoffeeScript borrows [chained comparisons](https://docs.python.org/3/reference/expressions.html#not-in) from Python — making it easy to test if a value falls within a certain range. 4 | 5 | ``` 6 | codeFor('comparisons', 'healthy') 7 | ``` 8 | -------------------------------------------------------------------------------- /documentation/sections/conditionals.md: -------------------------------------------------------------------------------- 1 | ## If, Else, Unless, and Conditional Assignment 2 | 3 | `if`/`else` statements can be written without the use of parentheses and curly brackets. As with functions and other block expressions, multi-line conditionals are delimited by indentation. There’s also a handy postfix form, with the `if` or `unless` at the end. 4 | 5 | CoffeeScript can compile `if` statements into JavaScript expressions, using the ternary operator when possible, and closure wrapping otherwise. There is no explicit ternary statement in CoffeeScript — you simply use a regular `if` statement on a single line. 6 | 7 | ``` 8 | codeFor('conditionals') 9 | ``` 10 | -------------------------------------------------------------------------------- /documentation/sections/contributing.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | Contributions are welcome! Feel free to fork [the repo](http://github.com/jashkenas/coffeescript) and submit a pull request. 4 | 5 | [Some features of ECMAScript are intentionally unsupported](#unsupported). Please review both the open and closed [issues on GitHub](http://github.com/jashkenas/coffeescript/issues) to see if the feature you’re looking for has already been discussed. As a general rule, we don’t support ECMAScript syntax for features that aren’t yet finalized (at Stage 4 in the proposal approval process). 6 | 7 | For more resources on adding to CoffeeScript, please see [the Wiki](https://github.com/jashkenas/coffeescript/wiki/%5BHowto%5D-Hacking-on-the-CoffeeScript-Compiler), especially [How The Parser Works](https://github.com/jashkenas/coffeescript/wiki/%5BHowTo%5D-How-parsing-works). 8 | 9 | There are several things you can do to increase your odds of having your pull request accepted: 10 | 11 | * Create tests! Any pull request should probably include basic tests to verify you didn’t break anything, or future changes won’t break your code. 12 | * Follow the style of the rest of the CoffeeScript codebase. 13 | * Ensure any ECMAScript syntax is mature (at Stage 4), with no further potential changes. 14 | * Add only features that have broad utility, rather than a feature aimed at a specific use case or framework. 15 | 16 | Of course, it’s entirely possible that you have a great addition, but it doesn’t fit within these constraints. Feel free to roll your own solution; you will have [plenty of company](https://github.com/jashkenas/coffeescript/wiki/In-The-Wild). 17 | -------------------------------------------------------------------------------- /documentation/sections/destructuring.md: -------------------------------------------------------------------------------- 1 | ## Destructuring Assignment 2 | 3 | Just like JavaScript (since ES2015), CoffeeScript has destructuring assignment syntax. When you assign an array or object literal to a value, CoffeeScript breaks up and matches both sides against each other, assigning the values on the right to the variables on the left. In the simplest case, it can be used for parallel assignment: 4 | 5 | ``` 6 | codeFor('parallel_assignment', 'theBait') 7 | ``` 8 | 9 | But it’s also helpful for dealing with functions that return multiple values. 10 | 11 | ``` 12 | codeFor('multiple_return_values', 'forecast') 13 | ``` 14 | 15 | Destructuring assignment can be used with any depth of array and object nesting, to help pull out deeply nested properties. 16 | 17 | ``` 18 | codeFor('object_extraction', 'name + "-" + street') 19 | ``` 20 | 21 | Destructuring assignment can even be combined with splats. 22 | 23 | ``` 24 | codeFor('patterns_and_splats', 'contents.join("")') 25 | ``` 26 | 27 | Expansion can be used to retrieve elements from the end of an array without having to assign the rest of its values. It works in function parameter lists as well. 28 | 29 | ``` 30 | codeFor('expansion', 'first + " " + last') 31 | ``` 32 | 33 | Destructuring assignment is also useful when combined with class constructors to assign properties to your instance from an options object passed to the constructor. 34 | 35 | ``` 36 | codeFor('constructor_destructuring', 'tim.age + " " + tim.height') 37 | ``` 38 | 39 | The above example also demonstrates that if properties are missing in the destructured object or array, you can, just like in JavaScript, provide defaults. Note though that unlike with the existential operator, the default is only applied with the value is missing or `undefined`—[passing `null` will set a value of `null`](#breaking-changes-default-values), not the default. 40 | -------------------------------------------------------------------------------- /documentation/sections/embedded.md: -------------------------------------------------------------------------------- 1 | ## Embedded JavaScript 2 | 3 | Hopefully, you’ll never need to use it, but if you ever need to intersperse snippets of JavaScript within your CoffeeScript, you can use backticks to pass it straight through. 4 | 5 | ``` 6 | codeFor('embedded', 'hi()') 7 | ``` 8 | 9 | Escape backticks with backslashes: `` \`​`` becomes `` `​``. 10 | 11 | Escape backslashes before backticks with more backslashes: `` \\\`​`` becomes `` \`​``. 12 | 13 | ``` 14 | codeFor('embedded_escaped', 'markdown()') 15 | ``` 16 | 17 | You can also embed blocks of JavaScript using triple backticks. That’s easier than escaping backticks, if you need them inside your JavaScript block. 18 | 19 | ``` 20 | codeFor('embedded_block', 'time()') 21 | ``` 22 | -------------------------------------------------------------------------------- /documentation/sections/es2015plus_output.md: -------------------------------------------------------------------------------- 1 | ### ES2015+ Output 2 | 3 | CoffeeScript 2 outputs the latest ES2015+ syntax. If you’re looking for a single tool that takes CoffeeScript input and generates JavaScript output that runs in any JavaScript runtime, assuming you opt out of certain newer features, stick to [CoffeeScript 1.x](/v1/). CoffeeScript 2 [breaks compatibility](#breaking-changes) with certain CoffeeScript 1.x features in order to conform with the ES2015+ specifications, and generate more idiomatic output (a CoffeeScript `=>` becomes an ES `=>`; a CoffeeScript `class` becomes an ES `class`; and so on). 4 | 5 | Since the CoffeeScript 2 compiler outputs ES2015+ syntax, it is your responsibility to either ensure that your target JavaScript runtime(s) support all these features, or that you pass the output through another transpiler like [Babel](http://babeljs.io/), [Rollup](https://github.com/rollup/rollup) or [Traceur Compiler](https://github.com/google/traceur-compiler). In general, [CoffeeScript 2’s output is supported as is by Node.js 7.6+](http://node.green/), except for modules and JSX which require transpilation. 6 | 7 | There are many great task runners for setting up JavaScript build chains, such as [Gulp](http://gulpjs.com/), [Webpack](https://webpack.github.io/), [Grunt](https://gruntjs.com/) and [Broccoli](http://broccolijs.com/). If you’re looking for a very minimal solution to get started, you can use [babel-preset-env](https://babeljs.io/docs/plugins/preset-env/) and the command line: 8 | 9 | ```bash 10 | npm install --global coffeescript@next 11 | npm install --save-dev coffeescript@next babel-cli babel-preset-env 12 | coffee --print *.coffee | babel --presets env > app.js 13 | ``` 14 | 15 | Note that [babel-preset-env](https://babeljs.io/docs/plugins/preset-env/) doesn’t automatically supply polyfills for your code. CoffeeScript itself will output [`Array.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) if you use the `in` operator, or destructuring or spread/rest syntax; and [`Function.bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) if you use a bound (`=>`) method in a class. Both are supported in Internet Explorer 9+ and all more recent browsers, but you will need to supply polyfills if you need to support Internet Explorer 8 or below and are using features that would cause these methods to be output, or in your own code are using similarly modern methods. One option is [`babel-polyfill`](https://babeljs.io/docs/usage/polyfill/), though there are many [other](https://hackernoon.com/polyfills-everything-you-ever-wanted-to-know-or-maybe-a-bit-less-7c8de164e423) [strategies](https://philipwalton.com/articles/loading-polyfills-only-when-needed/). 16 | -------------------------------------------------------------------------------- /documentation/sections/examples.md: -------------------------------------------------------------------------------- 1 | ## Examples 2 | 3 | The [best list of open-source CoffeeScript examples](https://github.com/trending?l=coffeescript&since=monthly) can be found on GitHub. But just to throw out a few more: 4 | 5 | * **GitHub**’s [Hubot](https://hubot.github.com/), a friendly IRC robot that can perform any number of useful and useless tasks. 6 | * **sstephenson**’s [Pow](http://pow.cx/), a zero-configuration Rack server, with comprehensive annotated source. 7 | * **technoweenie**’s [Coffee-Resque](https://github.com/technoweenie/coffee-resque), a port of [Resque](https://github.com/defunkt/resque) for Node.js. 8 | * **stephank**’s [Orona](https://github.com/stephank/orona), a remake of the Bolo tank game for modern browsers. 9 | * **GitHub**’s [Atom](https://atom.io/), a hackable text editor built on web technologies. 10 | * **Basecamp**’s [Trix](https://trix-editor.org/), a rich text editor for web apps. 11 | -------------------------------------------------------------------------------- /documentation/sections/existential_operator.md: -------------------------------------------------------------------------------- 1 | ## The Existential Operator 2 | 3 | It’s a little difficult to check for the existence of a variable in JavaScript. `if (variable) …` comes close, but fails for zero, the empty string, and false (to name just the most common cases). CoffeeScript’s existential operator `?` returns true unless a variable is `null` or `undefined` or undeclared, which makes it analogous to Ruby’s `nil?`. 4 | 5 | It can also be used for safer conditional assignment than the JavaScript pattern `a = a || value` provides, for cases where you may be handling numbers or strings. 6 | 7 | ``` 8 | codeFor('existence', 'footprints') 9 | ``` 10 | 11 | Note that if the compiler knows that `a` is in scope and therefore declared, `a?` compiles to `a != null`, _not_ `a !== null`. The `!=` makes a loose comparison to `null`, which does double duty also comparing against `undefined`. The reverse also holds for `not a?` or `unless a?`. 12 | 13 | ``` 14 | codeFor('existence_declared') 15 | ``` 16 | 17 | If a variable might be undeclared, the compiler does a thorough check. This is what JavaScript coders _should_ be typing when they want to check if a mystery variable exists. 18 | 19 | ``` 20 | codeFor('existence_undeclared') 21 | ``` 22 | 23 | The accessor variant of the existential operator `?.` can be used to soak up null references in a chain of properties. Use it instead of the dot accessor `.` in cases where the base value may be `null` or `undefined`. If all of the properties exist then you’ll get the expected result, if the chain is broken, `undefined` is returned instead of the `TypeError` that would be raised otherwise. 24 | 25 | ``` 26 | codeFor('soaks') 27 | ``` 28 | 29 | For completeness: 30 | 31 | | Example | Definition | 32 | | --- | --- | 33 | | `a?` | tests that `a` is in scope and `a != null` | 34 | | `a ? b` | returns `a` if `a` is in scope and `a != null`; otherwise, `b` | 35 | | `a?.b` or `a?['b']` | returns `a.b` if `a` is in scope and `a != null`; otherwise, `undefined` | 36 | | `a?(b, c)` or `a? b, c`  | returns the result of calling `a` (with arguments `b` and `c`) if `a` is in scope and callable; otherwise, `undefined` | 37 | | `a ?= b` | assigns the value of `b` to `a` if `a` is not in scope or if `a == null`; produces the new value of `a` | -------------------------------------------------------------------------------- /documentation/sections/expressions.md: -------------------------------------------------------------------------------- 1 | ## Everything is an Expression (at least, as much as possible) 2 | 3 | You might have noticed how even though we don’t add return statements to CoffeeScript functions, they nonetheless return their final value. The CoffeeScript compiler tries to make sure that all statements in the language can be used as expressions. Watch how the `return` gets pushed down into each possible branch of execution in the function below. 4 | 5 | ``` 6 | codeFor('expressions', 'eldest') 7 | ``` 8 | 9 | Even though functions will always return their final value, it’s both possible and encouraged to return early from a function body writing out the explicit return (`return value`), when you know that you’re done. 10 | 11 | Because variable declarations occur at the top of scope, assignment can be used within expressions, even for variables that haven’t been seen before: 12 | 13 | ``` 14 | codeFor('expressions_assignment', 'six') 15 | ``` 16 | 17 | Things that would otherwise be statements in JavaScript, when used as part of an expression in CoffeeScript, are converted into expressions by wrapping them in a closure. This lets you do useful things, like assign the result of a comprehension to a variable: 18 | 19 | ``` 20 | codeFor('expressions_comprehension', 'globals') 21 | ``` 22 | 23 | As well as silly things, like passing a `try`/`catch` statement directly into a function call: 24 | 25 | ``` 26 | codeFor('expressions_try', true) 27 | ``` 28 | 29 | There are a handful of statements in JavaScript that can’t be meaningfully converted into expressions, namely `break`, `continue`, and `return`. If you make use of them within a block of code, CoffeeScript won’t try to perform the conversion. 30 | -------------------------------------------------------------------------------- /documentation/sections/fat_arrow.md: -------------------------------------------------------------------------------- 1 | ## Bound (Fat Arrow) Functions 2 | 3 | In JavaScript, the `this` keyword is dynamically scoped to mean the object that the current function is attached to. If you pass a function as a callback or attach it to a different object, the original value of `this` will be lost. If you’re not familiar with this behavior, [this Digital Web article](http://64.13.255.16/articles/scope_in_javascript/) gives a good overview of the quirks. 4 | 5 | The fat arrow `=>` can be used to both define a function, and to bind it to the current value of `this`, right on the spot. This is helpful when using callback-based libraries like Prototype or jQuery, for creating iterator functions to pass to `each`, or event-handler functions to use with `on`. Functions created with the fat arrow are able to access properties of the `this` where they’re defined. 6 | 7 | ``` 8 | codeFor('fat_arrow') 9 | ``` 10 | 11 | If we had used `->` in the callback above, `@customer` would have referred to the undefined “customer” property of the DOM element, and trying to call `purchase()` on it would have raised an exception. 12 | 13 | The fat arrow was one of the most popular features of CoffeeScript, and ES2015 [adopted it](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions); so CoffeeScript 2 compiles `=>` to ES `=>`. 14 | -------------------------------------------------------------------------------- /documentation/sections/functions.md: -------------------------------------------------------------------------------- 1 | ## Functions 2 | 3 | Functions are defined by an optional list of parameters in parentheses, an arrow, and the function body. The empty function looks like this: `->` 4 | 5 | ``` 6 | codeFor('functions', 'cube(5)') 7 | ``` 8 | 9 | Functions may also have default values for arguments, which will be used if the incoming argument is missing (`undefined`). 10 | 11 | ``` 12 | codeFor('default_args', 'fill("cup")') 13 | ``` 14 | -------------------------------------------------------------------------------- /documentation/sections/generators.md: -------------------------------------------------------------------------------- 1 | ## Generator Functions 2 | 3 | CoffeeScript supports ES2015 [generator functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*) through the `yield` keyword. There's no `function*(){}` nonsense — a generator in CoffeeScript is simply a function that yields. 4 | 5 | ``` 6 | codeFor('generators', 'ps.next().value') 7 | ``` 8 | 9 | `yield*` is called `yield from`, and `yield return` may be used if you need to force a generator that doesn’t yield. 10 | 11 |
12 | 13 | You can iterate over a generator function using `for…from`. 14 | 15 | ``` 16 | codeFor('generator_iteration', 'getFibonacciNumbers(10)') 17 | ``` 18 | -------------------------------------------------------------------------------- /documentation/sections/heregexes.md: -------------------------------------------------------------------------------- 1 | ## Block Regular Expressions 2 | 3 | Similar to block strings and comments, CoffeeScript supports block regexes — extended regular expressions that ignore internal whitespace and can contain comments and interpolation. Modeled after Perl’s `/x` modifier, CoffeeScript’s block regexes are delimited by `///` and go a long way towards making complex regular expressions readable. To quote from the CoffeeScript source: 4 | 5 | ``` 6 | codeFor('heregexes') 7 | ``` 8 | -------------------------------------------------------------------------------- /documentation/sections/installation.md: -------------------------------------------------------------------------------- 1 | ## Installation 2 | 3 | The command-line version of `coffee` is available as a [Node.js](https://nodejs.org/) utility. The [core compiler](/v<%= majorVersion %>/browser-compiler/coffeescript.js) however, does not depend on Node, and can be run in any JavaScript environment, or in the browser (see [Try CoffeeScript](#try)). 4 | 5 | To install, first make sure you have a working copy of the latest stable version of [Node.js](https://nodejs.org/). You can then install CoffeeScript globally with [npm](https://www.npmjs.com/): 6 | 7 | ```bash 8 | npm install --global coffeescript@next 9 | ``` 10 | 11 | This will make the `coffee` and `cake` commands available globally. 12 | 13 | When you need CoffeeScript as a dependency of a project, within that project’s folder you can install it locally: 14 | 15 | ```bash 16 | npm install --save coffeescript@next 17 | ``` 18 | 19 | The `coffee` and `cake` commands will first look in the current folder to see if CoffeeScript is installed locally, and use that version if so. This allows different versions of CoffeeScript to be installed globally and locally. 20 | -------------------------------------------------------------------------------- /documentation/sections/introduction.md: -------------------------------------------------------------------------------- 1 | **CoffeeScript is a little language that compiles into JavaScript.** Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way. 2 | 3 | The golden rule of CoffeeScript is: _“It’s just JavaScript.”_ The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable, pretty-printed, and tends to run as fast or faster than the equivalent handwritten JavaScript. 4 | 5 | **Latest Version:** [<%= fullVersion %>](https://github.com/jashkenas/coffeescript/tarball/<%= fullVersion %>) 6 | 7 | ```bash 8 | npm install -g coffeescript@next 9 | ``` 10 | -------------------------------------------------------------------------------- /documentation/sections/jsx.md: -------------------------------------------------------------------------------- 1 | ## JSX 2 | 3 | [JSX](https://facebook.github.io/react/docs/introducing-jsx.html) is JavaScript containing interspersed XML elements. While conceived for [React](https://facebook.github.io/react/), it is not specific to any particular library or framework. 4 | 5 | CoffeeScript supports interspersed XML elements, without the need for separate plugins or special settings. The XML elements will be compiled as such, outputting JSX that could be parsed like any normal JSX file, for example by [Babel with the React JSX transform](https://babeljs.io/docs/plugins/transform-react-jsx/). CoffeeScript does _not_ output `React.createElement` calls or any code specific to React or any other framework. It is up to you to attach another step in your build chain to convert this JSX to whatever function calls you wish the XML elements to compile to. 6 | 7 | Just like in JSX and HTML, denote XML tags using `<` and `>`. You can interpolate CoffeeScript code inside a tag using `{` and `}`. To avoid compiler errors, when using `<` and `>` to mean “less than” or “greater than,” you should wrap the operators in spaces to distinguish them from XML tags. So `i < len`, not `i▶ _button on the right. The CoffeeScript on the left is editable, and the JavaScript will update as you edit._ 6 | 7 | First, the basics: CoffeeScript uses significant whitespace to delimit blocks of code. You don’t need to use semicolons `;` to terminate expressions, ending the line will do just as well (although semicolons can still be used to fit multiple expressions onto a single line). Instead of using curly braces `{ }` to surround blocks of code in [functions](#literals), [if-statements](#conditionals), [switch](#switch), and [try/catch](#try), use indentation. 8 | 9 | You don’t need to use parentheses to invoke a function if you’re passing arguments. The implicit call wraps forward to the end of the line or block expression.
10 | `console.log sys.inspect object` → `console.log(sys.inspect(object));` 11 | -------------------------------------------------------------------------------- /documentation/sections/lexical_scope.md: -------------------------------------------------------------------------------- 1 | ## Lexical Scoping and Variable Safety 2 | 3 | The CoffeeScript compiler takes care to make sure that all of your variables are properly declared within lexical scope — you never need to write `var` yourself. 4 | 5 | ``` 6 | codeFor('scope', 'inner') 7 | ``` 8 | 9 | Notice how all of the variable declarations have been pushed up to the top of the closest scope, the first time they appear. `outer` is not redeclared within the inner function, because it’s already in scope; `inner` within the function, on the other hand, should not be able to change the value of the external variable of the same name, and therefore has a declaration of its own. 10 | 11 | Because you don’t have direct access to the `var` keyword, it’s impossible to shadow an outer variable on purpose, you may only refer to it. So be careful that you’re not reusing the name of an external variable accidentally, if you’re writing a deeply nested function. 12 | 13 | Although suppressed within this documentation for clarity, all CoffeeScript output (except in files with `import` or `export` statements) is wrapped in an anonymous function: `(function(){ … })();`. This safety wrapper, combined with the automatic generation of the `var` keyword, make it exceedingly difficult to pollute the global namespace by accident. (The safety wrapper can be disabled with the [`bare` option](#usage), and is unnecessary and automatically disabled when using modules.) 14 | 15 | If you’d like to create top-level variables for other scripts to use, attach them as properties on `window`; attach them as properties on the `exports` object in CommonJS; or use an [`export` statement](#modules). If you’re targeting both CommonJS and the browser, the [existential operator](#existential-operator) (covered below), gives you a reliable way to figure out where to add them: `exports ? this`. 16 | 17 | Since CoffeeScript takes care of all variable declaration, it is not possible to declare variables with ES2015’s `let` or `const`. [This is intentional](#unsupported-let-const); we feel that the simplicity gained by not having to think about variable declaration outweighs the benefit of having three separate ways to declare variables. -------------------------------------------------------------------------------- /documentation/sections/literate.md: -------------------------------------------------------------------------------- 1 | ## Literate CoffeeScript 2 | 3 | Besides being used as an ordinary programming language, CoffeeScript may also be written in “literate” mode. If you name your file with a `.litcoffee` extension, you can write it as a Markdown document — a document that also happens to be executable CoffeeScript code. The compiler will treat any indented blocks (Markdown’s way of indicating source code) as executable code, and ignore the rest as comments. Code blocks must also be separated from comments by at least one blank line. 4 | 5 | Just for kicks, a little bit of the compiler is currently implemented in this fashion: See it [as a document](https://gist.github.com/jashkenas/3fc3c1a8b1009c00d9df), [raw](https://raw.githubusercontent.com/jashkenas/coffeescript/master/src/scope.litcoffee), and [properly highlighted in a text editor](http://cl.ly/LxEu). 6 | 7 | A few caveats: 8 | 9 | * Code blocks need to maintain consistent indentation relative to each other. When the compiler parses your Literate CoffeeScript file, it first discards all the non-code block lines and then parses the remainder as a regular CoffeeScript file. Therefore the code blocks need to be written as if the comment lines don’t exist, with consistent indentation (including whether they are indented with tabs or spaces). 10 | * Along those lines, code blocks within list items or blockquotes are not treated as executable code. Since list items and blockquotes imply their own indentation, it would be ambiguous how to treat indentation between successive code blocks when some are within these other blocks and some are not. 11 | * List items can be at most only one paragraph long. The second paragraph of a list item would be indented after a blank line, and therefore indistinguishable from a code block. 12 | -------------------------------------------------------------------------------- /documentation/sections/loops.md: -------------------------------------------------------------------------------- 1 | ## Loops and Comprehensions 2 | 3 | Most of the loops you’ll write in CoffeeScript will be **comprehensions** over arrays, objects, and ranges. Comprehensions replace (and compile into) `for` loops, with optional guard clauses and the value of the current array index. Unlike for loops, array comprehensions are expressions, and can be returned and assigned. 4 | 5 | ``` 6 | codeFor('array_comprehensions') 7 | ``` 8 | 9 | Comprehensions should be able to handle most places where you otherwise would use a loop, `each`/`forEach`, `map`, or `select`/`filter`, for example:
10 | `shortNames = (name for name in list when name.length < 5)`
11 | If you know the start and end of your loop, or would like to step through in fixed-size increments, you can use a range to specify the start and end of your comprehension. 12 | 13 | ``` 14 | codeFor('range_comprehensions', 'countdown') 15 | ``` 16 | 17 | Note how because we are assigning the value of the comprehensions to a variable in the example above, CoffeeScript is collecting the result of each iteration into an array. Sometimes functions end with loops that are intended to run only for their side-effects. Be careful that you’re not accidentally returning the results of the comprehension in these cases, by adding a meaningful return value — like `true` — or `null`, to the bottom of your function. 18 | 19 | To step through a range comprehension in fixed-size chunks, use `by`, for example: 20 | `evens = (x for x in [0..10] by 2)` 21 | 22 | If you don’t need the current iteration value you may omit it: 23 | `browser.closeCurrentTab() for [0...count]` 24 | 25 | Comprehensions can also be used to iterate over the keys and values in an object. Use `of` to signal comprehension over the properties of an object instead of the values in an array. 26 | 27 | ``` 28 | codeFor('object_comprehensions', 'ages.join(", ")') 29 | ``` 30 | 31 | If you would like to iterate over just the keys that are defined on the object itself, by adding a `hasOwnProperty` check to avoid properties that may be inherited from the prototype, use `for own key, value of object`. 32 | 33 | To iterate a generator function, use `from`. See [Generator Functions](#generator-iteration). 34 | 35 | The only low-level loop that CoffeeScript provides is the `while` loop. The main difference from JavaScript is that the `while` loop can be used as an expression, returning an array containing the result of each iteration through the loop. 36 | 37 | ``` 38 | codeFor('while', 'lyrics.join("\\n")') 39 | ``` 40 | 41 | For readability, the `until` keyword is equivalent to `while not`, and the `loop` keyword is equivalent to `while true`. 42 | 43 | When using a JavaScript loop to generate functions, it’s common to insert a closure wrapper in order to ensure that loop variables are closed over, and all the generated functions don’t just share the final values. CoffeeScript provides the `do` keyword, which immediately invokes a passed function, forwarding any arguments. 44 | 45 | ``` 46 | codeFor('do') 47 | ``` 48 | -------------------------------------------------------------------------------- /documentation/sections/modules.md: -------------------------------------------------------------------------------- 1 | ## Modules 2 | 3 | ES2015 modules are supported in CoffeeScript, with very similar `import` and `export` syntax: 4 | 5 | ``` 6 | codeFor('modules') 7 | ``` 8 | 9 |
10 | 11 | Note that the CoffeeScript compiler **does not resolve modules**; writing an `import` or `export` statement in CoffeeScript will produce an `import` or `export` statement in the resulting output. It is your responsibility attach another transpiler, such as [Traceur Compiler](https://github.com/google/traceur-compiler), [Babel](http://babeljs.io/) or [Rollup](https://github.com/rollup/rollup), to convert this ES2015 syntax into code that will work in your target runtimes. 12 | 13 | Also note that any file with an `import` or `export` statement will be output without a [top-level function safety wrapper](#lexical-scope); in other words, importing or exporting modules will automatically trigger [bare](#usage) mode for that file. This is because per the ES2015 spec, `import` or `export` statements must occur at the topmost scope. 14 | -------------------------------------------------------------------------------- /documentation/sections/nodejs_usage.md: -------------------------------------------------------------------------------- 1 | ### Node.js 2 | 3 | If you’d like to use Node.js’ CommonJS to `require` CoffeeScript files, e.g. `require './app.coffee'`, you must first “register” CoffeeScript as an extension: 4 | 5 | ```coffee 6 | require 'coffeescript/register' 7 | 8 | App = require './app' # The .coffee extension is optional 9 | ``` 10 | 11 | If you want to use the compiler’s API, for example to make an app that compiles strings of CoffeeScript on the fly, you can `require` the full module: 12 | 13 | ```coffee 14 | CoffeeScript = require 'coffeescript' 15 | 16 | eval CoffeeScript.compile 'console.log "Mmmmm, I could really go for some #{Math.pi}"' 17 | ``` 18 | 19 | The `compile` method has the signature `compile(code, options)` where `code` is a string of CoffeeScript code, and the optional `options` is an object with some or all of the following properties: 20 | 21 | * `options.sourceMap`, boolean: if true, a source map will be generated; and instead of returning a string, `compile` will return an object of the form `{js, v3SourceMap, sourceMap}`. 22 | * `options.inlineMap`, boolean: if true, output the source map as a base64-encoded string in a comment at the bottom. 23 | * `options.filename`, string: the filename to use for the source map. It can include a path (relative or absolute). 24 | * `options.bare`, boolean: if true, output without the [top-level function safety wrapper](#lexical-scope). 25 | * `options.header`, boolean: if true, output the `Generated by CoffeeScript` header. 26 | -------------------------------------------------------------------------------- /documentation/sections/objects_and_arrays.md: -------------------------------------------------------------------------------- 1 | ## Objects and Arrays 2 | 3 | The CoffeeScript literals for objects and arrays look very similar to their JavaScript cousins. When each property is listed on its own line, the commas are optional. Objects may be created using indentation instead of explicit braces, similar to [YAML](http://yaml.org). 4 | 5 | ``` 6 | codeFor('objects_and_arrays', 'song.join(" … ")') 7 | ``` 8 | 9 | In JavaScript, you can’t use reserved words, like `class`, as properties of an object, without quoting them as strings. CoffeeScript notices reserved words used as keys in objects and quotes them for you, so you don’t have to worry about it (say, when using jQuery). 10 | 11 | ``` 12 | codeFor('objects_reserved') 13 | ``` 14 | 15 | CoffeeScript has a shortcut for creating objects when you want the key to be set with a variable of the same name. 16 | 17 | ``` 18 | codeFor('objects_shorthand') 19 | ``` 20 | -------------------------------------------------------------------------------- /documentation/sections/operators.md: -------------------------------------------------------------------------------- 1 | ## Operators and Aliases 2 | 3 | Because the `==` operator frequently causes undesirable coercion, is intransitive, and has a different meaning than in other languages, CoffeeScript compiles `==` into `===`, and `!=` into `!==`. In addition, `is` compiles into `===`, and `isnt` into `!==`. 4 | 5 | You can use `not` as an alias for `!`. 6 | 7 | For logic, `and` compiles to `&&`, and `or` into `||`. 8 | 9 | Instead of a newline or semicolon, `then` can be used to separate conditions from expressions, in `while`, `if`/`else`, and `switch`/`when` statements. 10 | 11 | As in [YAML](http://yaml.org/), `on` and `yes` are the same as boolean `true`, while `off` and `no` are boolean `false`. 12 | 13 | `unless` can be used as the inverse of `if`. 14 | 15 | As a shortcut for `this.property`, you can use `@property`. 16 | 17 | You can use `in` to test for array presence, and `of` to test for JavaScript object-key presence. 18 | 19 | In a `for` loop, `from` compiles to the [ES2015 `of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of). (Yes, it’s unfortunate; the CoffeeScript `of` predates the ES2015 `of`.) 20 | 21 | To simplify math expressions, `**` can be used for exponentiation and `//` performs integer division. `%` works just like in JavaScript, while `%%` provides [“dividend dependent modulo”](https://en.wikipedia.org/wiki/Modulo_operation): 22 | 23 | ``` 24 | codeFor('modulo') 25 | ``` 26 | 27 | All together now: 28 | 29 | | CoffeeScript | JavaScript | 30 | | --- | --- | 31 | | `is` | `===` | 32 | | `isnt` | `!==` | 33 | | `not` | `!` | 34 | | `and` | `&&` | 35 | | `or` | `||` | 36 | | `true`, `yes`, `on` | `true` | 37 | | `false`, `no`, `off`  | `false` | 38 | | `@`, `this` | `this` | 39 | | `a in b` | `[].indexOf.call(b, a) >= 0` | 40 | | `a of b` | `a in b` | 41 | | `for a from b` | `for (a of b)` | 42 | | `a ** b` | `Math.pow(a, b)` | 43 | | `a // b` | `Math.floor(a / b)` | 44 | | `a %% b` | `(a % b + b) % b` | 45 | 46 | ``` 47 | codeFor('aliases') 48 | ``` 49 | -------------------------------------------------------------------------------- /documentation/sections/overview.md: -------------------------------------------------------------------------------- 1 | ## Overview 2 | 3 | _CoffeeScript on the topleft, compiled JavaScript output on the bottomright. The CoffeeScript is editable!_ 4 | 5 | ``` 6 | codeFor('overview', 'cubes', false) 7 | ``` 8 | -------------------------------------------------------------------------------- /documentation/sections/prototypal_inheritance.md: -------------------------------------------------------------------------------- 1 | ## Prototypal Inheritance 2 | 3 | In addition to supporting ES2015 classes, CoffeeScript provides a shortcut for working with prototypes. The `::` operator gives you quick access to an object’s prototype: 4 | 5 | ``` 6 | codeFor('prototypes', '"one_two".dasherize()') 7 | ``` 8 | -------------------------------------------------------------------------------- /documentation/sections/resources.md: -------------------------------------------------------------------------------- 1 | ## Resources 2 | 3 | * [CoffeeScript on GitHub](https://github.com/jashkenas/coffeescript/) 4 | * [CoffeeScript Issues](https://github.com/jashkenas/coffeescript/issues)
5 | Bug reports, feature proposals, and ideas for changes to the language belong here. 6 | * [CoffeeScript Google Group](https://groups.google.com/forum/#!forum/coffeescript)
7 | If you’d like to ask a question, the mailing list is a good place to get help. 8 | * [The CoffeeScript Wiki](https://github.com/jashkenas/coffeescript/wiki)
9 | If you’ve ever learned a neat CoffeeScript tip or trick, or ran into a gotcha — share it on the wiki. The wiki also serves as a directory of handy [text editor extensions](https://github.com/jashkenas/coffeescript/wiki/Text-editor-plugins), [web framework plugins](https://github.com/jashkenas/coffeescript/wiki/Web-framework-plugins), and general [CoffeeScript build tools](https://github.com/jashkenas/coffeescript/wiki/Build-tools). 10 | * [The FAQ](https://github.com/jashkenas/coffeescript/wiki/FAQ)
11 | Perhaps your CoffeeScript-related question has been asked before. Check the FAQ first. 12 | * [JS2Coffee](http://js2.coffee/)
13 | Is a very well done reverse JavaScript-to-CoffeeScript compiler. It’s not going to be perfect (infer what your JavaScript classes are, when you need bound functions, and so on…) — but it’s a great starting point for converting simple scripts. 14 | * [High-Rez Logo](https://github.com/jashkenas/coffeescript/tree/master/documentation/images)
15 | The CoffeeScript logo is available in SVG for use in presentations. 16 | -------------------------------------------------------------------------------- /documentation/sections/screencasts.md: -------------------------------------------------------------------------------- 1 | ## Screencasts 2 | 3 | * [A Sip of CoffeeScript](http://coffeescript.codeschool.com/) is a [Code School Course](https://www.codeschool.com) which combines 6 screencasts with in-browser coding to make learning fun. The first level is free to try out. 4 | * [Meet CoffeeScript](https://www.pluralsight.com/courses/meet-coffeescript) is a 75-minute long screencast by PeepCode, now [PluralSight](https://www.pluralsight.com/). Highly memorable for its animations which demonstrate transforming CoffeeScript into the equivalent JS. 5 | * If you’re looking for less of a time commitment, RailsCasts’ [CoffeeScript Basics](http://railscasts.com/episodes/267-coffeescript-basics) should have you covered, hitting all of the important notes about CoffeeScript in 11 minutes. 6 | -------------------------------------------------------------------------------- /documentation/sections/scripts.md: -------------------------------------------------------------------------------- 1 | ## `"text/coffeescript"` Script Tags 2 | 3 | While it’s not recommended for serious use, CoffeeScripts may be included directly within the browser using ` 7 | 8 | 32 | 33 | 34 | 35 |

CoffeeScript Test Suite

36 | 37 |

 38 | 
 39 | 
137 | 
138 | <%= tests %>
139 | 
140 | 
141 | 
142 | 


--------------------------------------------------------------------------------
/documentation/v1/body_jsxy.html:
--------------------------------------------------------------------------------
1 | 
2 | 3 | <%= htmlFor('jsxy') %> 4 |
5 | -------------------------------------------------------------------------------- /documentation/v1/code.coffee: -------------------------------------------------------------------------------- 1 | fs = require 'fs' 2 | CoffeeScript = require '../../lib/coffeescript' 3 | 4 | 5 | module.exports = -> 6 | counter = 0 7 | hljs = require 'highlight.js' 8 | hljs.configure classPrefix: '' 9 | (file, executable = no, showLoad = yes) -> 10 | counter++ 11 | cs = fs.readFileSync "documentation/examples/#{file}.coffee", 'utf-8' 12 | js = CoffeeScript.compile cs, bare: yes 13 | js = js.replace /^\/\/ generated.*?\n/i, '' 14 | 15 | cshtml = "
#{hljs.highlight('coffeescript', cs).value}
" 16 | jshtml = "
#{hljs.highlight('javascript', js).value}
" 17 | append = if executable is yes then '' else "alert(#{executable});".replace /"/g, '"' 18 | if executable and executable isnt yes 19 | cs.replace /(\S)\s*\Z/m, "$1\n\nalert #{executable}" 20 | run = if executable is yes then 'run' else "run: #{executable}" 21 | name = "example#{counter}" 22 | script = "" 23 | load = if showLoad then "
load
" else '' 24 | button = if executable then """
#{run}
""" else '' 25 | "
#{cshtml}#{jshtml}#{script}#{load}#{button}
" 26 | -------------------------------------------------------------------------------- /documentation/v1/docs.coffee: -------------------------------------------------------------------------------- 1 | sourceFragment = "try:" 2 | 3 | # Set up the compilation function, to run when you stop typing. 4 | compileSource = -> 5 | source = $('#repl_source').val() 6 | results = $('#repl_results') 7 | window.compiledJS = '' 8 | try 9 | window.compiledJS = CoffeeScript.compile source, bare: on 10 | el = results[0] 11 | if el.innerText 12 | el.innerText = window.compiledJS 13 | else 14 | results.text(window.compiledJS) 15 | results.removeClass 'error' 16 | $('.minibutton.run').removeClass 'error' 17 | catch {location, message} 18 | if location? 19 | message = "Error on line #{location.first_line + 1}: #{message}" 20 | results.text(message).addClass 'error' 21 | $('.minibutton.run').addClass 'error' 22 | 23 | # Update permalink 24 | $('#repl_permalink').attr 'href', "##{sourceFragment}#{encodeURIComponent source}" 25 | 26 | # Listen for keypresses and recompile. 27 | $('#repl_source').keyup -> compileSource() 28 | 29 | # Use tab key to insert tabs 30 | $('#repl_source').keydown (e) -> 31 | if e.keyCode is 9 32 | e.preventDefault() 33 | textbox = e.target 34 | # Insert tab character at caret or in selection 35 | textbox.value = textbox.value[0...textbox.selectionStart] + "\t" + textbox.value[textbox.selectionEnd...] 36 | # Put caret in correct position 37 | textbox.selectionEnd = ++textbox.selectionStart 38 | 39 | # Eval the compiled js. 40 | evalJS = -> 41 | try 42 | eval window.compiledJS 43 | catch error then alert error 44 | 45 | # Load the console with a string of CoffeeScript. 46 | window.loadConsole = (coffee) -> 47 | $('#repl_source').val coffee 48 | compileSource() 49 | $('.navigation.try').addClass('active') 50 | false 51 | 52 | # Helper to hide the menus. 53 | closeMenus = -> 54 | $('.navigation.active').removeClass 'active' 55 | 56 | $('.minibutton.run').click -> evalJS() 57 | 58 | # Bind navigation buttons to open the menus. 59 | $('.navigation').click (e) -> 60 | return if e.target.tagName.toLowerCase() is 'a' 61 | return false if $(e.target).closest('.repl_wrapper').length 62 | if $(this).hasClass('active') 63 | closeMenus() 64 | else 65 | closeMenus() 66 | $(this).addClass 'active' 67 | false 68 | 69 | $(document).on 'click', '[href="#try"]', (e) -> 70 | $('.navigation.try').addClass 'active' 71 | 72 | # Dismiss console if Escape pressed or click falls outside console 73 | # Trigger Run button on Ctrl-Enter 74 | $(document.body) 75 | .keydown (e) -> 76 | closeMenus() if e.which == 27 77 | evalJS() if e.which == 13 and (e.metaKey or e.ctrlKey) and $('.minibutton.run:visible').length 78 | .click (e) -> 79 | return false if $(e.target).hasClass('minibutton') 80 | closeMenus() 81 | 82 | $('#open_webchat').click -> 83 | $(this).replaceWith $('') 84 | 85 | $("#repl_permalink").click (e) -> 86 | window.location = $(this).attr("href") 87 | false 88 | 89 | # If source code is included in location.hash, display it. 90 | hash = decodeURIComponent location.hash.replace(/^#/, '') 91 | if hash.indexOf(sourceFragment) == 0 92 | src = hash.substr sourceFragment.length 93 | loadConsole src 94 | 95 | compileSource() 96 | -------------------------------------------------------------------------------- /documentation/v1/scripts.html: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /documentation/v1/styles.html: -------------------------------------------------------------------------------- 1 | 5 | -------------------------------------------------------------------------------- /documentation/v1/tomorrow.css: -------------------------------------------------------------------------------- 1 | /* Highlight.js syntax highlighting */ 2 | /* http://jmblog.github.com/color-themes-for-google-code-highlightjs */ 3 | /* Forked from http://softwaremaniacs.org/media/soft/highlight/styles/tomorrow.css */ 4 | .tomorrow-comment, pre .comment, pre .title { 5 | color: #8e908c; 6 | } 7 | 8 | .tomorrow-red, pre .variable, pre .tag, pre .regexp, pre .ruby .constant, pre .xml .tag .title, pre .xml .pi, pre .xml .doctype, pre .html .doctype, pre .css .id, pre .css .class, pre .css .pseudo { 9 | color: #c82829; 10 | } 11 | 12 | .tomorrow-orange, pre .number, pre .preprocessor, pre .built_in, pre .params, pre .constant { 13 | color: #000000; 14 | } 15 | 16 | .tomorrow-yellow, pre .class, pre .ruby .class .title, pre .css .rules .attribute { 17 | color: #eab700; 18 | } 19 | 20 | .tomorrow-green, pre .string, pre .value, pre .inheritance, pre .header, pre .ruby .symbol, pre .xml .cdata { 21 | color: #718c00; 22 | } 23 | 24 | .tomorrow-aqua, pre .css .hexcolor { 25 | color: #3e999f; 26 | } 27 | 28 | .tomorrow-blue, pre .function, pre .function .title, pre .python .decorator, pre .python .title, pre .ruby .function .title, pre .ruby .title .keyword, pre .perl .sub, pre .javascript .title, pre .coffeescript .title { 29 | color: #21439C; 30 | } 31 | 32 | .tomorrow-purple, pre .keyword, pre .reserved, pre .javascript .function { 33 | color: #FF5600; 34 | } 35 | 36 | pre .subst { 37 | color: #A535AE; 38 | } 39 | 40 | pre .literal { 41 | color: #A535AE; 42 | } 43 | 44 | pre .property { 45 | color: #A535AE; 46 | } 47 | 48 | pre .class .title { 49 | color: #21439C; 50 | } 51 | 52 | pre .coffeescript .javascript, 53 | pre .javascript .xml, 54 | pre .tex .formula, 55 | pre .xml .javascript, 56 | pre .xml .vbscript, 57 | pre .xml .css, 58 | pre .xml .cdata { 59 | opacity: 0.5; 60 | } 61 | -------------------------------------------------------------------------------- /documentation/v2/code.coffee: -------------------------------------------------------------------------------- 1 | fs = require 'fs' 2 | _ = require 'underscore' 3 | CoffeeScript = require '../../lib/coffeescript' 4 | 5 | 6 | module.exports = -> 7 | (file, run = no) -> 8 | cs = fs.readFileSync "documentation/examples/#{file}.coffee", 'utf-8' 9 | js = CoffeeScript.compile cs, bare: yes # This is just the initial JavaScript output; it is replaced by dynamic compilation on changes of the CoffeeScript pane 10 | render = _.template fs.readFileSync('documentation/v2/code.html', 'utf-8') 11 | output = render {file, cs, js, run} 12 | -------------------------------------------------------------------------------- /documentation/v2/code.html: -------------------------------------------------------------------------------- 1 | 18 | -------------------------------------------------------------------------------- /documentation/v2/navbar.html: -------------------------------------------------------------------------------- 1 | 16 | -------------------------------------------------------------------------------- /documentation/v2/scripts.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 12 | -------------------------------------------------------------------------------- /documentation/v2/styles.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | -------------------------------------------------------------------------------- /documentation/v2/try.html: -------------------------------------------------------------------------------- 1 | 16 | -------------------------------------------------------------------------------- /documentation/v2/twilight.css: -------------------------------------------------------------------------------- 1 | /* Adapted from https://github.com/FarhadG/code-mirror-themes/blob/master/themes/twilight.css */ 2 | 3 | .cm-s-twilight { 4 | letter-spacing: 0.3px; 5 | color: #f8f8f8; 6 | } 7 | .cm-s-twilight .CodeMirror-lines { 8 | padding: 0.5em 0; 9 | } 10 | .cm-s-twilight div.CodeMirror-cursor { 11 | border-left: 3px solid #f8f8f8; 12 | } 13 | .cm-s-twilight .CodeMirror-activeline-background { 14 | background: #ffffff08; 15 | } 16 | .cm-s-twilight .CodeMirror-selected { 17 | background: #ddf0ff33; 18 | } 19 | .cm-s-twilight .cm-comment { 20 | font-style: italic; 21 | color: #5f5a60; 22 | } 23 | .cm-s-twilight .cm-keyword { 24 | color: #cda869; 25 | } 26 | .cm-s-twilight .cm-string { 27 | color: #8f9d6a; 28 | } 29 | .cm-s-twilight .cm-property { 30 | color: #dad085; 31 | } 32 | .cm-s-twilight .cm-atom { 33 | color: #dad085; 34 | } 35 | .cm-s-twilight .cm-number { 36 | color: #dad085; 37 | } 38 | .cm-s-twilight .cm-operator { 39 | color: #cda869; 40 | } 41 | 42 | /* Uneditable code blocks are inverted, so use darker versions of the above */ 43 | 44 | .uneditable-code-block .comment { 45 | font-style: italic; 46 | color: #837B85; 47 | } 48 | .uneditable-code-block .class, 49 | .uneditable-code-block .function, 50 | .uneditable-code-block .keyword, 51 | .uneditable-code-block .reserved, 52 | .uneditable-code-block .title { 53 | color: #534328; 54 | } 55 | .uneditable-code-block .string 56 | .uneditable-code-block .value 57 | .uneditable-code-block .inheritance 58 | .uneditable-code-block .header { 59 | color: #3A4029; 60 | } 61 | .uneditable-code-block .variable, 62 | .uneditable-code-block .literal, 63 | .uneditable-code-block .tag, 64 | .uneditable-code-block .regexp, 65 | .uneditable-code-block .subst, 66 | .uneditable-code-block .property { 67 | color: #474429; 68 | } 69 | .uneditable-code-block .number, 70 | .uneditable-code-block .preprocessor, 71 | .uneditable-code-block .built_in, 72 | .uneditable-code-block .params, 73 | .uneditable-code-block .constant { 74 | color: #474429; 75 | } 76 | -------------------------------------------------------------------------------- /jsx_examples/greeting.coffee: -------------------------------------------------------------------------------- 1 | UserGreeting = -> 2 | %h1 Welcome back! 3 | 4 | GuestGreeting = -> 5 | %h1 Please sign up. 6 | 7 | Greeting = ({isLoggedIn}) -> 8 | # return %UserGreeting if isLoggedIn 9 | if isLoggedIn 10 | %UserGreeting 11 | else 12 | %GuestGreeting 13 | 14 | ReactDOM.render( 15 | %Greeting{ isLoggedIn: no } 16 | document.getElementById 'root' 17 | ) 18 | 19 | LoginButton = ({onClick}) -> 20 | %button{ onClick } Login 21 | 22 | LogoutButton = ({onClick}) -> 23 | %button{ onClick } Logout 24 | 25 | class LoginControl extends React.Component 26 | constructor: (props) -> 27 | super props 28 | @state = isLoggedIn: no 29 | 30 | handleLoginClick: => 31 | @setState isLoggedIn: yes 32 | 33 | handleLogoutClick: => 34 | @setState isLoggedIn: no 35 | 36 | render: -> 37 | {isLoggedIn} = @state 38 | 39 | %div 40 | %Greeting{ isLoggedIn } 41 | = if isLoggedIn 42 | %LogoutButton( onClick={@handleLogoutClick} ) 43 | else 44 | %LoginButton( onClick={@handleLoginClick} ) 45 | -------------------------------------------------------------------------------- /jsx_examples/mailbox.coffee: -------------------------------------------------------------------------------- 1 | Mailbox = ({unreadMessages: {length}}) -> 2 | %div 3 | %h1 Hello! 4 | = length and 5 | %h2 You have {length} unread messages. 6 | = if length 7 | %h2 You have {length} unread messages. 8 | -------------------------------------------------------------------------------- /jsx_examples/name_form.coffee: -------------------------------------------------------------------------------- 1 | class NameForm extends React.Component 2 | constructor: (props) -> 3 | super props 4 | @state = value: '' 5 | 6 | onChange: ({target: {value}}) => 7 | @setState {value} 8 | 9 | onSubmit: (event) => 10 | alert "A name was submitted: #{@state.value}" 11 | event.preventDefault() 12 | 13 | render: -> 14 | {value} = @state 15 | 16 | %form{ @onSubmit } 17 | %label 18 | Name: 19 | %input{ type: 'text', value, @onChange } 20 | %input( 21 | type='submit' 22 | value='Submit' 23 | ) 24 | -------------------------------------------------------------------------------- /jsx_examples/table_items.coffee: -------------------------------------------------------------------------------- 1 | %Table 2 | = for item in items 3 | {is_accessory, parentId, id} = item 4 | itemId = if is_accessory then parentId else id 5 | hidden = not allHidden and removedItemIds.indexOf(itemId) >= 0 6 | %ItemRow{ 7 | key: id 8 | item 9 | quantityOverride: itemQuantityOverrides[itemId] or null 10 | className: 'js-Hiding is-hidden' if hidden 11 | } 12 | -------------------------------------------------------------------------------- /jsx_examples/toggle.coffee: -------------------------------------------------------------------------------- 1 | class Toggle extends React.Component 2 | constructor: (props) -> 3 | super props 4 | @state = isToggleOn: yes 5 | 6 | handleClick: => 7 | @setState (prevState) -> 8 | isToggleOn: not prevState.isToggleOn 9 | 10 | render: -> 11 | %button{ onClick: @handleClick } 12 | = if @state.isToggleOn 13 | 'ON' 14 | else 15 | 'OFF' 16 | 17 | #ReactDOM.render %Toggle, 18 | ReactDOM.render( 19 | %Toggle 20 | document.getElementById 'root' 21 | ) 22 | -------------------------------------------------------------------------------- /lib/coffeescript/register.js: -------------------------------------------------------------------------------- 1 | // Generated by CoffeeScript 0.1.6 2 | (function() { 3 | var CoffeeScript, Module, binary, child_process, ext, findExtension, fork, helpers, i, len, loadFile, path, ref; 4 | 5 | CoffeeScript = require('./'); 6 | 7 | child_process = require('child_process'); 8 | 9 | helpers = require('./helpers'); 10 | 11 | path = require('path'); 12 | 13 | // Load and run a CoffeeScript file for Node, stripping any `BOM`s. 14 | loadFile = function(module, filename) { 15 | var answer; 16 | answer = CoffeeScript._compileFile(filename, false, true); 17 | return module._compile(answer, filename); 18 | }; 19 | 20 | // If the installed version of Node supports `require.extensions`, register 21 | // CoffeeScript as an extension. 22 | if (require.extensions) { 23 | ref = CoffeeScript.FILE_EXTENSIONS; 24 | for (i = 0, len = ref.length; i < len; i++) { 25 | ext = ref[i]; 26 | require.extensions[ext] = loadFile; 27 | } 28 | // Patch Node's module loader to be able to handle multi-dot extensions. 29 | // This is a horrible thing that should not be required. 30 | Module = require('module'); 31 | findExtension = function(filename) { 32 | var curExtension, extensions; 33 | extensions = path.basename(filename).split('.'); 34 | if (extensions[0] === '') { 35 | // Remove the initial dot from dotfiles. 36 | extensions.shift(); 37 | } 38 | // Start with the longest possible extension and work our way shortwards. 39 | while (extensions.shift()) { 40 | curExtension = '.' + extensions.join('.'); 41 | if (Module._extensions[curExtension]) { 42 | return curExtension; 43 | } 44 | } 45 | return '.js'; 46 | }; 47 | Module.prototype.load = function(filename) { 48 | var extension; 49 | this.filename = filename; 50 | this.paths = Module._nodeModulePaths(path.dirname(filename)); 51 | extension = findExtension(filename); 52 | Module._extensions[extension](this, filename); 53 | return this.loaded = true; 54 | }; 55 | } 56 | 57 | // If we're on Node, patch `child_process.fork` so that Coffee scripts are able 58 | // to fork both CoffeeScript files, and JavaScript files, directly. 59 | if (child_process) { 60 | ({fork} = child_process); 61 | binary = require.resolve('../../bin/coffee'); 62 | child_process.fork = function(path, args, options) { 63 | if (helpers.isCoffee(path)) { 64 | if (!Array.isArray(args)) { 65 | options = args || {}; 66 | args = []; 67 | } 68 | args = [path].concat(args); 69 | path = binary; 70 | } 71 | return fork(path, args, options); 72 | }; 73 | } 74 | 75 | }).call(this); 76 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "coffeescript-jsxy", 3 | "description": "Unfancy JavaScript/JSX", 4 | "keywords": [ 5 | "javascript", 6 | "language", 7 | "coffeescript", 8 | "compiler", 9 | "react" 10 | ], 11 | "authors": [ 12 | "Jeremy Ashkenas", 13 | "Julian Rosse" 14 | ], 15 | "version": "0.1.7", 16 | "license": "MIT", 17 | "engines": { 18 | "node": ">=6" 19 | }, 20 | "directories": { 21 | "lib": "./lib/coffeescript" 22 | }, 23 | "main": "./lib/coffeescript/index", 24 | "browser": "./lib/coffeescript/browser", 25 | "bin": { 26 | "coffee": "./bin/coffee", 27 | "cake": "./bin/cake" 28 | }, 29 | "files": [ 30 | "bin", 31 | "lib", 32 | "register.js", 33 | "repl.js" 34 | ], 35 | "scripts": { 36 | "test": "node ./bin/cake test", 37 | "test-harmony": "node --harmony ./bin/cake test" 38 | }, 39 | "homepage": "https://github.com/helixbass/coffeescript-jsxy", 40 | "bugs": "https://github.com/helixbass/coffeescript-jsxy/issues", 41 | "repository": { 42 | "type": "git", 43 | "url": "git://github.com/helixbass/coffeescript-jsxy.git" 44 | }, 45 | "devDependencies": { 46 | "babel-core": "~6.26.0", 47 | "babel-preset-babili": "~0.1.4", 48 | "babel-preset-env": "~1.6.0", 49 | "babili": "^0.1.4", 50 | "docco": "~0.7.0", 51 | "highlight.js": "~9.12.0", 52 | "jison": ">=0.4.17", 53 | "markdown-it": "~8.4.0", 54 | "underscore": "~1.8.3", 55 | "webpack": "~3.5.5" 56 | }, 57 | "dependencies": {} 58 | } 59 | -------------------------------------------------------------------------------- /register.js: -------------------------------------------------------------------------------- 1 | require('./lib/coffeescript/register'); 2 | -------------------------------------------------------------------------------- /repl.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/coffeescript/repl'); 2 | -------------------------------------------------------------------------------- /src/browser.coffee: -------------------------------------------------------------------------------- 1 | # This **Browser** compatibility layer extends core CoffeeScript functions 2 | # to make things work smoothly when compiling code directly in the browser. 3 | # We add support for loading remote Coffee scripts via **XHR**, and 4 | # `text/coffeescript` script tags, source maps via data-URLs, and so on. 5 | 6 | CoffeeScript = require './coffeescript' 7 | compile = CoffeeScript.compile 8 | 9 | # Use standard JavaScript `eval` to eval code. 10 | CoffeeScript.eval = (code, options = {}) -> 11 | options.bare ?= on 12 | eval compile code, options 13 | 14 | # Running code does not provide access to this scope. 15 | CoffeeScript.run = (code, options = {}) -> 16 | options.bare = on 17 | options.shiftLine = on 18 | Function(compile code, options)() 19 | 20 | # Export this more limited `CoffeeScript` than what is exported by 21 | # `index.coffee`, which is intended for a Node environment. 22 | module.exports = CoffeeScript 23 | 24 | # If we’re not in a browser environment, we’re finished with the public API. 25 | return unless window? 26 | 27 | # Include source maps where possible. If we’ve got a base64 encoder, a 28 | # JSON serializer, and tools for escaping unicode characters, we’re good to go. 29 | # Ported from https://developer.mozilla.org/en-US/docs/DOM/window.btoa 30 | if btoa? and JSON? 31 | compile = (code, options = {}) -> 32 | options.inlineMap = true 33 | CoffeeScript.compile code, options 34 | 35 | # Load a remote script from the current domain via XHR. 36 | CoffeeScript.load = (url, callback, options = {}, hold = false) -> 37 | options.sourceFiles = [url] 38 | xhr = if window.ActiveXObject 39 | new window.ActiveXObject('Microsoft.XMLHTTP') 40 | else 41 | new window.XMLHttpRequest() 42 | xhr.open 'GET', url, true 43 | xhr.overrideMimeType 'text/plain' if 'overrideMimeType' of xhr 44 | xhr.onreadystatechange = -> 45 | if xhr.readyState is 4 46 | if xhr.status in [0, 200] 47 | param = [xhr.responseText, options] 48 | CoffeeScript.run param... unless hold 49 | else 50 | throw new Error "Could not load #{url}" 51 | callback param if callback 52 | xhr.send null 53 | 54 | # Activate CoffeeScript in the browser by having it compile and evaluate 55 | # all script tags with a content-type of `text/coffeescript`. 56 | # This happens on page load. 57 | runScripts = -> 58 | scripts = window.document.getElementsByTagName 'script' 59 | coffeetypes = ['text/coffeescript', 'text/literate-coffeescript'] 60 | coffees = (s for s in scripts when s.type in coffeetypes) 61 | index = 0 62 | 63 | execute = -> 64 | param = coffees[index] 65 | if param instanceof Array 66 | CoffeeScript.run param... 67 | index++ 68 | execute() 69 | 70 | for script, i in coffees 71 | do (script, i) -> 72 | options = literate: script.type is coffeetypes[1] 73 | source = script.src or script.getAttribute('data-src') 74 | if source 75 | options.filename = source 76 | CoffeeScript.load source, 77 | (param) -> 78 | coffees[i] = param 79 | execute() 80 | options 81 | true 82 | else 83 | # `options.filename` defines the filename the source map appears as 84 | # in Developer Tools. If a script tag has an `id`, use that as the 85 | # filename; otherwise use `coffeescript`, or `coffeescript1` etc., 86 | # leaving the first one unnumbered for the common case that there’s 87 | # only one CoffeeScript script block to parse. 88 | options.filename = if script.id and script.id isnt '' then script.id else "coffeescript#{if i isnt 0 then i else ''}" 89 | options.sourceFiles = ['embedded'] 90 | coffees[i] = [script.innerHTML, options] 91 | 92 | execute() 93 | 94 | # Listen for window load, both in decent browsers and in IE. 95 | if window.addEventListener 96 | window.addEventListener 'DOMContentLoaded', runScripts, no 97 | else 98 | window.attachEvent 'onload', runScripts 99 | -------------------------------------------------------------------------------- /src/cake.coffee: -------------------------------------------------------------------------------- 1 | # `cake` is a simplified version of [Make](http://www.gnu.org/software/make/) 2 | # ([Rake](http://rake.rubyforge.org/), [Jake](https://github.com/280north/jake)) 3 | # for CoffeeScript. You define tasks with names and descriptions in a Cakefile, 4 | # and can call them from the command line, or invoke them from other tasks. 5 | # 6 | # Running `cake` with no arguments will print out a list of all the tasks in the 7 | # current directory's Cakefile. 8 | 9 | # External dependencies. 10 | fs = require 'fs' 11 | path = require 'path' 12 | helpers = require './helpers' 13 | optparse = require './optparse' 14 | CoffeeScript = require './' 15 | 16 | # Register .coffee extension 17 | CoffeeScript.register() 18 | 19 | # Keep track of the list of defined tasks, the accepted options, and so on. 20 | tasks = {} 21 | options = {} 22 | switches = [] 23 | oparse = null 24 | 25 | # Mixin the top-level Cake functions for Cakefiles to use directly. 26 | helpers.extend global, 27 | 28 | # Define a Cake task with a short name, an optional sentence description, 29 | # and the function to run as the action itself. 30 | task: (name, description, action) -> 31 | [action, description] = [description, action] unless action 32 | tasks[name] = {name, description, action} 33 | 34 | # Define an option that the Cakefile accepts. The parsed options hash, 35 | # containing all of the command-line options passed, will be made available 36 | # as the first argument to the action. 37 | option: (letter, flag, description) -> 38 | switches.push [letter, flag, description] 39 | 40 | # Invoke another task in the current Cakefile. 41 | invoke: (name) -> 42 | missingTask name unless tasks[name] 43 | tasks[name].action options 44 | 45 | # Run `cake`. Executes all of the tasks you pass, in order. Note that Node's 46 | # asynchrony may cause tasks to execute in a different order than you'd expect. 47 | # If no tasks are passed, print the help screen. Keep a reference to the 48 | # original directory name, when running Cake tasks from subdirectories. 49 | exports.run = -> 50 | global.__originalDirname = fs.realpathSync '.' 51 | process.chdir cakefileDirectory __originalDirname 52 | args = process.argv[2..] 53 | CoffeeScript.run fs.readFileSync('Cakefile').toString(), filename: 'Cakefile' 54 | oparse = new optparse.OptionParser switches 55 | return printTasks() unless args.length 56 | try 57 | options = oparse.parse(args) 58 | catch e 59 | return fatalError "#{e}" 60 | invoke arg for arg in options.arguments 61 | 62 | # Display the list of Cake tasks in a format similar to `rake -T` 63 | printTasks = -> 64 | relative = path.relative or path.resolve 65 | cakefilePath = path.join relative(__originalDirname, process.cwd()), 'Cakefile' 66 | console.log "#{cakefilePath} defines the following tasks:\n" 67 | for name, task of tasks 68 | spaces = 20 - name.length 69 | spaces = if spaces > 0 then Array(spaces + 1).join(' ') else '' 70 | desc = if task.description then "# #{task.description}" else '' 71 | console.log "cake #{name}#{spaces} #{desc}" 72 | console.log oparse.help() if switches.length 73 | 74 | # Print an error and exit when attempting to use an invalid task/option. 75 | fatalError = (message) -> 76 | console.error message + '\n' 77 | console.log 'To see a list of all tasks/options, run "cake"' 78 | process.exit 1 79 | 80 | missingTask = (task) -> fatalError "No such task: #{task}" 81 | 82 | # When `cake` is invoked, search in the current and all parent directories 83 | # to find the relevant Cakefile. 84 | cakefileDirectory = (dir) -> 85 | return dir if fs.existsSync path.join dir, 'Cakefile' 86 | parent = path.normalize path.join dir, '..' 87 | return cakefileDirectory parent unless parent is dir 88 | throw new Error "Cakefile not found in #{process.cwd()}" 89 | -------------------------------------------------------------------------------- /src/register.coffee: -------------------------------------------------------------------------------- 1 | CoffeeScript = require './' 2 | child_process = require 'child_process' 3 | helpers = require './helpers' 4 | path = require 'path' 5 | 6 | # Load and run a CoffeeScript file for Node, stripping any `BOM`s. 7 | loadFile = (module, filename) -> 8 | answer = CoffeeScript._compileFile filename, no, yes 9 | module._compile answer, filename 10 | 11 | # If the installed version of Node supports `require.extensions`, register 12 | # CoffeeScript as an extension. 13 | if require.extensions 14 | for ext in CoffeeScript.FILE_EXTENSIONS 15 | require.extensions[ext] = loadFile 16 | 17 | # Patch Node's module loader to be able to handle multi-dot extensions. 18 | # This is a horrible thing that should not be required. 19 | Module = require 'module' 20 | 21 | findExtension = (filename) -> 22 | extensions = path.basename(filename).split '.' 23 | # Remove the initial dot from dotfiles. 24 | extensions.shift() if extensions[0] is '' 25 | # Start with the longest possible extension and work our way shortwards. 26 | while extensions.shift() 27 | curExtension = '.' + extensions.join '.' 28 | return curExtension if Module._extensions[curExtension] 29 | '.js' 30 | 31 | Module::load = (filename) -> 32 | @filename = filename 33 | @paths = Module._nodeModulePaths path.dirname filename 34 | extension = findExtension filename 35 | Module._extensions[extension](this, filename) 36 | @loaded = true 37 | 38 | # If we're on Node, patch `child_process.fork` so that Coffee scripts are able 39 | # to fork both CoffeeScript files, and JavaScript files, directly. 40 | if child_process 41 | {fork} = child_process 42 | binary = require.resolve '../../bin/coffee' 43 | child_process.fork = (path, args, options) -> 44 | if helpers.isCoffee path 45 | unless Array.isArray args 46 | options = args or {} 47 | args = [] 48 | args = [path].concat args 49 | path = binary 50 | fork path, args, options 51 | -------------------------------------------------------------------------------- /test/booleans.coffee: -------------------------------------------------------------------------------- 1 | # Boolean Literals 2 | # ---------------- 3 | 4 | # TODO: add method invocation tests: true.toString() is "true" 5 | 6 | test "#764 Booleans should be indexable", -> 7 | toString = Boolean::toString 8 | 9 | eq toString, true['toString'] 10 | eq toString, false['toString'] 11 | eq toString, yes['toString'] 12 | eq toString, no['toString'] 13 | eq toString, on['toString'] 14 | eq toString, off['toString'] 15 | 16 | eq toString, true.toString 17 | eq toString, false.toString 18 | eq toString, yes.toString 19 | eq toString, no.toString 20 | eq toString, on.toString 21 | eq toString, off.toString 22 | -------------------------------------------------------------------------------- /test/cluster.coffee: -------------------------------------------------------------------------------- 1 | # Cluster Module 2 | # --------- 3 | 4 | return if testingBrowser? 5 | 6 | cluster = require 'cluster' 7 | 8 | if cluster.isMaster 9 | test "#2737 - cluster module can spawn workers from a coffeescript process", -> 10 | cluster.once 'exit', (worker, code) -> 11 | eq code, 0 12 | 13 | cluster.fork() 14 | else 15 | process.exit 0 16 | -------------------------------------------------------------------------------- /test/compilation.coffee: -------------------------------------------------------------------------------- 1 | # Compilation 2 | # ----------- 3 | 4 | # helper to assert that a string should fail compilation 5 | cantCompile = (code) -> 6 | throws -> CoffeeScript.compile code 7 | 8 | 9 | test "ensure that carriage returns don't break compilation on Windows", -> 10 | doesNotThrow -> CoffeeScript.compile 'one\r\ntwo', bare: on 11 | 12 | test "#3089 - don't mutate passed in options to compile", -> 13 | opts = {} 14 | CoffeeScript.compile '1 + 1', opts 15 | ok !opts.scope 16 | 17 | test "--bare", -> 18 | eq -1, CoffeeScript.compile('x = y', bare: on).indexOf 'function' 19 | ok 'passed' is CoffeeScript.eval '"passed"', bare: on, filename: 'test' 20 | 21 | test "header (#1778)", -> 22 | header = "// Generated by CoffeeScript #{CoffeeScript.VERSION}\n" 23 | eq 0, CoffeeScript.compile('x = y', header: on).indexOf header 24 | 25 | test "header is disabled by default", -> 26 | header = "// Generated by CoffeeScript #{CoffeeScript.VERSION}\n" 27 | eq -1, CoffeeScript.compile('x = y').indexOf header 28 | 29 | test "multiple generated references", -> 30 | a = {b: []} 31 | a.b[true] = -> this == a.b 32 | c = 0 33 | d = [] 34 | ok a.b[0<++c<2] d... 35 | 36 | test "splat on a line by itself is invalid", -> 37 | cantCompile "x 'a'\n...\n" 38 | 39 | test "Issue 750", -> 40 | 41 | cantCompile 'f(->' 42 | 43 | cantCompile 'a = (break)' 44 | 45 | cantCompile 'a = (return 5 for item in list)' 46 | 47 | cantCompile 'a = (return 5 while condition)' 48 | 49 | cantCompile 'a = for x in y\n return 5' 50 | 51 | test "Issue #986: Unicode identifiers", -> 52 | λ = 5 53 | eq λ, 5 54 | 55 | test "#2516: Unicode spaces should not be part of identifiers", -> 56 | a = (x) -> x * 2 57 | b = 3 58 | eq 6, a b # U+00A0 NO-BREAK SPACE 59 | eq 6, a b # U+1680 OGHAM SPACE MARK 60 | eq 6, a b # U+2000 EN QUAD 61 | eq 6, a b # U+2001 EM QUAD 62 | eq 6, a b # U+2002 EN SPACE 63 | eq 6, a b # U+2003 EM SPACE 64 | eq 6, a b # U+2004 THREE-PER-EM SPACE 65 | eq 6, a b # U+2005 FOUR-PER-EM SPACE 66 | eq 6, a b # U+2006 SIX-PER-EM SPACE 67 | eq 6, a b # U+2007 FIGURE SPACE 68 | eq 6, a b # U+2008 PUNCTUATION SPACE 69 | eq 6, a b # U+2009 THIN SPACE 70 | eq 6, a b # U+200A HAIR SPACE 71 | eq 6, a b # U+202F NARROW NO-BREAK SPACE 72 | eq 6, a b # U+205F MEDIUM MATHEMATICAL SPACE 73 | eq 6, a b # U+3000 IDEOGRAPHIC SPACE 74 | 75 | # #3560: Non-breaking space (U+00A0) (before `'c'`) 76 | eq 5, {c: 5}[ 'c' ] 77 | 78 | # A line where every space in non-breaking 79 | eq 1 + 1, 2   80 | 81 | test "don't accidentally stringify keywords", -> 82 | ok (-> this == 'this')() is false 83 | 84 | test "#1026: no if/else/else allowed", -> 85 | cantCompile ''' 86 | if a 87 | b 88 | else 89 | c 90 | else 91 | d 92 | ''' 93 | 94 | test "#1050: no closing asterisk comments from within block comments", -> 95 | cantCompile "### */ ###" 96 | 97 | test "#1273: escaping quotes at the end of heredocs", -> 98 | cantCompile '"""\\"""' # """\""" 99 | cantCompile '"""\\\\\\"""' # """\\\""" 100 | 101 | test "#1106: __proto__ compilation", -> 102 | object = eq 103 | @["__proto__"] = true 104 | ok __proto__ 105 | 106 | test "reference named hasOwnProperty", -> 107 | CoffeeScript.compile 'hasOwnProperty = 0; a = 1' 108 | 109 | test "#1055: invalid keys in real (but not work-product) objects", -> 110 | cantCompile "@key: value" 111 | 112 | test "#1066: interpolated strings are not implicit functions", -> 113 | cantCompile '"int#{er}polated" arg' 114 | 115 | test "#2846: while with empty body", -> 116 | CoffeeScript.compile 'while 1 then', {sourceMap: true} 117 | 118 | test "#2944: implicit call with a regex argument", -> 119 | CoffeeScript.compile 'o[key] /regex/' 120 | 121 | test "#3001: `own` shouldn't be allowed in a `for`-`in` loop", -> 122 | cantCompile "a for own b in c" 123 | 124 | test "#2994: single-line `if` requires `then`", -> 125 | cantCompile "if b else x" 126 | -------------------------------------------------------------------------------- /test/eval.coffee: -------------------------------------------------------------------------------- 1 | if vm = require? 'vm' 2 | 3 | test "CoffeeScript.eval runs in the global context by default", -> 4 | global.punctuation = '!' 5 | code = ''' 6 | global.fhqwhgads = "global superpower#{global.punctuation}" 7 | ''' 8 | result = CoffeeScript.eval code 9 | eq result, 'global superpower!' 10 | eq fhqwhgads, 'global superpower!' 11 | 12 | test "CoffeeScript.eval can run in, and modify, a Script context sandbox", -> 13 | createContext = vm.Script.createContext ? vm.createContext 14 | sandbox = createContext() 15 | sandbox.foo = 'bar' 16 | code = ''' 17 | global.foo = 'not bar!' 18 | ''' 19 | result = CoffeeScript.eval code, {sandbox} 20 | eq result, 'not bar!' 21 | eq sandbox.foo, 'not bar!' 22 | 23 | test "CoffeeScript.eval can run in, but cannot modify, an ordinary object sandbox", -> 24 | sandbox = {foo: 'bar'} 25 | code = ''' 26 | global.foo = 'not bar!' 27 | ''' 28 | result = CoffeeScript.eval code, {sandbox} 29 | eq result, 'not bar!' 30 | eq sandbox.foo, 'bar' 31 | -------------------------------------------------------------------------------- /test/helpers.coffee: -------------------------------------------------------------------------------- 1 | # Helpers 2 | # ------- 3 | 4 | # pull the helpers from `CoffeeScript.helpers` into local variables 5 | {starts, ends, repeat, compact, count, merge, extend, flatten, del, baseFileName} = CoffeeScript.helpers 6 | 7 | 8 | # `starts` 9 | 10 | test "the `starts` helper tests if a string starts with another string", -> 11 | ok starts('01234', '012') 12 | ok not starts('01234', '123') 13 | 14 | test "the `starts` helper can take an optional offset", -> 15 | ok starts('01234', '34', 3) 16 | ok not starts('01234', '01', 1) 17 | 18 | 19 | # `ends` 20 | 21 | test "the `ends` helper tests if a string ends with another string", -> 22 | ok ends('01234', '234') 23 | ok not ends('01234', '012') 24 | 25 | test "the `ends` helper can take an optional offset", -> 26 | ok ends('01234', '012', 2) 27 | ok not ends('01234', '234', 6) 28 | 29 | 30 | # `repeat` 31 | 32 | test "the `repeat` helper concatenates a given number of times", -> 33 | eq 'asdasdasd', repeat('asd', 3) 34 | 35 | test "`repeat`ing a string 0 times always returns the empty string", -> 36 | eq '', repeat('whatever', 0) 37 | 38 | 39 | # `compact` 40 | 41 | test "the `compact` helper removes falsey values from an array, preserves truthy ones", -> 42 | allValues = [1, 0, false, obj={}, [], '', ' ', -1, null, undefined, true] 43 | truthyValues = [1, obj, [], ' ', -1, true] 44 | arrayEq truthyValues, compact(allValues) 45 | 46 | 47 | # `count` 48 | 49 | test "the `count` helper counts the number of occurrences of a string in another string", -> 50 | eq 1/0, count('abc', '') 51 | eq 0, count('abc', 'z') 52 | eq 1, count('abc', 'a') 53 | eq 1, count('abc', 'b') 54 | eq 2, count('abcdc', 'c') 55 | eq 2, count('abcdabcd','abc') 56 | 57 | 58 | # `merge` 59 | 60 | test "the `merge` helper makes a new object with all properties of the objects given as its arguments", -> 61 | ary = [0, 1, 2, 3, 4] 62 | obj = {} 63 | merged = merge obj, ary 64 | ok merged isnt obj 65 | ok merged isnt ary 66 | for own key, val of ary 67 | eq val, merged[key] 68 | 69 | 70 | # `extend` 71 | 72 | test "the `extend` helper performs a shallow copy", -> 73 | ary = [0, 1, 2, 3] 74 | obj = {} 75 | # should return the object being extended 76 | eq obj, extend(obj, ary) 77 | # should copy the other object's properties as well (obviously) 78 | eq 2, obj[2] 79 | 80 | 81 | # `flatten` 82 | 83 | test "the `flatten` helper flattens an array", -> 84 | success = yes 85 | (success and= typeof n is 'number') for n in flatten [0, [[[1]], 2], 3, [4]] 86 | ok success 87 | 88 | 89 | # `del` 90 | 91 | test "the `del` helper deletes a property from an object and returns the deleted value", -> 92 | obj = [0, 1, 2] 93 | eq 1, del(obj, 1) 94 | ok 1 not of obj 95 | 96 | 97 | # `baseFileName` 98 | 99 | test "the `baseFileName` helper returns the file name to write to", -> 100 | ext = '.js' 101 | sourceToCompiled = 102 | '.coffee': ext 103 | 'a.coffee': 'a' + ext 104 | 'b.coffee': 'b' + ext 105 | 'coffee.coffee': 'coffee' + ext 106 | 107 | '.litcoffee': ext 108 | 'a.litcoffee': 'a' + ext 109 | 'b.litcoffee': 'b' + ext 110 | 'coffee.litcoffee': 'coffee' + ext 111 | 112 | '.lit': ext 113 | 'a.lit': 'a' + ext 114 | 'b.lit': 'b' + ext 115 | 'coffee.lit': 'coffee' + ext 116 | 117 | '.coffee.md': ext 118 | 'a.coffee.md': 'a' + ext 119 | 'b.coffee.md': 'b' + ext 120 | 'coffee.coffee.md': 'coffee' + ext 121 | 122 | for sourceFileName, expectedFileName of sourceToCompiled 123 | name = baseFileName sourceFileName, yes 124 | filename = name + ext 125 | eq filename, expectedFileName 126 | -------------------------------------------------------------------------------- /test/importing.coffee: -------------------------------------------------------------------------------- 1 | # Importing 2 | # --------- 3 | 4 | unless window? or testingBrowser? 5 | test "coffeescript modules can be imported and executed", -> 6 | 7 | magicKey = __filename 8 | magicValue = 0xFFFF 9 | 10 | if global[magicKey]? 11 | if exports? 12 | local = magicValue 13 | exports.method = -> local 14 | else 15 | global[magicKey] = {} 16 | if require?.extensions? 17 | ok require(__filename).method() is magicValue 18 | delete global[magicKey] 19 | 20 | test "javascript modules can be imported", -> 21 | magicVal = 1 22 | for module in 'import.js import2 .import2 import.extension.js import.unknownextension .coffee .coffee.md'.split ' ' 23 | ok require("./importing/#{module}").value?() is magicVal, module 24 | 25 | test "coffeescript modules can be imported", -> 26 | magicVal = 2 27 | for module in '.import.coffee import.coffee import.extension.coffee'.split ' ' 28 | ok require("./importing/#{module}").value?() is magicVal, module 29 | 30 | test "literate coffeescript modules can be imported", -> 31 | magicVal = 3 32 | # Leading space intentional to check for index.coffee.md 33 | for module in ' .import.coffee.md import.coffee.md import.litcoffee import.extension.coffee.md'.split ' ' 34 | ok require("./importing/#{module}").value?() is magicVal, module 35 | -------------------------------------------------------------------------------- /test/importing/.coffee: -------------------------------------------------------------------------------- 1 | // Required by ../importing.coffee 2 | module.exports = {value: function(){return 1;}}; 3 | -------------------------------------------------------------------------------- /test/importing/.coffee.md: -------------------------------------------------------------------------------- 1 | // Required by ../importing.coffee 2 | module.exports = {value: function(){return 1;}}; 3 | -------------------------------------------------------------------------------- /test/importing/.import.coffee: -------------------------------------------------------------------------------- 1 | # Required by ../importing.coffee 2 | module.exports = {value: -> 2} 3 | -------------------------------------------------------------------------------- /test/importing/.import.coffee.md: -------------------------------------------------------------------------------- 1 | Required by ../importing.coffee 2 | 3 | module.exports = {value: -> 3} 4 | -------------------------------------------------------------------------------- /test/importing/.import2: -------------------------------------------------------------------------------- 1 | // Required by ../importing.coffee 2 | module.exports = {value: function(){return 1;}}; 3 | -------------------------------------------------------------------------------- /test/importing/import.coffee: -------------------------------------------------------------------------------- 1 | # Required by ../importing.coffee 2 | module.exports = {value: -> 2} 3 | -------------------------------------------------------------------------------- /test/importing/import.coffee.md: -------------------------------------------------------------------------------- 1 | Required by ../importing.coffee 2 | 3 | module.exports = {value: -> 3} 4 | -------------------------------------------------------------------------------- /test/importing/import.extension.coffee: -------------------------------------------------------------------------------- 1 | # Required by ../importing.coffee 2 | module.exports = {value: -> 2} 3 | -------------------------------------------------------------------------------- /test/importing/import.extension.coffee.md: -------------------------------------------------------------------------------- 1 | Required by ../importing.coffee 2 | 3 | module.exports = {value: -> 3} 4 | -------------------------------------------------------------------------------- /test/importing/import.extension.js: -------------------------------------------------------------------------------- 1 | // Required by ../importing.coffee 2 | module.exports = {value: function(){return 1;}}; 3 | -------------------------------------------------------------------------------- /test/importing/import.js: -------------------------------------------------------------------------------- 1 | // Required by ../importing.coffee 2 | module.exports = {value: function(){return 1;}}; 3 | -------------------------------------------------------------------------------- /test/importing/import.litcoffee: -------------------------------------------------------------------------------- 1 | Required by ../importing.coffee 2 | 3 | module.exports = {value: -> 3} 4 | -------------------------------------------------------------------------------- /test/importing/import.unknownextension: -------------------------------------------------------------------------------- 1 | // Required by ../importing.coffee 2 | module.exports = {value: function(){return 1;}}; 3 | -------------------------------------------------------------------------------- /test/importing/import2: -------------------------------------------------------------------------------- 1 | // Required by ../importing.coffee 2 | module.exports = {value: function(){return 1;}}; 3 | -------------------------------------------------------------------------------- /test/importing/index.coffee.md: -------------------------------------------------------------------------------- 1 | Required by ../importing.coffee 2 | 3 | module.exports = {value: -> 3} 4 | -------------------------------------------------------------------------------- /test/importing/shebang.coffee: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env coffee 2 | 3 | process.stdout.write JSON.stringify(process.argv) 4 | -------------------------------------------------------------------------------- /test/importing/shebang_extra_args.coffee: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env coffee -- 2 | 3 | process.stdout.write JSON.stringify(process.argv) 4 | -------------------------------------------------------------------------------- /test/importing/shebang_initial_space.coffee: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env coffee 2 | 3 | process.stdout.write JSON.stringify(process.argv) 4 | -------------------------------------------------------------------------------- /test/importing/shebang_initial_space_extra_args.coffee: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env coffee extra 2 | 3 | process.stdout.write JSON.stringify(process.argv) 4 | -------------------------------------------------------------------------------- /test/javascript_literals.coffee: -------------------------------------------------------------------------------- 1 | # JavaScript Literals 2 | # ------------------- 3 | 4 | test "inline JavaScript is evaluated", -> 5 | eq '\\`', ` 6 | // Inline JS 7 | "\\\\\`" 8 | ` 9 | 10 | test "escaped backticks are output correctly", -> 11 | `var a = \`2 + 2 = ${4}\`` 12 | eq a, '2 + 2 = 4' 13 | 14 | test "backslashes before a newline don’t break JavaScript blocks", -> 15 | `var a = \`To be, or not\\ 16 | to be.\`` 17 | eq a, ''' 18 | To be, or not\\ 19 | to be.''' 20 | 21 | test "block inline JavaScript is evaluated", -> 22 | ``` 23 | var a = 1; 24 | var b = 2; 25 | ``` 26 | c = 3 27 | ```var d = 4;``` 28 | eq a + b + c + d, 10 29 | 30 | test "block inline JavaScript containing backticks", -> 31 | ``` 32 | // This is a comment with `backticks` 33 | var a = 42; 34 | var b = `foo ${'bar'}`; 35 | var c = 3; 36 | var d = 'foo`bar`'; 37 | ``` 38 | eq a + c, 45 39 | eq b, 'foo bar' 40 | eq d, 'foo`bar`' 41 | 42 | test "block JavaScript can end with an escaped backtick character", -> 43 | ```var a = \`hello\```` 44 | ``` 45 | var b = \`world${'!'}\```` 46 | eq a, 'hello' 47 | eq b, 'world!' 48 | 49 | test "JavaScript block only escapes backslashes followed by backticks", -> 50 | eq `'\\\n'`, '\\\n' 51 | 52 | test "escaped JavaScript blocks speed round", -> 53 | # The following has escaped backslashes because they’re required in strings, but the intent is this: 54 | # `hello` → hello; 55 | # `\`hello\`` → `hello`; 56 | # `\`Escaping backticks in JS: \\\`hello\\\`\`` → `Escaping backticks in JS: \`hello\``; 57 | # `Single backslash: \ ` → Single backslash: \ ; 58 | # `Double backslash: \\ ` → Double backslash: \\ ; 59 | # `Single backslash at EOS: \\` → Single backslash at EOS: \; 60 | # `Double backslash at EOS: \\\\` → Double backslash at EOS: \\; 61 | for [input, output] in [ 62 | ['`hello`', 'hello;'] 63 | ['`\\`hello\\``', '`hello`;'] 64 | ['`\\`Escaping backticks in JS: \\\\\\`hello\\\\\\`\\``', '`Escaping backticks in JS: \\`hello\\``;'] 65 | ['`Single backslash: \\ `', 'Single backslash: \\ ;'] 66 | ['`Double backslash: \\\\ `', 'Double backslash: \\\\ ;'] 67 | ['`Single backslash at EOS: \\\\`', 'Single backslash at EOS: \\;'] 68 | ['`Double backslash at EOS: \\\\\\\\`', 'Double backslash at EOS: \\\\;'] 69 | ] 70 | eqJS input, output 71 | -------------------------------------------------------------------------------- /test/literate.litcoffee: -------------------------------------------------------------------------------- 1 | # Literate CoffeeScript Test 2 | 3 | comment comment 4 | 5 | testsCount = 0 # Track the number of tests run in this file, to make sure they all run 6 | 7 | test "basic literate CoffeeScript parsing", -> 8 | ok yes 9 | testsCount++ 10 | 11 | now with a... 12 | 13 | test "broken up indentation", -> 14 | 15 | ... broken up ... 16 | 17 | do -> 18 | 19 | ... nested block. 20 | 21 | ok yes 22 | testsCount++ 23 | 24 | Code must be separated from text by a blank line. 25 | 26 | test "code blocks must be preceded by a blank line", -> 27 | 28 | The next line is part of the text and will not be executed. 29 | fail() 30 | 31 | ok yes 32 | testsCount++ 33 | 34 | Code in `backticks is not parsed` and... 35 | 36 | test "comments in indented blocks work", -> 37 | do -> 38 | do -> 39 | # Regular comment. 40 | 41 | ### 42 | Block comment. 43 | ### 44 | 45 | ok yes 46 | testsCount++ 47 | 48 | Regular [Markdown](http://example.com/markdown) features, like links 49 | and unordered lists, are fine: 50 | 51 | * I 52 | 53 | * Am 54 | 55 | * A 56 | 57 | * List 58 | 59 | --- 60 | 61 | # keep track of whether code blocks are executed or not 62 | executed = false 63 | 64 |

65 | 66 | if true 67 | executed = true # should not execute, this is just HTML para, not code! 68 | 69 |

70 | 71 | test "should ignore code blocks inside HTML", -> 72 | eq executed, false 73 | testsCount++ 74 | 75 | --- 76 | 77 | * A list item followed by a code block: 78 | 79 | test "basic literate CoffeeScript parsing", -> 80 | ok yes 81 | testsCount++ 82 | 83 | --- 84 | 85 | * Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 86 | Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi, 87 | viverra nec, fringilla in, laoreet vitae, risus. 88 | 89 | * Donec sit amet nisl. Aliquam semper ipsum sit amet velit. 90 | Suspendisse id sem consectetuer libero luctus adipiscing. 91 | 92 | --- 93 | 94 | This is [an example][id] reference-style link. 95 | [id]: http://example.com/ "Optional Title Here" 96 | 97 | --- 98 | 99 | executed = no 100 | 101 | 1986. What a great season. 102 | 103 | executed = yes 104 | 105 | and test... 106 | 107 | test "should recognize indented code blocks in lists with empty line as separator", -> 108 | ok executed 109 | testsCount++ 110 | 111 | --- 112 | 113 | executed = no 114 | 115 | 1986\. What a great season. 116 | executed = yes 117 | 118 | and test... 119 | 120 | test "should ignore indented code in escaped list like number", -> 121 | eq executed, no 122 | testsCount++ 123 | 124 | one last test! 125 | 126 | test "block quotes should render correctly", -> 127 | quote = ''' 128 | foo 129 | and bar! 130 | ''' 131 | eq quote, 'foo\n and bar!' 132 | testsCount++ 133 | 134 | and finally, how did we do? 135 | 136 | test "all spaced literate CoffeeScript tests executed", -> 137 | eq testsCount, 9 138 | -------------------------------------------------------------------------------- /test/literate_tabbed.litcoffee: -------------------------------------------------------------------------------- 1 | # Tabbed Literate CoffeeScript Test 2 | 3 | comment comment 4 | 5 | testsCount = 0 # Track the number of tests run in this file, to make sure they all run 6 | 7 | test "basic literate CoffeeScript parsing", -> 8 | ok yes 9 | testsCount++ 10 | 11 | now with a... 12 | 13 | test "broken up indentation", -> 14 | 15 | ... broken up ... 16 | 17 | do -> 18 | 19 | ... nested block. 20 | 21 | ok yes 22 | testsCount++ 23 | 24 | Code must be separated from text by a blank line. 25 | 26 | test "code blocks must be preceded by a blank line", -> 27 | 28 | The next line is part of the text and will not be executed. 29 | fail() 30 | 31 | ok yes 32 | testsCount++ 33 | 34 | Code in `backticks is not parsed` and... 35 | 36 | test "comments in indented blocks work", -> 37 | do -> 38 | do -> 39 | # Regular comment. 40 | 41 | ### 42 | Block comment. 43 | ### 44 | 45 | ok yes 46 | testsCount++ 47 | 48 | Regular [Markdown](http://example.com/markdown) features, like links 49 | and unordered lists, are fine: 50 | 51 | * I 52 | 53 | * Am 54 | 55 | * A 56 | 57 | * List 58 | 59 | --- 60 | 61 | # keep track of whether code blocks are executed or not 62 | executed = false 63 | 64 |

65 | 66 | if true 67 | executed = true # should not execute, this is just HTML para, not code! 68 | 69 |

70 | 71 | test "should ignore code blocks inside HTML", -> 72 | eq executed, false 73 | testsCount++ 74 | 75 | --- 76 | 77 | * A list item followed by a code block: 78 | 79 | test "basic literate CoffeeScript parsing", -> 80 | ok yes 81 | testsCount++ 82 | 83 | --- 84 | 85 | * Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 86 | Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi, 87 | viverra nec, fringilla in, laoreet vitae, risus. 88 | 89 | * Donec sit amet nisl. Aliquam semper ipsum sit amet velit. 90 | Suspendisse id sem consectetuer libero luctus adipiscing. 91 | 92 | --- 93 | 94 | This is [an example][id] reference-style link. 95 | [id]: http://example.com/ "Optional Title Here" 96 | 97 | --- 98 | 99 | executed = no 100 | 101 | 1986. What a great season. 102 | 103 | executed = yes 104 | 105 | and test... 106 | 107 | test "should recognize indented code blocks in lists with empty line as separator", -> 108 | ok executed 109 | testsCount++ 110 | 111 | --- 112 | 113 | executed = no 114 | 115 | 1986\. What a great season. 116 | executed = yes 117 | 118 | and test... 119 | 120 | test "should ignore indented code in escaped list like number", -> 121 | eq executed, no 122 | testsCount++ 123 | 124 | one last test! 125 | 126 | test "block quotes should render correctly", -> 127 | quote = ''' 128 | foo 129 | and bar! 130 | ''' 131 | eq quote, 'foo\n\t\tand bar!' 132 | testsCount++ 133 | 134 | and finally, how did we do? 135 | 136 | test "all tabbed literate CoffeeScript tests executed", -> 137 | eq testsCount, 9 138 | -------------------------------------------------------------------------------- /test/numbers.coffee: -------------------------------------------------------------------------------- 1 | # Number Literals 2 | # --------------- 3 | 4 | # * Decimal Integer Literals 5 | # * Octal Integer Literals 6 | # * Hexadecimal Integer Literals 7 | # * Scientific Notation Integer Literals 8 | # * Scientific Notation Non-Integer Literals 9 | # * Non-Integer Literals 10 | # * Binary Integer Literals 11 | 12 | 13 | # Binary Integer Literals 14 | # Binary notation is understood as would be decimal notation. 15 | 16 | test "Parser recognises binary numbers", -> 17 | eq 4, 0b100 18 | 19 | # Decimal Integer Literals 20 | 21 | test "call methods directly on numbers", -> 22 | eq 4, 4.valueOf() 23 | eq '11', 4.toString 3 24 | 25 | eq -1, 3 -4 26 | 27 | #764: Numbers should be indexable 28 | eq Number::toString, 42['toString'] 29 | 30 | eq Number::toString, 42.toString 31 | 32 | eq Number::toString, 2e308['toString'] # Infinity 33 | 34 | 35 | # Non-Integer Literals 36 | 37 | # Decimal number literals. 38 | value = .25 + .75 39 | ok value is 1 40 | value = 0.0 + -.25 - -.75 + 0.0 41 | ok value is 0.5 42 | 43 | #764: Numbers should be indexable 44 | eq Number::toString, 4['toString'] 45 | eq Number::toString, 4.2['toString'] 46 | eq Number::toString, .42['toString'] 47 | eq Number::toString, (4)['toString'] 48 | 49 | eq Number::toString, 4.toString 50 | eq Number::toString, 4.2.toString 51 | eq Number::toString, .42.toString 52 | eq Number::toString, (4).toString 53 | 54 | test '#1168: leading floating point suppresses newline', -> 55 | eq 1, do -> 56 | 1 57 | .5 + 0.5 58 | 59 | test "Python-style octal literal notation '0o777'", -> 60 | eq 511, 0o777 61 | eq 1, 0o1 62 | eq 1, 0o00001 63 | eq parseInt('0777', 8), 0o777 64 | eq '777', 0o777.toString 8 65 | eq 4, 0o4.valueOf() 66 | eq Number::toString, 0o777['toString'] 67 | eq Number::toString, 0o777.toString 68 | 69 | test "#2060: Disallow uppercase radix prefixes and exponential notation", -> 70 | for char in ['b', 'o', 'x', 'e'] 71 | program = "0#{char}0" 72 | doesNotThrow -> CoffeeScript.compile program, bare: yes 73 | throws -> CoffeeScript.compile program.toUpperCase(), bare: yes 74 | 75 | test "#2224: hex literals with 0b or B or E", -> 76 | eq 176, 0x0b0 77 | eq 177, 0x0B1 78 | eq 225, 0xE1 79 | 80 | test "Infinity", -> 81 | eq Infinity, CoffeeScript.eval "0b#{Array(1024 + 1).join('1')}" 82 | eq Infinity, CoffeeScript.eval "0o#{Array(342 + 1).join('7')}" 83 | eq Infinity, CoffeeScript.eval "0x#{Array(256 + 1).join('f')}" 84 | eq Infinity, CoffeeScript.eval Array(500 + 1).join('9') 85 | eq Infinity, 2e308 86 | 87 | test "NaN", -> 88 | ok isNaN 1/NaN 89 | -------------------------------------------------------------------------------- /test/option_parser.coffee: -------------------------------------------------------------------------------- 1 | # Option Parser 2 | # ------------- 3 | 4 | # Ensure that the OptionParser handles arguments correctly. 5 | return unless require? 6 | {OptionParser} = require './../lib/coffeescript/optparse' 7 | 8 | flags = [ 9 | ['-r', '--required [DIR]', 'desc required'] 10 | ['-o', '--optional', 'desc optional'] 11 | ['-l', '--list [FILES*]', 'desc list'] 12 | ] 13 | 14 | banner = ''' 15 | banner text 16 | ''' 17 | 18 | opt = new OptionParser flags, banner 19 | 20 | test "basic arguments", -> 21 | args = ['one', 'two', 'three', '-r', 'dir'] 22 | result = opt.parse args 23 | arrayEq args, result.arguments 24 | eq undefined, result.required 25 | 26 | test "boolean and parameterised options", -> 27 | result = opt.parse ['--optional', '-r', 'folder', 'one', 'two'] 28 | ok result.optional 29 | eq 'folder', result.required 30 | arrayEq ['one', 'two'], result.arguments 31 | 32 | test "list options", -> 33 | result = opt.parse ['-l', 'one.txt', '-l', 'two.txt', 'three'] 34 | arrayEq ['one.txt', 'two.txt'], result.list 35 | arrayEq ['three'], result.arguments 36 | 37 | test "-- and interesting combinations", -> 38 | result = opt.parse ['-o','-r','a','-r','b','-o','--','-a','b','--c','d'] 39 | arrayEq ['-a', 'b', '--c', 'd'], result.arguments 40 | ok result.optional 41 | eq 'b', result.required 42 | 43 | args = ['--','-o','a','-r','c','-o','--','-a','arg0','-b','arg1'] 44 | result = opt.parse args 45 | eq undefined, result.optional 46 | eq undefined, result.required 47 | arrayEq args[1..], result.arguments 48 | 49 | test "throw if multiple flags try to use the same short or long name", -> 50 | throws -> new OptionParser [ 51 | ['-r', '--required [DIR]', 'required'] 52 | ['-r', '--long', 'switch'] 53 | ] 54 | 55 | throws -> new OptionParser [ 56 | ['-a', '--append [STR]', 'append'] 57 | ['-b', '--append', 'append with -b short opt'] 58 | ] 59 | 60 | throws -> new OptionParser [ 61 | ['--just-long', 'desc'] 62 | ['--just-long', 'another desc'] 63 | ] 64 | 65 | throws -> new OptionParser [ 66 | ['-j', '--just-long', 'desc'] 67 | ['--just-long', 'another desc'] 68 | ] 69 | 70 | throws -> new OptionParser [ 71 | ['--just-long', 'desc'] 72 | ['-j', '--just-long', 'another desc'] 73 | ] 74 | 75 | test "outputs expected help text", -> 76 | expectedBanner = ''' 77 | 78 | banner text 79 | 80 | -r, --required desc required 81 | -o, --optional desc optional 82 | -l, --list desc list 83 | 84 | ''' 85 | ok opt.help() is expectedBanner 86 | 87 | expected = [ 88 | '' 89 | ' -r, --required desc required' 90 | ' -o, --optional desc optional' 91 | ' -l, --list desc list' 92 | '' 93 | ].join('\n') 94 | ok new OptionParser(flags).help() is expected 95 | -------------------------------------------------------------------------------- /test/parser.coffee: -------------------------------------------------------------------------------- 1 | # Parser 2 | # --------- 3 | 4 | test "operator precedence for logical operators", -> 5 | source = ''' 6 | a or b and c 7 | ''' 8 | block = CoffeeScript.nodes source 9 | [expression] = block.expressions 10 | eq expression.first.base.value, 'a' 11 | eq expression.operator, '||' 12 | eq expression.second.first.base.value, 'b' 13 | eq expression.second.operator, '&&' 14 | eq expression.second.second.base.value, 'c' 15 | 16 | test "operator precedence for bitwise operators", -> 17 | source = ''' 18 | a | b ^ c & d 19 | ''' 20 | block = CoffeeScript.nodes source 21 | [expression] = block.expressions 22 | eq expression.first.base.value, 'a' 23 | eq expression.operator, '|' 24 | eq expression.second.first.base.value, 'b' 25 | eq expression.second.operator, '^' 26 | eq expression.second.second.first.base.value, 'c' 27 | eq expression.second.second.operator, '&' 28 | eq expression.second.second.second.base.value, 'd' 29 | 30 | test "operator precedence for binary ? operator", -> 31 | source = ''' 32 | a ? b and c 33 | ''' 34 | block = CoffeeScript.nodes source 35 | [expression] = block.expressions 36 | eq expression.first.base.value, 'a' 37 | eq expression.operator, '?' 38 | eq expression.second.first.base.value, 'b' 39 | eq expression.second.operator, '&&' 40 | eq expression.second.second.base.value, 'c' 41 | 42 | test "new calls have a range including the new", -> 43 | source = ''' 44 | a = new B().c(d) 45 | ''' 46 | block = CoffeeScript.nodes source 47 | 48 | assertColumnRange = (node, firstColumn, lastColumn) -> 49 | eq node.locationData.first_line, 0 50 | eq node.locationData.first_column, firstColumn 51 | eq node.locationData.last_line, 0 52 | eq node.locationData.last_column, lastColumn 53 | 54 | [assign] = block.expressions 55 | outerCall = assign.value 56 | innerValue = outerCall.variable 57 | innerCall = innerValue.base 58 | 59 | assertColumnRange assign, 0, 15 60 | assertColumnRange outerCall, 4, 15 61 | assertColumnRange innerValue, 4, 12 62 | assertColumnRange innerCall, 4, 10 63 | 64 | test "location data is properly set for nested `new`", -> 65 | source = ''' 66 | new new A()() 67 | ''' 68 | block = CoffeeScript.nodes source 69 | 70 | assertColumnRange = (node, firstColumn, lastColumn) -> 71 | eq node.locationData.first_line, 0 72 | eq node.locationData.first_column, firstColumn 73 | eq node.locationData.last_line, 0 74 | eq node.locationData.last_column, lastColumn 75 | 76 | [outerCall] = block.expressions 77 | innerCall = outerCall.variable 78 | 79 | assertColumnRange outerCall, 0, 12 80 | assertColumnRange innerCall, 4, 10 81 | -------------------------------------------------------------------------------- /test/ranges.coffee: -------------------------------------------------------------------------------- 1 | # Range Literals 2 | # -------------- 3 | 4 | # TODO: add indexing and method invocation tests: [1..4][0] is 1, [0...3].toString() 5 | 6 | # shared array 7 | shared = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 8 | 9 | test "basic inclusive ranges", -> 10 | arrayEq [1, 2, 3] , [1..3] 11 | arrayEq [0, 1, 2] , [0..2] 12 | arrayEq [0, 1] , [0..1] 13 | arrayEq [0] , [0..0] 14 | arrayEq [-1] , [-1..-1] 15 | arrayEq [-1, 0] , [-1..0] 16 | arrayEq [-1, 0, 1], [-1..1] 17 | 18 | test "basic exclusive ranges", -> 19 | arrayEq [1, 2, 3] , [1...4] 20 | arrayEq [0, 1, 2] , [0...3] 21 | arrayEq [0, 1] , [0...2] 22 | arrayEq [0] , [0...1] 23 | arrayEq [-1] , [-1...0] 24 | arrayEq [-1, 0] , [-1...1] 25 | arrayEq [-1, 0, 1], [-1...2] 26 | 27 | arrayEq [], [1...1] 28 | arrayEq [], [0...0] 29 | arrayEq [], [-1...-1] 30 | 31 | # Should not trigger implicit call, e.g. rest ... => rest(...) 32 | arrayEq [1, 2, 3] , [1 ... 4] 33 | arrayEq [0, 1, 2] , [0 ... 3] 34 | arrayEq [0, 1] , [0 ... 2] 35 | arrayEq [0] , [0 ... 1] 36 | arrayEq [-1] , [-1 ... 0] 37 | arrayEq [-1, 0] , [-1 ... 1] 38 | arrayEq [-1, 0, 1], [-1 ... 2] 39 | 40 | arrayEq [], [1 ... 1] 41 | arrayEq [], [0 ... 0] 42 | arrayEq [], [-1 ... -1] 43 | 44 | test "downward ranges", -> 45 | arrayEq shared, [9..0].reverse() 46 | arrayEq [5, 4, 3, 2] , [5..2] 47 | arrayEq [2, 1, 0, -1], [2..-1] 48 | 49 | arrayEq [3, 2, 1] , [3..1] 50 | arrayEq [2, 1, 0] , [2..0] 51 | arrayEq [1, 0] , [1..0] 52 | arrayEq [0] , [0..0] 53 | arrayEq [-1] , [-1..-1] 54 | arrayEq [0, -1] , [0..-1] 55 | arrayEq [1, 0, -1] , [1..-1] 56 | arrayEq [0, -1, -2], [0..-2] 57 | 58 | arrayEq [4, 3, 2], [4...1] 59 | arrayEq [3, 2, 1], [3...0] 60 | arrayEq [2, 1] , [2...0] 61 | arrayEq [1] , [1...0] 62 | arrayEq [] , [0...0] 63 | arrayEq [] , [-1...-1] 64 | arrayEq [0] , [0...-1] 65 | arrayEq [0, -1] , [0...-2] 66 | arrayEq [1, 0] , [1...-1] 67 | arrayEq [2, 1, 0], [2...-1] 68 | 69 | test "ranges with variables as enpoints", -> 70 | [a, b] = [1, 3] 71 | arrayEq [1, 2, 3], [a..b] 72 | arrayEq [1, 2] , [a...b] 73 | b = -2 74 | arrayEq [1, 0, -1, -2], [a..b] 75 | arrayEq [1, 0, -1] , [a...b] 76 | 77 | test "ranges with expressions as endpoints", -> 78 | [a, b] = [1, 3] 79 | arrayEq [2, 3, 4, 5, 6], [(a+1)..2*b] 80 | arrayEq [2, 3, 4, 5] , [(a+1)...2*b] 81 | 82 | # Should not trigger implicit call, e.g. rest ... => rest(...) 83 | arrayEq [2, 3, 4, 5] , [(a+1) ... 2*b] 84 | 85 | test "large ranges are generated with looping constructs", -> 86 | down = [99..0] 87 | eq 100, (len = down.length) 88 | eq 0, down[len - 1] 89 | 90 | up = [0...100] 91 | eq 100, (len = up.length) 92 | eq 99, up[len - 1] 93 | 94 | test "for-from loops over ranges", -> 95 | array1 = [] 96 | for x from [20..30] 97 | array1.push(x) 98 | break if x is 25 99 | arrayEq array1, [20, 21, 22, 23, 24, 25] 100 | 101 | test "for-from comprehensions over ranges", -> 102 | array1 = (x + 10 for x from [20..25]) 103 | ok array1.join(' ') is '30 31 32 33 34 35' 104 | 105 | array2 = (x for x from [20..30] when x %% 2 == 0) 106 | ok array2.join(' ') is '20 22 24 26 28 30' 107 | 108 | test "#1012 slices with arguments object", -> 109 | expected = [0..9] 110 | argsAtStart = (-> [arguments[0]..9]) 0 111 | arrayEq expected, argsAtStart 112 | argsAtEnd = (-> [0..arguments[0]]) 9 113 | arrayEq expected, argsAtEnd 114 | argsAtBoth = (-> [arguments[0]..arguments[1]]) 0, 9 115 | arrayEq expected, argsAtBoth 116 | 117 | test "#1409: creating large ranges outside of a function body", -> 118 | CoffeeScript.eval '[0..100]' 119 | -------------------------------------------------------------------------------- /test/repl.coffee: -------------------------------------------------------------------------------- 1 | return if global.testingBrowser 2 | 3 | os = require 'os' 4 | fs = require 'fs' 5 | path = require 'path' 6 | 7 | # REPL 8 | # ---- 9 | Stream = require 'stream' 10 | 11 | class MockInputStream extends Stream 12 | constructor: -> 13 | super() 14 | @readable = true 15 | 16 | resume: -> 17 | 18 | emitLine: (val) -> 19 | @emit 'data', Buffer.from("#{val}\n") 20 | 21 | class MockOutputStream extends Stream 22 | constructor: -> 23 | super() 24 | @writable = true 25 | @written = [] 26 | 27 | write: (data) -> 28 | # console.log 'output write', arguments 29 | @written.push data 30 | 31 | lastWrite: (fromEnd = -1) -> 32 | @written[@written.length - 1 + fromEnd].replace /\r?\n$/, '' 33 | 34 | # Create a dummy history file 35 | historyFile = path.join os.tmpdir(), '.coffee_history_test' 36 | fs.writeFileSync historyFile, '1 + 2\n' 37 | 38 | testRepl = (desc, fn) -> 39 | input = new MockInputStream 40 | output = new MockOutputStream 41 | repl = Repl.start {input, output, historyFile} 42 | test desc, -> fn input, output, repl 43 | 44 | ctrlV = { ctrl: true, name: 'v'} 45 | 46 | 47 | testRepl 'reads history file', (input, output, repl) -> 48 | input.emitLine repl.rli.history[0] 49 | eq '3', output.lastWrite() 50 | 51 | testRepl "starts with coffee prompt", (input, output) -> 52 | eq 'coffee> ', output.lastWrite(0) 53 | 54 | testRepl "writes eval to output", (input, output) -> 55 | input.emitLine '1+1' 56 | eq '2', output.lastWrite() 57 | 58 | testRepl "comments are ignored", (input, output) -> 59 | input.emitLine '1 + 1 # foo' 60 | eq '2', output.lastWrite() 61 | 62 | testRepl "output in inspect mode", (input, output) -> 63 | input.emitLine '"1 + 1\\n"' 64 | eq "'1 + 1\\n'", output.lastWrite() 65 | 66 | testRepl "variables are saved", (input, output) -> 67 | input.emitLine "foo = 'foo'" 68 | input.emitLine 'foobar = "#{foo}bar"' 69 | eq "'foobar'", output.lastWrite() 70 | 71 | testRepl "empty command evaluates to undefined", (input, output) -> 72 | # A regression fixed in Node 5.11.0 broke the handling of pressing enter in 73 | # the Node REPL; see https://github.com/nodejs/node/pull/6090 and 74 | # https://github.com/jashkenas/coffeescript/issues/4502. 75 | # Just skip this test for versions of Node < 6. 76 | return if parseInt(process.versions.node.split('.')[0], 10) < 6 77 | input.emitLine '' 78 | eq 'undefined', output.lastWrite() 79 | 80 | testRepl "ctrl-v toggles multiline prompt", (input, output) -> 81 | input.emit 'keypress', null, ctrlV 82 | eq '------> ', output.lastWrite(0) 83 | input.emit 'keypress', null, ctrlV 84 | eq 'coffee> ', output.lastWrite(0) 85 | 86 | testRepl "multiline continuation changes prompt", (input, output) -> 87 | input.emit 'keypress', null, ctrlV 88 | input.emitLine '' 89 | eq '....... ', output.lastWrite(0) 90 | 91 | testRepl "evaluates multiline", (input, output) -> 92 | # Stubs. Could assert on their use. 93 | output.cursorTo = (pos) -> 94 | output.clearLine = -> 95 | 96 | input.emit 'keypress', null, ctrlV 97 | input.emitLine 'do ->' 98 | input.emitLine ' 1 + 1' 99 | input.emit 'keypress', null, ctrlV 100 | eq '2', output.lastWrite() 101 | 102 | testRepl "variables in scope are preserved", (input, output) -> 103 | input.emitLine 'a = 1' 104 | input.emitLine 'do -> a = 2' 105 | input.emitLine 'a' 106 | eq '2', output.lastWrite() 107 | 108 | testRepl "existential assignment of previously declared variable", (input, output) -> 109 | input.emitLine 'a = null' 110 | input.emitLine 'a ?= 42' 111 | eq '42', output.lastWrite() 112 | 113 | testRepl "keeps running after runtime error", (input, output) -> 114 | input.emitLine 'a = b' 115 | input.emitLine 'a' 116 | eq 'undefined', output.lastWrite() 117 | 118 | testRepl "#4604: wraps an async function", (input, output) -> 119 | return unless global.supportsAsync 120 | input.emitLine 'await new Promise (resolve) -> setTimeout (-> resolve 33), 10' 121 | setTimeout -> 122 | eq '33', output.lastWrite() 123 | , 20 124 | 125 | process.on 'exit', -> 126 | try 127 | fs.unlinkSync historyFile 128 | catch exception # Already deleted, nothing else to do. 129 | -------------------------------------------------------------------------------- /test/scope.coffee: -------------------------------------------------------------------------------- 1 | # Scope 2 | # ----- 3 | 4 | # * Variable Safety 5 | # * Variable Shadowing 6 | # * Auto-closure (`do`) 7 | # * Global Scope Leaks 8 | 9 | test "reference `arguments` inside of functions", -> 10 | sumOfArgs = -> 11 | sum = (a,b) -> a + b 12 | sum = 0 13 | sum += num for num in arguments 14 | sum 15 | eq 10, sumOfArgs(0, 1, 2, 3, 4) 16 | 17 | test "assignment to an Object.prototype-named variable should not leak to outer scope", -> 18 | # FIXME: fails on IE 19 | (-> 20 | constructor = 'word' 21 | )() 22 | ok constructor isnt 'word' 23 | 24 | test "siblings of splat parameters shouldn't leak to surrounding scope", -> 25 | x = 10 26 | oops = (x, args...) -> 27 | oops(20, 1, 2, 3) 28 | eq x, 10 29 | 30 | test "catch statements should introduce their argument to scope", -> 31 | try throw '' 32 | catch e 33 | do -> e = 5 34 | eq 5, e 35 | 36 | test "loop variable should be accessible after for-of loop", -> 37 | d = (x for x of {1:'a',2:'b'}) 38 | ok x in ['1','2'] 39 | 40 | test "loop variable should be accessible after for-in loop", -> 41 | d = (x for x in [1,2]) 42 | eq x, 2 43 | 44 | test "loop variable should be accessible after for-from loop", -> 45 | d = (x for x from [1,2]) 46 | eq x, 2 47 | 48 | class Array then slice: fail # needs to be global 49 | class Object then hasOwnProperty: fail 50 | test "#1973: redefining Array/Object constructors shouldn't confuse __X helpers", -> 51 | arr = [1..4] 52 | arrayEq [3, 4], arr[2..] 53 | obj = {arr} 54 | for own k of obj 55 | eq arr, obj[k] 56 | 57 | test "#2255: global leak with splatted @-params", -> 58 | ok not x? 59 | arrayEq [0], ((@x...) -> @x).call {}, 0 60 | ok not x? 61 | 62 | test "#1183: super + fat arrows", -> 63 | dolater = (cb) -> cb() 64 | 65 | class A 66 | constructor: -> 67 | @_i = 0 68 | foo : (cb) -> 69 | dolater => 70 | @_i += 1 71 | cb() 72 | 73 | class B extends A 74 | constructor : -> 75 | super() 76 | foo : (cb) -> 77 | dolater => 78 | dolater => 79 | @_i += 2 80 | super cb 81 | 82 | b = new B 83 | b.foo => eq b._i, 3 84 | 85 | test "#1183: super + wrap", -> 86 | class A 87 | m : -> 10 88 | 89 | class B extends A 90 | constructor : -> super() 91 | m: -> r = try super() 92 | m: -> r = super() 93 | 94 | eq (new B).m(), 10 95 | 96 | test "#1183: super + closures", -> 97 | class A 98 | constructor: -> 99 | @i = 10 100 | foo : -> @i 101 | 102 | class B extends A 103 | foo : -> 104 | ret = switch 1 105 | when 0 then 0 106 | when 1 then super() 107 | ret 108 | eq (new B).foo(), 10 109 | 110 | test "#2331: bound super regression", -> 111 | class A 112 | @value = 'A' 113 | method: -> @constructor.value 114 | 115 | class B extends A 116 | method: => super() 117 | 118 | eq (new B).method(), 'A' 119 | 120 | test "#3259: leak with @-params within destructured parameters", -> 121 | fn = ({@foo}, [@bar], [{@baz}]) -> 122 | foo = bar = baz = false 123 | 124 | fn.call {}, {foo: 'foo'}, ['bar'], [{baz: 'baz'}] 125 | 126 | eq 'undefined', typeof foo 127 | eq 'undefined', typeof bar 128 | eq 'undefined', typeof baz 129 | -------------------------------------------------------------------------------- /test/soaks.coffee: -------------------------------------------------------------------------------- 1 | # Soaks 2 | # ----- 3 | 4 | # * Soaked Property Access 5 | # * Soaked Method Invocation 6 | # * Soaked Function Invocation 7 | 8 | 9 | # Soaked Property Access 10 | 11 | test "soaked property access", -> 12 | nonce = {} 13 | obj = a: b: nonce 14 | eq nonce , obj?.a.b 15 | eq nonce , obj?['a'].b 16 | eq nonce , obj.a?.b 17 | eq nonce , obj?.a?['b'] 18 | eq undefined, obj?.a?.non?.existent?.property 19 | 20 | test "soaked property access caches method calls", -> 21 | nonce ={} 22 | obj = fn: -> a: nonce 23 | eq nonce , obj.fn()?.a 24 | eq undefined, obj.fn()?.b 25 | 26 | test "soaked property access caching", -> 27 | nonce = {} 28 | counter = 0 29 | fn = -> 30 | counter++ 31 | 'self' 32 | obj = 33 | self: -> @ 34 | prop: nonce 35 | eq nonce, obj[fn()]()[fn()]()[fn()]()?.prop 36 | eq 3, counter 37 | 38 | test "method calls on soaked methods", -> 39 | nonce = {} 40 | obj = null 41 | eq undefined, obj?.a().b() 42 | obj = a: -> b: -> nonce 43 | eq nonce , obj?.a().b() 44 | 45 | test "postfix existential operator mixes well with soaked property accesses", -> 46 | eq false, nonexistent?.property? 47 | 48 | test "function invocation with soaked property access", -> 49 | id = (_) -> _ 50 | eq undefined, id nonexistent?.method() 51 | 52 | test "if-to-ternary should safely parenthesize soaked property accesses", -> 53 | ok (if nonexistent?.property then false else true) 54 | 55 | test "#726: don't check for a property on a conditionally-referenced nonexistent thing", -> 56 | eq undefined, nonexistent?[Date()] 57 | 58 | test "#756: conditional assignment edge cases", -> 59 | # TODO: improve this test 60 | a = null 61 | ok isNaN a?.b.c + 1 62 | eq undefined, a?.b.c += 1 63 | eq undefined, ++a?.b.c 64 | eq undefined, delete a?.b.c 65 | 66 | test "operations on soaked properties", -> 67 | # TODO: improve this test 68 | a = b: {c: 0} 69 | eq 1, a?.b.c + 1 70 | eq 1, a?.b.c += 1 71 | eq 2, ++a?.b.c 72 | eq yes, delete a?.b.c 73 | 74 | 75 | # Soaked Method Invocation 76 | 77 | test "soaked method invocation", -> 78 | nonce = {} 79 | counter = 0 80 | obj = 81 | self: -> @ 82 | increment: -> counter++; @ 83 | eq obj , obj.self?() 84 | eq undefined, obj.method?() 85 | eq nonce , obj.self?().property = nonce 86 | eq undefined, obj.method?().property = nonce 87 | eq obj , obj.increment().increment().self?() 88 | eq 2 , counter 89 | 90 | test "#733: conditional assignments", -> 91 | a = b: {c: null} 92 | eq a.b?.c?(), undefined 93 | a.b?.c or= (it) -> it 94 | eq a.b?.c?(1), 1 95 | eq a.b?.c?([2, 3]...), 2 96 | 97 | 98 | # Soaked Function Invocation 99 | 100 | test "soaked function invocation", -> 101 | nonce = {} 102 | id = (_) -> _ 103 | eq nonce , id?(nonce) 104 | eq nonce , (id? nonce) 105 | eq undefined, nonexistent?(nonce) 106 | eq undefined, (nonexistent? nonce) 107 | 108 | test "soaked function invocation with generated functions", -> 109 | nonce = {} 110 | id = (_) -> _ 111 | maybe = (fn, arg) -> if typeof fn is 'function' then () -> fn(arg) 112 | eq maybe(id, nonce)?(), nonce 113 | eq (maybe id, nonce)?(), nonce 114 | eq (maybe false, nonce)?(), undefined 115 | 116 | test "soaked constructor invocation", -> 117 | eq 42 , +new Number? 42 118 | eq undefined, new Other? 42 119 | 120 | test "soaked constructor invocations with caching and property access", -> 121 | semaphore = 0 122 | nonce = {} 123 | class C 124 | constructor: -> 125 | ok false if semaphore 126 | semaphore++ 127 | prop: nonce 128 | eq nonce, (new C())?.prop 129 | eq 1, semaphore 130 | 131 | test "soaked function invocation safe on non-functions", -> 132 | eq undefined, (0)?(1) 133 | eq undefined, (0)? 1, 2 134 | -------------------------------------------------------------------------------- /test/sourcemap.coffee: -------------------------------------------------------------------------------- 1 | return if global.testingBrowser 2 | 3 | SourceMap = require '../src/sourcemap' 4 | 5 | vlqEncodedValues = [ 6 | [1, 'C'], 7 | [-1, 'D'], 8 | [2, 'E'], 9 | [-2, 'F'], 10 | [0, 'A'], 11 | [16, 'gB'], 12 | [948, 'o7B'] 13 | ] 14 | 15 | test "encodeVlq tests", -> 16 | for pair in vlqEncodedValues 17 | eq ((new SourceMap).encodeVlq pair[0]), pair[1] 18 | 19 | test "SourceMap tests", -> 20 | map = new SourceMap 21 | map.add [0, 0], [0, 0] 22 | map.add [1, 5], [2, 4] 23 | map.add [1, 6], [2, 7] 24 | map.add [1, 9], [2, 8] 25 | map.add [3, 0], [3, 4] 26 | 27 | testWithFilenames = map.generate { 28 | sourceRoot: '' 29 | sourceFiles: ['source.coffee'] 30 | generatedFile: 'source.js' 31 | } 32 | 33 | deepEqual testWithFilenames, { 34 | version: 3 35 | file: 'source.js' 36 | sourceRoot: '' 37 | sources: ['source.coffee'] 38 | names: [] 39 | mappings: 'AAAA;;IACK,GAAC,CAAG;IAET' 40 | } 41 | 42 | deepEqual map.generate(), { 43 | version: 3 44 | file: '' 45 | sourceRoot: '' 46 | sources: [''] 47 | names: [] 48 | mappings: 'AAAA;;IACK,GAAC,CAAG;IAET' 49 | } 50 | 51 | # Look up a generated column - should get back the original source position. 52 | arrayEq map.sourceLocation([2,8]), [1,9] 53 | 54 | # Look up a point further along on the same line - should get back the same source position. 55 | arrayEq map.sourceLocation([2,10]), [1,9] 56 | 57 | test "#3075: v3 source map fields", -> 58 | { js, v3SourceMap, sourceMap } = CoffeeScript.compile 'console.log Date.now()', 59 | filename: 'tempus_fugit.coffee' 60 | sourceMap: yes 61 | sourceRoot: './www_root/coffee/' 62 | 63 | v3SourceMap = JSON.parse v3SourceMap 64 | arrayEq v3SourceMap.sources, ['tempus_fugit.coffee'] 65 | eq v3SourceMap.sourceRoot, './www_root/coffee/' 66 | -------------------------------------------------------------------------------- /test/support/helpers.coffee: -------------------------------------------------------------------------------- 1 | # See [http://wiki.ecmascript.org/doku.php?id=harmony:egal](http://wiki.ecmascript.org/doku.php?id=harmony:egal). 2 | egal = (a, b) -> 3 | if a is b 4 | a isnt 0 or 1/a is 1/b 5 | else 6 | a isnt a and b isnt b 7 | 8 | # A recursive functional equivalence helper; uses egal for testing equivalence. 9 | arrayEgal = (a, b) -> 10 | if egal a, b then yes 11 | else if a instanceof Array and b instanceof Array 12 | return no unless a.length is b.length 13 | return no for el, idx in a when not arrayEgal el, b[idx] 14 | yes 15 | 16 | diffOutput = (expectedOutput, actualOutput) -> 17 | expectedOutputLines = expectedOutput.split '\n' 18 | actualOutputLines = actualOutput.split '\n' 19 | for line, i in actualOutputLines 20 | if line isnt expectedOutputLines[i] 21 | actualOutputLines[i] = "#{yellow}#{line}#{reset}" 22 | """Expected generated JavaScript to be: 23 | #{reset}#{expectedOutput}#{red} 24 | but instead it was: 25 | #{reset}#{actualOutputLines.join '\n'}#{red}""" 26 | 27 | exports.eq = (a, b, msg) -> 28 | ok egal(a, b), msg or 29 | "Expected #{reset}#{a}#{red} to equal #{reset}#{b}#{red}" 30 | 31 | exports.arrayEq = (a, b, msg) -> 32 | ok arrayEgal(a, b), msg or 33 | "Expected #{reset}#{a}#{red} to deep equal #{reset}#{b}#{red}" 34 | 35 | exports.eqJS = (input, expectedOutput, msg, {options = {}} = {}) -> 36 | actualOutput = CoffeeScript.compile input, {bare: yes, options...} 37 | .replace /^\s+|\s+$/g, '' # Trim leading/trailing whitespace. 38 | ok egal(expectedOutput, actualOutput), msg or diffOutput expectedOutput, actualOutput 39 | 40 | exports.isWindows = -> process.platform is 'win32' 41 | --------------------------------------------------------------------------------