Skip to content

expose code location to MacroContext #10171

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

Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Before starting make sure you have the following installed:

- [git](https://git-scm.com)
- [Node](https://nodejs.org) at LTS
- [Yarn](https://yarnpkg.com) at v1
- [Yarn](https://yarnpkg.com) at v4 and/or enable corepack
- [Rust](https://www.rust-lang.org/tools/install) stable
- [Flow](https://flow.org/en/docs/editors) IDE autocompletion and type-checking

Expand Down
14 changes: 14 additions & 0 deletions packages/transformers/js/src/JSTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ type MacroAsset = {|
// NOTE: Make sure this is in sync with the TypeScript definition in the @parcel/macros package.
type MacroContext = {|
addAsset(asset: MacroAsset): void,
loc: SourceLocation,
invalidateOnFileChange(FilePath): void,
invalidateOnFileCreate(FileCreateInvalidation): void,
invalidateOnEnvChange(string): void,
Expand Down Expand Up @@ -544,6 +545,19 @@ export default (new Transformer({
specifierType: 'esm',
});
},
loc: {
filePath: asset.filePath,
start: {
line:
loc.start_line + Number(asset.meta.startLine ?? 1) - 1,
column: loc.start_col,
},
end: {
line:
loc.end_line + Number(asset.meta.startLine ?? 1) - 1,
column: loc.end_col,
},
},
invalidateOnFileChange(filePath) {
asset.invalidateOnFileChange(filePath);
},
Expand Down
21 changes: 21 additions & 0 deletions packages/utils/macros/macros.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export interface MacroContext {
/** Adds an asset as a dependency of the JS module that called this macro. */
addAsset(asset: MacroAsset): void;
/** The source location of the macro call. */
loc: SourceLocation;
/** Invalidate the macro call whenever the given file changes. */
invalidateOnFileChange(filePath: string): void;
/** Invalidate the macro call when a file matching the given pattern is created. */
Expand All @@ -13,6 +15,25 @@ export interface MacroContext {
invalidateOnBuild(): void;
}

/**
* Source locations are 1-based, meaning lines and columns start at 1
*/
export type SourceLocation = {
readonly filePath: string;

/** inclusive */
readonly start: {
readonly line: number;
readonly column: number;
};

/** exclusive */
readonly end: {
readonly line: number;
readonly column: number;
};
};

export interface MacroAsset {
/** The type of the asset (e.g. `'css'`). */
type: string;
Expand Down
Loading