-
Notifications
You must be signed in to change notification settings - Fork 411
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
chore(ssr): use subpath imports for estree validators #5102
Changes from all commits
60093f1
f1b3047
2c92781
ef101cb
6ac97d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,15 +16,11 @@ import type { | |
Statement as EsStatement, | ||
} from 'estree'; | ||
import type { Checker } from 'estree-toolkit/dist/generated/is-type'; | ||
import type { Validator } from './estree/validators'; | ||
|
||
/** Placeholder value to use to opt out of validation. */ | ||
const NO_VALIDATION = false; | ||
|
||
/** A function that accepts a node and checks that it is a particular type of node. */ | ||
type Validator<T extends EsNode | null = EsNode | null> = ( | ||
node: EsNode | null | undefined | ||
) => node is T; | ||
|
||
/** | ||
* A pointer to a previous value in the template literal, indicating that the value should be re-used. | ||
* @see {@linkcode esTemplate} | ||
|
@@ -93,15 +89,20 @@ const getReplacementNode = ( | |
: validateReplacement(replacementNode)) | ||
) { | ||
const expectedType = | ||
(validateReplacement as any).__debugName || | ||
validateReplacement.name || | ||
'(could not determine)'; | ||
validateReplacement.__debugName || validateReplacement.name || '(could not determine)'; | ||
const actualType = Array.isArray(replacementNode) | ||
? `[${replacementNode.map((n) => n && n.type).join(', ')}]` | ||
: replacementNode?.type; | ||
throw new Error( | ||
const error = new Error( | ||
`Validation failed for templated node. Expected type ${expectedType}, but received ${actualType}.` | ||
); | ||
|
||
if (validateReplacement?.__stack) { | ||
error.message += `\n${validateReplacement.__stack.split('\n')[1]}`; | ||
error.stack = validateReplacement.__stack; | ||
} | ||
|
||
throw error; | ||
Comment on lines
-96
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the type information, there's no need for |
||
} | ||
|
||
return replacementNode; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,13 @@ | |
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
import { is } from 'estree-toolkit'; | ||
/* eslint-disable-next-line no-restricted-imports -- This is the only place where we should be importing `is` from estree-toolkit. */ | ||
import { is as _is } from 'estree-toolkit'; | ||
|
||
import { entries } from '@lwc/shared'; | ||
import type { Checker } from 'estree-toolkit/dist/generated/is-type'; | ||
import type { Node } from 'estree-toolkit/dist/helpers'; // estree's `Node` is not compatible? | ||
|
||
import type { Node as EsNode } from 'estree'; | ||
/** A validator that returns `true` if the node is `null`. */ | ||
type NullableChecker<T extends Node> = (node: Node | null | undefined) => node is T | null; | ||
|
||
|
@@ -26,9 +28,29 @@ export function isNullableOf<T extends Node>(validator: Checker<T>): NullableChe | |
|
||
isNullableOf.__debugName = 'isNullableOf'; | ||
|
||
/** A function that accepts a node and checks that it is a particular type of node. */ | ||
export type Validator<T extends EsNode | null = EsNode | null> = { | ||
(node: EsNode | null | undefined): node is T; | ||
__debugName?: string; | ||
__stack?: string; | ||
}; | ||
|
||
const is: { | ||
[K in keyof typeof _is]: (typeof _is)[K] & { __debugName?: string; __stack?: string }; | ||
} = { ..._is }; | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
// Modifying another package's exports is a code smell! | ||
for (const [key, val] of entries(is)) { | ||
(val as any).__debugName = key; | ||
val.__debugName = key; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. I guess the only immediate win is making this compatible with esm, which admittedly isn't much for a debug-only feature. |
||
Object.defineProperty(is, key, { | ||
get: function get() { | ||
const stack: { stack?: string } = {}; | ||
Error.captureStackTrace(stack, get); | ||
val.__stack = stack.stack; | ||
return val; | ||
}, | ||
}); | ||
Comment on lines
+44
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feature, if deemed useful, can be added in a separate PR. |
||
} | ||
} | ||
|
||
export { is }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["rollup", "node"] | ||
"types": ["rollup", "node"], | ||
"rootDir": "src" | ||
}, | ||
"include": ["src/"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,10 @@ function getLwcErrorFromParse5Error(ctx: ParserCtx, code: string) { | |
} | ||
} | ||
|
||
export function parseHTML(ctx: ParserCtx, source: string) { | ||
export function parseHTML( | ||
ctx: ParserCtx, | ||
source: string | ||
): parse5.DefaultTreeAdapterTypes.DocumentFragment { | ||
Comment on lines
-39
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes an error/warning caught after the tsconfig changes:
|
||
const onParseError = (err: parse5.ParserError) => { | ||
const { code, ...location } = err; | ||
|
||
|
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 avoid package
imports
/exports
. We've had issues in the past with Rollup/Jest and how it consumesexports
.