Skip to content

Commit

Permalink
ts-html-plugin: solve path resolution issue on tests, add debug file …
Browse files Browse the repository at this point in the history
…options to tsserver on tests, align test script tag to execute test with the build
  • Loading branch information
JacopoPatroclo committed Mar 4, 2024
1 parent b611195 commit 42cd262
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_modules
index.tsbuildinfo
dist
coverage

*.log
4 changes: 2 additions & 2 deletions packages/ts-html-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"build": "tsc -p tsconfig.build.json",
"dev": "tsc -p tsconfig.build.json --watch",
"prepack": "npm run build",
"test": "node --require @swc-node/register --test test/**/*.test.ts",
"test-procedure": "pnpm build && pnpm install && pnpm test"
"test": "pnpm build && pnpm install && pnpm test-exec",
"test-exec": "node --require @swc-node/register --test test/**/*.test.ts"
},
"dependencies": {
"chalk": "^4.1.2",
Expand Down
8 changes: 7 additions & 1 deletion packages/ts-html-plugin/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,13 @@ export function isSafeAttribute(
(type.aliasSymbol.parent?.escapedName === 'Html' ||
// @ts-expect-error - When using export as namespace X, parent.escapedName ends up
// as a complete (without resolving symlinks) quoted import path to its original file.
type.aliasSymbol.parent?.escapedName.endsWith('@kitajs/html/index"'))
type.aliasSymbol.parent?.escapedName.endsWith('@kitajs/html/index"') ||
// This is needed because of the resolved path of the parent if is installed with pnpm is a symlink
// that ts resolves to the original file path, so the path is not related to the node_modules but instead
// is absolute to the file system (this is only here because of the monorepo setup, it is not needed when used as a package)
// @ts-expect-error - When using export as namespace X, parent.escapedName ends up
(process.env.KITAJS_TESTING === 'true' &&
type.aliasSymbol.parent?.escapedName.endsWith('packages/html/index"')))
) {
return true;
}
Expand Down
24 changes: 8 additions & 16 deletions packages/ts-html-plugin/test/unsafe-tags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ it('Detect xss prone usage', async () => {
<>
<div>
<div>{html}</div>
<div>{object}</div>
<div>{union}</div>
<div>
{['a', 'b', 'c'].map((i) => (
Expand All @@ -38,42 +37,35 @@ it('Detect xss prone usage', async () => {
},
{
start: { line: 38, offset: 15 },
end: { line: 38, offset: 21 },
end: { line: 38, offset: 20 },
text: Xss.message,
code: Xss.code,
category: 'error'
},
{
start: { line: 39, offset: 15 },
end: { line: 39, offset: 20 },
start: { line: 41, offset: 16 },
end: { line: 41, offset: 17 },
text: Xss.message,
code: Xss.code,
category: 'error'
},
{
start: { line: 42, offset: 16 },
end: { line: 42, offset: 17 },
start: { line: 44, offset: 19 },
end: { line: 44, offset: 20 },
text: Xss.message,
code: Xss.code,
category: 'error'
},
{
start: { line: 45, offset: 19 },
end: { line: 45, offset: 20 },
start: { line: 47, offset: 15 },
end: { line: 47, offset: 44 },
text: Xss.message,
code: Xss.code,
category: 'error'
},
{
start: { line: 48, offset: 15 },
end: { line: 48, offset: 44 },
text: Xss.message,
code: Xss.code,
category: 'error'
},
{
start: { line: 49, offset: 15 },
end: { line: 49, offset: 52 },
end: { line: 48, offset: 52 },
text: Xss.message,
code: Xss.code,
category: 'error'
Expand Down
6 changes: 5 additions & 1 deletion packages/ts-html-plugin/test/util/lang-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ export class TSLangServer {
) {
this.server = fork(require.resolve('typescript/lib/tsserver'), {
cwd: projectPath,
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
env: {
TSS_LOG: debug ? `-level verbose -file ${projectPath}/tss.log` : undefined,
KITAJS_TESTING: 'true'
}
});

this.exitPromise = deferred();
Expand Down

0 comments on commit 42cd262

Please sign in to comment.