Permalink
Browse files

Add escapeDirective

  • Loading branch information...
1 parent d1ef7b9 commit 40a394771c9a741181ccfd546a1e116102e6acfe @Constellation Constellation committed Oct 10, 2012
Showing with 90 additions and 9 deletions.
  1. +30 −1 escodegen.js
  2. +60 −8 test/directive.js
View
@@ -453,6 +453,31 @@
return result;
}
+ function escapeDirective(str) {
+ var i, iz, ch, single, buf, quote;
+
+ buf = str;
+ if (typeof buf[0] === 'undefined') {
+ buf = stringToArray(buf);
+ }
+
+ quote = quotes === 'double' ? '"' : '\'';
+ for (i = 0, iz = buf.length; i < iz; i += 1) {
+ ch = buf[i];
+ if (ch === '\'') {
+ quote = '"';
+ break;
+ } else if (ch === '"') {
+ quote = '\'';
+ break;
+ } else if (ch === '\\') {
+ i += 1;
+ }
+ }
+
+ return quote + str + quote;
+ }
+
function escapeString(str) {
var result = '', i, len, ch, next, singleQuotes = 0, doubleQuotes = 0, single;
@@ -1196,7 +1221,11 @@
break;
case Syntax.DirectiveStatement:
- result = escapeString(stmt.directive) + semicolon;
+ if (stmt.raw) {
+ result = stmt.raw + semicolon;
+ } else {
+ result = escapeDirective(stmt.directive) + semicolon;
+ }
break;
case Syntax.DoWhileStatement:
View
@@ -38,10 +38,7 @@ data = {
type: 'Program',
body: [{
type: 'DirectiveStatement',
- directive: {
- type: 'Literal',
- value: 'use strict',
- }
+ directive: 'use strict',
}]
},
@@ -104,10 +101,65 @@ data = {
type: 'BlockStatement',
body: [{
type: 'DirectiveStatement',
- directive: {
- type: 'Literal',
- value: 'use strict',
- }
+ directive: 'use strict',
+ }]
+ }
+ }
+ }]
+ },
+
+ '(function () {\n "use strict";\n});': {
+ type: 'Program',
+ body: [{
+ type: 'ExpressionStatement',
+ expression: {
+ type: 'FunctionExpression',
+ id: null,
+ params: [],
+ body: {
+ type: 'BlockStatement',
+ body: [{
+ type: 'DirectiveStatement',
+ directive: 'use strict',
+ raw: '"use strict"'
+ }]
+ }
+ }
+ }]
+ },
+
+ '(function () {\n \'use\\u0020strict\';\n});': {
+ type: 'Program',
+ body: [{
+ type: 'ExpressionStatement',
+ expression: {
+ type: 'FunctionExpression',
+ id: null,
+ params: [],
+ body: {
+ type: 'BlockStatement',
+ body: [{
+ type: 'DirectiveStatement',
+ directive: 'use\\u0020strict',
+ }]
+ }
+ }
+ }]
+ },
+
+ '(function () {\n "use\\u0020strict\'";\n});': {
+ type: 'Program',
+ body: [{
+ type: 'ExpressionStatement',
+ expression: {
+ type: 'FunctionExpression',
+ id: null,
+ params: [],
+ body: {
+ type: 'BlockStatement',
+ body: [{
+ type: 'DirectiveStatement',
+ directive: 'use\\u0020strict\'',
}]
}
}

0 comments on commit 40a3947

Please sign in to comment.