Skip to content

Commit

Permalink
WIP: jest is the worst
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Apr 10, 2024
1 parent c50dbe1 commit 79631de
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 56 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@
"source-map-js": "1.0.2"
},
"pnpm": {
"overrides": {
"ember-template-recast": "^6.1.4",
"@glimmer/reference": "^0.91.1",
"@glimmer/syntax": "^0.91.1",
"@glimmer/validator": "^0.91.1"
},
"packageExtensions": {
"ts-jest": {
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-lib/src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async function processFile(
} else if (fn.endsWith('.hbs')) {
debug('Processing %s using hbs extractor', fn)
const {parseFile} = await import('./hbs_extractor.js')
parseFile(source, fn, opts)
await parseFile(source, fn, opts)
} else if (fn.endsWith('.gts') || fn.endsWith('.gjs')) {
debug('Processing %s as gts/gjs file', fn)
const {parseFile} = await import('./gts_extractor.js')
Expand Down
8 changes: 6 additions & 2 deletions packages/cli-lib/src/gts_extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {parseScript} from './parse_script'
let {Preprocessor} = require('content-tag')
let p = new Preprocessor()

export function parseFile(source: string, fileName: string, options: any) {
export async function parseFile(
source: string,
fileName: string,
options: any
) {
const scriptParseFn = parseScript(options, fileName)
const transformedSource = p.process(source, {filename: fileName})

Expand All @@ -20,6 +24,6 @@ export function parseFile(source: string, fileName: string, options: any) {
const parseResult = p.parse(source, {filename: fileName})

for (let parsed of parseResult) {
parseHbsFile(parsed.contents, fileName, options)
await parseHbsFile(parsed.contents, fileName, options)
}
}
39 changes: 32 additions & 7 deletions packages/cli-lib/src/hbs_extractor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {transform} from 'ember-template-recast'
import type {AST} from '@glimmer/syntax'
// We can't import these types because @glimmer/syntax is ESM
// and our TS is compiled to CJS
// and TSC doesn't know how to ignore the compatibility problems with that
// when we're just doing type impotrs.
// import type {AST} from '@glimmer/syntax'
import {Opts} from '@formatjs/ts-transformer'

function extractText(
node: AST.MustacheStatement | AST.SubExpression,
// node: AST.MustacheStatement | AST.SubExpression,
node: any,
fileName: string,
options: Opts
) {
Expand Down Expand Up @@ -41,19 +45,40 @@ function extractText(
}
}

export function parseFile(source: string, fileName: string, options: any) {
export async function parseFile(
source: string,
fileName: string,
options: any
) {
// ember-template-recast has ESM dependencies (@glimmer/syntax)
// even though ember-template-recast is usable in CJS environments...
// TSC doesn't agree.
//
// this repo is actually FAKE TS - it's actually compiled to CJS.
// so any dependency that uses types from an ESM-sub-dependency, must be
// await imported.
//
// Most of the problem is actually an incompatibility between CJS + MJS await imports
// and jest's poor-support of ESM (mjs extensions).
// as soon as this repo is off of jest, extract.ts can await import mts files
// with 0 issue.
// @ts-ignore
const {transform} = await (import('ember-template-recast') as any)

let visitor = function () {
return {
MustacheStatement(node: AST.MustacheStatement) {
// MustacheStatement(node: AST.MustacheStatement) {
MustacheStatement(node: any) {
extractText(node, fileName, options)
},
SubExpression(node: AST.SubExpression) {
// SubExpression(node: AST.SubExpression) {
SubExpression(node: any) {
extractText(node, fileName, options)
},
}
}

// SAFETY: ember-template-recast's types are out of date,
// but it does not affect runtime
transform(source, visitor as any)
transform(source, visitor)
}
2 changes: 1 addition & 1 deletion packages/cli-lib/tests/unit/gjs_extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {join} from 'path'
test('gts_extractor', async function () {
let messages: MessageDescriptor[] = []
const fixturePath = join(__dirname, './fixtures/comp.gjs')
parseFile(await readFile(fixturePath, 'utf8'), fixturePath, {
await parseFile(await readFile(fixturePath, 'utf8'), fixturePath, {
onMsgExtracted(_: any, msgs: any) {
messages = messages.concat(msgs)
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-lib/tests/unit/gts_extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {join} from 'path'
test('gts_extractor', async function () {
let messages: MessageDescriptor[] = []
const fixturePath = join(__dirname, './fixtures/comp.gts')
parseFile(await readFile(fixturePath, 'utf8'), fixturePath, {
await parseFile(await readFile(fixturePath, 'utf8'), fixturePath, {
onMsgExtracted(_: any, msgs: any) {
messages = messages.concat(msgs)
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-lib/tests/unit/hbs_extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {join} from 'path'
test('hbs_extractor', async function () {
let messages: MessageDescriptor[] = []
const fixturePath = join(__dirname, './fixtures/comp.hbs')
parseFile(await readFile(fixturePath, 'utf8'), fixturePath, {
await parseFile(await readFile(fixturePath, 'utf8'), fixturePath, {
onMsgExtracted(_: any, msgs: any) {
messages = messages.concat(msgs)
},
Expand Down
50 changes: 7 additions & 43 deletions pnpm-lock.yaml

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

0 comments on commit 79631de

Please sign in to comment.