Skip to content

Commit

Permalink
feat: better TypeScript support
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Nov 10, 2019
1 parent cd9e76f commit 66f65c5
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 95 deletions.
17 changes: 14 additions & 3 deletions .eslintrc.js
@@ -1,4 +1,13 @@
const { jest, js, test, ts, mdx } = require('@1stg/eslint-config/overrides')
const {
config,
jest,
js,
test,
ts,
mdx,
} = require('@1stg/eslint-config/overrides')

const tsBase = ts[0]

module.exports = {
extends: ['@1stg/eslint-config/base', 'plugin:import/typescript'],
Expand All @@ -9,9 +18,10 @@ module.exports = {
'@typescript-eslint/parser': [],
'babel-eslint': ['.ts'],
},
...ts.settings,
...tsBase.settings,
},
overrides: [
tsBase,
{
files: '.*rc.js',
rules: {
Expand All @@ -20,10 +30,11 @@ module.exports = {
},
{
...js,
files: '*.{js,ts}',
files: 'test/*.{js,ts}',
},
jest,
mdx,
test,
config,
],
}
2 changes: 1 addition & 1 deletion .prettierrc.js
Expand Up @@ -5,7 +5,7 @@ module.exports = {
overrides: [
...config.overrides,
{
files: '*.ts',
files: 'test/*.ts',
options: {
parser: 'babel',
},
Expand Down
17 changes: 13 additions & 4 deletions README.md
Expand Up @@ -59,9 +59,9 @@ npm i -D babel-preset-proposal-typescript
| option | description | defaults |
| ------------------------ | -------------------------------------------------------------------------------------------------------- | ------------------------------------- |
| `classLoose` | whether to use loose mode for class properties and private methods | `true` |
| `decoratorsBeforeExport` | See [Babel Document](https://babeljs.io/docs/en/babel-plugin-proposal-decorators#decoratorsbeforeexport) | N/A |
| `decoratorsBeforeExport` | See [Babel Document](https://babeljs.io/docs/en/babel-plugin-proposal-decorators#decoratorsbeforeexport) | `undefined` |
| `decoratorsLegacy` | whether to use legacy decorators semantic | `true` |
| `isTSX` | whether to enable `jsx` plugin with `typescript` | `false`, `true` but for `/\.[jt]sx$/` |
| `isTSX` | whether to enable `jsx` plugin with `typescript` | `false`, but `true` for `/\.[jt]sx$/` |
| `pipelineOperator` | implementation of pipeline operator | `"minimal"` |

## Usage
Expand Down Expand Up @@ -92,11 +92,20 @@ require('@babel/core').transform('code', {

### Via webpack

Pipe codes through `babel-loader` to `ts-loader` or `awesome-typescript-loader`.
Pipe codes through `babel-loader`.

```js
loader = {
test: /\.ts$/,
test: /\.[jt]sx?$/,
loader: 'babel-loader',
options: {
presets: ['proposal-typescript'],
},
}

// if you prefer `ts-loader` or `awesome-typescript-loader`
loader = {
test: /\.tsx?$/,
use: [
{
loader: 'ts-loader',
Expand Down
21 changes: 14 additions & 7 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "babel-preset-proposal-typescript",
"version": "1.4.5",
"version": "1.4.6",
"description": "Yet another Babel preset for TypeScript, only transforms proposals which TypeScript does not support now.",
"repository": "git@github.com/rx-ts/babel-preset-proposal-typescript.git",
"author": "JounQin <admin@1stg.me>",
Expand All @@ -9,10 +9,13 @@
"node": ">=6.9.0"
},
"main": "lib/cjs",
"module": "lib/esm",
"module": "lib",
"es2015": "lib/es2015",
"fesm5": "lib/esm",
"types": "lib",
"files": [
"lib"
"lib",
"!*.tsbuildinfo"
],
"keywords": [
"babel-preset",
Expand All @@ -24,8 +27,12 @@
"typescript"
],
"scripts": {
"build": "r",
"lint": "cross-env PARSER_NO_WATCH=true eslint . --ext js,md,ts -f friendly",
"build": "run-p build:*",
"build:r": "r",
"build:ts": "tsc -P src",
"lint": "run-p lint:*",
"lint:es": "cross-env PARSER_NO_WATCH=true eslint . --ext js,md,ts -f friendly",
"lint:tsc": "tsc -P src --noEmit",
"postinstall": "bash scripts/postinstall.sh || exit 0",
"prepublishOnly": "yarn test",
"test": "jest"
Expand Down Expand Up @@ -61,11 +68,11 @@
"@types/jest": "^24.0.22",
"babel-jest": "^24.9.0",
"babel-preset-proposal-typescript": "link:.",
"jest": "^24.9.0"
"jest": "^24.9.0",
"npm-run-all": "^4.1.5"
},
"resolutions": {
"prettier": "^1.19.1",
"rollup-plugin-url": "^2.2.4",
"typescript": "~3.6.4"
}
}
150 changes: 81 additions & 69 deletions src/index.ts
Expand Up @@ -17,77 +17,89 @@ import { declare } from '@babel/helper-plugin-utils'

import syntaxV8intrinsic from './v8intrinsic'

export default declare((api, opts) => {
api.assertVersion(7)
import { ConfigAPI } from '@babel/core'

const {
classLoose = true,
decoratorsBeforeExport,
decoratorsLegacy = true,
isTSX,
pipelineOperator = 'minimal',
} = opts
export interface ProposalTypeScriptOptions {
classLoose?: boolean
decoratorsBeforeExport?: boolean
decoratorsLegacy?: boolean
isTSX?: boolean
pipelineOperator?: 'minimal'
}

return {
plugins: [
[
syntaxDecorators,
{
decoratorsBeforeExport,
legacy: decoratorsLegacy,
},
],
syntaxDynamicImport,
[
syntaxTypeScript,
{
isTSX,
},
],
syntaxV8intrinsic,
[
proposalClassProperties,
{
loose: classLoose,
},
],
proposalDoExpressions,
proposalFunctionBind,
proposalFunctionSent,
proposalJsonStrings,
proposalLogicalAssignmentOperators,
proposalNullishCoalescingOperator,
proposalOptionalChaining,
proposalPartialApplication,
[
proposalPipelineOperator,
{
proposal: pipelineOperator,
},
],
[
proposalPrivateMethods,
{
loose: classLoose,
},
],
proposalThrowExpression,
],
// no need to override if it has been enabled
overrides: isTSX
? undefined
: [
export default declare(
(
api: ConfigAPI,
{
classLoose = true,
decoratorsBeforeExport,
decoratorsLegacy = true,
isTSX,
pipelineOperator = 'minimal',
}: ProposalTypeScriptOptions,
) => {
api.assertVersion(7)
return {
plugins: [
[
syntaxDecorators,
{
test: /\.[jt]sx$/,
plugins: [
[
syntaxTypeScript,
{
isTSX: true,
},
],
],
decoratorsBeforeExport,
legacy: decoratorsLegacy,
},
],
syntaxDynamicImport,
[
syntaxTypeScript,
{
isTSX,
},
],
syntaxV8intrinsic,
[
proposalClassProperties,
{
loose: classLoose,
},
],
proposalDoExpressions,
proposalFunctionBind,
proposalFunctionSent,
proposalJsonStrings,
proposalLogicalAssignmentOperators,
proposalNullishCoalescingOperator,
proposalOptionalChaining,
proposalPartialApplication,
[
proposalPipelineOperator,
{
proposal: pipelineOperator,
},
],
}
})
[
proposalPrivateMethods,
{
loose: classLoose,
},
],
proposalThrowExpression,
],
// no need to override if it has been enabled
overrides: isTSX
? undefined
: [
{
test: /\.[jt]sx$/,
plugins: [
[
syntaxTypeScript,
{
isTSX: true,
},
],
],
},
],
}
},
)
8 changes: 8 additions & 0 deletions src/shim.d.ts
@@ -0,0 +1,8 @@
declare module '@babel/helper-plugin-utils' {
import { ConfigAPI, PluginItem } from '@babel/core'
export const declare: (
fn: (api: ConfigAPI, options: {}) => PluginItem,
) => PluginItem
}

declare module '@babel/*'
7 changes: 7 additions & 0 deletions src/tsconfig.json
@@ -0,0 +1,7 @@
{
"extends": "@1stg/tsconfig/lib",
"compilerOptions": {
"baseUrl": ".",
"outDir": "../lib"
}
}
8 changes: 5 additions & 3 deletions src/v8intrinsic.ts
@@ -1,11 +1,13 @@
import { declare } from '@babel/helper-plugin-utils'

export default declare((api, options) => {
import { ConfigAPI, TransformOptions } from '@babel/core'

export default declare((api: ConfigAPI) => {
api.assertVersion(7)
return {
name: 'v8intrinsic',
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push('v8intrinsic')
manipulateOptions(_opts: {}, parserOpts: TransformOptions) {
parserOpts.plugins!.push('v8intrinsic')
},
}
})
1 change: 1 addition & 0 deletions test/class-properties.ts
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/member-ordering */
const Test = () => {
//
}
Expand Down
2 changes: 1 addition & 1 deletion test/function-sent.ts
@@ -1,4 +1,4 @@
function* adder(total?: number = 0) {
function* adder(total? = 0) {
let increment = 1
let request
do {
Expand Down
1 change: 1 addition & 0 deletions test/v8intrinsic.spec.ts
Expand Up @@ -6,6 +6,7 @@ const proposal = 'v8intrinsic'

test(proposal, () => {
expect(() => execute(proposal)).toThrowErrorMatchingSnapshot()
// eslint-disable-next-line @typescript-eslint/no-require-imports
expect(() => require('./v8intrinsic')).toThrowErrorMatchingSnapshot()
expect(
execSync(`node --allow-natives-syntax ${resolve(proposal)}.ts`).toString(),
Expand Down

0 comments on commit 66f65c5

Please sign in to comment.