GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Import assertion by Kingwl · Pull Request #40698 · microsoft/TypeScript

Conversation

@Kingwl

@Kingwl Wenlu Wang (Kingwl) commented Sep 22, 2020

Copy link
Copy Markdown
Contributor

Fixes #40694

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

saschanaz, DjDeveloperr, styfle, justinfagnani, TylorS, patrickshipe, dnalborczyk, jonathantneal, kripod, rostacik, and 5 more reacted with rocket emoji
All reactions
  • 🚀 15 reactions
@typescript-bot TypeScript Bot (typescript-bot) added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Sep 22, 2020
@Kingwl
Add parsing
e1b3b78
@Kingwl
Wenlu Wang (Kingwl) force-pushed the import_assertion branch from f945374 to e1b3b78 Compare September 22, 2020 10:44
Wenlu Wang (Kingwl) added 5 commits September 22, 2020 22:00
@Kingwl
fix all api
66d2273
@Kingwl
check gramma of import call
5f3cea6
@Kingwl
Add more part of assertion
5a1af04
@Kingwl
Add some case
4b51ca9
@Kingwl
Add baseline
375b433
@Kingwl

Wenlu Wang (Kingwl) commented Sep 22, 2020

Copy link
Copy Markdown
Contributor Author

TypeScript Bot (@typescript-bot) pack this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Sep 22, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Heya Wenlu Wang (@Kingwl), I've started to run the tarball bundle task on this PR at 375b433. You can monitor the build here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Kingwl
Wenlu Wang (Kingwl) marked this pull request as ready for review September 22, 2020 16:59
Jack-Works
Jack Works (Jack-Works) reviewed Sep 23, 2020
View reviewed changes
Comment thread src/compiler/checker.ts Outdated
function checkGrammarImportAssertion(declaration: ImportDeclaration | ExportDeclaration) {
const target = getEmitScriptTarget(compilerOptions);
if (target < ScriptTarget.ESNext && declaration.assertClause) {
grammarErrorOnNode(declaration.assertClause, Diagnostics.Import_assertions_are_not_available_when_targeting_lower_than_esnext);

ghost Sep 23, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the current specification of Import Assertion, the import clause cannot affect the representation of the source text. Does it mean it can be safely ignored (when downlevel, drop the whole assert clause instead of emitting an error)?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

ghost Sep 23, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well. IMO, downlevel means something could map to another. Ignore is not a good idea.

But maybe we could control the error by module options rather than target.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

ghost Sep 23, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import Assertions are an enrichment to the import statement, provides the runtime with extra information of which it can make a decision about. I could see the need to allow an error by default, but allow the error to be ignored and the node to be dropped from the emit.

The biggest use case is supporting JSON modules, and there are several runtimes that already support JSON modules without the assertion, but I can see people wanting to start writing the assertions in the code for the runtimes that will only support it with the assertion clause.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@kitsonk Kitson Kelly (kitsonk) mentioned this pull request Sep 23, 2020
Import Assertions/JSON Modules denoland/deno#7623
Closed
kingwl
use module insted of target
41a881c
@typescript-bot TypeScript Bot (typescript-bot) assigned Ron Buckton (rbuckton) Sep 23, 2020
@typescript-bot TypeScript Bot (typescript-bot) added For Milestone Bug PRs that fix a bug with a specific milestone and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Sep 23, 2020
kingwl added 3 commits September 23, 2020 21:56
kingwl
strip assertion in d.ts
46a3eb1
kingwl
Merge branch 'master' into import_assertion
a484ea2
kingwl
Merge branch 'master' into import_assertion
d64f7ba
@typescript-bot

ghost commented Nov 12, 2020

Copy link
Copy Markdown
Contributor

The TypeScript team hasn't accepted the linked issue #40694. If you can get it accepted, this PR will have a better chance of being reviewed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kingwl
Merge branch 'master' into import_assertion
68c8c5d
@Kingwl

ghost commented Dec 31, 2020

Copy link
Copy Markdown
Contributor Author

uP.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kingwl added 4 commits January 7, 2021 17:39
kingwl
Merge branch 'master' into import_assertion
29fe0d3
kingwl
Update new baseline
adcfd1b
kingwl
Merge branch 'master' into import_assertion
fb01eb3
kingwl
accept baseline
4f22a60
Jack-Works
Jack Works (Jack-Works) reviewed Mar 8, 2021
View reviewed changes
Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Jack-Works
Jack Works (Jack-Works) reviewed Mar 8, 2021
View reviewed changes
Comment thread src/compiler/diagnosticMessages.json Resolved

Uh oh!

There was an error while loading. Please reload this page.

kingwl
Revert error number changes
f5e594d
Wenlu Wang added 2 commits August 13, 2021 15:57
Wenlu Wang
Merge branch 'main' into import_assertion
f941d92
Wenlu Wang
Add some comment
106ff06
@Kingwl

ghost commented Sep 8, 2021

Copy link
Copy Markdown
Contributor Author

Up.

xiaoxiangmoe, hronro, and andrewbranch reacted with thumbs up emoji orta reacted with heart emoji
All reactions
  • 👍 3 reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rbuckton

ghost commented Sep 9, 2021

Copy link
Copy Markdown
Contributor

Sorry for the delay Wenlu Wang (@Kingwl). I'm looking at this again today.

andrewbranch and Kingwl reacted with hooray emoji
All reactions
  • 🎉 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rbuckton
Ron Buckton (rbuckton) suggested changes Sep 9, 2021
View reviewed changes

ghost left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I will be OOF through Tuesday and may not be able to respond to feedback until Wednesday.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/emitter.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/emitter.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/utilitiesPublic.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/types.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/checker.ts Outdated
// see: parseArgumentOrArrayLiteralElement...we use this function which parse arguments of callExpression to parse specifier for dynamic import.
// parseArgumentOrArrayLiteralElement allows spread element to be in an argument list which is not allowed as specifier in dynamic import.
if (isSpreadElement(nodeArguments[0])) {
if (nodeArguments.length && isSpreadElement(nodeArguments[0])) {

ghost Sep 9, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check that all elements of the assert property of the second argument have type string? Per https://tc39.es/proposal-import-assertions/#sec-evaluate-import-call, Step 10.d.v.3:

image

No coercion to string is performed here, the algorithm explicitly checks for strings.

We might want to consider something like the ImportMeta interface for the import argument, something like:

// es5.d.ts
interface ImportCallOptions {
  assert?: ImportAssertions;
}
interface ImportAssertions {
  type?: string;
  [key: string]: string | undefined; // not ideal, but necessary so that `type` is optional.
}

Then we can check against the global ImportCallOptions type, and if other assertions or options are added later, they can be introduced using global augmentation similar to ImportMeta.

We could consider this as a follow-on PR if necessary, but it would be nice to have.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

ghost Sep 9, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: if we wanted to be more specific with type we'd need to create an open-ended union via an interface as well:

interface ImportAssertions {
  type?: ImportTypeAssertion;
  [key: string]: string | undefined;
}

// see ArrayBufferTypes for reference
interface ImportTypeAssertionTypes {
  json: "json";
}

type ImportTypeAssertion = Extract<ImportTypeAssertions[keyof ImportTypeAssertions], string>;

That way, we have better completions for type and if a host environment supported other values for type, they could also be added via augmentation, i.e.:

interface ImportTypeAssertionTypes {
  css: "css";
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

ghost Sep 9, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the nodeArguments.length check shouldn't be necessary if checkGrammarImportCallArguments already asserts that nodeArguments.length can't be zero.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

ghost Sep 9, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to ensure that nodeArguments[1] (if present) is not a spread element.

I would suggest this approach:

const spreadElement = forEach(nodeArguments, isSpreadElement);
if (spreadElement) {
  return grammarErrorOnNode(spreadElement, Diagnostics.Specifier_of_dynamic_import_cannot_be_spread_element);
}

That way it checks all arguments, but only reports on the first error (which is almost always the case for grammar errors).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

ghost Sep 16, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check the assert clause too?Or just the second parameter of dynamic import.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/baselines/reference/importAssertion1(module=esnext).errors.txt Resolved

Uh oh!

There was an error while loading. Please reload this page.

rbuckton
Ron Buckton (rbuckton) reviewed Sep 9, 2021
View reviewed changes
Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@justinfagnani Justin Fagnani (justinfagnani) mentioned this pull request Sep 13, 2021
Implement Import Assertions (stage 3) #40694
Closed
5 tasks
kingwl added 3 commits September 14, 2021 19:36
kingwl
Merge branch 'main' into import_assertion
7df4964
kingwl
Fix cr issues
6ea3c55
kingwl
Fix more issue
fc51c42
@IanVS Ian VanSchooten (IanVS) mentioned this pull request Sep 15, 2021
Support Import Assertions vitejs/vite#4934
Closed
rbuckton
Ron Buckton (rbuckton) suggested changes Sep 15, 2021
View reviewed changes

ghost left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more minor changes. Daniel Rosenwasser (@DanielRosenwasser), Andrew Branch (@andrewbranch) do we want to add something like #40698 (comment) in this PR or in a follow up?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/compiler/checker.ts

ghost Sep 15, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove extra space

Suggested change
if (nodeArguments.length === 0 || nodeArguments.length > 2) {
if (nodeArguments.length === 0 || nodeArguments.length > 2) {

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/compiler/checker.ts
Comment on lines 43191 to 43194

ghost Sep 15, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. This doesn't quite do what we want. We want to report the error on the spread element, not the first argument (which might not be the spread element). I think we want this instead:

Suggested change
const spreadElement = forEach(nodeArguments, isSpreadElement);
if (spreadElement) {
return grammarErrorOnNode(nodeArguments[0], Diagnostics.Specifier_of_dynamic_import_cannot_be_spread_element);
}
const spreadElement = find(nodeArguments, isSpreadElement);
if (spreadElement) {
return grammarErrorOnNode(spreadElement, Diagnostics.Specifier_of_dynamic_import_cannot_be_spread_element);
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/compiler/types.ts

ghost Sep 15, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave | undefined here instead of adding ?. My previous comment was specific to the create. For a create, there's no sense breaking existing code because they weren't previously passing an argument. For an update, we do want to break existing code because there's now a new node they may not be appropriately handling.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/compiler/types.ts Outdated

ghost Sep 15, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also would leave | undefined here. We want to break callers here since they could be forgetting to handle a node they weren't previously handling.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser) reviewed Sep 16, 2021
View reviewed changes
Comment thread src/compiler/diagnosticMessages.json Outdated
"category": "Message",
"code": 1449
},
"Dynamic import must only have a specifier and an optional assertion as arguments": {

ghost Sep 16, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Dynamic import must only have a specifier and an optional assertion as arguments": {
"Dynamic imports can only accept a path and an optional assertion as arguments": {

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

ghost Sep 17, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, but I think module specifier is a more correct term than path

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser) reviewed Sep 16, 2021
View reviewed changes
Comment thread src/compiler/diagnosticMessages.json Outdated
"code": 1323
},
"Dynamic import must have one specifier as an argument.": {
"Dynamic import only supports a second argument when the '--module' option is set to 'esnext'.": {

ghost Sep 16, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Dynamic import only supports a second argument when the '--module' option is set to 'esnext'.": {
"Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.": {

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@andrewbranch

ghost commented Sep 16, 2021

Copy link
Copy Markdown
Member

Do we need to check that all elements of the assert property of the second argument have type string?

Let’s try to get this in this PR if possible. Preventing runtime errors seems high priority to me. I personally like the idea of an open-ended interface, but I think that warrants some design discussion to make sure we’re all on the same page. Let’s discuss at Friday’s meeting.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Sep 17, 2021
Design Meeting Notes, 9/17/2021 #45944
Closed
@rbuckton

ghost commented Sep 17, 2021

Copy link
Copy Markdown
Contributor

Wenlu Wang (@Kingwl), if you don't mind I'm going to make some of these minor changes and add a basic implementation of #40698 (comment) that we can expand on later.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rbuckton

ghost commented Sep 17, 2021

Copy link
Copy Markdown
Contributor

I found a small bug in program.ts related to import("./module", {}) in that we aren't resolving ./module because the argument count isn't 1. I'll push changes up shortly. It is definitely strange to not get completions at | in import("./module", { | }), and not getting a signature help popup.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Ron Buckton added 2 commits September 17, 2021 15:20
Ron Buckton
Incorporate PR feedback, fix module resolution for import()
bf7ca71
Ron Buckton
Merge branch 'main' into import_assertion
463138a
# Conflicts:
#	src/compiler/diagnosticMessages.json
@Kingwl

ghost commented Sep 18, 2021

Copy link
Copy Markdown
Contributor Author

Never mind. Thanks for your changes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rbuckton

ghost commented Sep 20, 2021

Copy link
Copy Markdown
Contributor

I fixed completions for import("path", { | }), but we still don't handle completions in import ... assert { }. This is probably fine, since we've typed ImportAssertions to just [key: string]: string so you wouldn't really get useful completions right now anyways. If we decide to make type a property of ImportAssertions in a later PR, we'll want to re-investigate completions for import/export with assert.

jonathantneal reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rbuckton
Ron Buckton (rbuckton) approved these changes Sep 20, 2021
View reviewed changes
Ron Buckton
Add contextual type and completions for ImportCall options argument
b07c6e9
@rbuckton
Ron Buckton (rbuckton) force-pushed the import_assertion branch from 9accee9 to b07c6e9 Compare September 20, 2021 21:14
@rbuckton
Ron Buckton (rbuckton) merged commit ec114b8 into microsoft:main Sep 20, 2021
@rbuckton

ghost commented Sep 20, 2021

Copy link
Copy Markdown
Contributor

Thanks for all the effort you put into this Wenlu Wang (@Kingwl)!

robpalme and kirillgroshkov reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@dnalborczyk dnalborczyk mentioned this pull request Nov 7, 2021
feat: add support for import assertions rollup/rollup#4267
Closed
2 of 9 tasks
@KSXGitHub Khải (KSXGitHub) mentioned this pull request Oct 23, 2022
Caching and loading non-importable resources denoland/deno#5987
Closed
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) DanielRosenwasser left review comments
@andrewbranch Andrew Branch (andrewbranch) andrewbranch left review comments
+3 more reviewers
@kitsonk Kitson Kelly (kitsonk) kitsonk left review comments
@Jack-Works Jack Works (Jack-Works) Jack-Works left review comments
@rbuckton Ron Buckton (rbuckton) rbuckton approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@rbuckton Ron Buckton (rbuckton)

Labels

For Milestone Bug PRs that fix a bug with a specific milestone

Projects

PR Backlog
Archived in project

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Implement Import Assertions (stage 3)

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@Kingwl @typescript-bot @andrewbranch @rbuckton @DanielRosenwasser @kitsonk @Jack-Works
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.