Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove strict mode and target check; Refactoring
  • Loading branch information
graphemecluster committed Dec 14, 2022
commit cecc6ef9ae9f1752d499067eb74674cf59a98844
19 changes: 0 additions & 19 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ import {
nodeIsPresent,
NonNullChain,
NonNullExpression,
NumericLiteral,
objectAllocator,
ObjectLiteralExpression,
OptionalChain,
Expand Down Expand Up @@ -293,7 +292,6 @@ import {
TextRange,
ThisExpression,
ThrowStatement,
TokenFlags,
tokenToString,
tracing,
TracingNode,
Expand Down Expand Up @@ -2565,19 +2563,6 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
}
}

function checkStrictModeStringLiteral(node: StringLiteral) {
if (languageVersion >= ScriptTarget.ES5 && inStrictMode && node.rangesOfOctalSequences) {
file.bindDiagnostics.push(...node.rangesOfOctalSequences.map(
range => createFileDiagnostic(file, range.pos, range.end - range.pos, Diagnostics.Octal_escape_sequences_are_not_allowed_in_strict_mode)));
}
}

function checkStrictModeNumericLiteral(node: NumericLiteral) {
if (languageVersion >= ScriptTarget.ES5 && inStrictMode && node.numericLiteralFlags & TokenFlags.Octal) {
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode));
}
}

function checkStrictModePostfixUnaryExpression(node: PostfixUnaryExpression) {
// Grammar checking
// The identifier eval or arguments may not appear as the LeftHandSideExpression of an
Expand Down Expand Up @@ -2822,10 +2807,6 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
return checkStrictModeCatchClause(node as CatchClause);
case SyntaxKind.DeleteExpression:
return checkStrictModeDeleteExpression(node as DeleteExpression);
case SyntaxKind.StringLiteral:
return checkStrictModeStringLiteral(node as StringLiteral);
case SyntaxKind.NumericLiteral:
return checkStrictModeNumericLiteral(node as NumericLiteral);
case SyntaxKind.PostfixUnaryExpression:
return checkStrictModePostfixUnaryExpression(node as PostfixUnaryExpression);
case SyntaxKind.PrefixUnaryExpression:
Expand Down
28 changes: 1 addition & 27 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ import {
isCatchClause,
isCatchClauseVariableDeclarationOrBindingElement,
isCheckJsEnabledForFile,
isChildOfNodeWithKind,
isClassDeclaration,
isClassElement,
isClassExpression,
Expand Down Expand Up @@ -46944,33 +46943,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return false;
}

function checkGrammarNumericLiteral(node: NumericLiteral): boolean {
// Grammar checking
if (node.numericLiteralFlags & TokenFlags.Octal) {
let diagnosticMessage: DiagnosticMessage | undefined;
if (languageVersion >= ScriptTarget.ES5) {
diagnosticMessage = Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher_Use_the_syntax_0;
}
else if (isChildOfNodeWithKind(node, SyntaxKind.LiteralType)) {
diagnosticMessage = Diagnostics.Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0;
}
else if (isChildOfNodeWithKind(node, SyntaxKind.EnumMember)) {
diagnosticMessage = Diagnostics.Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0;
}
if (diagnosticMessage) {
const withMinus = isPrefixUnaryExpression(node.parent) && node.parent.operator === SyntaxKind.MinusToken;
const literal = (withMinus ? "-" : "") + "0o" + node.text;
return grammarErrorOnNode(withMinus ? node.parent : node, diagnosticMessage, literal);
}
}

function checkGrammarNumericLiteral(node: NumericLiteral) {
// Realism (size) checking
checkNumericLiteralValueSize(node);

return false;
}

function checkNumericLiteralValueSize(node: NumericLiteral) {
// We should test against `getTextOfNode(node)` rather than `node.text`, because `node.text` for large numeric literals can contain "."
// e.g. `node.text` for numeric literal `1100000000000000000000` is `1.1e21`.
const isFractional = getTextOfNode(node).indexOf(".") !== -1;
Expand Down
22 changes: 7 additions & 15 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@
"category": "Error",
"code": 1084
},
"Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '{0}'.": {
"category": "Error",
"code": 1085
},
"'{0}' modifier cannot appear on a constructor declaration.": {
"category": "Error",
"code": 1089
Expand Down Expand Up @@ -351,7 +347,7 @@
"category": "Error",
"code": 1120
},
"Octal literals are not allowed in strict mode.": {
"Octal literals are not allowed. Use the syntax '{0}'.": {
"category": "Error",
"code": 1121
},
Expand Down Expand Up @@ -1549,14 +1545,18 @@
"category": "Message",
"code": 1483
},
"Octal escape sequences are not allowed in strict mode.": {
"Octal escape sequences are not allowed. Use the syntax '{0}'.": {
"category": "Error",
"code": 1484
},
"Octal escape sequences are not allowed in template strings.": {
"'{0}' is not allowed.": {
"category": "Error",
"code": 1485
},
"Decimals with leading zeros are not allowed.": {
"category": "Error",
"code": 1486
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
Expand Down Expand Up @@ -6372,14 +6372,6 @@
"category": "Error",
"code": 8016
},
"Octal literal types must use ES2015 syntax. Use the syntax '{0}'.": {
"category": "Error",
"code": 8017
},
"Octal literals are not allowed in enums members initializer. Use the syntax '{0}'.": {
"category": "Error",
"code": 8018
},
"Report errors in .js files.": {
"category": "Message",
"code": 8019
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1112,11 +1112,10 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
}

// @api
function createStringLiteral(text: string, isSingleQuote?: boolean, hasExtendedUnicodeEscape?: boolean, rangesOfOctalSequences?: ReadonlyTextRange[]): StringLiteral {
function createStringLiteral(text: string, isSingleQuote?: boolean, hasExtendedUnicodeEscape?: boolean): StringLiteral {
const node = createBaseStringLiteral(text, isSingleQuote);
node.hasExtendedUnicodeEscape = hasExtendedUnicodeEscape;
if (hasExtendedUnicodeEscape) node.transformFlags |= TransformFlags.ContainsES2015;
node.rangesOfOctalSequences = rangesOfOctalSequences;
return node;
}

Expand Down
18 changes: 12 additions & 6 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2098,8 +2098,8 @@ namespace Parser {
parseErrorAt(range.pos, range.end, message, arg0);
}

function scanError(message: DiagnosticMessage, length: number): void {
parseErrorAtPosition(scanner.getTextPos(), length, message);
function scanError(message: DiagnosticMessage, length: number, arg0?: any): void {
parseErrorAtPosition(scanner.getTextPos(), length, message, arg0);
}

function getNodePos(): number {
Expand Down Expand Up @@ -2154,6 +2154,10 @@ namespace Parser {
return currentToken = scanner.reScanTemplateToken(isTaggedTemplate);
}

function reScanTemplateHeadOrNoSubstitutionTemplate(): SyntaxKind {
return currentToken = scanner.reScanTemplateHeadOrNoSubstitutionTemplate();
}

function reScanLessThanToken(): SyntaxKind {
return currentToken = scanner.reScanLessThanToken();
}
Expand Down Expand Up @@ -3603,7 +3607,9 @@ namespace Parser {
}

function parseTemplateHead(isTaggedTemplate: boolean): TemplateHead {
reScanTemplateToken(isTaggedTemplate);
if (!isTaggedTemplate && scanner.getTokenFlags() & TokenFlags.ContainsInvalidEscape) {
reScanTemplateHeadOrNoSubstitutionTemplate();
}
const fragment = parseLiteralLikeNode(token());
Debug.assert(fragment.kind === SyntaxKind.TemplateHead, "Template head has wrong token kind");
return fragment as TemplateHead;
Expand Down Expand Up @@ -3631,7 +3637,7 @@ namespace Parser {
// We also do not need to check for negatives because any prefix operator would be part of a
// parent unary expression.
kind === SyntaxKind.NumericLiteral ? factory.createNumericLiteral(scanner.getTokenValue(), scanner.getNumericLiteralFlags()) :
kind === SyntaxKind.StringLiteral ? factory.createStringLiteral(scanner.getTokenValue(), /*isSingleQuote*/ undefined, scanner.hasExtendedUnicodeEscape(), scanner.getRangesOfOctalSequences()) :
kind === SyntaxKind.StringLiteral ? factory.createStringLiteral(scanner.getTokenValue(), /*isSingleQuote*/ undefined, scanner.hasExtendedUnicodeEscape()) :
isLiteralKind(kind) ? factory.createLiteralLikeNode(kind, scanner.getTokenValue()) :
Debug.fail();

Expand Down Expand Up @@ -6409,8 +6415,8 @@ namespace Parser {
function parsePrimaryExpression(): PrimaryExpression {
switch (token()) {
case SyntaxKind.NoSubstitutionTemplateLiteral:
if (scanner.getTokenFlags() & TokenFlags.ContainsOctalOrInvalidEscape) {
reScanTemplateToken(/* isTaggedTemplate */ false);
if (scanner.getTokenFlags() & TokenFlags.ContainsInvalidEscape) {
reScanTemplateHeadOrNoSubstitutionTemplate();
}
// falls through
case SyntaxKind.NumericLiteral:
Expand Down
1 change: 0 additions & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,6 @@ export const plainJSErrors: Set<number> = new Set([
Diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode.code,
Diagnostics.Invalid_use_of_0_in_strict_mode.code,
Diagnostics.A_label_is_not_allowed_here.code,
Diagnostics.Octal_literals_are_not_allowed_in_strict_mode.code,
Diagnostics.with_statements_are_not_allowed_in_strict_mode.code,
// grammar errors
Diagnostics.A_break_statement_can_only_be_used_within_an_enclosing_iteration_or_switch_statement.code,
Expand Down
Loading