Skip to content

Commit

Permalink
feat: support scripts. (#3)
Browse files Browse the repository at this point in the history
* test: commented.
  • Loading branch information
knightedcodemonkey authored Jun 3, 2024
1 parent cd48afa commit 53d9d4f
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 10 deletions.
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ export default tseslint.config(
'no-console': 'error',
},
},
{
files: ['test/fixtures/**/*'],
rules: {
'@typescript-eslint/no-unused-vars': 'off',
},
},
)
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@knighted/reparse",
"version": "1.1.0",
"version": "1.2.0",
"description": "Multiple swc parsings of the same file with correct spans.",
"type": "module",
"main": "dist/reparse.js",
Expand Down
2 changes: 1 addition & 1 deletion src/child.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { parseSync, parseFileSync } from '@swc/core'
const baseConfig = {
comments: false,
target: 'esnext',
isModule: true,
isModule: 'unknown',
}
const tsConfig = {
syntax: 'typescript',
Expand Down
2 changes: 1 addition & 1 deletion src/childSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { parseSync, parseFileSync } from '@swc/core'
const baseConfig = {
comments: false,
target: 'esnext',
isModule: true,
isModule: 'unknown',
}
const tsConfig = {
syntax: 'typescript',
Expand Down
10 changes: 5 additions & 5 deletions src/reparse.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { fork, spawnSync } from 'node:child_process'
import { join } from 'node:path'

import type { Module } from '@swc/core'
import type { Module, Script } from '@swc/core'

type Lang = 'ts' | 'es'

const isModule = (msg: unknown): msg is Module => {
if (msg && typeof msg === 'object' && 'type' in msg && msg.type === 'Module') {
const isModuleOrScript = (msg: unknown): msg is Module | Script => {
if (msg && typeof msg === 'object' && 'type' in msg && (msg.type === 'Module' || msg.type === 'Script')) {
return true
}

Expand All @@ -17,9 +17,9 @@ const forkChild = (...args: string[]) => {
serialization: 'advanced',
})

return new Promise<Module>((resolve, reject) => {
return new Promise<Module | Script>((resolve, reject) => {
child.on('message', msg => {
if (isModule(msg)) {
if (isModuleOrScript(msg)) {
resolve(msg)
}

Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/commented.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Some start comment.
*/

function foo() {
// Some inline comment.
return 'bar'
}

/**
* Some end comment.
*/
16 changes: 16 additions & 0 deletions test/reparse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,20 @@ describe('@knighted/reparse', () => {
assert.equal(ast3.span.start, ast4.span.start)
assert.equal(ast3.span.end, ast4.span.end)
})

it('works with comments', async () => {
const ast1 = await reparseFile(join(fixtures, 'commented.js'))
const ast2 = await reparseFile(join(fixtures, 'commented.js'))

assert.equal(ast1.span.start, ast2.span.start)
assert.equal(ast1.span.end, ast2.span.end)
})

it.skip('works with large files', async () => {
const ast1 = await reparseFile(join(fixtures, 'large.js'))
const ast2 = await reparseFile(join(fixtures, 'large.js'))

assert.equal(ast1.span.start, ast2.span.start)
assert.equal(ast1.span.end, ast2.span.end)
})
})

0 comments on commit 53d9d4f

Please sign in to comment.