-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: inject source location data to Slice usage and show errors in p…
…laces where user renders them and not in gatsby internals (#36849) * feat: inject source location data to Slice usage and show errors in places where user renders them and not in gatsby internals * update snapshot with added line and column numbers when alias/allowEmpty can't be evaluated * Update packages/gatsby-cli/src/reporter/errors.ts Co-authored-by: Ty Hopp <tyhopp@users.noreply.github.com> * update jsdoc description of new babel plugin to match what it's doing instead of description that was copy&paste from StaticImage plugin * Update packages/gatsby/cache-dir/slice.js * Normalize renderedByLocation casing Co-authored-by: Lennart <lekoarts@gmail.com> Co-authored-by: Ty Hopp <tyhopp@users.noreply.github.com> Co-authored-by: Lennart <lekoarts@gmail.com>
- Loading branch information
1 parent
6dc2b57
commit 4423795
Showing
12 changed files
with
382 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/gatsby/src/utils/babel/babel-plugin-add-slice-placeholder-location.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { relative } from "path" | ||
import type { PluginObj, types as BabelTypes, PluginPass } from "@babel/core" | ||
import { ObjectProperty } from "@babel/types" | ||
import { store } from "../../redux" | ||
|
||
/** | ||
* This is a plugin that finds <Slice> placeholder components and injects the __renderedByLocation prop | ||
* with filename and location in the file where the placeholder was found. This is later used to provide | ||
* more useful error messages when the user props are invalid showing codeframe where user tries to render it | ||
* instead of codeframe of the Slice component itself (internals of gatsby) that is not useful for the user. | ||
*/ | ||
|
||
export default function addSlicePlaceholderLocation( | ||
this: PluginPass, | ||
{ | ||
types: t, | ||
}: { | ||
types: typeof BabelTypes | ||
} | ||
): PluginObj { | ||
return { | ||
name: `babel-plugin-add-slice-placeholder-location`, | ||
visitor: { | ||
JSXOpeningElement(nodePath): void { | ||
if (!nodePath.get(`name`).referencesImport(`gatsby`, `Slice`)) { | ||
return | ||
} | ||
|
||
if (this.file.opts.filename) { | ||
const __renderedByLocationProperties: Array<ObjectProperty> = [ | ||
t.objectProperty( | ||
t.identifier(`fileName`), | ||
t.stringLiteral( | ||
relative( | ||
store.getState().program.directory, | ||
this.file.opts.filename | ||
) | ||
) | ||
), | ||
] | ||
|
||
if (nodePath.node.loc?.start.line) { | ||
__renderedByLocationProperties.push( | ||
t.objectProperty( | ||
t.identifier(`lineNumber`), | ||
t.numericLiteral(nodePath.node.loc.start.line) | ||
) | ||
) | ||
|
||
if (nodePath.node.loc?.start.column) { | ||
__renderedByLocationProperties.push( | ||
t.objectProperty( | ||
t.identifier(`columnNumber`), | ||
t.numericLiteral(nodePath.node.loc.start.column + 1) | ||
) | ||
) | ||
} | ||
|
||
if (nodePath.node.loc?.end.line) { | ||
__renderedByLocationProperties.push( | ||
t.objectProperty( | ||
t.identifier(`endLineNumber`), | ||
t.numericLiteral(nodePath.node.loc.end.line) | ||
) | ||
) | ||
|
||
if (nodePath.node.loc?.end.column) { | ||
__renderedByLocationProperties.push( | ||
t.objectProperty( | ||
t.identifier(`endColumnNumber`), | ||
t.numericLiteral(nodePath.node.loc.end.column + 1) | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
const newProp = t.jsxAttribute( | ||
t.jsxIdentifier(`__renderedByLocation`), | ||
t.jsxExpressionContainer( | ||
t.objectExpression(__renderedByLocationProperties) | ||
) | ||
) | ||
|
||
nodePath.node.attributes.push(newProp) | ||
} | ||
}, | ||
}, | ||
} | ||
} |
Oops, something went wrong.