diff --git a/packages/parser-css/README.md b/packages/parser-css/README.md index 994d3f1ee83..bad2fc79db5 100644 --- a/packages/parser-css/README.md +++ b/packages/parser-css/README.md @@ -42,6 +42,7 @@ This `parser` emits the following events: See the [PostCSS `walk*` APIs][postcss-walk] for help navigating the AST. * `code`: a string containing the raw stylesheet source code. + * `element`: an `IAsyncHTMLElement` reference if the source was inline in HTML; `null` otherwise. * `resource`: the parsed resource. If the CSS is in a `style tag` and not a file, the value will be `Inline CSS`. diff --git a/packages/parser-css/src/parser.ts b/packages/parser-css/src/parser.ts index 23e22f06652..b6a28a6db56 100644 --- a/packages/parser-css/src/parser.ts +++ b/packages/parser-css/src/parser.ts @@ -20,7 +20,7 @@ export default class CSSParser extends Parser { engine.on('element::style', this.parseStyleTag.bind(this)); } - private async emitCSS(code: string, resource: string) { + private async emitCSS(code: string, resource: string, element: IAsyncHTMLElement | null) { try { await this.engine.emitAsync(`parse::start::css`, { resource }); @@ -31,6 +31,7 @@ export default class CSSParser extends Parser { await this.engine.emitAsync(`parse::end::css`, { ast, code, + element, resource }); @@ -43,7 +44,7 @@ export default class CSSParser extends Parser { const code = fetchEnd.response.body.content; const resource = fetchEnd.resource; - await this.emitCSS(code, resource); + await this.emitCSS(code, resource, null); } private isCSSType(element: IAsyncHTMLElement) { @@ -76,6 +77,6 @@ export default class CSSParser extends Parser { const code = this.getStyleContent(await element.outerHTML()); const resource: string = 'Inline CSS'; - await this.emitCSS(code, resource); + await this.emitCSS(code, resource, element); } } diff --git a/packages/parser-css/src/types.ts b/packages/parser-css/src/types.ts index f8989ba9732..a4a0a5dd79e 100644 --- a/packages/parser-css/src/types.ts +++ b/packages/parser-css/src/types.ts @@ -1,3 +1,4 @@ +import { IAsyncHTMLElement } from 'hint/dist/src/lib/types'; import { Event, Events } from 'hint/dist/src/lib/types/events'; import { Root } from 'postcss'; @@ -11,6 +12,8 @@ export type StyleParse = Event & { ast: Root; /** The raw stylesheet source code */ code: string; + /** The originating `); } - } as Partial; + } as Partial as IAsyncHTMLElement; }; diff --git a/packages/parser-css/tests/interface-tests.ts b/packages/parser-css/tests/interface-tests.ts index f6e86d7ae5c..4cfbecc6a8b 100644 --- a/packages/parser-css/tests/interface-tests.ts +++ b/packages/parser-css/tests/interface-tests.ts @@ -77,6 +77,7 @@ test('If a style tag is inline CSS, then we should parse the stylesheet and emit t.is(args[0], 'parse::end::css'); t.is(data.code, code); + t.is(data.element, element); t.is(data.resource, 'Inline CSS'); }); @@ -105,5 +106,6 @@ test('If fetch::end::css is received, then we should parse the stylesheet and em t.is(args[0], 'parse::end::css'); t.is(data.code, code); + t.is(data.element, null); t.is(data.resource, 'styles.css'); }); diff --git a/packages/parser-javascript/README.md b/packages/parser-javascript/README.md index ab5b5aee057..2802114c903 100644 --- a/packages/parser-javascript/README.md +++ b/packages/parser-javascript/README.md @@ -40,10 +40,11 @@ This `parser` emits the following events: * `parse::end::javascript`, of type `ScriptParse` which contains the following information: -* `resource`: the parsed resource. If the JavaScript is in - a `script tag` and not a file, the value will be `Internal - javascript`. -* `sourceCode`: a `eslint` `SourceCode` object. + * `element`: an `IAsyncHTMLElement` reference if the source was inline in HTML; `null` otherwise. + * `resource`: the parsed resource. If the JavaScript is in + a `script tag` and not a file, the value will be `Internal + javascript`. + * `sourceCode`: a `eslint` `SourceCode` object. diff --git a/packages/parser-javascript/src/parser.ts b/packages/parser-javascript/src/parser.ts index 8ac081369dc..a3796a02938 100644 --- a/packages/parser-javascript/src/parser.ts +++ b/packages/parser-javascript/src/parser.ts @@ -28,7 +28,7 @@ export default class JavascriptParser extends Parser { engine.on('element::script', this.parseJavascriptTag.bind(this)); } - private async emitScript(code: string, resource: string) { + private async emitScript(code: string, resource: string, element: IAsyncHTMLElement | null) { try { await this.engine.emitAsync(`parse::start::javascript`, { resource }); @@ -36,6 +36,7 @@ export default class JavascriptParser extends Parser { await this.engine.emitAsync(`parse::end::javascript`, { ast, + element, resource, sourceCode: new SourceCode(code, ast) }); @@ -48,7 +49,7 @@ export default class JavascriptParser extends Parser { const code = fetchEnd.response.body.content; const resource = fetchEnd.resource; - await this.emitScript(code, resource); + await this.emitScript(code, resource, null); } private hasSrcAttribute(element: IAsyncHTMLElement) { @@ -91,6 +92,6 @@ export default class JavascriptParser extends Parser { const code = this.getScriptContent(await element.outerHTML()); const resource: string = 'Internal javascript'; - await this.emitScript(code, resource); + await this.emitScript(code, resource, element); } } diff --git a/packages/parser-javascript/src/types.ts b/packages/parser-javascript/src/types.ts index 59de3b606cd..e7f14897080 100644 --- a/packages/parser-javascript/src/types.ts +++ b/packages/parser-javascript/src/types.ts @@ -1,11 +1,14 @@ import { AST, SourceCode } from 'eslint'; +import { IAsyncHTMLElement } from 'hint/dist/src/lib/types'; import { Event, Events } from 'hint/dist/src/lib/types/events'; /** The object emitted by the `javascript` parser */ export type ScriptParse = Event & { /** The ast generated from the script */ ast: AST.Program; + /** The originating