Skip to content

Commit d561370

Browse files
authored
Remove ScriptTarget from source affecting options (#1205)
1 parent 212b45a commit d561370

File tree

39 files changed

+376
-183
lines changed

39 files changed

+376
-183
lines changed

internal/api/encoder/encoder_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@ import (
1717
"gotest.tools/v3/assert"
1818
)
1919

20-
var parseCompilerOptions = core.SourceFileAffectingCompilerOptions{
21-
EmitScriptTarget: core.ScriptTargetLatest,
22-
}
23-
2420
func TestEncodeSourceFile(t *testing.T) {
2521
t.Parallel()
2622
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
27-
FileName: "/test.ts",
28-
Path: "/test.ts",
29-
CompilerOptions: parseCompilerOptions,
23+
FileName: "/test.ts",
24+
Path: "/test.ts",
3025
}, "import { bar } from \"bar\";\nexport function foo<T, U>(a: string, b: string): any {}\nfoo();", core.ScriptKindTS)
3126
t.Run("baseline", func(t *testing.T) {
3227
t.Parallel()
@@ -46,9 +41,8 @@ func BenchmarkEncodeSourceFile(b *testing.B) {
4641
fileContent, err := os.ReadFile(filePath)
4742
assert.NilError(b, err)
4843
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
49-
FileName: "/checker.ts",
50-
Path: "/checker.ts",
51-
CompilerOptions: parseCompilerOptions,
44+
FileName: "/checker.ts",
45+
Path: "/checker.ts",
5246
}, string(fileContent), core.ScriptKindTS)
5347

5448
for b.Loop() {

internal/ast/ast.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10053,10 +10053,6 @@ func (node *SourceFile) ParseOptions() SourceFileParseOptions {
1005310053
return node.parseOptions
1005410054
}
1005510055

10056-
func (node *SourceFile) LanguageVersion() core.ScriptTarget {
10057-
return node.parseOptions.CompilerOptions.EmitScriptTarget
10058-
}
10059-
1006010056
func (node *SourceFile) Text() string {
1006110057
return node.text
1006210058
}

internal/astnav/tokens_test.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ var testFiles = []string{
2525
filepath.Join(repo.TypeScriptSubmodulePath, "src/services/mapCode.ts"),
2626
}
2727

28-
var parseCompilerOptions = core.SourceFileAffectingCompilerOptions{
29-
EmitScriptTarget: core.ScriptTargetLatest,
30-
}
31-
3228
func TestGetTokenAtPosition(t *testing.T) {
3329
t.Parallel()
3430
repo.SkipIfNoTypeScriptSubmodule(t)
@@ -57,9 +53,8 @@ func TestGetTokenAtPosition(t *testing.T) {
5753
}
5854
`
5955
file := parser.ParseSourceFile(ast.SourceFileParseOptions{
60-
FileName: "/file.ts",
61-
Path: "/file.ts",
62-
CompilerOptions: parseCompilerOptions,
56+
FileName: "/file.ts",
57+
Path: "/file.ts",
6358
}, fileText, core.ScriptKindTS)
6459
assert.Equal(t, astnav.GetTokenAtPosition(file, 0), astnav.GetTokenAtPosition(file, 0))
6560
})
@@ -96,9 +91,8 @@ func baselineTokens(t *testing.T, testName string, includeEOF bool, getTSTokens
9691
}
9792
tsTokens := getTSTokens(string(fileText), positions)
9893
file := parser.ParseSourceFile(ast.SourceFileParseOptions{
99-
FileName: "/file.ts",
100-
Path: "/file.ts",
101-
CompilerOptions: parseCompilerOptions,
94+
FileName: "/file.ts",
95+
Path: "/file.ts",
10296
}, string(fileText), core.ScriptKindTS)
10397

10498
var output strings.Builder
@@ -433,9 +427,8 @@ export function isAnyDirectorySeparator(charCode: number): boolean {
433427
t.Run(testCase.name, func(t *testing.T) {
434428
t.Parallel()
435429
file := parser.ParseSourceFile(ast.SourceFileParseOptions{
436-
FileName: "/file.ts",
437-
Path: "/file.ts",
438-
CompilerOptions: parseCompilerOptions,
430+
FileName: "/file.ts",
431+
Path: "/file.ts",
439432
}, testCase.fileContent, core.ScriptKindTS)
440433
token := astnav.FindPrecedingToken(file, testCase.position)
441434
assert.Equal(t, token.Kind, testCase.expectedKind)

internal/binder/binder.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ const (
4242

4343
type Binder struct {
4444
file *ast.SourceFile
45-
languageVersion core.ScriptTarget
4645
bindFunc func(*ast.Node) bool
4746
unreachableFlow *ast.FlowNode
4847
reportedUnreachableFlow *ast.FlowNode
@@ -121,7 +120,6 @@ func bindSourceFile(file *ast.SourceFile) {
121120
b := getBinder()
122121
defer putBinder(b)
123122
b.file = file
124-
b.languageVersion = b.options().EmitScriptTarget
125123
b.inStrictMode = b.options().BindInStrictMode && !file.IsDeclarationFile || ast.IsExternalModule(file)
126124
b.unreachableFlow = b.newFlowNode(ast.FlowFlagsUnreachable)
127125
b.reportedUnreachableFlow = b.newFlowNode(ast.FlowFlagsUnreachable)
@@ -1181,7 +1179,6 @@ func (b *Binder) bindParameter(node *ast.Node) {
11811179
func (b *Binder) bindFunctionDeclaration(node *ast.Node) {
11821180
b.checkStrictModeFunctionName(node)
11831181
if b.inStrictMode {
1184-
b.checkStrictModeFunctionDeclaration(node)
11851182
b.bindBlockScopedDeclaration(node, ast.SymbolFlagsFunction, ast.SymbolFlagsFunctionExcludes)
11861183
} else {
11871184
b.declareSymbolAndAddToSymbolTable(node, ast.SymbolFlagsFunction, ast.SymbolFlagsFunctionExcludes)
@@ -1364,17 +1361,6 @@ func (b *Binder) checkStrictModeFunctionName(node *ast.Node) {
13641361
}
13651362
}
13661363

1367-
func (b *Binder) checkStrictModeFunctionDeclaration(node *ast.Node) {
1368-
if b.languageVersion < core.ScriptTargetES2015 {
1369-
// Report error if function is not top level function declaration
1370-
if b.blockScopeContainer.Kind != ast.KindSourceFile && b.blockScopeContainer.Kind != ast.KindModuleDeclaration && !ast.IsFunctionLikeOrClassStaticBlockDeclaration(b.blockScopeContainer) {
1371-
// We check first if the name is inside class declaration or class expression; if so give explicit message
1372-
// otherwise report generic error message.
1373-
b.errorOnNode(node, b.getStrictModeBlockScopeFunctionDeclarationMessage(node))
1374-
}
1375-
}
1376-
}
1377-
13781364
func (b *Binder) getStrictModeBlockScopeFunctionDeclarationMessage(node *ast.Node) *diagnostics.Message {
13791365
// Provide specialized messages to help the user understand why we think they're in strict mode.
13801366
if ast.GetContainingClass(node) != nil {
@@ -1443,7 +1429,7 @@ func (b *Binder) checkStrictModeWithStatement(node *ast.Node) {
14431429

14441430
func (b *Binder) checkStrictModeLabeledStatement(node *ast.Node) {
14451431
// Grammar checking for labeledStatement
1446-
if b.inStrictMode && b.options().EmitScriptTarget >= core.ScriptTargetES2015 {
1432+
if b.inStrictMode {
14471433
data := node.AsLabeledStatement()
14481434
if ast.IsDeclarationStatement(data.Statement) || ast.IsVariableStatement(data.Statement) {
14491435
b.errorOnFirstToken(data.Label, diagnostics.A_label_is_not_allowed_here)

internal/checker/jsx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ func (c *Checker) getJsxFragmentFactoryEntity(location *ast.Node) *ast.EntityNam
14221422
}
14231423

14241424
func (c *Checker) parseIsolatedEntityName(name string) *ast.Node {
1425-
result := parser.ParseIsolatedEntityName(name, c.languageVersion)
1425+
result := parser.ParseIsolatedEntityName(name)
14261426
if result != nil {
14271427
markAsSynthetic(result)
14281428
}

internal/core/compileroptions.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ type SourceFileAffectingCompilerOptions struct {
355355
AllowUnreachableCode Tristate
356356
AllowUnusedLabels Tristate
357357
BindInStrictMode bool
358-
EmitScriptTarget ScriptTarget
359358
ShouldPreserveConstEnums bool
360359
}
361360

@@ -365,7 +364,6 @@ func (options *CompilerOptions) SourceFileAffecting() SourceFileAffectingCompile
365364
AllowUnreachableCode: options.AllowUnreachableCode,
366365
AllowUnusedLabels: options.AllowUnusedLabels,
367366
BindInStrictMode: options.AlwaysStrict.IsTrue() || options.Strict.IsTrue(),
368-
EmitScriptTarget: options.GetEmitScriptTarget(),
369367
ShouldPreserveConstEnums: options.ShouldPreserveConstEnums(),
370368
}
371369
})

internal/execute/tsc.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,8 @@ func fmtMain(sys System, input, output string) ExitStatus {
7474
text := fileContent
7575
pathified := tspath.ToPath(input, sys.GetCurrentDirectory(), true)
7676
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
77-
FileName: string(pathified),
78-
Path: pathified,
79-
CompilerOptions: core.SourceFileAffectingCompilerOptions{
80-
EmitScriptTarget: core.ScriptTargetLatest,
81-
},
77+
FileName: string(pathified),
78+
Path: pathified,
8279
JSDocParsingMode: ast.JSDocParsingModeParseAll,
8380
}, text, core.GetScriptKindFromFileName(string(pathified)))
8481
ast.SetParentInChildren(sourceFile.AsNode())

internal/format/api_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ func applyBulkEdits(text string, edits []core.TextChange) string {
3333
return b.String()
3434
}
3535

36-
var parseCompilerOptions = core.SourceFileAffectingCompilerOptions{
37-
EmitScriptTarget: core.ScriptTargetLatest,
38-
}
39-
4036
func TestFormat(t *testing.T) {
4137
t.Parallel()
4238

@@ -60,9 +56,8 @@ func TestFormat(t *testing.T) {
6056
assert.NilError(t, err)
6157
text := string(fileContent)
6258
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
63-
FileName: "/checker.ts",
64-
Path: "/checker.ts",
65-
CompilerOptions: parseCompilerOptions,
59+
FileName: "/checker.ts",
60+
Path: "/checker.ts",
6661
}, text, core.ScriptKindTS)
6762
ast.SetParentInChildren(sourceFile.AsNode())
6863
edits := format.FormatDocument(ctx, sourceFile)
@@ -91,9 +86,8 @@ func BenchmarkFormat(b *testing.B) {
9186
assert.NilError(b, err)
9287
text := string(fileContent)
9388
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
94-
FileName: "/checker.ts",
95-
Path: "/checker.ts",
96-
CompilerOptions: parseCompilerOptions,
89+
FileName: "/checker.ts",
90+
Path: "/checker.ts",
9791
}, text, core.ScriptKindTS)
9892
ast.SetParentInChildren(sourceFile.AsNode())
9993

internal/parser/parser.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,10 @@ func (p *Parser) parseJSONText() *ast.SourceFile {
175175
return result
176176
}
177177

178-
func ParseIsolatedEntityName(text string, languageVersion core.ScriptTarget) *ast.EntityName {
178+
func ParseIsolatedEntityName(text string) *ast.EntityName {
179179
p := getParser()
180180
defer putParser(p)
181181
p.initializeState(ast.SourceFileParseOptions{
182-
CompilerOptions: core.SourceFileAffectingCompilerOptions{
183-
EmitScriptTarget: languageVersion,
184-
},
185182
JSDocParsingMode: ast.JSDocParsingModeParseAll,
186183
}, text, core.ScriptKindJS)
187184
p.nextToken()
@@ -213,7 +210,6 @@ func (p *Parser) initializeState(opts ast.SourceFileParseOptions, sourceText str
213210
}
214211
p.scanner.SetText(p.sourceText)
215212
p.scanner.SetOnError(p.scanError)
216-
p.scanner.SetScriptTarget(p.opts.CompilerOptions.EmitScriptTarget)
217213
p.scanner.SetLanguageVariant(p.languageVariant)
218214
p.scanner.SetScriptKind(p.scriptKind)
219215
p.scanner.SetJSDocParsingMode(p.opts.JSDocParsingMode)

internal/parser/parser_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,8 @@ func BenchmarkParse(b *testing.B) {
4040
jsdocMode := jsdoc.mode
4141

4242
opts := ast.SourceFileParseOptions{
43-
FileName: fileName,
44-
Path: path,
45-
CompilerOptions: core.SourceFileAffectingCompilerOptions{
46-
EmitScriptTarget: core.ScriptTargetESNext,
47-
},
43+
FileName: fileName,
44+
Path: path,
4845
JSDocParsingMode: jsdocMode,
4946
}
5047

@@ -138,11 +135,8 @@ func FuzzParser(f *testing.F) {
138135
path := tspath.Path(fileName)
139136

140137
opts := ast.SourceFileParseOptions{
141-
FileName: fileName,
142-
Path: path,
143-
CompilerOptions: core.SourceFileAffectingCompilerOptions{
144-
EmitScriptTarget: scriptTarget,
145-
},
138+
FileName: fileName,
139+
Path: path,
146140
JSDocParsingMode: jsdocParsingMode,
147141
}
148142

internal/printer/printer.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -844,10 +844,9 @@ func (p *Printer) shouldAllowTrailingComma(node *ast.Node, list *ast.NodeList) b
844844
return false
845845
}
846846

847-
target := p.currentSourceFile.LanguageVersion()
848847
switch node.Kind {
849848
case ast.KindObjectLiteralExpression:
850-
return target >= core.ScriptTargetES5
849+
return true
851850
case ast.KindArrayLiteralExpression,
852851
ast.KindArrowFunction,
853852
ast.KindConstructor,
@@ -874,11 +873,11 @@ func (p *Printer) shouldAllowTrailingComma(node *ast.Node, list *ast.NodeList) b
874873
case ast.KindFunctionDeclaration,
875874
ast.KindFunctionExpression,
876875
ast.KindMethodDeclaration:
877-
return target >= core.ScriptTargetES2015 || list == node.FunctionLikeData().TypeParameters
876+
return true
878877
case ast.KindCallExpression:
879-
return target >= core.ScriptTargetES2015 || list == node.AsCallExpression().TypeArguments
878+
return true
880879
case ast.KindNewExpression:
881-
return target >= core.ScriptTargetES2015 || list == node.AsNewExpression().TypeArguments
880+
return true
882881
}
883882

884883
return false

internal/scanner/scanner.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ type ScannerState struct {
207207
}
208208

209209
type Scanner struct {
210-
text string
211-
// TODO: remove this? unused except for (unimplemented) regex checking
212-
languageVersion core.ScriptTarget
210+
text string
213211
languageVariant core.LanguageVariant
214212
onError ErrorCallback
215213
skipTrivia bool
@@ -222,7 +220,7 @@ func defaultScanner() Scanner {
222220
// Using a function rather than a global is intentional; this function is
223221
// inlined as pure code (zeroing + moves), whereas a global requires write
224222
// barriers since the memory is mutable.
225-
return Scanner{languageVersion: core.ScriptTargetLatest, skipTrivia: true}
223+
return Scanner{skipTrivia: true}
226224
}
227225

228226
func NewScanner() *Scanner {
@@ -339,10 +337,6 @@ func (s *Scanner) SetOnError(errorCallback ErrorCallback) {
339337
s.onError = errorCallback
340338
}
341339

342-
func (s *Scanner) SetScriptTarget(scriptTarget core.ScriptTarget) {
343-
s.languageVersion = scriptTarget
344-
}
345-
346340
func (s *Scanner) SetScriptKind(scriptKind core.ScriptKind) {
347341
s.scriptKind = scriptKind
348342
}
@@ -2237,7 +2231,6 @@ func GetScannerForSourceFile(sourceFile *ast.SourceFile, pos int) *Scanner {
22372231
s := NewScanner()
22382232
s.text = sourceFile.Text()
22392233
s.pos = pos
2240-
s.languageVersion = sourceFile.LanguageVersion()
22412234
s.languageVariant = sourceFile.LanguageVariant
22422235
s.Scan()
22432236
return s

internal/testutil/parsetestutil/parsetestutil.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ import (
1111
"github.com/microsoft/typescript-go/internal/tspath"
1212
)
1313

14-
var parseCompilerOptions = core.SourceFileAffectingCompilerOptions{
15-
EmitScriptTarget: core.ScriptTargetLatest,
16-
}
17-
1814
// Simplifies parsing an input string into a SourceFile for testing purposes.
1915
func ParseTypeScript(text string, jsx bool) *ast.SourceFile {
2016
fileName := core.IfElse(jsx, "/main.tsx", "/main.ts")
2117
file := parser.ParseSourceFile(ast.SourceFileParseOptions{
2218
FileName: fileName,
2319
Path: tspath.Path(fileName),
24-
CompilerOptions: parseCompilerOptions,
2520
JSDocParsingMode: ast.JSDocParsingModeParseNone,
2621
}, text, core.GetScriptKindFromFileName(fileName))
2722
ast.SetParentInChildren(file.AsNode())

testdata/baselines/reference/submodule/compiler/blockScopedFunctionDeclarationInStrictClass.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
blockScopedFunctionDeclarationInStrictClass.ts(4,22): error TS1251: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'. Class definitions are automatically in strict mode.
21
blockScopedFunctionDeclarationInStrictClass.ts(7,9): error TS2304: Cannot find name 'foo'.
32

43

5-
==== blockScopedFunctionDeclarationInStrictClass.ts (2 errors) ====
4+
==== blockScopedFunctionDeclarationInStrictClass.ts (1 errors) ====
65
class c {
76
method() {
87
if (true) {
98
function foo() { }
10-
~~~
11-
!!! error TS1251: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'. Class definitions are automatically in strict mode.
129
foo(); // ok
1310
}
1411
foo(); // not ok
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--- old.blockScopedFunctionDeclarationInStrictClass.errors.txt
2+
+++ new.blockScopedFunctionDeclarationInStrictClass.errors.txt
3+
@@= skipped -0, +0 lines =@@
4+
-blockScopedFunctionDeclarationInStrictClass.ts(4,22): error TS1251: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'. Class definitions are automatically in strict mode.
5+
blockScopedFunctionDeclarationInStrictClass.ts(7,9): error TS2304: Cannot find name 'foo'.
6+
7+
8+
-==== blockScopedFunctionDeclarationInStrictClass.ts (2 errors) ====
9+
+==== blockScopedFunctionDeclarationInStrictClass.ts (1 errors) ====
10+
class c {
11+
method() {
12+
if (true) {
13+
function foo() { }
14+
- ~~~
15+
-!!! error TS1251: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'. Class definitions are automatically in strict mode.
16+
foo(); // ok
17+
}
18+
foo(); // not ok

testdata/baselines/reference/submodule/compiler/blockScopedFunctionDeclarationInStrictModule.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
blockScopedFunctionDeclarationInStrictModule.ts(2,14): error TS1252: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'. Modules are automatically in strict mode.
21
blockScopedFunctionDeclarationInStrictModule.ts(6,10): error TS2304: Cannot find name 'foo'.
32

43

5-
==== blockScopedFunctionDeclarationInStrictModule.ts (2 errors) ====
4+
==== blockScopedFunctionDeclarationInStrictModule.ts (1 errors) ====
65
if (true) {
76
function foo() { }
8-
~~~
9-
!!! error TS1252: Function declarations are not allowed inside blocks in strict mode when targeting 'ES5'. Modules are automatically in strict mode.
107
foo(); // ok
118
}
129

0 commit comments

Comments
 (0)