Skip to content

Commit

Permalink
Add support for Typescript to carbon/react (#12222)
Browse files Browse the repository at this point in the history
* refactor: add build support for TS

* refactor: convert Checkbox and Text to typescript

* refactor: add ts extensions to jest

* refactor: add ts extensions to jsTransform.js

* refactor: add type exports to barrels

* refactor: do not convert TextDirectionContext to TS

* chore: add comment

* refactor: update Text with context

* refactor: use @type jsdoc assertion

* refactor: fix build and turn off explicit any rule

* chore: yarn dedup

* chore: run install withh yarn 3.2.4
  • Loading branch information
mbarrer committed Nov 1, 2022
1 parent 9cb3ddb commit fa262d5
Show file tree
Hide file tree
Showing 49 changed files with 539 additions and 224 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
19 changes: 19 additions & 0 deletions config/eslint-config-carbon/plugins/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ module.exports = {
],
},
overrides: [
{
files: ['*.ts', '*.tsx'],
plugins: ['@typescript-eslint'],
extends: ['plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
rules: {
'no-unused-vars': 'off', // Disabled in favor of @typescript-eslint/no-unused-vars
'@typescript-eslint/no-unused-vars': [
'error',
{
args: 'after-used',
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
},
],
'@typescript-eslint/no-empty-function': 'off', // Disabled to support default empty functions used in PropTypes
'@typescript-eslint/no-explicit-any': 'off', // TODO: Enable once stricter typings of internal utilities are supported
},
},
// Sometimes we'll want to define a quick component in a story to use as a
// wrapper for a component we're documenting. For example:
//
Expand Down
8 changes: 5 additions & 3 deletions config/jest-config-carbon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'use strict';

module.exports = {
moduleFileExtensions: ['js', 'json', 'node'],
moduleFileExtensions: ['tsx', 'ts', 'js', 'json', 'node'],
moduleNameMapper: {
// This mapping is the result of updating to Jest 28. We currently require
// this as the version of uuid that gets resolved is ESM but we would like
Expand All @@ -27,9 +27,11 @@ module.exports = {
'<rootDir>/**/*-(spec|test).js?(x)',
],
transform: {
'^.+\\.(mjs|cjs|js|jsx)$': require.resolve('./transform/jsTransform.js'),
'^.+\\.(mjs|cjs|js|jsx|ts|tsx)$': require.resolve(
'./transform/jsTransform.js'
),
'^.+\\.s?css$': require.resolve('./transform/cssTransform.js'),
'^(?!.*\\.(js|jsx|css|json)$)': require.resolve(
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': require.resolve(
'./transform/fileTransform.js'
),
},
Expand Down
1 change: 1 addition & 0 deletions config/jest-config-carbon/transform/jsTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const babelOptions = {
},
],
'@babel/preset-react',
'@babel/preset-typescript',
],
plugins: [
'@babel/plugin-proposal-class-properties',
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"postinstall": "husky install"
},
"resolutions": {
"@types/react": "~17.0.2",
"@types/prop-types": "15.7.4",
"ast-types": "^0.14.0",
"node-sass": "^6.0.0",
"react": "~17.0.2",
Expand Down
5 changes: 4 additions & 1 deletion packages/react/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ react-docgen.json

# Cypress artifacts
cypress/videos
cypress/screenshots
cypress/screenshots

# Typescript
tsconfig.tsbuildinfo
2 changes: 1 addition & 1 deletion packages/react/.storybook/.babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
'use strict';

module.exports = {
presets: ['babel-preset-carbon'],
presets: ['babel-preset-carbon', '@babel/preset-typescript'],
};
3 changes: 3 additions & 0 deletions packages/react/.storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ module.exports = {
},
framework: '@storybook/react',
stories,
typescript: {
reactDocgen: 'react-docgen', // Favor docgen from prop-types instead of TS interfaces
},
webpack(config) {
const babelLoader = config.module.rules.find((rule) => {
return rule.use.some(({ loader }) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^21.0.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-typescript": "^8.5.0",
"@storybook/addon-a11y": "^6.5.6",
"@storybook/addon-actions": "^6.5.6",
"@storybook/addon-docs": "^6.5.6",
Expand All @@ -88,6 +89,8 @@
"@storybook/manager-webpack5": "^6.5.6",
"@storybook/react": "^6.5.6",
"@storybook/theming": "^6.5.6",
"@typescript-eslint/eslint-plugin": "^5.38.1",
"@typescript-eslint/parser": "^5.38.1",
"autoprefixer": "^10.4.0",
"babel-loader": "^8.2.3",
"babel-plugin-dev-expression": "^0.2.3",
Expand Down Expand Up @@ -117,6 +120,7 @@
"storybook-readme": "^5.0.9",
"stream-browserify": "^3.0.0",
"style-loader": "^3.3.1",
"typescript": "^4.8.4",
"webpack": "^5.65.0",
"webpack-dev-server": "^4.7.4"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
*/

import PropTypes from 'prop-types';
import React from 'react';
import React, { HTMLAttributes } from 'react';
import cx from 'classnames';
import { usePrefix } from '../../internal/usePrefix';

const CheckboxSkeleton = ({ className, ...rest }) => {
const CheckboxSkeleton = ({
className,
...rest
}: HTMLAttributes<HTMLDivElement>) => {
const prefix = usePrefix();
return (
<div
Expand Down
130 changes: 0 additions & 130 deletions packages/react/src/components/Checkbox/Checkbox.js

This file was deleted.

Loading

0 comments on commit fa262d5

Please sign in to comment.