Skip to content

Commit 6a65aac

Browse files
committed
Merge pull request #103 from eslint/issue96
Fix: Disallow octals in template strings (fixes #96)
2 parents 08e1b33 + 4f763b0 commit 6a65aac

File tree

5 files changed

+45
-34
lines changed

5 files changed

+45
-34
lines changed

espree.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,6 @@ function scanTemplate() {
974974
var cooked = "",
975975
ch,
976976
escapedSequence,
977-
octal = false,
978977
start = index,
979978
terminated = false,
980979
tail = false,
@@ -999,8 +998,13 @@ function scanTemplate() {
999998
} else if (ch === "\\") {
1000999
ch = source[index++];
10011000
escapedSequence = scanEscapeSequence(ch);
1001+
1002+
if (escapedSequence.octal) {
1003+
throwError({}, Messages.TemplateOctalLiteral);
1004+
}
1005+
10021006
cooked += escapedSequence.ch;
1003-
octal = escapedSequence.octal || octal;
1007+
10041008
} else if (syntax.isLineTerminator(ch.charCodeAt(0))) {
10051009
++lineNumber;
10061010
if (ch === "\r" && source[index] === "\n") {
@@ -1037,7 +1041,6 @@ function scanTemplate() {
10371041
},
10381042
head: head,
10391043
tail: tail,
1040-
octal: octal,
10411044
lineNumber: lineNumber,
10421045
lineStart: lineStart,
10431046
range: [start, index]
@@ -2770,10 +2773,6 @@ function parseTemplateElement(option) {
27702773
marker = markerCreate();
27712774
token = lex();
27722775

2773-
if (strict && token.octal) {
2774-
throwError(token, Messages.StrictOctalLiteral);
2775-
}
2776-
27772776
return markerApply(
27782777
marker,
27792778
astNodeFactory.createTemplateElement(

lib/messages.js

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ module.exports = {
6868
StrictVarName: "Variable name may not be eval or arguments in strict mode",
6969
StrictParamName: "Parameter name eval or arguments is not allowed in strict mode",
7070
StrictParamDupe: "Strict mode function may not have duplicate parameter names",
71+
TemplateOctalLiteral: "Octal literals are not allowed in template strings.",
7172
ParameterAfterRestParameter: "Rest parameter must be final parameter of an argument list",
7273
DefaultRestParameter: "Rest parameter can not have a default value",
7374
ElementAfterSpreadElement: "Spread must be the final element of an element list",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
"index":4,
3+
"lineNumber":1,
4+
"column":5,
5+
"description":"Octal literals are not allowed in template strings."
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`\07`;

tests/lib/ecma-features.js

+31-27
Original file line numberDiff line numberDiff line change
@@ -122,46 +122,50 @@ describe("ecmaFeatures", function() {
122122
});
123123
});
124124

125-
leche.withData(moduleTestFiles, function(filename) {
125+
describe("Modules", function() {
126126

127-
var code = shelljs.cat(path.resolve(FIXTURES_DIR, filename) + ".src.js");
127+
leche.withData(moduleTestFiles, function(filename) {
128128

129-
it("should parse correctly when sourceType is module", function() {
130-
var expected = require(path.resolve(__dirname, "../../", FIXTURES_DIR, filename) + ".result.js");
131-
var result;
129+
var code = shelljs.cat(path.resolve(FIXTURES_DIR, filename) + ".src.js");
132130

133-
config.sourceType = "module";
131+
it("should parse correctly when sourceType is module", function() {
132+
var expected = require(path.resolve(__dirname, "../../", FIXTURES_DIR, filename) + ".result.js");
133+
var result;
134134

135-
// set sourceType of program node to module
136-
if (expected.type === "Program") {
137-
expected.sourceType = "module";
138-
}
135+
config.sourceType = "module";
139136

140-
try {
141-
result = espree.parse(code, config);
142-
} catch (ex) {
137+
// set sourceType of program node to module
138+
if (expected.type === "Program") {
139+
expected.sourceType = "module";
140+
}
143141

144-
// if the result is an error, create an error object so deepEqual works
145-
if (expected.message || expected.description) {
142+
try {
143+
result = espree.parse(code, config);
144+
} catch (ex) {
146145

147-
var expectedError = new Error(expected.message || expected.description);
148-
Object.keys(expected).forEach(function(key) {
149-
expectedError[key] = expected[key];
150-
});
151-
expected = expectedError;
152-
} else {
153-
throw ex;
154-
}
146+
// if the result is an error, create an error object so deepEqual works
147+
if (expected.message || expected.description) {
155148

156-
result = ex; // if an error is thrown, match the error
149+
var expectedError = new Error(expected.message || expected.description);
150+
Object.keys(expected).forEach(function(key) {
151+
expectedError[key] = expected[key];
152+
});
153+
expected = expectedError;
154+
} else {
155+
throw ex;
156+
}
157157

158-
}
159-
assert.deepEqual(result, expected);
160-
});
158+
result = ex; // if an error is thrown, match the error
159+
160+
}
161+
assert.deepEqual(result, expected);
162+
});
161163

164+
});
162165
});
163166

164167

168+
165169
leche.withData(mixFiles, function(filename) {
166170

167171
var features = path.dirname(filename),

0 commit comments

Comments
 (0)