Skip to content
This repository was archived by the owner on Jan 2, 2021. It is now read-only.

Commit 52246d7

Browse files
committed
feat: inital release
0 parents  commit 52246d7

File tree

13 files changed

+286
-0
lines changed

13 files changed

+286
-0
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 2

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/node_modules
2+
*.js.map
3+
coverage/
4+
.nyc_output/
5+
npm-debug.log
6+
dist/

.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- stable
5+
- '6'
6+
- '4'
7+
matrix:
8+
fast_finish: true
9+
branches:
10+
only:
11+
- master
12+
- /^greenkeeper-.*$/
13+
notifications:
14+
email:
15+
on_success: never
16+
cache:
17+
directories:
18+
- node_modules
19+
20+
before_script: 'npm run linter'
21+
script: 'npm run test'
22+
after_script: 'npm run coverage'

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
"typescript.tsdk": "node_modules/typescript/lib"
4+
}

.vscode/tasks.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "0.1.0",
5+
"command": "npm",
6+
"isShellCommand": true,
7+
"showOutput": "always",
8+
"suppressTaskName": true,
9+
"tasks": [
10+
{
11+
"taskName": "build",
12+
"args": ["run", "build", "--", "--watch"],
13+
"isBackground": true,
14+
"isBuildCommand": true
15+
}
16+
]
17+
}

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Markus Wolf
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './dist/src/index';

package.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"name": "typescript-patternplate-resolver",
3+
"version": "0.1.0",
4+
"description": "TypeScript server plugin to inject pattern resolution algorithm in the language service",
5+
"main": "dist/src/index.js",
6+
"files": [
7+
"dist",
8+
"index.d.ts"
9+
],
10+
"scripts": {
11+
"linter": "tslint --project ./tsconfig.json",
12+
"start": "npm test",
13+
"clean": "rimraf dist",
14+
"prebuild": "npm run clean",
15+
"build": "tsc --inlineSourceMap",
16+
"pretest": "npm run build",
17+
"test": "nyc ava",
18+
"coverage": "nyc report --reporter=lcov && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
19+
"prerelease": "npm test",
20+
"release": "standard-version",
21+
"postrelease": "git push --follow-tags origin master && npm publish"
22+
},
23+
"author": {
24+
"name": "Markus Wolf",
25+
"email": "knister.peter@shadowrun-clan.de"
26+
},
27+
"repository": {
28+
"type": "git",
29+
"url": "KnisterPeter/typescript-patternplate-resolver"
30+
},
31+
"license": "MIT",
32+
"devDependencies": {
33+
"@knisterpeter/standard-tslint": "^1.4.0",
34+
"@types/node": "^7.0.15",
35+
"ava": "^0.19.1",
36+
"conventional-changelog-cli": "^1.3.1",
37+
"coveralls": "^2.13.1",
38+
"cz-conventional-changelog": "^2.0.0",
39+
"nyc": "^10.3.0",
40+
"rimraf": "^2.6.1",
41+
"source-map-support": "^0.4.15",
42+
"standard-version": "^4.0.0",
43+
"tslint": "^5.1.0"
44+
},
45+
"dependencies": {
46+
"find-root": "^1.0.0",
47+
"typescript": "^2.3.2"
48+
},
49+
"config": {
50+
"commitizen": {
51+
"path": "./node_modules/cz-conventional-changelog"
52+
}
53+
},
54+
"ava": {
55+
"files": [
56+
"dist/tests/**/*-test.js"
57+
],
58+
"source": [
59+
"dist/src/**/*.js"
60+
],
61+
"require": [
62+
"source-map-support/register"
63+
]
64+
},
65+
"nyc": {
66+
"all": true,
67+
"cache": true,
68+
"exclude": [
69+
"wallaby.conf.js",
70+
"node_modules",
71+
"coverage",
72+
"dist/tests",
73+
"tests"
74+
]
75+
}
76+
}

src/index.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import findRoot = require('find-root');
2+
import * as path from 'path';
3+
import * as ts_module from 'typescript/lib/tsserverlibrary';
4+
5+
function init(modules: { typescript: typeof ts_module }):
6+
{ create(info: ts.server.PluginCreateInfo): ts.LanguageService } {
7+
const ts = modules.typescript;
8+
9+
function resolveToPattern(info: ts.server.PluginCreateInfo, moduleName: string, containingFile: string,
10+
parts: string[], patternJson: string): ts_module.ResolvedModule {
11+
if (moduleName === 'Pattern' && containingFile.endsWith('demo.tsx')) {
12+
const resolvedFileName = path.sep + path.join(...parts, 'index.tsx');
13+
info.project.projectService.logger.info(`Resolved Pattern to ${resolvedFileName}`);
14+
return { resolvedFileName };
15+
} else {
16+
const pkg = JSON.parse(ts.sys.readFile(patternJson));
17+
// info.project.projectService.logger.info(`Dependency to ${moduleName}`);
18+
if (pkg.patterns && moduleName in pkg.patterns) {
19+
const root = findRoot(patternJson);
20+
// info.project.projectService.logger.info(`Root path ${root}`);
21+
const resolvedFileName = path.join(root, 'patterns', pkg.patterns[moduleName], 'index.tsx');
22+
return { resolvedFileName };
23+
}
24+
}
25+
return undefined as any;
26+
}
27+
28+
function create(info: ts.server.PluginCreateInfo): ts.LanguageService {
29+
const resolveModuleNames = info.languageServiceHost.resolveModuleNames;
30+
info.languageServiceHost.resolveModuleNames = function(moduleNames: string[], containingFile: string):
31+
ts_module.ResolvedModule[] {
32+
33+
const resolvedNames = moduleNames.map(moduleName => {
34+
// info.project.projectService.logger.info(`Resolve ${moduleName} in ${containingFile}`);
35+
const parts = containingFile.split(path.sep);
36+
parts.pop();
37+
const patternJson = path.sep + path.join(...parts, 'pattern.json');
38+
// info.project.projectService.logger.info(`Pattern? to resolve ${patternJson}`);
39+
if (ts.sys.fileExists(patternJson)) {
40+
const resolvedModule = resolveToPattern(info, moduleName, containingFile, parts, patternJson);
41+
if (resolvedModule) {
42+
return resolvedModule;
43+
}
44+
}
45+
if (resolveModuleNames) {
46+
// info.project.projectService.logger.info(`Resolve ${moduleName} on orig LSHost`);
47+
const result = resolveModuleNames.call(info.languageServiceHost, [moduleName],
48+
containingFile) as ts_module.ResolvedModule[];
49+
if (result && result.length > 0) {
50+
return result[0];
51+
}
52+
}
53+
return undefined;
54+
});
55+
if (resolvedNames && resolvedNames.length > 0) {
56+
return resolvedNames as any;
57+
}
58+
59+
if (resolveModuleNames) {
60+
info.project.projectService.logger.info(`Resolve all on orig LSHost`);
61+
return resolveModuleNames.call(info.languageServiceHost, moduleNames, containingFile);
62+
}
63+
64+
// fallback to standard resolving
65+
return moduleNames.map(moduleName => {
66+
const result = ts.resolveModuleName(moduleName, containingFile,
67+
info.project.getCompilerOptions(),
68+
{ fileExists: ts.sys.fileExists, readFile: ts.sys.readFile });
69+
if (result.resolvedModule) {
70+
return result.resolvedModule;
71+
}
72+
73+
// note: as any is a quirk here, since the CompilerHost interface does not allow strict null checks
74+
return undefined as any;
75+
});
76+
};
77+
return info.languageService;
78+
}
79+
80+
return { create };
81+
}
82+
83+
export = init;

tsconfig.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"lib": [
5+
"es2017"
6+
],
7+
"module": "commonjs",
8+
"noFallthroughCasesInSwitch": true,
9+
"noImplicitAny": true,
10+
"noImplicitReturns": true,
11+
"noImplicitThis": true,
12+
"noUnusedLocals": true,
13+
"noUnusedParameters": true,
14+
"outDir": "dist",
15+
"pretty": true,
16+
"rootDir": ".",
17+
"strict": true,
18+
"strictNullChecks": true,
19+
"stripInternal": true,
20+
"suppressImplicitAnyIndexErrors": true,
21+
"target": "es5"
22+
},
23+
"exclude": [
24+
"index.d.ts",
25+
"dist",
26+
"node_modules"
27+
]
28+
}

0 commit comments

Comments
 (0)