Skip to content

Commit

Permalink
Convert to TypeScript (#135)
Browse files Browse the repository at this point in the history
Fixes #135 
* Convert all .js to .ts - and update build
* Bump version number
* Update Node version for builds on CircleCI
* Update test coverage & test coverage reporting
  • Loading branch information
TroyAlford authored Jun 2, 2020
1 parent 947f4db commit d0ff9d3
Show file tree
Hide file tree
Showing 52 changed files with 2,350 additions and 1,105 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
],
"presets": [
"@babel/preset-env",
"@babel/preset-react"
"@babel/preset-react",
"@babel/preset-typescript"
],
"highlightCode": true
}
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jobs:
build:
working_directory: ~/react-jsx-parser
docker:
- image: circleci/node:11.10.1
- image: circleci/node:12.9.1
steps:
- checkout
- restore_cache:
Expand All @@ -17,7 +17,7 @@ jobs:
test:
working_directory: ~/react-jsx-parser
docker:
- image: circleci/node:11.10.1
- image: circleci/node:12.9.1
steps:
- checkout
- restore_cache:
Expand Down
9 changes: 7 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
"jest": true,
"node": true
},
"parser": "babel-eslint",
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/no-unused-vars": "error",
"arrow-parens": ["error", "as-needed"],
"comma-dangle": ["error", "always-multiline"],
"default-case": "off",
"semi": ["error", "never"],
"import/extensions": ["error", { "js": "never", "scss": "always", "ts": "never" }],
"import/no-unresolved": "off",
"import/prefer-default-export": "off",
"lines-between-class-members": "off",
"no-case-declarations": "off",
"no-unused-vars": "off",
"object-curly-newline": ["error", { "ObjectPattern": { "multiline": true } }],
"operator-linebreak": "off",
"react/destructuring-assignment": "off",
"react/jsx-filename-extension": ["error", { "extensions": [".js"] }],
"react/jsx-filename-extension": ["error", { "extensions": [".test.js", ".tsx"] }],
"react/jsx-one-expression-per-line": "off",
"react/jsx-props-no-spreading": "off",
"react/prop-types": "off",
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
**/coverage
**/node_modules
**/npm-debug.log
**/test-coverage
**/yarn-error.log
35 changes: 35 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const path = require('path')

module.exports = {
collectCoverageFrom: [
'**/source/**/*.{ts,tsx,js}',
'!**/node_modules/**',
'!**/lib/**',
],
coverageDirectory: path.resolve(__dirname, 'test-coverage'),
coverageReporters: ['html', 'lcov'],
coverageThreshold: {
global: { branches: 85, functions: 95, lines: 95, statements: 95 },
},
globals: {
moduleFileExtensions: ['ts', 'tsx', 'js', 'json'],
},
moduleDirectories: ['node_modules'],
/* eslint-disable sort-keys */
moduleNameMapper: {
'^(.*).scss$': path.resolve(__dirname, 'jest/mock.styles.ts'),
'^(.*).(jpg|png|gif|eot|otf|svg|ttf|woff2?)$': path.resolve(__dirname, 'jest/mock.files.ts'),
},
/* eslint-enable sort-keys */
modulePaths: [
'<rootDir>/mocks',
'<rootDir>/node_modules',
],
setupFilesAfterEnv: [
path.resolve(__dirname, 'jest/jest.setup.ts'),
],
testEnvironment: 'jest-environment-jsdom-fourteen',
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
},
}
File renamed without changes.
1 change: 1 addition & 0 deletions jest/mock.files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {} // eslint-disable-line
1 change: 1 addition & 0 deletions jest/mock.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'fake-file' // eslint-disable-line
2 changes: 1 addition & 1 deletion lib/cjs/react-jsx-parser.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/cjs/react-jsx-parser.min.js.map

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions lib/types/components/JsxParser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { Component } from 'react';
import type { Expression, JSXElement, JSXIdentifier, JSXMemberExpression, MemberExpression } from '../types/acorn-jsx';
declare type BlacklistedAttr = string | RegExp;
declare type Props = {
allowUnknownElements?: boolean;
bindings?: {
[key: string]: any;
};
blacklistedAttrs?: BlacklistedAttr[];
blacklistedTags?: string[];
className?: string;
components?: React.JSXElementConstructor<any>[];
componentsOnly?: boolean;
disableFragments?: boolean;
disableKeyGeneration?: boolean;
jsx?: string;
onError?: (error: Error) => void;
showWarnings?: boolean;
renderError?: ({ error: string }: {
error: any;
}) => JSX.Element;
renderInWrapper?: boolean;
renderUnrecognized?: (tagName: string) => JSX.Element;
};
export declare type ParsedJSX = JSX.Element | boolean | string;
export declare type ParsedTree = ParsedJSX | ParsedJSX[];
export default class JsxParser extends Component<Props> {
static displayName: string;
static defaultProps: {
allowUnknownElements: boolean;
bindings: {};
blacklistedAttrs: RegExp[];
blacklistedTags: string[];
className: string;
components: any[];
componentsOnly: boolean;
disableFragments: boolean;
disableKeyGeneration: boolean;
jsx: string;
onError: () => void;
showWarnings: boolean;
renderError: any;
renderInWrapper: boolean;
renderUnrecognized: () => any;
};
ParsedChildren: ParsedTree;
parseJSX: (jsx: string) => JSX.Element | JSX.Element[];
parseExpression: (expression: Expression) => any;
parseMemberExpression: (expression: MemberExpression) => any;
parseName: (element: JSXIdentifier | JSXMemberExpression) => string;
parseElement: (element: JSXElement) => JSX.Element | JSX.Element[];
render: () => JSX.Element;
}
export {};
//# sourceMappingURL=JsxParser.d.ts.map
1 change: 1 addition & 0 deletions lib/types/components/JsxParser.d.ts.map

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

9 changes: 9 additions & 0 deletions lib/types/constants/attributeNames.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare const _default: {
class: string;
for: string;
maxlength: string;
colspan: string;
rowspan: string;
};
export default _default;
//# sourceMappingURL=attributeNames.d.ts.map
1 change: 1 addition & 0 deletions lib/types/constants/attributeNames.d.ts.map

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

5 changes: 5 additions & 0 deletions lib/types/constants/specialTags.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare const VOID_ELEMENTS: string[];
export default VOID_ELEMENTS;
export declare function canHaveChildren(tagName: string): boolean;
export declare function canHaveWhitespace(tagName: string): boolean;
//# sourceMappingURL=specialTags.d.ts.map
1 change: 1 addition & 0 deletions lib/types/constants/specialTags.d.ts.map

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

8 changes: 8 additions & 0 deletions lib/types/helpers/camelCase.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Converts a string from other cases to camelCase
* @param string the value to camelCase
* @example
* camelCase('foo-bar') // 'fooBar'
*/
export declare const camelCase: (string: string) => string;
//# sourceMappingURL=camelCase.d.ts.map
1 change: 1 addition & 0 deletions lib/types/helpers/camelCase.d.ts.map

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

11 changes: 11 additions & 0 deletions lib/types/helpers/hash.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Hashes a value
* @param value the value to hash
* @param radix the base-n to hash into (default 16)
*/
export declare const hash: (value?: string, radix?: number) => string;
/**
* Hashes a Math.random() value, returning it in base16
*/
export declare const randomHash: () => string;
//# sourceMappingURL=hash.d.ts.map
1 change: 1 addition & 0 deletions lib/types/helpers/hash.d.ts.map

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

9 changes: 9 additions & 0 deletions lib/types/helpers/parseStyle.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare type Style = string | Partial<CSSStyleDeclaration>;
/**
* Converts a CSS Style string
* @param {string | Partial<CSSStyleDeclaration>} style A string to convert, or object to return
* @returns {Partial<CSSStyleDeclaration>} a partial CSSStyleDeclaration
*/
export declare const parseStyle: (style: Style) => Partial<CSSStyleDeclaration>;
export {};
//# sourceMappingURL=parseStyle.d.ts.map
1 change: 1 addition & 0 deletions lib/types/helpers/parseStyle.d.ts.map

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

10 changes: 10 additions & 0 deletions lib/types/helpers/resolvePath.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Returns the result of a path query from an object
* @param {any} object the object to search
* @param {string} path the path, whose value will be retrieved
* @returns {any} the value (undefined if the path doesn't exist)
* @example
* resolvePath({ foo: { bar: { baz: 3 } } }, 'foo.bar.baz') // 3
*/
export declare const resolvePath: (object: any, path: string) => any;
//# sourceMappingURL=resolvePath.d.ts.map
1 change: 1 addition & 0 deletions lib/types/helpers/resolvePath.d.ts.map

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

2 changes: 2 additions & 0 deletions lib/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './components/JsxParser';
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions lib/types/index.d.ts.map

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

123 changes: 123 additions & 0 deletions lib/types/types/acorn-jsx.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
export declare type BaseExpression = {
type: string;
start: number;
};
export declare type JSXAttribute = BaseExpression & {
type: 'JSXAttribute';
elements?: Expression[];
expression?: null | Expression;
name: {
name: string;
};
value: Expression;
};
export declare type JSXAttributeExpression = BaseExpression & {
type: 'JSXAttributeExpression';
argument?: Expression;
};
export declare type JSXElement = BaseExpression & {
type: 'JSXElement';
children: JSXElement[];
openingElement: JSXElement & {
attributes: JSXAttribute[];
};
name: JSXIdentifier | JSXMemberExpression;
};
export declare type JSXExpressionContainer = BaseExpression & {
type: 'JSXExpressionContainer';
expression: Expression;
};
export declare type JSXIdentifier = BaseExpression & {
type: 'JSXIdentifier';
name: string;
};
export declare type JSXMemberExpression = BaseExpression & {
type: 'JSXMemberExpression';
object: JSXIdentifier | JSXMemberExpression;
property: JSXIdentifier | JSXMemberExpression;
};
export declare type JSXSpreadAttribute = BaseExpression & {
type: 'JSXSpreadAttribute';
argument: Identifier;
};
export declare type JSXText = BaseExpression & {
type: 'JSXText';
value: string;
};
export declare type ArrayExpression = BaseExpression & {
type: 'ArrayExpression';
elements: Expression[];
};
export declare type BinaryExpression = BaseExpression & {
type: 'BinaryExpression';
left: Expression;
operator: string;
right: Expression;
};
export declare type CallExpression = BaseExpression & {
type: 'CallExpression';
arguments: Expression[];
callee?: Expression;
};
export declare type ConditionalExpression = BaseExpression & {
type: 'ConditionalExpression';
alternate: Expression;
consequent: Expression;
test: Expression;
};
export declare type ExpressionStatement = BaseExpression & {
type: 'ExpressionStatement';
expression: Expression;
};
export declare type Identifier = BaseExpression & {
type: 'Identifier';
name: string;
};
export declare type Literal = BaseExpression & {
type: 'Literal';
value: string;
};
export declare type LogicalExpression = BaseExpression & {
type: 'LogicalExpression';
left: Expression;
operator: string;
right: Expression;
};
export declare type MemberExpression = BaseExpression & {
type: 'MemberExpression';
computed: boolean;
name?: string;
object: Literal | MemberExpression;
property?: MemberExpression;
raw?: string;
};
export declare type ObjectExpression = BaseExpression & {
type: 'ObjectExpression';
properties: [{
key: {
name?: string;
value?: string;
};
value: Expression;
}];
};
export declare type TemplateElement = BaseExpression & {
type: 'TemplateElement';
value: {
cooked: string;
};
};
export declare type TemplateLiteral = BaseExpression & {
type: 'TemplateLiteral';
expressions: Expression[];
quasis: Expression[];
};
export declare type UnaryExpression = BaseExpression & {
type: 'UnaryExpression';
operator: string;
argument: {
value: any;
};
};
export declare type Expression = JSXAttribute | JSXAttributeExpression | JSXElement | JSXExpressionContainer | JSXSpreadAttribute | JSXText | ArrayExpression | BinaryExpression | CallExpression | ConditionalExpression | ExpressionStatement | Identifier | Literal | LogicalExpression | MemberExpression | ObjectExpression | TemplateElement | TemplateLiteral | UnaryExpression;
//# sourceMappingURL=acorn-jsx.d.ts.map
1 change: 1 addition & 0 deletions lib/types/types/acorn-jsx.d.ts.map

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

2 changes: 1 addition & 1 deletion lib/umd/react-jsx-parser.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/umd/react-jsx-parser.min.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit d0ff9d3

Please sign in to comment.