Skip to content

Commit

Permalink
Merge branch 'master' into add/skip-interface-or-type
Browse files Browse the repository at this point in the history
  • Loading branch information
pzavolinsky committed Jan 20, 2020
2 parents ea08755 + ce0eb53 commit ad20d6b
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 5 deletions.
11 changes: 11 additions & 0 deletions example/absolute-paths-2-tsconfigs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions example/absolute-paths-2-tsconfigs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"description": "Example used by integration tests (ispec)",
"repository": {
"type": "git",
"url": "https://github.com/pzavolinsky/ts-unused-exports.git"
},
"license": "MIT",
"scripts": {
"build": "tsc -p ./tsconfig.json",
"test": "npm i && npm run build && npm run test:test",
"test:test": "../../bin/ts-unused-exports ./tsconfig.json"
},
"dependencies": {
"typescript": "^3.6.4"
},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MyComponent1 } from 'components/MyComponent';

const comp1: MyComponent1 = new MyComponent1();

export class UnusedComponent {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"include": ["./src"],
"compilerOptions": {
"baseUrl": "src"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export class MyComponent1 {}
export class UnusedComponent {}
21 changes: 21 additions & 0 deletions example/absolute-paths-2-tsconfigs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"baseDir": ".",
"compilerOptions": {
"allowJs": true,
"moduleResolution": "node",
"baseUrl": "src/config",
"lib": ["es6", "es2017", "dom"],
"strict": true,
"noEmit": true,
"resolveJsonModule": true,
"downlevelIteration": true,
"module": "commonjs",
"types": ["node"],
"jsx": "react",
"target": "es5",
"sourceMap": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
}
}
13 changes: 13 additions & 0 deletions example/absolute-paths-simple/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions example/absolute-paths-simple/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "absolute-paths-simple",
"version": "1.0.0",
"description": "Test harness",
"main": "main.js",
"scripts": {
"build": "tsc",
"test": "npm i && npm run build && ../../bin/ts-unused-exports ./tsconfig.json --ignorePaths=to-ignore"
},
"dependencies": {
"typescript": "^3.7.4"
},
"author": "",
"license": "MIT"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class MyComponent1 {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class UnusedComponent {}
3 changes: 3 additions & 0 deletions example/absolute-paths-simple/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { MyComponent1 } from 'components/MyComponent';

export class UnusedClassFromMain {}
7 changes: 7 additions & 0 deletions example/absolute-paths-simple/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"baseDir": ".",
"compilerOptions": {
"baseUrl": "src"
},
"include": ["./src"]
}
12 changes: 12 additions & 0 deletions features/export-array.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ Scenario: export simple array
"""
When analyzing "tsconfig.json"
Then the result is { "a.ts": ["unused"] }

Scenario: export simple array, with alias
Given file "a.ts" is
"""
export const [x, y, z, unused] = [1, 2, 3, 'unused'];
"""
And file "b.ts" is
"""
import { x as x1, y, z } from './a';
"""
When analyzing "tsconfig.json"
Then the result is { "a.ts": ["unused"] }
57 changes: 57 additions & 0 deletions features/import-absolute-paths.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Feature: absolute-paths without paths set in tsconfig

Background:
Given file "tsconfig.json" is
"""
{
"baseDir": ".",
"compilerOptions": {
"baseUrl": "src"
},
"include": [
"./src"
]
}
"""

Scenario: Import component using absolute path
Given file "./src/components/MyComponent.ts" is
"""
export class MyComponent1 {};
export class UnusedComponent {};
"""
And file "src/b.ts" is
"""
import {MyComponent1} from "components/MyComponent";
"""
When analyzing "tsconfig.json"
Then the result is { "src/components/MyComponent.ts": ["UnusedComponent"] }

Scenario: Import component using absolute path, from sub-folder
Given file "./src/components/MyComponent.ts" is
"""
export class MyComponent1 {};
export class UnusedComponent {};
"""
And file "src/subFolder/b.ts" is
"""
import {MyComponent1} from "components/MyComponent";
"""
When analyzing "tsconfig.json"
Then the result is { "src/components/MyComponent.ts": ["UnusedComponent"] }

Scenario: Import function using absolute path
Given file "src/helpers/form/myFunctions.ts" is
"""
export function doSomething({isTheFieldRequired} = defaultRequiredParam) {}
export function unusedDoSomething() {}
"""
And file "src/b.ts" is
"""
import { doSomething } from "helpers/form/myFunctions";
"""
When analyzing "tsconfig.json"
Then the result is { "src/helpers/form/myFunctions.ts": ["unusedDoSomething"] }

# TODO xxx test namespace using absolute path - search ON
# When analyzing "tsconfig.json" with files ["--searchNamespaces"]
31 changes: 31 additions & 0 deletions features/import-alias.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Feature: import alias

Scenario: One unused export const
Given file "a.ts" is
"""
export const a = 1;
export const unused_a = 1;
"""
And file "b.ts" is import { a as a1 } from './a';
When analyzing "tsconfig.json"
Then the result at a.ts is ["unused_a"]

Scenario: Used export default - import * as x
Given file "a.ts" is export default 1;
And file "b.ts" is import * as ax from './a';
When analyzing "tsconfig.json"
Then the result at a.ts is ["default"]
# see star.feature - Import * marks 'default' as unused

Scenario: Used export default - import x
Given file "a.ts" is export default 1;
And file "b.ts" is import x from './a';
When analyzing "tsconfig.json"
Then the result is {}

Scenario: Export { symbol as x } from
Given file "a.ts" is export const a = 1;
And file "b.ts" is export { a as a1 } from './a';
And file "c.ts" is import { a1 as a2 } from './b';
When analyzing "tsconfig.json"
Then the result is {}
3 changes: 2 additions & 1 deletion features/star.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Feature: import * / export *

Scenario: Import * marks `default` as unused
Scenario: Import * marks 'default' as unused
Given file "a.ts" is
"""
export const a = 1;
Expand All @@ -9,6 +9,7 @@ Scenario: Import * marks `default` as unused
And file "b.ts" is import * as all from './a';
When analyzing "tsconfig.json"
Then the result at a.ts is ["default"]
# note: default could still be used, as all.default

Scenario: Import * and default
Given file "a.ts" is
Expand Down
24 changes: 21 additions & 3 deletions ispec/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,32 @@ echo "== Integration Tests =="
# Exit on error:
set -e;

function run_itest()
{
../../ispec/_run-and-check-exit-code.sh
}

function install_and_run_itest()
{
npm i > /dev/null && run_itest
}

pushd ../example/simple
../../ispec/_run-and-check-exit-code.sh
run_itest
popd

pushd ../example/tsx
npm i > /dev/null && ../../ispec/_run-and-check-exit-code.sh
install_and_run_itest
popd

pushd ../example/with-paths
npm i > /dev/null && ../../ispec/_run-and-check-exit-code.sh
install_and_run_itest
popd

pushd ../example/absolute-paths-simple
install_and_run_itest
popd

pushd ../example/absolute-paths-2-tsconfigs
install_and_run_itest
popd
2 changes: 1 addition & 1 deletion src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const processImports = (imports: Imports, exportMap: ExportMap): void => {
};

imports[key].forEach(imp =>
imp == '*'
imp === '*'
? Object.keys(ex)
.filter(e => e != 'default')
.forEach(addUsage)
Expand Down

0 comments on commit ad20d6b

Please sign in to comment.