-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a7a7bfe
Showing
21 changed files
with
737 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
specials: | ||
- bin | ||
- eslint | ||
- istanbul | ||
- mocha | ||
- prettier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
env: | ||
node: true | ||
es6: true | ||
parser: '@typescript-eslint/parser' | ||
parserOptions: | ||
project: tsconfig.json | ||
plugins: | ||
- import | ||
- prettier | ||
settings: | ||
jsdoc: | ||
mode: typescript | ||
extends: | ||
- eslint:recommended | ||
- plugin:node/recommended | ||
- plugin:@typescript-eslint/eslint-recommended | ||
- plugin:@typescript-eslint/recommended | ||
- plugin:import/errors | ||
- plugin:import/warnings | ||
- plugin:import/typescript | ||
- plugin:jsdoc/recommended | ||
- 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 | ||
- Number | ||
- number | ||
- String | ||
- string | ||
- Boolean | ||
- 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' | ||
import/no-cycle: error | ||
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' | ||
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 | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Distributable | ||
node_modules/ | ||
dist/ | ||
|
||
# Coverage | ||
.nyc_output/ | ||
coverage/ | ||
|
||
# Package managers | ||
package-lock.json | ||
yarn.lock | ||
pnpm-lock.yaml | ||
|
||
# IDE | ||
*.code-workspace | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
require: | ||
- 'ts-node/register' | ||
- 'source-map-support/register' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
extends: '@istanbuljs/nyc-config-typescript' | ||
include: | ||
- 'src/**' | ||
exclude: | ||
- '**/*.spec.ts' | ||
reporter: | ||
- lcov | ||
- text | ||
all: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
arrowParens: avoid | ||
printWidth: 108 | ||
semi: false | ||
singleQuote: true | ||
trailingComma: es5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language: node_js | ||
node_js: | ||
- '10' | ||
before_install: | ||
- npm install -g coveralls | ||
after_success: | ||
- coveralls < ./coverage/lcov.info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Slune | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
[![npm package](https://badge.fury.io/js/simple-ts-transform.svg)](https://www.npmjs.com/package/simple-ts-transform) | ||
[![License](https://img.shields.io/github/license/slune-org/simple-ts-transform.svg)](https://github.com/slune-org/simple-ts-transform/blob/master/LICENSE) | ||
[![Build Status](https://travis-ci.org/slune-org/simple-ts-transform.svg?branch=master)](https://travis-ci.org/slune-org/simple-ts-transform) | ||
[![Coverage Status](https://coveralls.io/repos/github/slune-org/simple-ts-transform/badge.svg?branch=master)](https://coveralls.io/github/slune-org/simple-ts-transform?branch=master) | ||
[![Issues](https://img.shields.io/github/issues/slune-org/simple-ts-transform.svg)](https://github.com/slune-org/simple-ts-transform/issues) | ||
|
||
# simple-ts-transform - Library to help create simple typescript transformers | ||
|
||
This package provides a simple API to build _TypeScript_ transformers, based on a shared context and multiple node visitors. | ||
|
||
For usage examples, you can look at: | ||
|
||
- [ts-transform-asset](https://github.com/slune-org/ts-transform-asset) | ||
- [ts-transform-auto-require](https://github.com/slune-org/ts-transform-auto-require) | ||
|
||
You may also be interrested in: | ||
|
||
- [ts-transform-test-compiler](https://github.com/slune-org/ts-transform-test-compiler) | ||
|
||
# Language/langue | ||
|
||
Because Slune is French firm, you will find all documents and messages in French. Other translations are welcome. | ||
|
||
Anyway, because English is the language of programming, the code, including variable names and comments, are in English. | ||
|
||
:fr: Une version française de ce document se trouve [ici](doc/fr/README.md). | ||
|
||
# Installation | ||
|
||
Installation is done using `npm install` command: | ||
|
||
```bash | ||
$ npm install --save simple-ts-transform | ||
``` | ||
|
||
If you prefer using `yarn`: | ||
|
||
```bash | ||
$ yarn add simple-ts-transform | ||
``` | ||
|
||
# Usage | ||
|
||
## Context | ||
|
||
First, create the context class. You can put in it whatever you need for your visitors. The context is created when the compilation starts and is updated for each file. Your node visitors can access the context, and of course, can also modify it if needed. | ||
|
||
The context class constructor will be called with: | ||
|
||
- a `ts.Program` parameter, | ||
- an `any` 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' | ||
|
||
export default class MyContext implements NodeVisitorContext { | ||
public readonly basePath: string | ||
public fileName?: string | ||
public constructor(program: Program, public readonly configuration: any) { | ||
this.basePath = program.getCompilerOptions().rootDir || program.getCurrentDirectory() | ||
} | ||
public initNewFile(_context: TransformationContext, sourceFile: SourceFile): void { | ||
this.fileName = sourceFile.fileName | ||
} | ||
} | ||
``` | ||
|
||
## Node visitors | ||
|
||
Then, you can create the node visitors. The node visitors are created for each file after the context is initialized and before starting the visit. | ||
|
||
Because each node visitor is only managing one single type of node, this node type must be given as generic parameter `N` to the implemented interface. | ||
|
||
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 `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' | ||
|
||
export default class MyFileNameInserter implements NodeVisitor<StringLiteral> { | ||
private readonly fileName: string | ||
public constructor(private readonly context: MyContext) { | ||
this.fileName = context.fileName! | ||
} | ||
public wants(node: Node): node is StringLiteral { | ||
return isStringLiteral(node) | ||
} | ||
public visit(node: StringLiteral) { | ||
return [createStringLiteral(this.fileName + ': ' + node.getText().slice(1, -1)] | ||
} | ||
} | ||
``` | ||
## Transformer | ||
You finally can create the transformer. For this, simply call `buildTransformer` and provide the context class and the node visitors in the order they have to be executed. | ||
```typescript | ||
// index.ts | ||
import buildTransformer from 'simple-ts-transform' | ||
import MyContext from './MyContext' | ||
import MyFileNameInserter from './MyFileNameInserter' | ||
import OtherVisitor from './OtherVisitor' | ||
|
||
const transformer = buildTransformer(MyContext, [MyFileNameInserter, OtherVisitor]) | ||
export default transformer | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const result = 'Hello' | ||
export default result |
Oops, something went wrong.