Skip to content

Commit a6945b6

Browse files
authored
expose code location to MacroContext (#10171)
1 parent b66f371 commit a6945b6

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Before starting make sure you have the following installed:
1616

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

packages/transformers/js/src/JSTransformer.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ type MacroAsset = {|
165165
// NOTE: Make sure this is in sync with the TypeScript definition in the @parcel/macros package.
166166
type MacroContext = {|
167167
addAsset(asset: MacroAsset): void,
168+
loc: SourceLocation,
168169
invalidateOnFileChange(FilePath): void,
169170
invalidateOnFileCreate(FileCreateInvalidation): void,
170171
invalidateOnEnvChange(string): void,
@@ -544,6 +545,19 @@ export default (new Transformer({
544545
specifierType: 'esm',
545546
});
546547
},
548+
loc: {
549+
filePath: asset.filePath,
550+
start: {
551+
line:
552+
loc.start_line + Number(asset.meta.startLine ?? 1) - 1,
553+
column: loc.start_col,
554+
},
555+
end: {
556+
line:
557+
loc.end_line + Number(asset.meta.startLine ?? 1) - 1,
558+
column: loc.end_col,
559+
},
560+
},
547561
invalidateOnFileChange(filePath) {
548562
asset.invalidateOnFileChange(filePath);
549563
},

packages/utils/macros/macros.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export interface MacroContext {
22
/** Adds an asset as a dependency of the JS module that called this macro. */
33
addAsset(asset: MacroAsset): void;
4+
/** The source location of the macro call. */
5+
loc: SourceLocation;
46
/** Invalidate the macro call whenever the given file changes. */
57
invalidateOnFileChange(filePath: string): void;
68
/** Invalidate the macro call when a file matching the given pattern is created. */
@@ -13,6 +15,25 @@ export interface MacroContext {
1315
invalidateOnBuild(): void;
1416
}
1517

18+
/**
19+
* Source locations are 1-based, meaning lines and columns start at 1
20+
*/
21+
export type SourceLocation = {
22+
readonly filePath: string;
23+
24+
/** inclusive */
25+
readonly start: {
26+
readonly line: number;
27+
readonly column: number;
28+
};
29+
30+
/** exclusive */
31+
readonly end: {
32+
readonly line: number;
33+
readonly column: number;
34+
};
35+
};
36+
1637
export interface MacroAsset {
1738
/** The type of the asset (e.g. `'css'`). */
1839
type: string;

0 commit comments

Comments
 (0)