Skip to content

Commit

Permalink
chore: use SWC to transpile Jest test sources (#4108)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrocat101 committed May 22, 2023
1 parent cc276e2 commit 5adcc4b
Show file tree
Hide file tree
Showing 22 changed files with 143 additions and 342 deletions.
30 changes: 30 additions & 0 deletions .swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// The default (lowest build target) transpilation settings.
{
"$schema": "https://json.schemastore.org/swcrc",
"sourceMaps": false,
"module": {
// To match the tsc output.
"strictMode": false,
"type": "commonjs",
"noInterop": false
},
"jsc": {
"externalHelpers": true,
"target": "es5",
"parser": {
"syntax": "typescript",
"tsx": true,
"decorators": false,
"dynamicImport": true
},
"transform": {
"react": {
"throwIfNamespace": false,
"useBuiltins": false,
"pragma": "React.createElement",
"pragmaFrag": "React.Fragment"
}
},
"keepClassNames": false
}
}
6 changes: 6 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ CONFIG_FILES = [
for f in CONFIG_FILES
]

copy_to_bin(
name = "swcrc",
srcs = [".swcrc"],
visibility = ["//visibility:public"],
)

TSCONFIG_FILES = [
"tsconfig.es6.json",
"tsconfig.node.json",
Expand Down
7 changes: 2 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
globals: {
'ts-jest': {
tsconfig: 'tsconfig.json',
},
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
verbose: true,
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
},
"author": "Seth Bertalotto <sbertal@verizonmedia.com>",
"dependencies": {
"@swc/helpers": "^0.5.1",
"jest-cli": "^29.5.0",
"jest-junit": "^16.0.0",
"magic-string": "^0.30.0"
Expand Down
25 changes: 15 additions & 10 deletions packages/cli-lib/src/vue_extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import type {
CompoundExpressionNode,
DirectiveNode,
} from '@vue/compiler-core'
import {NodeTypes} from '@vue/compiler-core'

// Duplicate `NodeTypes` here because they are defined as
// const enum, which does not work with swc transpilation: https://github.com/vuejs/core/issues/1228
const ELEMENT = 1 as const
const SIMPLE_EXPRESSION = 4 as const
const INTERPOLATION = 5 as const
const DIRECTIVE = 7 as const
const COMPOUND_EXPRESSION = 8 as const

export type ScriptParseFn = (source: string) => void

Expand All @@ -25,21 +32,19 @@ function walk(
return
}
if (
node.type !== NodeTypes.ELEMENT &&
node.type !== NodeTypes.COMPOUND_EXPRESSION &&
node.type !== NodeTypes.INTERPOLATION
node.type !== ELEMENT &&
node.type !== COMPOUND_EXPRESSION &&
node.type !== INTERPOLATION
) {
return
}
visitor(node)
if (node.type === NodeTypes.INTERPOLATION) {
if (node.type === INTERPOLATION) {
visitor(node.content)
} else if (node.type === NodeTypes.ELEMENT) {
} else if (node.type === ELEMENT) {
node.children.forEach(n => walk(n, visitor))
node.props
.filter(
(prop): prop is DirectiveNode => prop.type === NodeTypes.DIRECTIVE
)
.filter((prop): prop is DirectiveNode => prop.type === DIRECTIVE)
.filter(prop => !!prop.exp)
.forEach(prop => visitor(prop.exp!))
} else {
Expand All @@ -57,7 +62,7 @@ function templateSimpleExpressionNodeVisitor(parseScriptFn: ScriptParseFn) {
if (typeof n !== 'object') {
return
}
if (n.type !== NodeTypes.SIMPLE_EXPRESSION) {
if (n.type !== SIMPLE_EXPRESSION) {
return
}

Expand Down
1 change: 1 addition & 0 deletions packages/cli-lib/tests/unit/vue_extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {parseScript} from '../../src/parse_script'
import {parseFile} from '../../src/vue_extractor'
import {readFile} from 'fs-extra'
import {join} from 'path'

test('vue_extractor', async function () {
let messages: MessageDescriptor[] = []
const fixturePath = join(__dirname, './fixtures/comp.vue')
Expand Down
2 changes: 1 addition & 1 deletion packages/icu-messageformat-parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function parse(message: string, opts: ParserOptions = {}) {
}
return result.val
}
export {ParserOptions}
export type {ParserOptions}
export * from './types'
// only for testing
export const _Parser = Parser
3 changes: 1 addition & 2 deletions packages/intl-segmenter/benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Suite, Event} from 'benchmark'
import {__read} from 'tslib'

//@ts-ignore
// const Segmenter = Intl.Segmenter
Expand All @@ -16,7 +15,7 @@ Etiam viverra dignissim pharetra. Praesent gravida finibus tellus eu tempus. Pra
Etiam malesuada libero sem, sit amet consequat dolor blandit non. In ut venenatis ligula. Pellentesque mattis imperdiet turpis id efficitur. Nunc felis lorem, vulputate a porttitor laoreet, mattis ac quam. Etiam molestie mi ac erat tincidunt interdum. Vestibulum sed nulla sit amet justo auctor fermentum. Sed varius ipsum lectus, sit amet eleifend nisi mollis at. In ac libero quis purus blandit venenatis. Praesent hendrerit quam vel risus tempor vehicula id eu enim. `

//multiply the input string to increase the size
inputString = __read(Array(10).keys()).reduce(res => res + inputString, '')
inputString = inputString.repeat(10)

console.log(
`Locale: ${locale}\nInput string size: ${Math.round(
Expand Down
8 changes: 4 additions & 4 deletions packages/intl-segmenter/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/

import {Segmenter} from './src/segmenter'

import {__read} from 'tslib'
import {segmentationTests} from './tests/test-utils'

//parse argvs
Expand Down Expand Up @@ -117,7 +115,9 @@ const debugRun = (
)
}
debugRun(granularity, range)
const failingRulesArray = __read(failingRules.values())
const failingRulesArray = Array.from(failingRules.values())
if (failingRulesArray.length) {
console.log(`Mismatching rules: ${__read(failingRules.values()).join(', ')}`)
console.log(
`Mismatching rules: ${Array.from(failingRules.values()).join(', ')}`
)
}
9 changes: 4 additions & 5 deletions packages/intl-segmenter/tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
*/
import {readFileSync} from 'node:fs'
import * as pathUtil from 'node:path'
import {__read, __spreadArray} from 'tslib'

function testDataFromLine(line: string) {
const [test, comment] = line.split('#')
const trimmedTest = test.trim()
const trimmedComment = comment.trim()

const testMatches = __read(
const testMatches = Array.from(
trimmedTest.matchAll(/\s?([÷×])\s?([0-9A-F]{4,})?\s?/g)
)

Expand All @@ -28,7 +27,7 @@ function testDataFromLine(line: string) {
throw new Error(`Error parsing test line: '${trimmedTest}'`)
}

const commentMatches = __read(
const commentMatches = Array.from(
trimmedComment.matchAll(/([×÷])\s(\[([0-9\.]+)\])?([^×÷]+)?/g)
)
totalMatchedLength = 0
Expand Down Expand Up @@ -61,13 +60,13 @@ function testDataFromLine(line: string) {
...testDetails
//ignore eot entries
.filter(({characterName}) => characterName !== 'EOT')
.map(({codePoint}) => codePoint)
.map(({codePoint}) => codePoint as number)
)

let segmentNr = 0
const expected = Object.values(testDetails).reduce((result, testPart) => {
if (!result.length) {
result[0] = String.fromCodePoint(testPart.codePoint)
result[0] = String.fromCodePoint(testPart.codePoint as number)
} else if (testPart.codePoint) {
if (testPart.breaks) {
segmentNr++
Expand Down
8 changes: 3 additions & 5 deletions packages/intl-segmenter/tests/ucd-segmentation-tests.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {Segmenter} from '../src/segmenter'

import {__read} from 'tslib'
import {segmentationTests} from './test-utils'

describe.each(Object.entries(segmentationTests))(
Expand All @@ -12,9 +10,9 @@ describe.each(Object.entries(segmentationTests))(
it.each(
ucdTests.map(test => [test.comment, test.testInput, test.expected])
)(`Test ${granularity} #%#: '%s'`, (_, testInput, expected) => {
const segmentedInput = __read(segmenter.segment(testInput as string)).map(
({segment}) => segment
)
const segmentedInput = Array.from(
segmenter.segment(testInput as string)
).map(result => result!.segment)
expect(segmentedInput).toEqual(expected)
})
}
Expand Down
8 changes: 2 additions & 6 deletions packages/react-intl/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
module.exports = {
preset: 'ts-jest',
globals: {
'ts-jest': {
tsconfig: 'tsconfig.json',
diagnostics: false,
},
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
testEnvironment: 'jsdom',
testRegex: ['/tests/(functional|unit)/.*\\.(ts|tsx)'],
Expand Down
1 change: 0 additions & 1 deletion packages/swc-plugin-experimental/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ jest_test(
"tests/transform.ts",
":index.wasm",
"//:node_modules/@swc/core",
"//:node_modules/@swc/jest",
"//:node_modules/@types/node",
] + glob([
"tests/**/*.test.ts",
Expand Down

0 comments on commit 5adcc4b

Please sign in to comment.