Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support experimental loader emitDiagnostic #7879

Merged
merged 4 commits into from
Sep 20, 2024

Conversation

h-a-n-a
Copy link
Collaborator

@h-a-n-a h-a-n-a commented Sep 12, 2024

Summary

closes #5913

Supported experimental loader API emitDiagnostic.

This works almost the same as emitError or emitWarning. But, it's for emitting rich-formatted diagnostics.

export interface DiagnosticLocation {
	/** Text for highlighting the location */
	text?: string;
	/** 1-based line */
	line: number;
	/** 0-based column in bytes */
	column: number;
	/** Length in bytes */
	length: number;
}

export interface Diagnostic {
	message: string;
	help?: string;
	sourceCode?: string;
	/**
	 * Location to the source code.
	 *
	 * If `sourceCode` is not provided, location will be omitted.
	 */
	location?: DiagnosticLocation;
	file?: string;
	severity: "error" | "warning";
}

interface LoaderExperiments {
	emitDiagnostic(diagnostic: Diagnostic): void;
}

Example

For loader:

/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function() {
	this.experiments.emitDiagnostic({
		message: "`React` is not defined",
		severity: "error",
		sourceCode: `<div></div>`,
		help: "try to import `React`",
		location: {
			line: 1,
			column: 1,
			length: 3,
		}
	});
	return ""
}

it outputs:

ERROR in (./with-help.js!) 1:1-4
  × ModuleError: `React` is not defined
   ╭────
 1 │ <div></div>
   ·  ───
   ╰────
  help: try to import `React`

Note: This is an experimental API and is rspack-specific.

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@github-actions github-actions bot added the release: feature release: feature related release(mr only) label Sep 12, 2024
Copy link

netlify bot commented Sep 12, 2024

Deploy Preview for rspack canceled.

Name Link
🔨 Latest commit cbc2c1d
🔍 Latest deploy log https://app.netlify.com/sites/rspack/deploys/66ec0b2fda3e320008af261a

@github-actions github-actions bot added the team The issue/pr is created by the member of Rspack. label Sep 12, 2024
@h-a-n-a h-a-n-a force-pushed the support-format-diagnostic branch 2 times, most recently from 941ddbd to 2b937fe Compare September 12, 2024 12:52
@h-a-n-a h-a-n-a marked this pull request as ready for review September 12, 2024 12:53
@h-a-n-a h-a-n-a force-pushed the support-format-diagnostic branch 2 times, most recently from a4fd487 to eecf87b Compare September 19, 2024 07:58
@hardfist
Copy link
Contributor

hardfist commented Sep 19, 2024

LGTM, waiting for @colinaaa's feedback on this

@colinaaa
Copy link
Contributor

It looks great to me! Thank you @h-a-n-a @hardfist

@h-a-n-a h-a-n-a enabled auto-merge (squash) September 20, 2024 03:40
@h-a-n-a h-a-n-a merged commit 7903e05 into main Sep 20, 2024
27 checks passed
@h-a-n-a h-a-n-a deleted the support-format-diagnostic branch September 20, 2024 03:41
JSerFeng pushed a commit that referenced this pull request Sep 29, 2024
* feat: init

* test: add

* test: add

* chore: cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release: feature release: feature related release(mr only) team The issue/pr is created by the member of Rspack.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: Support diagnostics reporting API like esbuild.formatMessages or swc HANDLER
3 participants