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

Add first class Javascript/Typescript support to the Mill build tool #4398

Merged
merged 25 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
*** xref:javascriptlib/module-config.adoc[]
*** xref:javascriptlib/testing.adoc[]
*** xref:javascriptlib/publishing.adoc[]
*** xref:javascriptlib/linting.adoc[]
* xref:comparisons/why-mill.adoc[]
** xref:comparisons/maven.adoc[]
** xref:comparisons/gradle.adoc[]
Expand Down
21 changes: 21 additions & 0 deletions docs/modules/ROOT/pages/javascriptlib/linting.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
= Linting Typescript Projects
:page-aliases: Linting_Typescript_Projects.adoc

include::partial$gtag-config.adoc[]

This page will discuss common topics around maintaining the code quality of Typescript
codebases using the Mill build tool

== Linting and AutoFormatting with Eslint and Prettier

Eslint and Prettier are tools that analyzes your Typescript source code, performing intelligent analyses and code quality checks, and is often able to automatically fix the issues that it discovers. It can also perform automated refactoring.

include::partial$example/javascriptlib/linting/1-linting.adoc[]

include::partial$example/javascriptlib/linting/2-autoformatting.adoc[]

== Code Coverage with Jest, Mocha, Vitest and Jasmine

Mill supports code coverage analysis with multiple Typescript testing frameworks.

include::partial$example/javascriptlib/linting/3-code-coverage.adoc[]
31 changes: 0 additions & 31 deletions example/javascriptlib/basic/1-simple/jest.config.ts

This file was deleted.

31 changes: 0 additions & 31 deletions example/javascriptlib/basic/3-custom-build-logic/jest.config.ts

This file was deleted.

31 changes: 0 additions & 31 deletions example/javascriptlib/basic/4-multi-modules/jest.config.ts

This file was deleted.

31 changes: 0 additions & 31 deletions example/javascriptlib/basic/5-client-server-hello/jest.config.ts

This file was deleted.

This file was deleted.

4 changes: 4 additions & 0 deletions example/javascriptlib/linting/1-linting/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
build
.git
7 changes: 7 additions & 0 deletions example/javascriptlib/linting/1-linting/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2
}
56 changes: 56 additions & 0 deletions example/javascriptlib/linting/1-linting/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package build

import mill._, javascriptlib._

object foo extends TypeScriptModule

// Mill supports code linting via `eslint` https://eslint.org, `typescript-eslint` https://typescript-eslint.io/getting-started
// and `prettier` https://prettier.io/docs/en out of the box.
// You can lint your projects code by providing a configuration for your preferred linter and running `mill _.checkFormatAll`.

// If both configuration files are present, the command `mill _.checkFormatAll` will default to eslint.
// If neither files are present, the command will exit with an error, you must include at least one configuration file.
// You can lint via a specificied linter via the commands `mill _.checkFormatEslint` to lint with eslint and
// `mill _.checkFormatPrettier` to lint with prettier.

// When using prettier you can specify the path to lint via command line argument, `mill _.checkFormatAll "*/**/*.ts"`
// just as you would when running `prettier --check` if no path is provided mill will default to using "*/**/*.ts".
// Also if a `.prettierignore` is not provided, mill will generate one ignoring "node_modules" and ".git".

// You can define `npmLintDeps` field to add dependencies specific to linting to your module.
// The task `npmLintDeps` works the same way as `npmDeps` or `npmDevDeps`.

/** Usage
> cat foo/src/foo.ts # initial poorly formatted source code
export class Foo{
static main(
args: string[
])
{console.log("Hello World!")
}
}

> mill foo.checkFormatAll # run linter - since both eslint and prettier configurations are present, mill will opt to use eslint.
monyedavid marked this conversation as resolved.
Show resolved Hide resolved
...
foo/src/foo.ts
2:1 error Expected indentation of 2 spaces but found 0 indent
3:1 error Expected indentation of 4 spaces but found 0 indent
5:1 error Opening curly brace does not appear on the same line as controlling statement brace-style
5:1 error Statement inside of curly braces should be on next line brace-style
5:1 error Requires a space after '{' block-spacing
5:1 error Expected indentation of 2 spaces but found 0 indent
5:14 error Strings must use singlequote quotes
5:29 error Missing semicolon semi
6:1 error Expected indentation of 2 spaces but found 0 indent
7:2 error Newline required at end of file but not found eol-last
...
...10 problems (10 errors, 0 warnings)
...10 errors and 0 warnings potentially fixable with running foo.reformatAll.

> rm -rf eslint.config.mjs # since there is no eslint config file `eslint.config.(js|mjs|cjs)`, mill will use prettier if a `.prettierrc` conf file is available.

> mill foo.checkFormatAll # run lint with prettier configuration.
Checking formatting...
[warn] foo/src/foo.ts
[warn] Code style issues found. Run foo.reformatAll to fix.
*/
monyedavid marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 34 additions & 0 deletions example/javascriptlib/linting/1-linting/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// @ts-check

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config({
files: ['*/**/*.ts'],
extends: [
eslint.configs.recommended,
...tseslint.configs.recommended,
],
rules: {
'@typescript-eslint/no-unused-vars': 'off',
// styling rules
'semi': ['error', 'always'],
'quotes': ['error', 'single'],
'comma-dangle': ['error', 'always-multiline'],
'max-len': ['error', {code: 80, ignoreUrls: true}],
'indent': ['error', 2, {SwitchCase: 1}],
'brace-style': ['error', '1tbs'],
'space-before-function-paren': ['error', 'never'],
'no-multi-spaces': 'error',
'array-bracket-spacing': ['error', 'never'],
'object-curly-spacing': ['error', 'always'],
'arrow-spacing': ['error', {before: true, after: true}],
'key-spacing': ['error', {beforeColon: false, afterColon: true}],
'keyword-spacing': ['error', {before: true, after: true}],
'space-infix-ops': 'error',
'block-spacing': ['error', 'always'],
'eol-last': ['error', 'always'],
'newline-per-chained-call': ['error', {ignoreChainWithDepth: 2}],
'padded-blocks': ['error', 'never'],
},
});
7 changes: 7 additions & 0 deletions example/javascriptlib/linting/1-linting/foo/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class Foo{
static main(
args: string[
])
{console.log("Hello World!")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
build
.git
7 changes: 7 additions & 0 deletions example/javascriptlib/linting/2-autoformatting/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2
}
52 changes: 52 additions & 0 deletions example/javascriptlib/linting/2-autoformatting/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package build

import mill._, javascriptlib._

object foo extends TypeScriptModule

// Mill supports code formatting via `eslint` and `prettier`.
// You can reformat your projects code by providing a configuration for your preferred linter and running `mill _.reformatAll`.

// If both configurations files are present, the command `mill _.reformatAll` will default to eslint.
// You can format via a specificied linter via the commands `mill _.reformatEslint` to format with eslint and
// `mill _.reformatPrettier` to format with prettier.

// When using prettier you can specify the path to reformat via command line argument, `mill _.reformatAll "*/**/*.ts"`
// just as you would when running `prettier --write` if no path is provided mill will default to using "*/**/*.ts".

/** Usage
> cat foo/src/foo.ts # initial poorly formatted source code
export class Foo{
static main(
args: string[
])
{console.log("Hello World!")
}
}

> mill foo.reformatAll
...
All matched files have been reformatted!

> cat foo/src/foo.ts # code formatted with eslint configuration.
export class Foo{
static main(
args: string[
]) {
console.log('Hello World!');
}
}

> rm -rf eslint.config.mjs # since there is no eslint config file `eslint.config.(js|mjs|cjs)`, mill will use the prettier configuration available.

> mill foo.reformatAll
...
All matched files have been reformatted!

> cat foo/src/foo.ts # code formatted with prettier configuration.
export class Foo {
static main(args: string[]) {
console.log('Hello World!');
}
}
*/
Loading
Loading