Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: tsfmt ignores extends in tsconfig #77

Open
Hotell opened this issue Feb 26, 2017 · 11 comments · Fixed by #80
Open

bug: tsfmt ignores extends in tsconfig #77

Hotell opened this issue Feb 26, 2017 · 11 comments · Fixed by #80
Assignees

Comments

@Hotell
Copy link

Hotell commented Feb 26, 2017

when running tsfmt --verify within project it ignores extends within tsconfig.json, so user has to specify all file paths manually within terminal which is very inconvenient

@vvakame vvakame self-assigned this Feb 27, 2017
vvakame added a commit that referenced this issue Feb 27, 2017
feat(tsfmt): add `extends` of tsconfig.json support closes #77
@Hotell
Copy link
Author

Hotell commented Feb 27, 2017

That was quick! Arigatou gozaimasu ! 🍻

@Hotell
Copy link
Author

Hotell commented Feb 27, 2017

can you release pls?

@vvakame
Copy link
Owner

vvakame commented Feb 27, 2017

wait a moment. I'll ship it in 3 hours.

@vvakame
Copy link
Owner

vvakame commented Feb 27, 2017

just released v4.2.0 and v5.0.0 😉

@Hotell
Copy link
Author

Hotell commented Feb 27, 2017

unfortunately it doesn't work. pls reopen

tsconfig.json

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "declaration": true,
    "declarationDir": "typings"
  }
}

Error:

tsfmt --verify
Error: [object Object]
    at makeFormatCodeOptions (/Users/hotell/Devel/github/Hotell/typescript-lib-starter/node_modules/typescript-formatter/lib/provider/tsconfigjson.ts:38:15)
    at next (/Users/hotell/Devel/github/Hotell/typescript-lib-starter/node_modules/typescript-formatter/lib/index.ts:52:23)
    at /Users/hotell/Devel/github/Hotell/typescript-lib-starter/node_modules/typescript-formatter/lib/index.ts:53:64
    at process._tickCallback (internal/process/next_tick.js:103:7)
    at Module.runMain (module.js:606:11)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

@vvakame vvakame reopened this Feb 28, 2017
@vvakame
Copy link
Owner

vvakame commented Feb 28, 2017

I'm working on https://github.com/vvakame/typescript-formatter/tree/fix-extends-tsconfig branch.
Since there is no working time, it may be a little late to fix it.

@Hotell
Copy link
Author

Hotell commented Feb 28, 2017

sure, no worries, thx!

@vvakame vvakame reopened this Feb 28, 2017
@vvakame
Copy link
Owner

vvakame commented Feb 28, 2017

just released v5.0.1.
please check it! this issue is solved my local env.

@Hotell
Copy link
Author

Hotell commented Feb 28, 2017

still doesn't work. Although I managed to make it work with slightly different config.

This doesn't work:

tsconfig.base.json

{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "commonjs",
    "target": "es5",
    "lib": [
      "es2015",
      "dom"
    ],
    "allowSyntheticDefaultImports": true,
    "suppressImplicitAnyIndexErrors": true,
    "forceConsistentCasingInFileNames": true,
    "pretty": true,
    "jsx": "react",
    "sourceMap": true,
    "outDir": "lib"
  },
  "files": [
    "./src/index.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "buildOnSave": false
}

tsconfig.json

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "declaration": true,
    "declarationDir": "typings"
  }
}

This does:

tsconfig.base.json

{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "commonjs",
    "target": "es5",
    "lib": [
      "es2015",
      "dom"
    ],
    "allowSyntheticDefaultImports": true,
    "suppressImplicitAnyIndexErrors": true,
    "forceConsistentCasingInFileNames": true,
    "pretty": true,
    "jsx": "react",
    "sourceMap": true,
    "outDir": "lib"
  },
  "include": [
    "./src"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "buildOnSave": false
}

tsconfig.json

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "declaration": true,
    "declarationDir": "typings"
  }
}

Solution

I had to use include instead of files in tsconfig.base.json

-> TSFMT v 5.0.1

@vvakame
Copy link
Owner

vvakame commented Mar 2, 2017

umm.. I tried with your settings.
But it is works.
Now, tsfmt is using to parse tsconfig.json by typescript package.
What version of tsc do you use?

export function readFilesFromTsconfig(configPath: string): string[] {
return readTsconfig(configPath).fileNames;
}
export function readTsconfig(configPath: string): ts.ParsedCommandLine {
// for `extends` support. It supported from TypeScript 2.1.1.
// `& { readFile(path: string): string; }` is backword compat for TypeScript compiler 2.0.3 support.
const host: ts.ParseConfigHost & { readFile(path: string): string; } = {
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
readDirectory: ts.sys.readDirectory,
fileExists: path => fs.existsSync(path),
readFile: (path: string) => fs.readFileSync(path, "utf-8"),
};
let rootConfig = parseJSON(fs.readFileSync(configPath, "utf-8"));
let parsed = ts.parseJsonConfigFileContent(rootConfig, host, path.dirname(configPath));
if (parsed.errors && parsed.errors.length !== 0) {
throw new Error(parsed.errors.map(e => e.messageText).join("\n"));
}
return parsed;
}

$  tsfmt -v
5.0.1
$ tsc -v
Version 2.2.1
$ tsfmt --verify --verbose
read: /private/tmp/tsfmt-issue77/tsconfig.json
replace:	  OFF
verify:	   ON
baseDir:	   /private/tmp/tsfmt-issue77
stdin:		OFF
files from tsconfig:	 ON
tsconfig:	 ON
tslint:	   ON
editorconfig: ON
vscode:    ON
tsfmt:		ON
read /private/tmp/tsfmt-issue77/tsconfig.json for /private/tmp/tsfmt-issue77/./src/index.ts
/private/tmp/tsfmt-issue77/./src/index.ts is not formatted

$ cat src/index.ts                                                                                                                                    1 ↵
class Sample{}

$ cat tsconfig.base.json
{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "commonjs",
    "target": "es5",
    "lib": [
      "es2015",
      "dom"
    ],
    "allowSyntheticDefaultImports": true,
    "suppressImplicitAnyIndexErrors": true,
    "forceConsistentCasingInFileNames": true,
    "pretty": true,
    "jsx": "react",
    "sourceMap": true,
    "outDir": "lib"
  },
  "files": [
    "./src/index.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "buildOnSave": false
}

$ cat tsconfig.json
{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "declaration": true,
    "declarationDir": "typings"
  }
}

@sethfowler
Copy link

@vvakame I think I ran into the same issue that @Hotell reported. It's true that if a file is listed directly in files, tsfmt will check it. However, tsfmt doesn't check the modules that those files import. So if I have this in my tsconfig.json:

{
  "files": [
    "./ts/main.ts"
  ]
}

And main.ts contains this:

import { foo } from './module.ts';
foo();

tsfmt won't format module.ts. Or at least, I can't get it to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants