Repository navigation
Octal escape sequences should be a lexical/syntactic error in strict mode and ES5 #396
Description
Activity
- addedHelp WantedYou can do thisYou can do this
on Apr 21, 2015 - addedGood First IssueWell scoped, documented and has the green lightWell scoped, documented and has the green light
on Sep 29, 2015 This has become an a problem since 1.8 release. I had ANSI escape codes in my TypeScript files, which were compiled to JS just fine, but 1.8 started adding
use strictand all of a sudden node started freaking out.I think a compiler error is due here if
use strictis inserted.I don't quite understand the status of this issue? It is still present in
2.1.4. Is the TypeScript leaving this for the community to fix? (if so, fine...I just want to know).I've run into this a number of times when using Artem Basov (@basart)'s
typestylepackage to import raw css via template strings. It is very unintuitive that template strings allow embedded octal literals (since the contents of the template string are NOT TypeScript or Javascript). Nevertheless, that is the standard behavior. As such, the TypeScript compiler shouldn't be outputting invalid template strings (i.e., in my opinion the TypeScript compiler should consider an invalidate template string as an error and not emit it). It is particularly nasty when you run this gets passed through bothtscandwebpackonly to throw an error in the browser. At that point, it is so far downstream it is often difficult for people to trace things back to the original issue.sandersn commented
on Jan 12, 2017 MemberMore actionsYes, that is what the 'Accepting PRs' label means.
Nathan Shively-Sanders (@sandersn) What's the process for adding new error messages? Just make something up?
6 remaining items
how would you test the change then?
I could add a test for #24209 and verify that
x as "\1"is assignable toy as "\x01", or something like that. I don't actually care about that type, but I do care about thetextfield in the AST being correct.Note that octals are disallowed completely (
SyntaxError) in template literal strings, whether in strict mode or not. They're technically only deprecated in normal ES5 code. So it should definitely be an error in template literals, and possibly just a warning outside of them.Also this may be obvious but
'\0'is not an octal sequence while all other one, two, or three-digit forms are, ie'\00'and'\1'.Here's a RegEx that includes all valid octals and excludes all invalid ones with the notable exception of
\0:/\\[0-3]?[0-7]{1,2}/g. Try it.elibarzilay commented
on Jun 3, 2021 ContributorMore actionsList of things to do for resolving this:
-
parser.ts:- Not really needed, but there is a comment here that is no longer relevant and should be removed:
// Octal literals are not allowed...
- Not really needed, but there is a comment here that is no longer relevant and should be removed:
-
scanner.ts:- At the end of
scanEscapeSequence()should add:but...if (isDigit(ch)) tokenFlags |= TokenFlags.ContainsInvalidEscape;
- At the end of
-
types.ts:ContainsInvalidEscape = 1 << 11is defined here but it's unused!TemplateLiteralLikeFlags = ContainsInvalidEscape⇒ this is fishy, probably should be its own value
-
binder.ts:- See
checkStrictModeNumericLiteraland make a similar checker for strings with theContainsInvalidEscapeflag.
- See
One decision to make is whether to allow octal escapes in TS when not producing strict-mode code. It would probably be easier to forbid them anyway (especially since they're never allowed in template strings) but if not, then the scanner mode should also parse the octal escapes in case the output is not strict.
Reacted by SlurpTheo-
Eli Barzilay (@elibarzilay)
Can I work on this issue?
Am I correct in understanding that I should work on the to-do list that you have written?
This is my first time contributing to Typescript and I'm looking forward to working on it!Yeah, just give it a shot and see how it goes 👍🏻
Reacted by Rento EzoeThank you! I'll try!
Eli Barzilay (@elibarzilay) Orta Therox (@orta)
Can I consider a fix to disallow octal escaping even if it is not strict-mode?
Do you have any concerns about this?(Is this a topic that should be discussed in PR rather than in this Issue?)
I'm afraid that would be over-stepping, as they are allowed in those modes: #396 (comment)
I see, too much.
Thanks for the quick reply!One decision to make is whether to allow octal escapes in TS when not producing strict-mode code. It would probably be easier to forbid them anyway (especially since they're never allowed in template strings)
#396 (comment)
↑I was just trying to make sure about this.So, is it the policy that octal escaping is allowed (or left allowed) when not in strict-mode?
I'm sorry if I misunderstood.Orta Therox (@orta)
#396 (comment)
If you could confirm this, it would be greatly appreciated!- locked as resolved and limited conversation to collaborators
on Oct 21, 2025
The following code should be disallowed in strict mode and ES5:
This is disallowed by Annex B.1.2 of the EcmaScript 5 spec.