Skip to content

Commit b0ad4d3

Browse files
feature: input methods like shallow and dependecy have the same output, so that mean deep dependecy also parse vue files as shallow
1 parent 1924a6f commit b0ad4d3

File tree

10 files changed

+72
-118
lines changed

10 files changed

+72
-118
lines changed

__tests__/lib/input/shallow.js

-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ test('shallow deps directory', async function () {
3535
expect(deps[0].file.match(/input.js/)).toBeTruthy();
3636
});
3737

38-
test('throws on non-string or object input', function () {
39-
return shallow([true], {}).catch(err => {
40-
expect(err.message).toBe('Indexes should be either strings or objects');
41-
});
42-
});
43-
4438
test('shallow deps literal', async function () {
4539
const obj = {
4640
file: 'foo.js',

package-lock.json

+6-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"remark-html": "^15.0.0",
3838
"remark-reference-links": "^6.0.0",
3939
"remark-toc": "^8.0.1",
40-
"detective": "^5.2.0",
4140
"konan": "^2.1.1",
4241
"resolve": "^1.20.0",
4342
"strip-json-comments": "^4.0.0",

src/index.js

-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import fs from 'fs';
2-
import path from 'path';
31
import _ from 'lodash';
42
import sort from './sort.js';
53
import { nest } from './nest.js';
64
import filterAccess from './filter_access.js';
75
import dependency from './input/dependency.js';
86
import shallow from './input/shallow.js';
97
import parseJavaScript from './parsers/javascript.js';
10-
import parseVueScript from './parsers/vue.js';
118
import github from './github.js';
129
import hierarchy from './hierarchy.js';
1310
import inferName from './infer/name.js';
@@ -107,17 +104,6 @@ function buildInternal(inputsAndConfig) {
107104
]);
108105

109106
const extractedComments = _.flatMap(inputs, function (sourceFile) {
110-
if (!sourceFile.source) {
111-
sourceFile.source = fs.readFileSync(sourceFile.file, 'utf8');
112-
}
113-
114-
if (!sourceFile.file) {
115-
sourceFile.file = '';
116-
}
117-
118-
if (path.extname(sourceFile.file) === '.vue') {
119-
return parseVueScript(sourceFile, config).map(buildPipeline);
120-
}
121107
return parseJavaScript(sourceFile, config).map(buildPipeline);
122108
}).filter(Boolean);
123109

@@ -146,10 +132,6 @@ function lintInternal(inputsAndConfig) {
146132
]);
147133

148134
const extractedComments = _.flatMap(inputs, sourceFile => {
149-
if (!sourceFile.source) {
150-
sourceFile.source = fs.readFileSync(sourceFile.file, 'utf8');
151-
}
152-
153135
return parseJavaScript(sourceFile, config).map(lintPipeline);
154136
}).filter(Boolean);
155137

src/input/dependency.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import mdeps from './moduleDeps.js';
22
import internalOnly from '../module_filters.js';
3-
import smartGlob from '../smart_glob.js';
3+
import smartGlob from './smart_glob.js';
44

55
/**
66
* Returns a array of dependencies, given an array of entry

src/input/moduleDeps.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import path from 'path';
22
import util from 'util';
3-
import { readFile } from 'fs/promises';
43
import r from 'resolve';
5-
import detective from 'detective';
4+
import readFileCode from './readFileCode.js';
65
import konan from 'konan';
6+
import { parseToAst } from '../parsers/parse_to_ast.js';
77

88
// const parseExst = ['.js', '.mjs', '.jsx', '.vue', '.ts', '.tsx'];
99
const resolveExst = ['.json', '.css', '.less', '.sass'];
@@ -35,9 +35,8 @@ class Deps {
3535
if (this.fileCache[file]) {
3636
return this.fileCache[file];
3737
}
38-
return readFile(file, {
39-
encoding: 'utf8'
40-
});
38+
39+
return readFileCode(file);
4140
}
4241

4342
async walk(id, parent) {
@@ -95,15 +94,10 @@ class Deps {
9594

9695
parseDeps(file, src) {
9796
try {
98-
try {
99-
return konan(src).strings;
100-
} catch (ex) {
101-
// konan does not support Vue (component) file, try to parse using detective (as a fallback)
102-
return detective(src);
103-
}
97+
const ast = parseToAst(src, file);
98+
return konan(ast).strings;
10499
} catch (ex) {
105100
console.error(`Parsing file ${file}: ${ex}`);
106-
return;
107101
}
108102
}
109103
}

src/input/readFileCode.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import path from 'path';
2+
import { readFile } from 'fs/promises';
3+
let vuecompiler = null;
4+
let vueVersion = 'v3';
5+
6+
async function vueParser(source) {
7+
if (!vuecompiler) {
8+
try {
9+
vuecompiler = await import('@vue/compiler-sfc');
10+
} catch {
11+
try {
12+
vuecompiler = await import('vue-template-compiler');
13+
vueVersion = 'v2';
14+
} catch (err) {
15+
console.error(
16+
'You need to load package vue-template-compiler for Vue 2 or @vue/compiler-sfc for Vue 3'
17+
);
18+
throw err;
19+
}
20+
}
21+
}
22+
23+
let component = {};
24+
if (vueVersion === 'v3') {
25+
component = vuecompiler.parse(source).descriptor;
26+
} else {
27+
component = vuecompiler.parseComponent(source);
28+
}
29+
30+
return component.script?.content || '';
31+
}
32+
33+
export default async function readFileCode(file) {
34+
let source = await readFile(file, {
35+
encoding: 'utf8'
36+
});
37+
38+
if (path.extname(file) === '.vue') {
39+
source = await vueParser(source);
40+
}
41+
return source;
42+
}

src/input/shallow.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import smartGlob from '../smart_glob.js';
1+
import smartGlob from './smart_glob.js';
2+
import readFileCode from './readFileCode.js';
23

34
/**
45
* A readable source for content that doesn't do dependency resolution, but
@@ -16,25 +17,22 @@ import smartGlob from '../smart_glob.js';
1617
* @param config parsing options
1718
* @returns promise with parsed files
1819
*/
19-
export default function (indexes, config) {
20+
export default async function (indexes, config) {
2021
const objects = [];
21-
const strings = [];
22-
for (const index of indexes) {
23-
if (typeof index === 'string') {
24-
strings.push(index);
25-
} else if (typeof index === 'object') {
26-
objects.push(index);
27-
} else {
28-
return Promise.reject(
29-
new Error('Indexes should be either strings or objects')
30-
);
22+
const paths = indexes.filter(v => {
23+
if (typeof v === 'object') {
24+
v.file = v.file ?? '';
25+
objects.push(v);
26+
return false;
3127
}
32-
}
33-
return Promise.resolve(
34-
objects.concat(
35-
smartGlob(strings, config.parseExtension).map(file => ({
36-
file
37-
}))
38-
)
28+
return typeof v === 'string';
29+
});
30+
const files = await Promise.all(
31+
smartGlob(paths, config.parseExtension).map(async file => ({
32+
source: await readFileCode(file),
33+
file
34+
}))
3935
);
36+
37+
return [...objects, ...files];
4038
}
File renamed without changes.

src/parsers/vue.js

-33
This file was deleted.

0 commit comments

Comments
 (0)