Skip to content

Commit 073008a

Browse files
Implement import meta check unlocked by impliednodeformat (microsoft#694)
1 parent cce4d90 commit 073008a

9 files changed

+96
-116
lines changed

internal/checker/checker.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9961,10 +9961,10 @@ func (c *Checker) checkNewTargetMetaProperty(node *ast.Node) *Type {
99619961

99629962
func (c *Checker) checkImportMetaProperty(node *ast.Node) *Type {
99639963
if c.moduleKind == core.ModuleKindNode16 || c.moduleKind == core.ModuleKindNodeNext {
9964-
// !!! Enable this once ImpliedNodeFormat is computed at program construction
9965-
// if ast.GetSourceFileOfNode(node).ImpliedNodeFormat != core.ModuleKindESNext {
9966-
// c.error(node, diagnostics.The_import_meta_meta_property_is_not_allowed_in_files_which_will_build_into_CommonJS_output)
9967-
// }
9964+
sourceFileMetaData := c.program.GetSourceFileMetaData(ast.GetSourceFileOfNode(node).Path())
9965+
if sourceFileMetaData == nil || sourceFileMetaData.ImpliedNodeFormat != core.ModuleKindESNext {
9966+
c.error(node, diagnostics.The_import_meta_meta_property_is_not_allowed_in_files_which_will_build_into_CommonJS_output)
9967+
}
99689968
} else if c.moduleKind < core.ModuleKindES2020 && c.moduleKind != core.ModuleKindSystem {
99699969
c.error(node, diagnostics.The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_es2020_es2022_esnext_system_node16_or_nodenext)
99709970
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
subfolder/index.js(2,11): error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.
2+
3+
4+
==== subfolder/index.js (1 errors) ====
5+
// cjs format file
6+
const x = import.meta.url;
7+
~~~~~~~~~~~
8+
!!! error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.
9+
export {x};
10+
==== index.js (0 errors) ====
11+
// esm format file
12+
const x = import.meta.url;
13+
export {x};
14+
==== package.json (0 errors) ====
15+
{
16+
"name": "package",
17+
"private": true,
18+
"type": "module"
19+
}
20+
==== subfolder/package.json (0 errors) ====
21+
{
22+
"type": "commonjs"
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
subfolder/index.js(2,11): error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.
2+
3+
4+
==== subfolder/index.js (1 errors) ====
5+
// cjs format file
6+
const x = import.meta.url;
7+
~~~~~~~~~~~
8+
!!! error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.
9+
export {x};
10+
==== index.js (0 errors) ====
11+
// esm format file
12+
const x = import.meta.url;
13+
export {x};
14+
==== package.json (0 errors) ====
15+
{
16+
"name": "package",
17+
"private": true,
18+
"type": "module"
19+
}
20+
==== subfolder/package.json (0 errors) ====
21+
{
22+
"type": "commonjs"
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
subfolder/index.ts(2,11): error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.
2+
3+
4+
==== subfolder/index.ts (1 errors) ====
5+
// cjs format file
6+
const x = import.meta.url;
7+
~~~~~~~~~~~
8+
!!! error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.
9+
export {x};
10+
==== index.ts (0 errors) ====
11+
// esm format file
12+
const x = import.meta.url;
13+
export {x};
14+
==== package.json (0 errors) ====
15+
{
16+
"name": "package",
17+
"private": true,
18+
"type": "module"
19+
}
20+
==== subfolder/package.json (0 errors) ====
21+
{
22+
"type": "commonjs"
23+
}

testdata/baselines/reference/submodule/conformance/nodeModulesImportMeta(module=node16).errors.txt.diff

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
subfolder/index.ts(2,11): error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.
2+
3+
4+
==== subfolder/index.ts (1 errors) ====
5+
// cjs format file
6+
const x = import.meta.url;
7+
~~~~~~~~~~~
8+
!!! error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.
9+
export {x};
10+
==== index.ts (0 errors) ====
11+
// esm format file
12+
const x = import.meta.url;
13+
export {x};
14+
==== package.json (0 errors) ====
15+
{
16+
"name": "package",
17+
"private": true,
18+
"type": "module"
19+
}
20+
==== subfolder/package.json (0 errors) ====
21+
{
22+
"type": "commonjs"
23+
}

testdata/baselines/reference/submodule/conformance/nodeModulesImportMeta(module=nodenext).errors.txt.diff

Lines changed: 0 additions & 28 deletions
This file was deleted.

testdata/baselines/reference/submoduleAccepted/conformance/nodeModulesAllowJsImportMeta(module=node16).errors.txt.diff

Lines changed: 0 additions & 28 deletions
This file was deleted.

testdata/baselines/reference/submoduleAccepted/conformance/nodeModulesAllowJsImportMeta(module=nodenext).errors.txt.diff

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)