Skip to content

Commit 69018f0

Browse files
authored
Merge pull request #9 from vitonsky/2-add-tests
test(#2): add tests
2 parents addf9fa + 5cebbc8 commit 69018f0

File tree

4 files changed

+104
-31
lines changed

4 files changed

+104
-31
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ name: "CodeQL"
88

99
on:
1010
push:
11-
branches: [ master ]
11+
branches: [master]
1212
pull_request:
1313
# The branches below must be a subset of the branches above
14-
branches: [ master ]
14+
branches: [master]
1515
schedule:
1616
# At 04:35 AM, every 3 days
1717
# test expression on https://crontab.cronhub.io/
18-
- cron: '35 4 */3 * *'
18+
- cron: "35 4 */3 * *"
1919

2020
workflow_dispatch:
2121

@@ -28,7 +28,7 @@ jobs:
2828
- uses: actions/checkout@v2
2929
- uses: actions/setup-node@v2
3030
with:
31-
node-version: '14'
31+
node-version: "20"
3232

3333
- name: Install deps
3434
run: npm install
@@ -47,40 +47,40 @@ jobs:
4747
strategy:
4848
fail-fast: false
4949
matrix:
50-
language: [ 'javascript', 'TypeScript' ]
50+
language: ["javascript", "TypeScript"]
5151
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
5252
# Learn more:
5353
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
5454

5555
steps:
56-
- name: Checkout repository
57-
uses: actions/checkout@v2
56+
- name: Checkout repository
57+
uses: actions/checkout@v2
5858

59-
# Initializes the CodeQL tools for scanning.
60-
- name: Initialize CodeQL
61-
uses: github/codeql-action/init@v1
62-
with:
63-
languages: ${{ matrix.language }}
64-
# If you wish to specify custom queries, you can do so here or in a config file.
65-
# By default, queries listed here will override any specified in a config file.
66-
# Prefix the list here with "+" to use these queries and those in the config file.
67-
# queries: ./path/to/local/query, your-org/your-repo/queries@main
59+
# Initializes the CodeQL tools for scanning.
60+
- name: Initialize CodeQL
61+
uses: github/codeql-action/init@v1
62+
with:
63+
languages: ${{ matrix.language }}
64+
# If you wish to specify custom queries, you can do so here or in a config file.
65+
# By default, queries listed here will override any specified in a config file.
66+
# Prefix the list here with "+" to use these queries and those in the config file.
67+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
6868

69-
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
70-
# If this step fails, then you should remove it and run the build manually (see below)
71-
- name: Autobuild
72-
uses: github/codeql-action/autobuild@v1
69+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
70+
# If this step fails, then you should remove it and run the build manually (see below)
71+
- name: Autobuild
72+
uses: github/codeql-action/autobuild@v1
7373

74-
# ℹ️ Command-line programs to run using the OS shell.
75-
# 📚 https://git.io/JvXDl
74+
# ℹ️ Command-line programs to run using the OS shell.
75+
# 📚 https://git.io/JvXDl
7676

77-
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
78-
# and modify them (or add more) to build your code if your project
79-
# uses a compiled language
77+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
78+
# and modify them (or add more) to build your code if your project
79+
# uses a compiled language
8080

81-
#- run: |
82-
# make bootstrap
83-
# make release
81+
#- run: |
82+
# make bootstrap
83+
# make release
8484

85-
- name: Perform CodeQL Analysis
86-
uses: github/codeql-action/analyze@v1
85+
- name: Perform CodeQL Analysis
86+
uses: github/codeql-action/analyze@v1

src/import-paths.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { RuleTester } from 'eslint';
2+
import path from 'path';
3+
4+
import rule from './import-paths';
5+
6+
const tester = new RuleTester({
7+
parserOptions: {
8+
// eslint-disable-next-line spellcheck/spell-checker
9+
ecmaVersion: 6,
10+
sourceType: 'module',
11+
},
12+
});
13+
14+
tester.run('import-paths', rule, {
15+
valid: [
16+
{
17+
name: 'relative import from not an alias directory are allowed',
18+
filename: path.resolve('./src/index.ts'),
19+
code: `import baz from './baz/index';`,
20+
},
21+
{
22+
name: 'import from an alias root are possible',
23+
filename: path.resolve('./src/index.ts'),
24+
code: `import baz from '@foo';`,
25+
},
26+
{
27+
name: 'files in alias directory may import files from the same alias',
28+
filename: path.resolve('./src/foo/index.ts'),
29+
code: `import z from '@foo/x/y/z';`,
30+
},
31+
{
32+
name: 'files in alias directory may import files from another aliases',
33+
filename: path.resolve('./src/foo/index.ts'),
34+
code: `import z from '@bar/x/y/z';`,
35+
},
36+
{
37+
name: 'relative imports from subdirectory is allowed for files inside alias',
38+
filename: path.resolve('./src/foo/index.ts'),
39+
code: `import z from './x/y/z';`,
40+
},
41+
{
42+
name: 'relative imports from subdirectory is allowed for files inside alias (case with escalate directory)',
43+
filename: path.resolve('./src/foo/x/y/z/index.ts'),
44+
code: `import foo from '../../../index';`,
45+
},
46+
{
47+
name: 'relative imports from subdirectory is allowed for files inside alias (case with out of alias directory)',
48+
filename: path.resolve('./src/foo/x/y/z/index.ts'),
49+
code: `import foo from '../../../../index';`,
50+
},
51+
],
52+
invalid: [
53+
{
54+
name: 'relative import from alias must be fixed',
55+
filename: path.resolve('./src/index.ts'),
56+
code: `import z from './foo/x/y/z';`,
57+
errors: ['Update import to @foo/x/y/z'],
58+
output: `import z from '@foo/x/y/z';`,
59+
},
60+
{
61+
name: 'relative import from alias used in another alias must be fixed',
62+
filename: path.resolve('./src/foo/index.ts'),
63+
code: `import z from '../bar/x/y/z';`,
64+
errors: ['Update import to @bar/x/y/z'],
65+
output: `import z from '@bar/x/y/z';`,
66+
},
67+
],
68+
});
File renamed without changes.

tsconfig.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
"rootDir": ".",
2929
"allowJs": true,
3030
"resolveJsonModule": true,
31-
"skipLibCheck": true
31+
"skipLibCheck": true,
32+
"baseUrl": ".",
33+
"paths": {
34+
"@foo/*": ["src/foo/*"],
35+
"@bar/*": ["src/bar/*"]
36+
}
3237
},
3338
"include": ["src/*.ts", "src/*.tsx", "src/**/*.ts", "src/**/*.tsx"]
3439
}

0 commit comments

Comments
 (0)