Skip to content

Commit

Permalink
Merge pull request #1292 from cucumber/html-circular-dep-fix
Browse files Browse the repository at this point in the history
Remove circular imports in typescript code, add linting
  • Loading branch information
aslakhellesoy committed Feb 5, 2021
2 parents b218f87 + 6ebbf91 commit cf0aa29
Show file tree
Hide file tree
Showing 94 changed files with 648 additions and 473 deletions.
3 changes: 3 additions & 0 deletions .templates/javascript/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
},
"plugins": [
"react",
"import",
"node",
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:import/typescript",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {
"import/no-cycle": "error",
"node/no-extraneous-import": "error",
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/ban-ts-comment": "off",
Expand Down
3 changes: 3 additions & 0 deletions c21e/javascript/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
},
"plugins": [
"react",
"import",
"node",
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:import/typescript",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {
"import/no-cycle": "error",
"node/no-extraneous-import": "error",
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/ban-ts-comment": "off",
Expand Down
1 change: 1 addition & 0 deletions c21e/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@typescript-eslint/parser": "^4.6.1",
"eslint": "^7.12.1",
"eslint-config-prettier": "^7.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.21.5",
Expand Down
3 changes: 3 additions & 0 deletions compatibility-kit/javascript/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
},
"plugins": [
"react",
"import",
"node",
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:import/typescript",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {
"import/no-cycle": "error",
"node/no-extraneous-import": "error",
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/ban-ts-comment": "off",
Expand Down
1 change: 1 addition & 0 deletions compatibility-kit/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@typescript-eslint/parser": "^4.6.1",
"eslint": "^7.12.1",
"eslint-config-prettier": "^7.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.21.5",
Expand Down
3 changes: 3 additions & 0 deletions create-meta/javascript/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
},
"plugins": [
"react",
"import",
"node",
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:import/typescript",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {
"import/no-cycle": "error",
"node/no-extraneous-import": "error",
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/ban-ts-comment": "off",
Expand Down
1 change: 1 addition & 0 deletions create-meta/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@typescript-eslint/parser": "^4.6.1",
"eslint": "^7.12.1",
"eslint-config-prettier": "^7.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.21.5",
Expand Down
3 changes: 3 additions & 0 deletions cucumber-expressions/javascript/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
},
"plugins": [
"react",
"import",
"node",
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:import/typescript",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {
"import/no-cycle": "error",
"node/no-extraneous-import": "error",
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/ban-ts-comment": "off",
Expand Down
2 changes: 2 additions & 0 deletions cucumber-expressions/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
"@typescript-eslint/parser": "^4.9.1",
"eslint": "^7.15.0",
"eslint-config-prettier": "^7.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.2.0",
"eslint-plugin-react": "^7.21.5",
"js-yaml": "^4.0.0",
"mocha": "^8.2.1",
"nyc": "^15.1.0",
"prettier": "^2.2.1",
Expand Down
2 changes: 1 addition & 1 deletion cucumber-expressions/javascript/src/Argument.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import TreeRegexp from './TreeRegexp'
import ParameterType from './ParameterType'
import Group from './Group'
import { CucumberExpressionError } from './Errors'
import CucumberExpressionError from './CucumberExpressionError'

export default class Argument<T> {
public static build(
Expand Down
2 changes: 1 addition & 1 deletion cucumber-expressions/javascript/src/CucumberExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
createOptionalMayNotBeEmpty,
createParameterIsNotAllowedInOptional,
createUndefinedParameterType,
CucumberExpressionError,
} from './Errors'
import Expression from './Expression'
import CucumberExpressionParser from './CucumberExpressionParser'
import { Node, NodeType } from './Ast'
import CucumberExpressionError from './CucumberExpressionError'

const ESCAPE_PATTERN = () => /([\\^[({$.|?*+})\]])/g

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class CucumberExpressionError extends Error {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ParameterTypeRegistry from './ParameterTypeRegistry'
import ParameterTypeMatcher from './ParameterTypeMatcher'
import ParameterType from './ParameterType'

Expand All @@ -7,7 +6,9 @@ import CombinatorialGeneratedExpressionFactory from './CombinatorialGeneratedExp
import GeneratedExpression from './GeneratedExpression'

export default class CucumberExpressionGenerator {
constructor(private readonly parameterTypeRegistry: ParameterTypeRegistry) {}
constructor(
private readonly parameterTypes: () => Iterable<ParameterType<any>>
) {}

public generateExpressions(text: string): ReadonlyArray<GeneratedExpression> {
const parameterTypeCombinations: Array<Array<ParameterType<any>>> = []
Expand Down Expand Up @@ -90,7 +91,7 @@ export default class CucumberExpressionGenerator {

private createParameterTypeMatchers(text: string): ParameterTypeMatcher[] {
let parameterMatchers: ParameterTypeMatcher[] = []
for (const parameterType of this.parameterTypeRegistry.parameterTypes) {
for (const parameterType of this.parameterTypes()) {
if (parameterType.useForSnippets) {
parameterMatchers = parameterMatchers.concat(
CucumberExpressionGenerator.createParameterTypeMatchers2(
Expand Down
9 changes: 1 addition & 8 deletions cucumber-expressions/javascript/src/Errors.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import ParameterType from './ParameterType'
import GeneratedExpression from './GeneratedExpression'
import { Located, Node, purposeOf, symbolOf, Token, TokenType } from './Ast'

export class CucumberExpressionError extends Error {}
import CucumberExpressionError from './CucumberExpressionError'

export function createAlternativeMayNotExclusivelyContainOptionals(
node: Node,
Expand Down Expand Up @@ -138,12 +137,6 @@ export function createCantEscaped(expression: string, index: number) {
)
}

export function createInvalidParameterTypeName(typeName: string) {
return new CucumberExpressionError(
`Illegal character in parameter name {${typeName}}. Parameter names may not contain '{', '}', '(', ')', '\\' or '/'`
)
}

export function createInvalidParameterTypeNameInNode(
token: Token,
expression: string
Expand Down
9 changes: 4 additions & 5 deletions cucumber-expressions/javascript/src/ParameterType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
createInvalidParameterTypeName,
CucumberExpressionError,
} from './Errors'
import CucumberExpressionError from './CucumberExpressionError'

const ILLEGAL_PARAMETER_NAME_PATTERN = /([[\]()$.|?*+])/
const UNESCAPE_PATTERN = () => /(\\([[$.|?*+\]]))/g
Expand All @@ -21,7 +18,9 @@ export default class ParameterType<T> {

public static checkParameterTypeName(typeName: string) {
if (!this.isValidParameterTypeName(typeName)) {
throw createInvalidParameterTypeName(typeName)
throw new CucumberExpressionError(
`Illegal character in parameter name {${typeName}}. Parameter names may not contain '{', '}', '(', ')', '\\' or '/'`
)
}
}

Expand Down
9 changes: 5 additions & 4 deletions cucumber-expressions/javascript/src/ParameterTypeRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import ParameterType from './ParameterType'

import CucumberExpressionGenerator from './CucumberExpressionGenerator'
import { AmbiguousParameterTypeError, CucumberExpressionError } from './Errors'
import { AmbiguousParameterTypeError } from './Errors'
import CucumberExpressionError from './CucumberExpressionError'

export default class ParameterTypeRegistry {
public static readonly INTEGER_REGEXPS = [/-?\d+/, /\d+/]
Expand Down Expand Up @@ -69,7 +70,7 @@ export default class ParameterTypeRegistry {
)
}

get parameterTypes() {
get parameterTypes(): IterableIterator<ParameterType<any>> {
return this.parameterTypeByName.values()
}

Expand All @@ -88,10 +89,10 @@ export default class ParameterTypeRegistry {
}
if (parameterTypes.length > 1 && !parameterTypes[0].preferForRegexpMatch) {
// We don't do this check on insertion because we only want to restrict
// ambiguiuty when we look up by Regexp. Users of CucumberExpression should
// ambiguity when we look up by Regexp. Users of CucumberExpression should
// not be restricted.
const generatedExpressions = new CucumberExpressionGenerator(
this
() => this.parameterTypes
).generateExpressions(text)
throw AmbiguousParameterTypeError.forRegExp(
parameterTypeRegexp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ describe('CucumberExpressionGenerator', () => {

beforeEach(() => {
parameterTypeRegistry = new ParameterTypeRegistry()
generator = new CucumberExpressionGenerator(parameterTypeRegistry)
generator = new CucumberExpressionGenerator(
() => parameterTypeRegistry.parameterTypes
)
})

it('documents expression generation', () => {
parameterTypeRegistry = new ParameterTypeRegistry()
generator = new CucumberExpressionGenerator(parameterTypeRegistry)
generator = new CucumberExpressionGenerator(
() => parameterTypeRegistry.parameterTypes
)
const undefinedStepText = 'I have 2 cucumbers and 1.5 tomato'
const generatedExpression = generator.generateExpressions(
undefinedStepText
Expand Down Expand Up @@ -267,7 +271,7 @@ describe('CucumberExpressionGenerator', () => {

const expressions = generator.generateExpressions('I download a picture')
assert.strictEqual(expressions.length, 1)
assert.notEqual(expressions[0].source, 'I {direction}load a picture')
assert.notStrictEqual(expressions[0].source, 'I {direction}load a picture')
assert.strictEqual(expressions[0].source, 'I download a picture')
})

Expand All @@ -278,7 +282,10 @@ describe('CucumberExpressionGenerator', () => {

const expressions = generator.generateExpressions('I watch the muppet show')
assert.strictEqual(expressions.length, 1)
assert.notEqual(expressions[0].source, 'I watch the m{direction}pet show')
assert.notStrictEqual(
expressions[0].source,
'I watch the m{direction}pet show'
)
assert.strictEqual(expressions[0].source, 'I watch the muppet show')
})

Expand All @@ -289,7 +296,7 @@ describe('CucumberExpressionGenerator', () => {

const expressions = generator.generateExpressions('I create a group')
assert.strictEqual(expressions.length, 1)
assert.notEqual(expressions[0].source, 'I create a gro{direction}')
assert.notStrictEqual(expressions[0].source, 'I create a gro{direction}')
assert.strictEqual(expressions[0].source, 'I create a group')
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import fs from 'fs'
// eslint-disable-next-line node/no-extraneous-import
import yaml from 'js-yaml' // why?
import yaml from 'js-yaml'
import assert from 'assert'
import { CucumberExpressionError } from '../src/Errors'
import CucumberExpressionParser from '../src/CucumberExpressionParser'
import CucumberExpressionError from '../src/CucumberExpressionError'

interface Expectation {
expression: string
Expand All @@ -14,7 +13,7 @@ interface Expectation {
describe('Cucumber expression parser', () => {
fs.readdirSync('testdata/ast').forEach((testcase) => {
const testCaseData = fs.readFileSync(`testdata/ast/${testcase}`, 'utf-8')
const expectation = yaml.safeLoad(testCaseData) as Expectation
const expectation = yaml.load(testCaseData) as Expectation
it(`${testcase}`, () => {
const parser = new CucumberExpressionParser()
if (expectation.exception == undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import CucumberExpression from '../src/CucumberExpression'
import ParameterTypeRegistry from '../src/ParameterTypeRegistry'
import ParameterType from '../src/ParameterType'
import fs from 'fs'
// eslint-disable-next-line node/no-extraneous-import
import yaml from 'js-yaml' // why?
import { CucumberExpressionError } from '../src/Errors'
import yaml from 'js-yaml'
import CucumberExpressionError from '../src/CucumberExpressionError'

interface Expectation {
expression: string
Expand All @@ -20,7 +19,7 @@ describe('CucumberExpression', () => {
`testdata/expression/${testcase}`,
'utf-8'
)
const expectation = yaml.safeLoad(testCaseData) as Expectation
const expectation = yaml.load(testCaseData) as Expectation
it(`${testcase}`, () => {
const parameterTypeRegistry = new ParameterTypeRegistry()
if (expectation.exception == undefined) {
Expand Down Expand Up @@ -51,7 +50,7 @@ describe('CucumberExpression', () => {

fs.readdirSync('testdata/regex').forEach((testcase) => {
const testCaseData = fs.readFileSync(`testdata/regex/${testcase}`, 'utf-8')
const expectation = yaml.safeLoad(testCaseData) as Expectation
const expectation = yaml.load(testCaseData) as Expectation
it(`${testcase}`, () => {
const parameterTypeRegistry = new ParameterTypeRegistry()
const expression = new CucumberExpression(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import fs from 'fs'
// eslint-disable-next-line node/no-extraneous-import
import yaml from 'js-yaml' // why?
import yaml from 'js-yaml'
import CucumberExpressionTokenizer from '../src/CucumberExpressionTokenizer'
import assert from 'assert'
import { CucumberExpressionError } from '../src/Errors'
import CucumberExpressionError from '../src/CucumberExpressionError'

interface Expectation {
expression: string
Expand All @@ -14,7 +13,7 @@ interface Expectation {
describe('Cucumber expression tokenizer', () => {
fs.readdirSync('testdata/tokens').forEach((testcase) => {
const testCaseData = fs.readFileSync(`testdata/tokens/${testcase}`, 'utf-8')
const expectation = yaml.safeLoad(testCaseData) as Expectation
const expectation = yaml.load(testCaseData) as Expectation
it(`${testcase}`, () => {
const tokenizer = new CucumberExpressionTokenizer()
if (expectation.exception == undefined) {
Expand Down

0 comments on commit cf0aa29

Please sign in to comment.