From ecaad8e7499f3fa0e3c70b5d0119eae50bee4ad2 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Fri, 20 Oct 2017 20:35:18 +0200 Subject: [PATCH] no-implicit-dependencies: strip BOM before parsing JSON (#3362) [no-log] --- src/rules/noImplicitDependenciesRule.ts | 3 ++- test/rules/no-implicit-dependencies/default/bom/package.json | 5 +++++ test/rules/no-implicit-dependencies/default/bom/test.ts.lint | 2 ++ .../no-implicit-dependencies/default/subdir/test.ts.lint | 3 +++ 4 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 test/rules/no-implicit-dependencies/default/bom/package.json create mode 100644 test/rules/no-implicit-dependencies/default/bom/test.ts.lint diff --git a/src/rules/noImplicitDependenciesRule.ts b/src/rules/noImplicitDependenciesRule.ts index c8ac50dd794..0990e692fb1 100644 --- a/src/rules/noImplicitDependenciesRule.ts +++ b/src/rules/noImplicitDependenciesRule.ts @@ -115,7 +115,8 @@ function getDependencies(fileName: string, options: Options): Set { const packageJsonPath = findPackageJson(path.resolve(path.dirname(fileName))); if (packageJsonPath !== undefined) { // don't use require here to avoid caching - const content = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")) as PackageJson; + // remove BOM from file content before parsing + const content = JSON.parse(fs.readFileSync(packageJsonPath, "utf8").replace(/^\uFEFF/, "")) as PackageJson; if (content.dependencies !== undefined) { addDependencies(result, content.dependencies); } diff --git a/test/rules/no-implicit-dependencies/default/bom/package.json b/test/rules/no-implicit-dependencies/default/bom/package.json new file mode 100644 index 00000000000..9c78824fcec --- /dev/null +++ b/test/rules/no-implicit-dependencies/default/bom/package.json @@ -0,0 +1,5 @@ +{ + "name": "bom", + "version": "1.0.0", + "dependencies": {} +} diff --git a/test/rules/no-implicit-dependencies/default/bom/test.ts.lint b/test/rules/no-implicit-dependencies/default/bom/test.ts.lint new file mode 100644 index 00000000000..3531bc9edbc --- /dev/null +++ b/test/rules/no-implicit-dependencies/default/bom/test.ts.lint @@ -0,0 +1,2 @@ +import * as ts from "typescript"; + ~~~~~~~~~~~~ [Module 'typescript' is not listed as dependency in package.json] diff --git a/test/rules/no-implicit-dependencies/default/subdir/test.ts.lint b/test/rules/no-implicit-dependencies/default/subdir/test.ts.lint index 34f7ff5532a..927a875463c 100644 --- a/test/rules/no-implicit-dependencies/default/subdir/test.ts.lint +++ b/test/rules/no-implicit-dependencies/default/subdir/test.ts.lint @@ -2,6 +2,9 @@ import {assert} from 'chai'; ~~~~~~ [err % ('chai')] import foo from 'foo'; +import Foo from 'Foo'; + ~~~~~ [err % ('Foo')] + if (foo) { const common = require('common'); ~~~~~~~~ [err % ('common')]