Skip to content

Commit

Permalink
Adapt to company standards
Browse files Browse the repository at this point in the history
Also upgade to Typescript 4
  • Loading branch information
sveyret committed Sep 1, 2020
1 parent e8e0f50 commit 6fc2474
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 167 deletions.
9 changes: 8 additions & 1 deletion .depcheckrc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
---
detectors:
- importDeclaration
- exportDeclaration
- importCallExpression
- requireCallExpression
- requireResolveCallExpression
- typescriptImportEqualsDeclaration
- typescriptImportType
specials:
- bin
- eslint
Expand Down
1 change: 0 additions & 1 deletion .eslintrc.full.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
extends:
- ./.eslintrc.yaml
rules:
Expand Down
107 changes: 67 additions & 40 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
---
env:
node: true
shared-node-browser: true
es6: true
es2017: true
parser: '@typescript-eslint/parser'
parserOptions:
project: tsconfig.json
project:
- tsconfig.json
plugins:
- prettier
settings:
Expand All @@ -21,18 +22,9 @@ extends:
- prettier
- prettier/@typescript-eslint
rules:
'@typescript-eslint/array-type':
- error
- default: array-simple
readonly: generic
'@typescript-eslint/consistent-type-definitions': error
curly: error
dot-notation: error
eqeqeq: error
'@typescript-eslint/explicit-function-return-type': 'off'
'@typescript-eslint/explicit-member-accessibility':
- error
- accessibility: explicit
id-blacklist:
- error
- any
Expand All @@ -44,66 +36,101 @@ rules:
- boolean
- Undefined
id-match: error
'@typescript-eslint/interface-name-prefix': 'off'
max-classes-per-file: 'off'
jsdoc/newline-after-description: error
no-bitwise: 'off'
no-caller: error
no-console: warn
no-eval: error
'@typescript-eslint/no-explicit-any': 'off'
no-extra-bind: error
import/no-extraneous-dependencies:
- error
- optionalDependencies: false
devDependencies:
- '**/*.spec.ts'
import/no-internal-modules: error
no-invalid-this: 'off'
node/no-missing-import: 'off'
no-new-wrappers: error
'@typescript-eslint/no-non-null-assertion': 'off'
'@typescript-eslint/no-parameter-properties': 'off'
no-restricted-syntax:
- error
- SequenceExpression
no-return-await: error
import/no-self-import: error
no-shadow:
- error
- hoist: all
no-template-curly-in-string: error
no-throw-literal: error
jsdoc/no-types: error
no-undef-init: error
node/no-unsupported-features/es-syntax: 'off'
no-unused-expressions:
- error
- allowShortCircuit: true
'@typescript-eslint/no-unused-vars':
- error
- argsIgnorePattern: '^_'
ignoreRestSiblings: true
'@typescript-eslint/no-use-before-define': 'off'
allowTernary: true
allowTaggedTemplates: true
object-shorthand: error
one-var:
- error
- never
prefer-arrow-callback:
- error
- allowUnboundThis: false
'@typescript-eslint/prefer-for-of': error
'@typescript-eslint/prefer-function-type': error
prefer-object-spread: error
prettier/prettier: warn
radix: error
require-atomic-updates: 'off'
jsdoc/require-description-complete-sentence: warn
spaced-comment: warn
valid-typeof: 'off'
space-in-parens: error
'@typescript-eslint/array-type':
- error
- default: array-simple
readonly: generic
'@typescript-eslint/consistent-type-definitions': error
'@typescript-eslint/explicit-function-return-type':
- error
- allowExpressions: true
allowTypedFunctionExpressions: true
'@typescript-eslint/explicit-member-accessibility':
- error
- accessibility: explicit
'@typescript-eslint/interface-name-prefix': 'off'
'@typescript-eslint/no-explicit-any': 'off'
'@typescript-eslint/no-non-null-assertion': 'off'
'@typescript-eslint/no-parameter-properties': 'off'
'@typescript-eslint/no-unused-vars':
- error
- argsIgnorePattern: '^_'
ignoreRestSiblings: true
'@typescript-eslint/no-use-before-define':
- error
- functions: false
classes: true
enums: false
variables: false
typedefs: false
'@typescript-eslint/prefer-for-of': error
'@typescript-eslint/prefer-function-type': error
'@typescript-eslint/unified-signatures': warn
import/no-extraneous-dependencies:
- warn
- optionalDependencies: false
devDependencies:
- '**/*.spec.ts'
import/no-internal-modules: warn
import/no-self-import: error
import/order:
- warn
- groups:
- - builtin
- external
- internal
newlines-between: always
alphabetize:
order: asc
caseInsensitive: true
jsdoc/newline-after-description: warn
jsdoc/no-types: warn
jsdoc/no-undefined-types: 'off'
jsdoc/require-description-complete-sentence:
- warn
- abbreviations:
- i.e.
- e.g.
jsdoc/require-hyphen-before-param-description: warn
jsdoc/require-jsdoc: 'off'
jsdoc/require-param-type: 'off'
jsdoc/require-returns-type: 'off'
spaced-comment: error
valid-typeof: 'off'
space-in-parens: error
'@typescript-eslint/unified-signatures': error
node/no-missing-import: 'off'
node/no-unsupported-features/es-syntax: 'off'
prettier/prettier: warn
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Distributable
# Binaries
node_modules/
dist/

Expand All @@ -10,6 +10,7 @@ coverage/
package-lock.json
yarn.lock
pnpm-lock.yaml
pnpm-debug.log

# IDE
*.code-workspace
Expand Down
1 change: 0 additions & 1 deletion .mocharc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
require:
- 'ts-node/register'
- 'source-map-support/register'
1 change: 0 additions & 1 deletion .nycrc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
extends: '@istanbuljs/nyc-config-typescript'
include:
- 'src/**'
Expand Down
1 change: 0 additions & 1 deletion .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
arrowParens: avoid
printWidth: 108
semi: false
Expand Down
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
language: node_js
node_js:
- '10'
- '12'
before_install:
- npm install -g coveralls
after_success:
- coveralls < ./coverage/lcov.info
branches:
only:
- master
- /^v\d+\.\d+\.\d+(-.*)?$/
2 changes: 1 addition & 1 deletion LICENCE → LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ You may also be interrested in:

# Language/langue

Because Slune is French firm, you will find all documents and messages in French. Other translations are welcome.
Documents, messages, code (including variable names and comments), are in English.

Anyway, because English is the language of programming, the code, including variable names and comments, are in English.
Anyway, because Slune is French firm, all documents and important messages must also be provided in French. Other translations are welcome.

:fr: Une version française de ce document se trouve [ici](doc/fr/README.md).

Expand All @@ -33,12 +33,6 @@ Installation is done using `npm install` command:
$ npm install --save simple-ts-transform
```

If you prefer using `yarn`:

```bash
$ yarn add simple-ts-transform
```

# Usage

## Context
Expand All @@ -48,22 +42,24 @@ First, create the context class. You can put in it whatever you need for your vi
The context class constructor will be called with:

- a `ts.Program` parameter,
- an `any` object containing the configuration provided to the transformer.
- an `unknown` object containing the configuration provided to the transformer.

The context class must also implement the `initNewFile(context: TransformationContext, sourceFile: SourceFile): void` method, called before visiting each new file.

```typescript
// MyContext.ts
import { NodeVisitorContext } from 'simple-ts-transform'
import { Program, SourceFile, TransformationContext } from 'typescript'
import type { NodeVisitorContext } from 'simple-ts-transform'
import type { NodeFactory, Program, SourceFile, TransformationContext } from 'typescript'

export default class MyContext implements NodeVisitorContext {
public readonly basePath: string
public fileName?: string
public constructor(program: Program, public readonly configuration: any) {
public factory!: NodeFactory
public fileName!: string
public constructor(program: Program, public readonly _configuration: unknown) {
this.basePath = program.getCompilerOptions().rootDir || program.getCurrentDirectory()
}
public initNewFile(_context: TransformationContext, sourceFile: SourceFile): void {
public initNewFile(context: TransformationContext, sourceFile: SourceFile): void {
this.factory = context.factory
this.fileName = sourceFile.fileName
}
}
Expand All @@ -79,13 +75,15 @@ The visitor constructor will be called with your context as single parameter.

Your visitor must implement the following methods:

- The method `wants(node: Node): node is N` make some basic checkings on the node to indicate if the visitor will manage it or not. This method also serves as a type guard to ensure the node is of appropriate type.
- The method `wants(node: Node): node is N` make some basic checks on the node to indicate if the visitor will manage it or not. This method also serves as a type guard to ensure the node is of appropriate type.
- The method `visit(node: N): Node[]` is given the node to visit in order to work on it. This method returns an array of 0, 1 or more nodes, so it can remove, update or create nodes. The nodes created by this visitor will be visited by all the following provided visitors.

```typescript
// MyFileNameInserter.ts
import { NodeVisitor } from 'simple-ts-transform'
import { Node, StringLiteral } from 'typescript'
import type { NodeVisitor } from 'simple-ts-transform'
import type { Node, StringLiteral } from 'typescript'
import { isStringLiteral } from 'typescript'
import type { MyContext } from './MyContext'

export default class MyFileNameInserter implements NodeVisitor<StringLiteral> {
private readonly fileName: string
Expand All @@ -96,6 +94,7 @@ export default class MyFileNameInserter implements NodeVisitor<StringLiteral> {
return isStringLiteral(node)
}
public visit(node: StringLiteral) {
const { createStringLiteral } = this.context.factory
return [createStringLiteral(this.fileName + ': ' + node.getText().slice(1, -1)]
}
}
Expand Down Expand Up @@ -136,6 +135,8 @@ There is currently no way of declaring a transformer in the vanilla _TypeScript_
Note that the built transformer is of type `program` which is the default for `ttypescript`. That's why you don't need to add a `type` entry in the configuration.
# Contributions
# Contributing
Even though we cannot guarantee a response time, please feel free to file an issue if you have any question or problem using the package.
Even though we cannot guarantee a response time, please feel free to file an issue if you have any question or problem using the package. Pull request are also welcome.
_Pull Requests_ are welcome. You can, of course, submit corrections or improvements for code, but do not hesitate to also improve documentation, even for small spell or grammar errors.

0 comments on commit 6fc2474

Please sign in to comment.