Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typescript #24337

Merged
merged 6 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion airflow/www/.babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"env": {
"test": {
"presets": ["@babel/preset-env", "@babel/preset-react"],
"presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"],
"plugins": ["@babel/plugin-transform-runtime"]
}
}
Expand Down
28 changes: 27 additions & 1 deletion airflow/www/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@
"rules": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add typescript-eslint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was playing around with it. Let me make sure it's in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added!

"no-param-reassign": 1,
"react/prop-types": 0,
"react/jsx-props-no-spreading": 0
"react/jsx-props-no-spreading": 0,
"react/jsx-filename-extension": [1, { "extensions": [".jsx", ".tsx"] }],
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
],
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": true,
"optionalDependencies": false,
"peerDependencies": false
}
]
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
}
}
2 changes: 1 addition & 1 deletion airflow/www/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
const config = {
verbose: true,
transform: {
'^.+\\.jsx?$': 'babel-jest',
'^.+\\.[jt]sx?$': 'babel-jest',
},
testEnvironment: 'jsdom',
setupFilesAfterEnv: ['./jest-setup.js'],
Expand Down
10 changes: 6 additions & 4 deletions airflow/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"dev": "NODE_ENV=dev webpack --watch --colors --progress --debug --output-pathinfo --devtool eval-cheap-source-map --mode development",
"prod": "NODE_ENV=production node --max_old_space_size=4096 ./node_modules/webpack/bin/webpack.js -p --colors --progress",
"build": "NODE_ENV=production webpack --colors --progress",
"lint": "eslint --ignore-path=.eslintignore --ext .js,.jsx .",
"lint:fix": "eslint --fix --ignore-path=.eslintignore --ext .js,.jsx ."
"lint": "eslint --ignore-path=.eslintignore --ext .js,.jsx,.ts,.tsx . && tsc --noEmit",
"lint:fix": "eslint --fix --ignore-path=.eslintignore --ext .js,.jsx,.ts,.tsx . && tsc --noEmit"
},
"author": "Apache",
"license": "Apache-2.0",
Expand All @@ -29,6 +29,7 @@
"@babel/plugin-transform-runtime": "^7.16.0",
"@babel/preset-env": "^7.16.0",
"@babel/preset-react": "^7.16.0",
"@babel/preset-typescript": "^7.17.12",
"@testing-library/jest-dom": "^5.15.0",
"@testing-library/react": "^12.1.2",
"@testing-library/react-hooks": "^7.0.2",
Expand Down Expand Up @@ -56,14 +57,15 @@
"imports-loader": "^1.1.0",
"jest": "^27.3.1",
"mini-css-extract-plugin": "1.6.0",
"moment": "^2.29.2",
"moment": "^2.29.3",
"moment-locales-webpack-plugin": "^1.2.0",
"nock": "^13.2.4",
"optimize-css-assets-webpack-plugin": "6.0.0",
"style-loader": "^1.2.1",
"stylelint": "^13.6.1",
"stylelint-config-standard": "^20.0.0",
"terser-webpack-plugin": "<5.0.0",
"typescript": "^4.6.3",
"url-loader": "4.1.0",
"webpack": "4.44.2",
"webpack-cli": "^3.3.12",
Expand All @@ -90,7 +92,7 @@
"jquery": ">=3.5.0",
"jshint": "^2.13.4",
"lodash": "^4.17.21",
"moment-timezone": "^0.5.28",
"moment-timezone": "^0.5.34",
"nvd3": "^1.8.6",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

/* global describe, test, expect, document, Event */
/* global describe, test, expect, document, CustomEvent */

import React from 'react';
import {
Expand All @@ -33,7 +33,7 @@ describe('Test Time and TimezoneProvider', () => {
test('Displays a UTC time correctly', () => {
const now = new Date();
const { getByText } = render(
<Time dateTime={now} />,
<Time dateTime={now.toISOString()} />,
{ wrapper: Wrapper },
);

Expand All @@ -48,7 +48,7 @@ describe('Test Time and TimezoneProvider', () => {
moment.tz.setDefault(tz);

const { getByText } = render(
<Time dateTime={now} />,
<Time dateTime={now.toISOString()} />,
{ wrapper: Wrapper },
);

Expand All @@ -60,17 +60,17 @@ describe('Test Time and TimezoneProvider', () => {
test('Updates based on timezone change', async () => {
const now = new Date();
const { getByText, queryByText } = render(
<Time dateTime={now} />,
<Time dateTime={now.toISOString()} />,
{ wrapper: Wrapper },
);

const utcTime = queryByText(moment.utc(now).format(defaultFormatWithTZ));
expect(utcTime).toBeDefined();

// Fire a custom timezone change event
const event = new Event(TimezoneEvent);
event.value = 'EST';
event.key = 'selected-timezone';
const event = new CustomEvent(TimezoneEvent, {
detail: 'EST',
});
await act(async () => {
fireEvent(document, event);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
* under the License.
*/

/* global moment */

import React from 'react';
import moment from 'moment-timezone';
import { useTimezone } from '../context/timezone';
import { defaultFormatWithTZ } from '../../datetime_utils';

const Time = ({ dateTime, format = defaultFormatWithTZ }) => {
interface Props {
dateTime: string;
format?: string;
}

const Time: React.FC<Props> = ({ dateTime, format = defaultFormatWithTZ }) => {
const { timezone } = useTimezone();
const time = moment(dateTime);

// eslint-disable-next-line no-underscore-dangle
if (!dateTime || !time._isValid) return null;
if (!dateTime || !time.isValid()) return null;

const formattedTime = time.tz(timezone).format(format);
const utcTime = time.tz('UTC').format(defaultFormatWithTZ);
Expand Down
4 changes: 2 additions & 2 deletions airflow/www/static/js/grid/context/timezone.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import React, { useContext, useEffect, useState } from 'react';
import { TimezoneEvent } from '../../datetime_utils';

const TimezoneContext = React.createContext(null);
const TimezoneContext = React.createContext({ timezone: 'UTC' });

export const TimezoneProvider = ({ children }) => {
const [timezone, setTimezone] = useState((moment.defaultZone && moment.defaultZone.name) || 'UTC');

const handleChange = (e) => {
if (e.value && e.value !== timezone) setTimezone(e.value);
if (e.detail && e.detail !== timezone) setTimezone(e.detail);
};

useEffect(() => {
Expand Down
8 changes: 4 additions & 4 deletions airflow/www/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
/* global $, moment, Airflow, window, localStorage, document, hostName, csrfToken, Event */
/* global $, moment, Airflow, window, localStorage, document, hostName, csrfToken, CustomEvent */

import {
dateTimeAttrFormat,
Expand Down Expand Up @@ -46,9 +46,9 @@ function changeDisplayedTimezone(tz) {
localStorage.setItem('selected-timezone', tz);

// dispatch an event that React can listen for
const event = new Event(TimezoneEvent);
event.value = tz;
event.key = 'selected-timezone';
const event = new CustomEvent(TimezoneEvent, {
detail: tz,
});
document.dispatchEvent(event);

setDisplayedTimezone(tz);
Expand Down
42 changes: 42 additions & 0 deletions airflow/www/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*!
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* TypeScript config
*/
{
"compilerOptions": {
"strict": true,
"allowJs": true,
"importsNotUsedAsValues": "error",
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"isolatedModules": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"jsx": "preserve",
"types": ["node", "jest"],
},
"include": [
"static/js",
],
"exclude": ["node_modules", "static/dist"]
}
6 changes: 4 additions & 2 deletions airflow/www/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ const config = {
extensions: [
'.js',
'.jsx',
'.ts',
'.tsx',
'.css',
],
},
Expand All @@ -100,11 +102,11 @@ const config = {
loader: 'imports-loader?define=>false',
},
{
test: /\.jsx?$/,
test: /\.[j|t]sx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react'],
presets: ['@babel/preset-react', '@babel/preset-typescript'],
},
},
// Extract css files
Expand Down
Loading