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

Report diagnostics only on certain files [WIP] #701

Merged
merged 9 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ messages are emitted via webpack which is not affected by this flag.
You can squelch certain TypeScript errors by specifying an array of diagnostic
codes to ignore.

#### reportFiles *(string[]) (default=[])*

Only report errors on files matching these glob patterns.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add an example here please? Examples rule the world when it comes to docs in my experience.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added an example


#### compiler *(string) (default='typescript')*

Allows use of TypeScript compilers other than the official one. Should be
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
"chalk": "^2.3.0",
"enhanced-resolve": "^3.0.0",
"loader-utils": "^1.0.2",
"micromatch": "^3.1.4",
"semver": "^5.0.1"
},
"devDependencies": {
"@types/micromatch": "^3.1.0",
"@types/semver": "^5.4.0",
"babel": "^6.0.0",
"babel-core": "^6.0.0",
Expand Down
11 changes: 8 additions & 3 deletions src/after-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ function provideCompilerOptionDiagnosticErrorsToWebpack(
formatErrors(
languageService!.getCompilerOptionsDiagnostics(),
loaderOptions, instance.colors, compiler,
{ file: configFilePath || 'tsconfig.json' }));
{ file: configFilePath || 'tsconfig.json' },
compilation.compiler.context));
}
}

Expand Down Expand Up @@ -152,13 +153,17 @@ function provideErrorsToWebpack(
removeTSLoaderErrors(module.errors);

// append errors
const formattedErrors = formatErrors(errors, loaderOptions, instance.colors, compiler, { module });
const formattedErrors = formatErrors(errors, loaderOptions,
instance.colors, compiler, { module },
compilation.compiler.context);
registerWebpackErrors(module.errors, formattedErrors);
registerWebpackErrors(compilation.errors, formattedErrors);
});
} else {
// otherwise it's a more generic error
registerWebpackErrors(compilation.errors, formatErrors(errors, loaderOptions, instance.colors, compiler, { file: filePath }));
registerWebpackErrors(compilation.errors, formatErrors(errors,
loaderOptions, instance.colors, compiler, { file: filePath },
compilation.compiler.context));
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export function getConfigFile(
);

if (configFile.error !== undefined) {
configFileError = formatErrors([configFile.error], loaderOptions, colors, compiler, { file: configFilePath })[0];
configFileError = formatErrors([configFile.error], loaderOptions,
colors, compiler, { file: configFilePath }, loader.context)[0];
}
} else {
if (compilerCompatible) { log.logInfo(compilerDetailsLogMessage); }
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function getLoaderOptions(loader: Webpack) {
}

type ValidLoaderOptions = keyof LoaderOptions;
const validLoaderOptions: ValidLoaderOptions[] = ['silent', 'logLevel', 'logInfoToStdOut', 'instance', 'compiler', 'contextAsConfigBasePath', 'configFile', 'transpileOnly', 'ignoreDiagnostics', 'errorFormatter', 'colors', 'compilerOptions', 'appendTsSuffixTo', 'appendTsxSuffixTo', 'entryFileCannotBeJs' /* DEPRECATED */, 'onlyCompileBundledFiles', 'happyPackMode', 'getCustomTransformers'];
const validLoaderOptions: ValidLoaderOptions[] = ['silent', 'logLevel', 'logInfoToStdOut', 'instance', 'compiler', 'contextAsConfigBasePath', 'configFile', 'transpileOnly', 'ignoreDiagnostics', 'errorFormatter', 'colors', 'compilerOptions', 'appendTsSuffixTo', 'appendTsxSuffixTo', 'entryFileCannotBeJs' /* DEPRECATED */, 'onlyCompileBundledFiles', 'happyPackMode', 'getCustomTransformers', 'reportFiles'];

/**
* Validate the supplied loader options.
Expand Down Expand Up @@ -148,7 +148,8 @@ function makeLoaderOptions(instanceName: string, configFileOptions: Partial<Load
entryFileCannotBeJs: false,
happyPackMode: false,
colors: true,
onlyCompileBundledFiles: false
onlyCompileBundledFiles: false,
reportFiles: []
} as Partial<LoaderOptions>, configFileOptions, loaderOptions);

options.ignoreDiagnostics = arrify(options.ignoreDiagnostics).map(Number);
Expand Down Expand Up @@ -248,7 +249,9 @@ function getTranspilationEmit(
if (!instance.loaderOptions.happyPackMode) {
registerWebpackErrors(
loader._module.errors,
formatErrors(diagnostics, instance.loaderOptions, instance.colors, instance.compiler, { module: loader._module })
formatErrors(diagnostics, instance.loaderOptions, instance.colors,
instance.compiler, { module: loader._module },
loader.context)
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ function successfulTypeScriptInstance(
const configParseResult = getConfigParseResult(compiler, configFile, basePath);

if (configParseResult.errors.length > 0 && !loaderOptions.happyPackMode) {
const errors = formatErrors(configParseResult.errors, loaderOptions, colors, compiler, { file: configFilePath });
const errors = formatErrors(configParseResult.errors, loaderOptions, colors,
compiler, { file: configFilePath }, loader.context);

registerWebpackErrors(loader._module.errors, errors);

Expand All @@ -100,7 +101,8 @@ function successfulTypeScriptInstance(
if (!loaderOptions.happyPackMode) {
registerWebpackErrors(
loader._module.errors,
formatErrors(diagnostics, loaderOptions, colors, compiler!, {file: configFilePath || 'tsconfig.json'}));
formatErrors(diagnostics, loaderOptions, colors, compiler!,
{file: configFilePath || 'tsconfig.json'}, loader.context));
}

const instance = {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ export interface LoaderOptions {
contextAsConfigBasePath: boolean;
transpileOnly: boolean;
ignoreDiagnostics: number[];
reportFiles: string[];
errorFormatter: (message: ErrorInfo, colors: Chalk) => string;
onlyCompileBundledFiles: boolean;
colors: boolean;
Expand Down
21 changes: 19 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as typescript from 'typescript';
import * as path from 'path';
import * as fs from 'fs';
import { Chalk } from 'chalk';
import * as micromatch from 'micromatch';

import constants = require('./constants');
import {
Expand Down Expand Up @@ -49,12 +50,28 @@ export function formatErrors(
loaderOptions: LoaderOptions,
colors: Chalk,
compiler: typeof typescript,
merge?: { file?: string; module?: WebpackModule }
merge: { file?: string; module?: WebpackModule },
context: string
): WebpackError[] {

return diagnostics
? diagnostics
.filter(diagnostic => loaderOptions.ignoreDiagnostics.indexOf(diagnostic.code) === -1)
.filter(diagnostic => {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we lose this blank line please? Reminds me - must add prettier to ts-loader

Copy link
Contributor Author

@freeman freeman Dec 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done (and +1 to prettier)

if (loaderOptions.ignoreDiagnostics.indexOf(diagnostic.code) !== -1) {
return false;
}

if (loaderOptions.reportFiles.length > 0 && diagnostic.file) {
const relativeFileName = path.relative(context, diagnostic.file.fileName);
const matchResult = micromatch([relativeFileName], loaderOptions.reportFiles);
if (matchResult.length === 0) {
return false;
}
}

return true;
})
.map<WebpackError>(diagnostic => {
const file = diagnostic.file;
const position = file === undefined ? undefined : file.getLineAndCharacterOfPosition(diagnostic.start!);
Expand Down
3 changes: 3 additions & 0 deletions test/comparison-tests/reportFiles/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './skip';
export let a: number
a = '10'
88 changes: 88 additions & 0 deletions test/comparison-tests/reportFiles/expectedOutput-2.6/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
__webpack_require__(1);
exports.a = '10';


/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
exports.a = '10';


/***/ })
/******/ ]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Asset Size Chunks Chunk Names
bundle.js 2.71 kB 0 [emitted] main
[0] ./.test/reportFiles/app.ts 78 bytes {0} [built]
[1] ./.test/reportFiles/skip.ts 59 bytes {0} [built]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Asset Size Chunks Chunk Names
bundle.js 2.71 kB 0 [emitted] main
[0] ./.test/reportFiles/app.ts 78 bytes {0} [built] [1 error]
[1] ./.test/reportFiles/skip.ts 59 bytes {0} [built]

ERROR in ./.test/reportFiles/app.ts
[tsl] ERROR in app.ts(3,1)
 TS2322: Type '"10"' is not assignable to type 'number'.
2 changes: 2 additions & 0 deletions test/comparison-tests/reportFiles/skip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export let a: number
a = '10'
4 changes: 4 additions & 0 deletions test/comparison-tests/reportFiles/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"compilerOptions": {
}
}
18 changes: 18 additions & 0 deletions test/comparison-tests/reportFiles/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
entry: './app.ts',
output: {
filename: 'bundle.js'
},
resolve: {
extensions: ['.ts', 'tsx', '.js']
},
module: {
rules: [
{
test: /\.tsx?$/, loader: 'ts-loader', options: {
reportFiles: [ '**/*.ts', '!skip.ts' ]
}
}
]
}
}
Loading