sprockets-strict-mode 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Jakub Suder
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # Sprockets Strict Mode
2
+
3
+ `sprockets-strict-mode` is a Sprockets processor that enables ECMAScript 5 strict mode for your JavaScript files.
4
+
5
+
6
+ ## What's strict mode?
7
+
8
+ The ES5 version of JavaScript introduced something called ["strict mode"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode). It's a directive that you can add to your JavaScript files, which makes the browser use a slightly stricter parser that prevents code from doing various things which are considered harmful or simply are errors that JavaScript previously didn't complain about. This includes things like:
9
+
10
+ - modifying or deleting object properties that should not be modified or deleted
11
+ - using the `with` keyword
12
+ - implicitly adding fields to `window` when you forget to add `var`
13
+
14
+ Instead of silently ignoring those, the browser will now throw an error that you can see in the JS console. While it's not really a replacement for [JSLint](http://www.jslint.com) or [JSHint](http://www.jshint.com), it can help you detect things like objects leaking to the global scope. It should also help you prepare your code to be compatible with future versions of JavaScript, and generally make your code a bit more "proper" (if you care about such things).
15
+
16
+ So how do you enable this strict mode? It's simple, you just need to add this line to the beginning of a file (or function) to make it strict:
17
+
18
+ ```js
19
+ "use strict";
20
+ ```
21
+
22
+ Yes, it's just an unassigned string. It doesn't do anything by itself, but it lets the JS interpreter know that you want to enable strict mode.
23
+
24
+ So why almost no one uses this feature? Mostly because people don't know (or care) about it, but also because you can't enable it globally and it's a bit annoying to have to type this again and again in every file.
25
+
26
+ But what if you could enable it globally? That's basically what this tiny gem does.
27
+
28
+
29
+ ## The processor
30
+
31
+ `sprockets-strict-mode` is an add-on to Sprockets (which is used by the Rails asset pipeline). You set it up as a postprocessor for JavaScript files, which means it acts as an invisible filter that all JavaScript files go through after they're processed by all the standard processors. The only thing it changes in those files is that it adds the strict mode line at the beginning. It skips empty files and files generated from CoffeeScript, since those are automatically strict mode compatible.
32
+
33
+ If you only use CoffeeScript to write your frontend, you DO NOT need this gem. (You might want to use it though if you have a mix of JavaScript and CoffeeScript files.)
34
+
35
+
36
+ ## Setting up
37
+
38
+ Add the gem to your gemfile:
39
+
40
+ ```rb
41
+ gem 'sprockets-strict-mode'
42
+ ```
43
+
44
+ Then tell Sprockets to use it, e.g. in `application.rb`, or even better, in an initializer:
45
+
46
+ ```rb
47
+ Rails.application.assets.register_postprocessor('application/javascript', Sprockets::StrictMode)
48
+ ```
49
+
50
+ I'd recommend to only enable this processor in development mode, not in production (but it's your choice).
51
+
52
+
53
+ ## Credits
54
+
55
+ Created by [Jakub Suder](http://psionides.eu), licensed under MIT License.
56
+
@@ -0,0 +1 @@
1
+ require 'sprockets/strict_mode'
@@ -0,0 +1,26 @@
1
+ require 'sprockets/processor'
2
+
3
+ module Sprockets
4
+ class StrictMode < Processor
5
+ STRICT_MODE_HEADER = %("use strict";\n\n)
6
+
7
+ def evaluate(context, locals)
8
+ add_strict_mode?(context) ? (STRICT_MODE_HEADER + data) : data
9
+ end
10
+
11
+
12
+ private
13
+
14
+ def add_strict_mode?(context)
15
+ !empty_file? && !coffeescript?(context)
16
+ end
17
+
18
+ def empty_file?
19
+ data.strip.empty?
20
+ end
21
+
22
+ def coffeescript?(context)
23
+ context.pathname.basename.to_s =~ /\.coffee(\.|$)/
24
+ end
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets-strict-mode
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jakub Suder
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sprockets
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description:
31
+ email: jakub.suder@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - MIT-LICENSE.txt
37
+ - README.markdown
38
+ - lib/sprockets/strict_mode.rb
39
+ - lib/sprockets-strict-mode.rb
40
+ homepage: http://github.com/jsuder/sprockets-strict-mode
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.25
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: A Sprockets processor that adds "use strict" to your JavaScript files
64
+ test_files: []